2 * (C) Copyright 2007-2010 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
4 * This file is released under the GPLv2. See the COPYING file for more
7 * Some of these functions are based on Linux's lib/string.c.
13 * strnlen - Find the length of a length-limited string
14 * @s: The string to be sized
15 * @count: The maximum number of bytes to search
17 size_t strnlen(const char *s
, size_t count
)
21 for (sc
= s
; count
-- && *sc
!= '\0'; ++sc
)
27 * strcmp - Compare two strings
31 int strcmp(const char *cs
, const char *ct
)
36 if ((__res
= *cs
- *ct
++) != 0 || !*cs
++)
43 * strncmp - Compare two strings
48 int strncmp(const char *cs
, const char *ct
, int len
)
53 if ((__res
= *cs
- *ct
++) != 0 || !*cs
++ || len
--)
60 * strcasecmp - Compare two strings ignoring case
64 int strcasecmp(const char *s1
, const char *s2
)
71 } while (c1
== c2
&& c1
!= 0);
76 * strncpy - Copy a length-limited, %NUL-terminated string
77 * @dest: Where to copy the string to
78 * @src: Where to copy the string from
79 * @count: The maximum number of bytes to copy
81 * The result is not %NUL-terminated if the source exceeds
84 * In the case where the length of @src is less than that of
85 * count, the remainder of @dest will be padded with %NUL.
88 char *strncpy(char *dest
, const char *src
, size_t count
)
93 if ((*tmp
= *src
) != 0)
102 * memmove - Copy one area of memory to another
103 * @dest: Where to copy to
104 * @src: Where to copy from
105 * @count: The size of the area.
107 * Unlike memcpy(), memmove() copes with overlapping areas.
109 void *memmove(void *dest
, const void *src
, size_t count
)
131 * strpbrk - Find the first occurrence of a set of characters
132 * @cs: The string to be searched
133 * @ct: The characters to search for
135 char *strpbrk(const char *cs
, const char *ct
)
137 const char *sc1
, *sc2
;
139 for (sc1
= cs
; *sc1
!= '\0'; ++sc1
) {
140 for (sc2
= ct
; *sc2
!= '\0'; ++sc2
) {
149 * strspn - Calculate the length of the initial substring of @s which only contain letters in @accept
150 * @s: The string to be searched
151 * @accept: The string to search for
153 size_t strspn(const char *s
, const char *accept
)
159 for (p
= s
; *p
!= '\0'; ++p
) {
160 for (a
= accept
; *a
!= '\0'; ++a
) {
172 * strsep - Split a string into tokens
173 * @s: The string to be searched
174 * @ct: The characters to search for
176 * strsep() updates @s to point after the token, ready for the next call.
178 * It returns empty tokens, too, behaving exactly like the libc function
179 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
180 * Same semantics, slimmer shape. ;)
182 char *strsep(char **s
, const char *ct
)
190 end
= strpbrk(sbegin
, ct
);
198 * strmsep - Split a string into tokens
199 * @s: The string to be searched
200 * @ct: The characters to search for
202 * strmsep() updates @s to point after the token, ready for the next call.
204 * It does not return empty tokens, like strsep.
206 char *strmsep(char **s
, const char *ct
)
211 /* consume leading separator characters */
212 *s
+= strspn(*s
, ct
);
214 return strsep(s
, ct
);
217 /* ASCII character info */
218 unsigned char _ascii_ctype
[] = {
219 _C
,_C
,_C
,_C
,_C
,_C
,_C
,_C
, /* 0-7 */
220 _C
,_C
|_S
,_C
|_S
,_C
|_S
,_C
|_S
,_C
|_S
,_C
,_C
, /* 8-15 */
221 _C
,_C
,_C
,_C
,_C
,_C
,_C
,_C
, /* 16-23 */
222 _C
,_C
,_C
,_C
,_C
,_C
,_C
,_C
, /* 24-31 */
223 _S
|_SP
,_P
,_P
,_P
,_P
,_P
,_P
,_P
, /* 32-39 */
224 _P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
, /* 40-47 */
225 _D
,_D
,_D
,_D
,_D
,_D
,_D
,_D
, /* 48-55 */
226 _D
,_D
,_P
,_P
,_P
,_P
,_P
,_P
, /* 56-63 */
227 _P
,_U
|_X
,_U
|_X
,_U
|_X
,_U
|_X
,_U
|_X
,_U
|_X
,_U
, /* 64-71 */
228 _U
,_U
,_U
,_U
,_U
,_U
,_U
,_U
, /* 72-79 */
229 _U
,_U
,_U
,_U
,_U
,_U
,_U
,_U
, /* 80-87 */
230 _U
,_U
,_U
,_P
,_P
,_P
,_P
,_P
, /* 88-95 */
231 _P
,_L
|_X
,_L
|_X
,_L
|_X
,_L
|_X
,_L
|_X
,_L
|_X
,_L
, /* 96-103 */
232 _L
,_L
,_L
,_L
,_L
,_L
,_L
,_L
, /* 104-111 */
233 _L
,_L
,_L
,_L
,_L
,_L
,_L
,_L
, /* 112-119 */
234 _L
,_L
,_L
,_P
,_P
,_P
,_P
,_C
, /* 120-127 */
235 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 128-143 */
236 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 144-159 */
237 _S
|_SP
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
, /* 160-175 */
238 _P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
,_P
, /* 176-191 */
239 _U
,_U
,_U
,_U
,_U
,_U
,_U
,_U
,_U
,_U
,_U
,_U
,_U
,_U
,_U
,_U
, /* 192-207 */
240 _U
,_U
,_U
,_U
,_U
,_U
,_U
,_P
,_U
,_U
,_U
,_U
,_U
,_U
,_U
,_L
, /* 208-223 */
241 _L
,_L
,_L
,_L
,_L
,_L
,_L
,_L
,_L
,_L
,_L
,_L
,_L
,_L
,_L
,_L
, /* 224-239 */
242 _L
,_L
,_L
,_L
,_L
,_L
,_L
,_P
,_L
,_L
,_L
,_L
,_L
,_L
,_L
,_L
}; /* 240-255 */