Imported Upstream version 3.0
[platform/upstream/gnu-efi.git] / lib / str.c
1 /*++
2
3 Copyright (c) 1998  Intel Corporation
4
5 Module Name:
6
7     str.c
8
9 Abstract:
10
11
12
13
14 Revision History
15
16 --*/
17
18 #include "lib.h"
19
20
21 INTN
22 StrCmp (
23     IN CHAR16   *s1,
24     IN CHAR16   *s2
25     )
26 // compare strings
27 {
28     return RtStrCmp(s1, s2);
29 }
30
31 INTN
32 StrnCmp (
33     IN CHAR16   *s1,
34     IN CHAR16   *s2,
35     IN UINTN    len
36     )
37 // compare strings
38 {
39     while (*s1  &&  len) {
40         if (*s1 != *s2) {
41             break;
42         }
43
44         s1  += 1;
45         s2  += 1;
46         len -= 1;
47     }
48
49     return len ? *s1 - *s2 : 0;
50 }
51
52
53 INTN EFIAPI
54 LibStubStriCmp (
55     IN EFI_UNICODE_COLLATION_INTERFACE  *This,
56     IN CHAR16                           *s1,
57     IN CHAR16                           *s2
58     )
59 {
60     return StrCmp (s1, s2);
61 }
62
63 VOID EFIAPI
64 LibStubStrLwrUpr (
65     IN EFI_UNICODE_COLLATION_INTERFACE  *This,
66     IN CHAR16                           *Str
67     )
68 {
69 }
70
71 INTN
72 StriCmp (
73     IN CHAR16   *s1,
74     IN CHAR16   *s2
75     )
76 // compare strings
77 {
78     if (UnicodeInterface == &LibStubUnicodeInterface)
79         return UnicodeInterface->StriColl(UnicodeInterface, s1, s2);
80     else
81         return uefi_call_wrapper(UnicodeInterface->StriColl, 3, UnicodeInterface, s1, s2);
82 }
83
84 VOID
85 StrLwr (
86     IN CHAR16   *Str
87     )
88 // lwoer case string
89 {
90     if (UnicodeInterface == &LibStubUnicodeInterface)
91         UnicodeInterface->StrLwr(UnicodeInterface, Str);
92     else uefi_call_wrapper(UnicodeInterface->StrLwr, 2, UnicodeInterface, Str);
93 }
94
95 VOID
96 StrUpr (
97     IN CHAR16   *Str
98     )
99 // upper case string
100 {
101     if (UnicodeInterface == &LibStubUnicodeInterface)
102         UnicodeInterface->StrUpr(UnicodeInterface, Str);
103     else uefi_call_wrapper(UnicodeInterface->StrUpr, 2, UnicodeInterface, Str);
104 }
105
106 VOID
107 StrCpy (
108     IN CHAR16   *Dest,
109     IN CHAR16   *Src
110     )
111 // copy strings
112 {
113     RtStrCpy (Dest, Src);
114 }
115
116 VOID
117 StrCat (
118     IN CHAR16   *Dest,
119     IN CHAR16   *Src
120     )
121 {   
122     RtStrCat(Dest, Src);
123 }
124
125 UINTN
126 StrLen (
127     IN CHAR16   *s1
128     )
129 // string length
130 {
131     return RtStrLen(s1);
132 }
133
134 UINTN
135 StrSize (
136     IN CHAR16   *s1
137     )
138 // string size
139 {
140     return RtStrSize(s1);
141 }
142
143 CHAR16 *
144 StrDuplicate (
145     IN CHAR16   *Src
146     )
147 // duplicate a string
148 {
149     CHAR16      *Dest;
150     UINTN       Size;
151
152     Size = StrSize(Src);
153     Dest = AllocatePool (Size);
154     if (Dest) {
155         CopyMem (Dest, Src, Size);
156     }
157     return Dest;
158 }
159
160 UINTN
161 strlena (
162     IN CHAR8    *s1
163     )
164 // string length
165 {
166     UINTN        len;
167     
168     for (len=0; *s1; s1+=1, len+=1) ;
169     return len;
170 }
171
172 UINTN
173 strcmpa (
174     IN CHAR8    *s1,
175     IN CHAR8    *s2
176     )
177 // compare strings
178 {
179     while (*s1) {
180         if (*s1 != *s2) {
181             break;
182         }
183
184         s1 += 1;
185         s2 += 1;
186     }
187
188     return *s1 - *s2;
189 }
190
191 UINTN
192 strncmpa (
193     IN CHAR8    *s1,
194     IN CHAR8    *s2,
195     IN UINTN    len
196     )
197 // compare strings
198 {
199     while (*s1  &&  len) {
200         if (*s1 != *s2) {
201             break;
202         }
203
204         s1  += 1;
205         s2  += 1;
206         len -= 1;
207     }
208
209     return len ? *s1 - *s2 : 0;
210 }
211
212
213
214 UINTN
215 xtoi (
216     CHAR16  *str
217     )
218 // convert hex string to uint
219 {
220     UINTN       u;
221     CHAR16      c;
222
223     // skip preceeding white space
224     while (*str && *str == ' ') {
225         str += 1;
226     }
227
228     // convert hex digits
229     u = 0;
230     while ((c = *(str++))) {
231         if (c >= 'a'  &&  c <= 'f') {
232             c -= 'a' - 'A';
233         }
234
235         if ((c >= '0'  &&  c <= '9')  ||  (c >= 'A'  &&  c <= 'F')) {
236             u = (u << 4)  |  (c - (c >= 'A' ? 'A'-10 : '0'));
237         } else {
238             break;
239         }
240     }
241
242     return u;
243 }
244
245 UINTN
246 Atoi (
247     CHAR16  *str
248     )
249 // convert hex string to uint
250 {
251     UINTN       u;
252     CHAR16      c;
253
254     // skip preceeding white space
255     while (*str && *str == ' ') {
256         str += 1;
257     }
258
259     // convert digits
260     u = 0;
261     while ((c = *(str++))) {
262         if (c >= '0' && c <= '9') {
263             u = (u * 10) + c - '0';
264         } else {
265             break;
266         }
267     }
268
269     return u;
270 }
271
272 BOOLEAN 
273 MetaMatch (
274     IN CHAR16   *String,
275     IN CHAR16   *Pattern
276     )
277 {
278     CHAR16  c, p, l;
279
280     for (; ;) {
281         p = *Pattern;
282         Pattern += 1;
283
284         switch (p) {
285         case 0:    
286             // End of pattern.  If end of string, TRUE match
287             return *String ? FALSE : TRUE;     
288
289         case '*':                               
290             // Match zero or more chars
291             while (*String) {
292                 if (MetaMatch (String, Pattern)) {
293                     return TRUE;
294                 }
295                 String += 1;
296             }
297             return MetaMatch (String, Pattern);
298
299         case '?':                               
300             // Match any one char
301             if (!*String) {
302                 return FALSE;
303             }
304             String += 1;
305             break;
306
307         case '[':                               
308             // Match char set
309             c = *String;
310             if (!c) {
311                 return FALSE;                       // syntax problem
312             }
313
314             l = 0;
315             while ((p = *Pattern++)) {
316                 if (p == ']') {
317                     return FALSE;
318                 }
319
320                 if (p == '-') {                     // if range of chars,
321                     p = *Pattern;                   // get high range
322                     if (p == 0 || p == ']') {
323                         return FALSE;               // syntax problem
324                     }
325
326                     if (c >= l && c <= p) {         // if in range, 
327                         break;                      // it's a match
328                     }
329                 }
330                 
331                 l = p;
332                 if (c == p) {                       // if char matches
333                     break;                          // move on
334                 }
335             }
336             
337             // skip to end of match char set
338             while (p && p != ']') {
339                 p = *Pattern;
340                 Pattern += 1;
341             }
342
343             String += 1;
344             break;
345
346         default:
347             c = *String;
348             if (c != p) {
349                 return FALSE;
350             }
351
352             String += 1;
353             break;
354         }
355     }
356 }
357
358
359 BOOLEAN EFIAPI
360 LibStubMetaiMatch (
361     IN EFI_UNICODE_COLLATION_INTERFACE  *This,
362     IN CHAR16                           *String,
363     IN CHAR16                           *Pattern
364     )
365 {
366     return MetaMatch (String, Pattern);
367 }
368
369
370 BOOLEAN 
371 MetaiMatch (
372     IN CHAR16   *String,
373     IN CHAR16   *Pattern
374     )
375 {
376     if (UnicodeInterface == &LibStubUnicodeInterface)
377         return UnicodeInterface->MetaiMatch(UnicodeInterface, String, Pattern);
378     else return uefi_call_wrapper(UnicodeInterface->MetaiMatch, 3, UnicodeInterface, String, Pattern);
379 }