loader: remove shouting from ORB's variable name
[hvf.git] / lib / string.c
blob2c7c0756de43cfb179ee52af38ad847c7a777ec3
1 /*
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
5 * details.
7 * Some of these functions are based on Linux's lib/string.c.
8 */
10 #include <string.h>
12 /**
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)
19 const char *sc;
21 for (sc = s; count-- && *sc != '\0'; ++sc)
22 /* nothing */;
23 return sc - s;
26 /**
27 * strcmp - Compare two strings
28 * @cs: One string
29 * @ct: Another string
31 int strcmp(const char *cs, const char *ct)
33 signed char __res;
35 while (1) {
36 if ((__res = *cs - *ct++) != 0 || !*cs++)
37 break;
39 return __res;
42 /**
43 * strncmp - Compare two strings
44 * @cs: One string
45 * @ct: Another string
46 * @len: max length
48 int strncmp(const char *cs, const char *ct, int len)
50 signed char __res;
52 while (1) {
53 if ((__res = *cs - *ct++) != 0 || !*cs++ || len--)
54 break;
56 return __res;
59 /**
60 * strcasecmp - Compare two strings ignoring case
61 * @s1: One string
62 * @s2: Another string
64 int strcasecmp(const char *s1, const char *s2)
66 int c1, c2;
68 do {
69 c1 = tolower(*s1++);
70 c2 = tolower(*s2++);
71 } while (c1 == c2 && c1 != 0);
72 return c1 - c2;
75 /**
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
82 * @count bytes.
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)
90 char *tmp = dest;
92 while (count) {
93 if ((*tmp = *src) != 0)
94 src++;
95 tmp++;
96 count--;
98 return dest;
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)
111 char *tmp;
112 const char *s;
114 if (dest <= src) {
115 tmp = dest;
116 s = src;
117 while (count--)
118 *tmp++ = *s++;
119 } else {
120 tmp = dest;
121 tmp += count;
122 s = src;
123 s += count;
124 while (count--)
125 *--tmp = *--s;
127 return dest;
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) {
141 if (*sc1 == *sc2)
142 return (char *)sc1;
145 return NULL;
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)
155 const char *p;
156 const char *a;
157 size_t count = 0;
159 for (p = s; *p != '\0'; ++p) {
160 for (a = accept; *a != '\0'; ++a) {
161 if (*p == *a)
162 break;
164 if (*a == '\0')
165 return count;
166 ++count;
168 return count;
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)
184 char *sbegin = *s;
185 char *end;
187 if (sbegin == NULL)
188 return NULL;
190 end = strpbrk(sbegin, ct);
191 if (end)
192 *end++ = '\0';
193 *s = end;
194 return sbegin;
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)
208 if (*s == NULL)
209 return NULL;
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 */