1 /******************************************************************************
2 *
3 * Copyright (C) 2004-2007, The Gentee Group. All rights reserved.
4 * This file is part of the Gentee open source project - http://www.gentee.com.
5 *
6 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE GENTEE LICENSE ("AGREEMENT").
7 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE CONSTITUTES RECIPIENTS
8 * ACCEPTANCE OF THE AGREEMENT.
9 *
10 * Author: Alexey Krivonogov ( gentee )
11 *
12 ******************************************************************************/
13
14 define <export>{
15 /*-----------------------------------------------------------------------------
16 * Id: patternflags D
17 *
18 * Summary: Flags for spattern.init method.
19 *
20 -----------------------------------------------------------------------------*/
21 QS_IGNCASE = 0x0001 // Case-insensitive search.
22 QS_WORD = 0x0002 // Search the whole word only.
23 QS_BEGINWORD = 0x0004 // Search words which start with the specified /
24 // pattern.
25
26 //-----------------------------------------------------------------------------
27 }
28
29 /*-----------------------------------------------------------------------------
30 * Id: spattern T
31 *
32 * Summary: The pattern structure for the searching. The spattern type is used
33 to search through the string for another string. Don't change
34 the fields of the spattern strcuture. The spattern variable
35 must be initialized with #a(spattern_init) method.
36 *
37 -----------------------------------------------------------------------------*/
38
39 type spattern
40 {
41 uint pattern // Hidden data.
42 uint size // The size of the pattern.
43 reserved shift[1024] // Hidden data.
44 uint flag // Search flags.
45 }
46
47 /*-----------------------------------------------------------------------------
48 * Id: spattern_init F2
49 *
50 * Summary: Creating data search pattern. Before search start-up, call this
51 method in order to initialize the search pattern. Then do a search
52 of the specified pattern with #a( spattern_search ).
53 *
54 * Params: pattern - Search string (pattern).
55 flag - Search flags.$$[patternflags]
56 *
57 * Return: #lng/retobj#
58 *
59 -----------------------------------------------------------------------------*/
60
61 method spattern spattern.init( buf pattern, uint flag )
62 {
63 qs_init( &this, pattern.ptr(), *pattern, flag )
64 return this
65 }
66
67 /*-----------------------------------------------------------------------------
68 * Id: spattern_search F2
69 *
70 * Summary: Search a pattern in another string. Before search start-up, call the
71 #a( spattern_init ) method in order to initialize the search
72 pattern.
73 *
74 * Params: src - String where the specified string will be searched (search /
75 pattern).
76 offset - Offset where the search must be started or proceeded.
77 *
78 * Return: The offset of the found fragment. If the offset is equal to string
79 size,no fragment is found.
80 *
81 -----------------------------------------------------------------------------*/
82
83 method uint spattern.search( buf src, uint offset )
84 {
85 return offset + qs_search( &this, src.ptr() + offset,
86 *src - offset )
87 }
88
89 /*-----------------------------------------------------------------------------
90 * Id: spattern_search_1 FA
91 *
92 * Summary: Search a pattern in a memory data.
93 *
94 * Params: ptr - The pointer to the memory data where the pattern will be /
95 searched.
96 size - The size of the memory data.
97 *
98 * Return: The offset of the found fragment. If the offset is equal to string
99 size,no fragment is found.
100 *
101 -----------------------------------------------------------------------------*/
102
103 method uint spattern.search( uint ptr, uint size )
104 {
105 return qs_search( &this, ptr, size )
106 }
107
108 /*-----------------------------------------------------------------------------
109 * Id: spattern_init_1 FA
110 *
111 * Summary: Creating data search pattern.
112 *
113 * Params: pattern - Search string (pattern).
114 flag - Search flags.$$[patternflags]
115 *
116 -----------------------------------------------------------------------------*/
117
118 method spattern spattern.init( str pattern, uint flag )
119 {
120 qs_init( &this, pattern.ptr(), *pattern, flag )
121 return this
122 }
123
124 /*-----------------------------------------------------------------------------
125 * Id: spattern_search_2 FA
126 *
127 * Summary: Search a pattern in another string.
128 *
129 * Params: src - String where the specified string will be searched (search /
130 pattern).
131 offset - Offset where the search must be started or proceeded.
132 *
133 * Return: The offset of the found fragment. If the offset is equal to string
134 size,no fragment is found.
135 *
136 -----------------------------------------------------------------------------*/
137
138 method uint spattern.search( str src, uint offset )
139 {
140 return offset + qs_search( &this, src.ptr() + offset,
141 *src - offset )
142 }
143
144 /*-----------------------------------------------------------------------------
145 ** Id: str_search F2
146 *
147 * Summary: Substring search. The method determines if the string has been
148 found inside another string or not.
149 *
150 * Params: pattern - Search string (pattern).
151 flag - Search flags.$$[patternflags]
152 *
153 * Return: The method returns 1 if the substring is found, otherwise the return
154 value is zero.
155 *
156 -----------------------------------------------------------------------------*/
157
158 method uint str.search( str pattern, uint flag )
159 {
160 spattern sp
161
162 sp.init( pattern, flag )
163 return sp.search( this, 0 ) < *this
164 }
165