]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/String/Searching.c
2 Search Functions for <string.h>.
4 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 #include <Library/BaseLib.h>
15 #include <Library/BaseMemoryLib.h>
17 #include <LibConfig.h>
21 /** The memchr function locates the first occurrence of c (converted to an
22 unsigned char) in the initial n characters (each interpreted as
23 unsigned char) of the object pointed to by s.
25 @return The memchr function returns a pointer to the located character,
26 or a null pointer if the character does not occur in the object.
29 memchr(const void *s
, int c
, size_t n
)
31 return ScanMem8( s
, (UINTN
)n
, (UINT8
)c
);
34 /** The strchr function locates the first occurrence of c (converted to a char)
35 in the string pointed to by s. The terminating null character is considered
36 to be part of the string.
38 @return The strchr function returns a pointer to the located character,
39 or a null pointer if the character does not occur in the string.
42 strchr(const char *s
, int c
)
50 } while(*s
++ != '\0');
54 static UINT8 BitMask
[] = {
55 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
58 #define WHICH8(c) ((unsigned char)(c) >> 3)
59 #define WHICH_BIT(c) (BitMask[((c) & 0x7)])
60 #define BITMAP64 ((UINT64 *)bitmap)
64 BuildBitmap(unsigned char * bitmap
, const char *s2
, int n
)
69 // Initialize bitmap. Bit 0 is always 1 which corresponds to '\0'
70 for (BITMAP64
[0] = index
= 1; index
< n
; index
++)
73 // Set bits in bitmap corresponding to the characters in s2
74 for (; *s2
!= '\0'; s2
++) {
77 bitmap
[index
] = bitmap
[index
] | bit
;
81 /** The strcspn function computes the length of the maximum initial segment of
82 the string pointed to by s1 which consists entirely of characters not from
83 the string pointed to by s2.
85 @return The strcspn function returns the length of the segment.
88 strcspn(const char *s1
, const char *s2
)
90 UINT8 bitmap
[ (((UCHAR_MAX
+ 1) / CHAR_BIT
) + (CHAR_BIT
- 1)) & ~7U];
95 if(*s1
== '\0') return 0;
97 BuildBitmap( bitmap
, s2
, sizeof(bitmap
) / sizeof(UINT64
));
99 for(str
= s1
; ; str
++) {
100 index
= WHICH8(*str
);
101 bit
= WHICH_BIT(*str
);
102 if ((bitmap
[index
] & bit
) != 0)
108 /** The strpbrk function locates the first occurrence in the string pointed to
109 by s1 of any character from the string pointed to by s2.
111 @return The strpbrk function returns a pointer to the character, or a
112 null pointer if no character from s2 occurs in s1.
115 strpbrk(const char *s1
, const char *s2
)
117 UINT8 bitmap
[ (((UCHAR_MAX
+ 1) / CHAR_BIT
) + (CHAR_BIT
- 1)) & ~7U];
121 BuildBitmap( bitmap
, s2
, sizeof(bitmap
) / sizeof(UINT64
));
123 for( ; *s1
!= '\0'; ++s1
) {
125 bit
= WHICH_BIT(*s1
);
126 if( (bitmap
[index
] & bit
) != 0) {
133 /** The strrchr function locates the last occurrence of c (converted to a char)
134 in the string pointed to by s. The terminating null character is considered
135 to be part of the string.
137 @return The strrchr function returns a pointer to the character, or a
138 null pointer if c does not occur in the string.
141 strrchr(const char *s
, int c
)
147 if( *s
== tgt
) found
= (char *)s
;
148 } while( *s
++ != '\0');
153 /** The strspn function computes the length of the maximum initial segment of
154 the string pointed to by s1 which consists entirely of characters from the
155 string pointed to by s2.
157 @return The strspn function returns the length of the segment.
160 strspn(const char *s1
, const char *s2
)
162 UINT8 bitmap
[ (((UCHAR_MAX
+ 1) / CHAR_BIT
) + (CHAR_BIT
- 1)) & ~7U];
167 BuildBitmap( bitmap
, s2
, sizeof(bitmap
) / sizeof(UINT64
));
169 for( ; *s1
!= '\0'; ++s1
) {
171 bit
= WHICH_BIT(*s1
);
172 if( (bitmap
[index
] & bit
) == 0) break;
178 /** The strstr function locates the first occurrence in the string pointed to
179 by s1 of the sequence of characters (excluding the terminating null
180 character) in the string pointed to by s2.
182 @return The strstr function returns a pointer to the located string, or a
183 null pointer if the string is not found. If s2 points to a string
184 with zero length, the function returns s1.
187 strstr(const char *s1
, const char *s2
)
189 return AsciiStrStr( s1
, s2
);
192 /** A sequence of calls to the strtok function breaks the string pointed to by
193 s1 into a sequence of tokens, each of which is delimited by a character
194 from the string pointed to by s2. The first call in the sequence has a
195 non-null first argument; subsequent calls in the sequence have a null first
196 argument. The separator string pointed to by s2 may be different from call
199 The first call in the sequence searches the string pointed to by s1 for the
200 first character that is not contained in the current separator string
201 pointed to by s2. If no such character is found, then there are no tokens
202 in the string pointed to by s1 and the strtok function returns a null
203 pointer. If such a character is found, it is the start of the first token.
205 The strtok function then searches from there for a character that is
206 contained in the current separator string. If no such character is found,
207 the current token extends to the end of the string pointed to by s1, and
208 subsequent searches for a token will return a null pointer. If such a
209 character is found, it is overwritten by a null character, which terminates
210 the current token. The strtok function saves a pointer to the following
211 character, from which the next search for a token will start.
213 Each subsequent call, with a null pointer as the value of the first
214 argument, starts searching from the saved pointer and behaves as
217 @return The strtok function returns a pointer to the first character of a
218 token, or a null pointer if there is no token.
221 strtok(char * __restrict s1
, const char * __restrict s2
)
223 static char *Next
= NULL
;
224 UINT8 bitmap
[ (((UCHAR_MAX
+ 1) / CHAR_BIT
) + (CHAR_BIT
- 1)) & ~7U];
230 && ((s1
= Next
) == NULL
))
235 // s2 can be different on each call, so build the bitmap each time.
236 BuildBitmap( bitmap
, s2
, sizeof(bitmap
) / sizeof(UINT64
));
238 // skip leading delimiters: all chars in s2
239 for( ; *s1
!= '\0'; ++s1
) {
241 bit
= WHICH_BIT(*s1
);
242 if( (bitmap
[index
] & bit
) == 0) break;
246 // Remember this point, it is the start of the token
249 // find the next delimiter and replace it with a '\0'
250 for( ; *s1
!= '\0'; ++s1
) {
252 bit
= WHICH_BIT(*s1
);
253 if( (bitmap
[index
] & bit
) != 0) {