1 | /*--------------------------------*-C-*---------------------------------* |
1 | /*--------------------------------*-C-*---------------------------------* |
2 | * File: strings.c |
2 | * File: strings.c |
3 | *----------------------------------------------------------------------* |
3 | *----------------------------------------------------------------------* |
4 | * $Id: strings.C,v 1.3 2004/01/31 00:20:21 pcg Exp $ |
4 | * $Id: strings.C,v 1.4 2004/02/01 01:34:41 pcg Exp $ |
5 | * |
5 | * |
6 | * All portions of code are copyright by their respective author/s. |
6 | * All portions of code are copyright by their respective author/s. |
7 | * Copyright (c) 1997-2001 Geoff Wing <gcw@pobox.com> |
7 | * Copyright (c) 1997-2001 Geoff Wing <gcw@pobox.com> |
8 | * |
8 | * |
9 | * This program is free software; you can redistribute it and/or modify |
9 | * This program is free software; you can redistribute it and/or modify |
… | |
… | |
32 | * Mark Olesen added this in 2.15 but for which OS & library? - Geoff Wing |
32 | * Mark Olesen added this in 2.15 but for which OS & library? - Geoff Wing |
33 | */ |
33 | */ |
34 | int |
34 | int |
35 | strcasecmp(const char *s1, const char *s2) |
35 | strcasecmp(const char *s1, const char *s2) |
36 | { |
36 | { |
37 | for ( ; tolower(*s1) == tolower(*s2); s1++, s2++) |
37 | for ( ; tolower(*s1) == tolower(*s2); s1++, s2++) |
38 | if (!*s1) |
38 | if (!*s1) |
39 | return 0; |
39 | return 0; |
40 | return (int)(tolower(*s1) - tolower(*s2)); |
40 | return (int)(tolower(*s1) - tolower(*s2)); |
41 | } |
41 | } |
42 | |
42 | |
43 | int |
43 | int |
44 | strncasecmp(const char *s1, const char *s2, size_t n) |
44 | strncasecmp(const char *s1, const char *s2, size_t n) |
45 | { |
45 | { |
46 | for ( ; n-- && (tolower(*s1) == tolower(*s2)); s1++, s2++) |
46 | for ( ; n-- && (tolower(*s1) == tolower(*s2)); s1++, s2++) |
47 | if (!*s1) |
47 | if (!*s1) |
|
|
48 | return 0; |
|
|
49 | if (n == 0) |
48 | return 0; |
50 | return 0; |
49 | if (n == 0) |
|
|
50 | return 0; |
|
|
51 | return (int)(tolower(*s1) - tolower(*s2)); |
51 | return (int)(tolower(*s1) - tolower(*s2)); |
52 | } |
52 | } |
53 | |
53 | |
54 | char * |
54 | char * |
55 | strcpy(char *d, const char *s) |
55 | strcpy(char *d, const char *s) |
56 | { |
56 | { |
57 | char *r = d; |
57 | char *r = d; |
58 | |
58 | |
59 | for ( ; (*r++ = *s++) != '\0'; ) ; |
59 | for ( ; (*r++ = *s++) != '\0'; ) ; |
60 | return d; |
60 | return d; |
61 | } |
61 | } |
62 | |
62 | |
63 | char * |
63 | char * |
64 | strncpy(char *d, const char *s, size_t len) |
64 | strncpy(char *d, const char *s, size_t len) |
65 | { |
65 | { |
66 | char *r = d; |
66 | char *r = d; |
67 | |
67 | |
68 | if (len) |
68 | if (len) |
69 | for ( ; len; len--) |
69 | for ( ; len; len--) |
70 | if ((*r++ = *s++) == '\0') { |
70 | if ((*r++ = *s++) == '\0') |
71 | for ( ; --len; ) |
71 | { |
|
|
72 | for ( ; --len; ) |
72 | *r++ = '\0'; |
73 | *r++ = '\0'; |
73 | break; |
74 | break; |
74 | } |
75 | } |
75 | return d; |
76 | return d; |
76 | } |
77 | } |
77 | |
78 | |
78 | int |
79 | int |
79 | strcmp(const char *s1, const char *s2) |
80 | strcmp(const char *s1, const char *s2) |
80 | { |
81 | { |
81 | for ( ; (*s1 == *s2++); ) |
82 | for ( ; (*s1 == *s2++); ) |
82 | if (*s1++ == '\0') |
83 | if (*s1++ == '\0') |
83 | return 0; |
84 | return 0; |
84 | return (int) ((unsigned char) *s1 - (unsigned char) *--s2); |
85 | return (int) ((unsigned char) *s1 - (unsigned char) *--s2); |
85 | } |
86 | } |
86 | |
87 | |
87 | int |
88 | int |
88 | strncmp(const char *s1, const char *s2, size_t len) |
89 | strncmp(const char *s1, const char *s2, size_t len) |
89 | { |
90 | { |
90 | if (len) { |
91 | if (len) |
|
|
92 | { |
91 | for ( ; len-- && (*s1++ == *s2++); ) ; |
93 | for ( ; len-- && (*s1++ == *s2++); ) ; |
92 | if (++len) |
94 | if (++len) |
93 | return (int) ((unsigned char) *--s1 - (unsigned char) *--s2); |
95 | return (int) ((unsigned char) *--s1 - (unsigned char) *--s2); |
94 | } |
96 | } |
95 | return 0; |
97 | return 0; |
96 | } |
98 | } |
97 | |
99 | |
98 | char * |
100 | char * |
99 | strcat(char *s1, const char *s2) |
101 | strcat(char *s1, const char *s2) |
100 | { |
102 | { |
101 | char *r = s1; |
103 | char *r = s1; |
102 | |
104 | |
103 | if (*r != '\0') |
105 | if (*r != '\0') |
104 | for ( ; *++r != '\0'; ) ; |
106 | for ( ; *++r != '\0'; ) ; |
105 | for ( ; (*r++ = *s2++) != '\0'; ) ; |
107 | for ( ; (*r++ = *s2++) != '\0'; ) ; |
106 | |
108 | |
107 | return s1; |
109 | return s1; |
108 | } |
110 | } |
109 | |
111 | |
110 | char * |
112 | char * |
111 | strncat(char *s1, const char *s2, size_t len) |
113 | strncat(char *s1, const char *s2, size_t len) |
112 | { |
114 | { |
113 | char *r = s1; |
115 | char *r = s1; |
114 | |
116 | |
115 | if (*r != '\0') |
117 | if (*r != '\0') |
116 | for ( ; *++r != '\0'; ) ; |
118 | for ( ; *++r != '\0'; ) ; |
117 | for ( ; len-- && ((*r++ = *s2++) != '\0'); ) ; |
119 | for ( ; len-- && ((*r++ = *s2++) != '\0'); ) ; |
118 | *r = '\0'; |
120 | *r = '\0'; |
119 | |
121 | |
120 | return s1; |
122 | return s1; |
121 | } |
123 | } |
122 | |
124 | |
123 | size_t |
125 | size_t |
124 | strlen(const char *s) |
126 | strlen(const char *s) |
125 | { |
127 | { |
126 | size_t len = 0; |
128 | size_t len = 0; |
127 | |
129 | |
128 | for ( ; *s++ != '\0'; len++) ; |
130 | for ( ; *s++ != '\0'; len++) ; |
129 | return len; |
131 | return len; |
130 | } |
132 | } |
131 | |
133 | |
132 | char * |
134 | char * |
133 | strdup(const char *s) |
135 | strdup(const char *s) |
134 | { |
136 | { |
135 | size_t len = STRLEN(s) + 1; |
137 | size_t len = STRLEN(s) + 1; |
136 | char *c; |
138 | char *c; |
137 | |
139 | |
138 | if ((c = malloc(len)) != NULL) |
140 | if ((c = malloc(len)) != NULL) |
139 | MEMCPY(c, s, len); |
141 | MEMCPY(c, s, len); |
140 | return c; |
142 | return c; |
141 | } |
143 | } |
142 | |
144 | |
143 | char * |
145 | char * |
144 | index(const char *s, int c) |
146 | index(const char *s, int c) |
145 | { |
147 | { |
146 | return STRCHR(s, c); |
148 | return STRCHR(s, c); |
147 | } |
149 | } |
148 | |
150 | |
149 | char * |
151 | char * |
150 | strchr(const char *s, int c) |
152 | strchr(const char *s, int c) |
151 | { |
153 | { |
152 | char *p = NULL; |
154 | char *p = NULL; |
153 | |
155 | |
154 | for (;;) { |
156 | for (;;) |
|
|
157 | { |
155 | if (*s == (char)c) { |
158 | if (*s == (char)c) |
|
|
159 | { |
156 | p = (char *)s; |
160 | p = (char *)s; |
157 | break; |
161 | break; |
158 | } |
162 | } |
159 | if (*s++ == '\0') |
163 | if (*s++ == '\0') |
160 | break; |
164 | break; |
161 | } |
165 | } |
162 | return p; |
166 | return p; |
163 | |
167 | |
164 | } |
168 | } |
165 | |
169 | |
166 | char * |
170 | char * |
167 | rindex(const char *s, int c) |
171 | rindex(const char *s, int c) |
168 | { |
172 | { |
169 | return STRRCHR(s, c); |
173 | return STRRCHR(s, c); |
170 | } |
174 | } |
171 | |
175 | |
172 | char * |
176 | char * |
173 | strrchr(const char *s, int c) |
177 | strrchr(const char *s, int c) |
174 | { |
178 | { |
175 | char *p = NULL; |
179 | char *p = NULL; |
176 | |
180 | |
177 | for (;;) { |
181 | for (;;) |
|
|
182 | { |
178 | if (*s == (char)c) |
183 | if (*s == (char)c) |
179 | p = (char *)s; |
184 | p = (char *)s; |
180 | if (*s++ == '\0') |
185 | if (*s++ == '\0') |
181 | break; |
186 | break; |
182 | } |
187 | } |
183 | return p; |
188 | return p; |
184 | } |
189 | } |
185 | |
190 | |
186 | void * |
191 | void * |
187 | memcpy(void *s1, const void *s2, size_t len) |
192 | memcpy(void *s1, const void *s2, size_t len) |
188 | { |
193 | { |
189 | /* has extra stack and time but less code space */ |
194 | /* has extra stack and time but less code space */ |
190 | return MEMMOVE(s1, s2, len); |
195 | return MEMMOVE(s1, s2, len); |
191 | } |
196 | } |
192 | |
197 | |
193 | /*--------------------------------------------------------------------------* |
198 | /*--------------------------------------------------------------------------* |
194 | * Possibly faster memmove() by Geoff Wing <mason@primenet.com.au> |
199 | * Possibly faster memmove() by Geoff Wing <mason@primenet.com.au> |
195 | *--------------------------------------------------------------------------*/ |
200 | *--------------------------------------------------------------------------*/ |
196 | void * |
201 | void * |
197 | memmove(void *d, const void *s, size_t len) |
202 | memmove(void *d, const void *s, size_t len) |
198 | { |
203 | { |
199 | u_intp_t i; |
204 | u_intp_t i; |
200 | unsigned char *dst = (unsigned char *)d; |
205 | unsigned char *dst = (unsigned char *)d; |
201 | const unsigned char *src = (const unsigned char *)s; |
206 | const unsigned char *src = (const unsigned char *)s; |
202 | |
207 | |
203 | if (len && d != s) { |
208 | if (len && d != s) |
|
|
209 | { |
204 | if ((u_intp_t)d < (u_intp_t)s) { |
210 | if ((u_intp_t)d < (u_intp_t)s) |
205 | /* forwards */ |
211 | { |
|
|
212 | /* forwards */ |
206 | i = (-(u_intp_t)dst) & (SIZEOF_INT_P - 1); |
213 | i = (-(u_intp_t)dst) & (SIZEOF_INT_P - 1); |
207 | if (len >= 16 && i == ((-(u_intp_t)src) & (SIZEOF_INT_P - 1))) { |
214 | if (len >= 16 && i == ((-(u_intp_t)src) & (SIZEOF_INT_P - 1))) |
|
|
215 | { |
208 | /* speed up since src & dst are offset correctly */ |
216 | /* speed up since src & dst are offset correctly */ |
209 | len -= (size_t)i; |
217 | len -= (size_t)i; |
210 | for ( ; i--; ) |
218 | for ( ; i--; ) |
211 | *dst++ = *src++; |
219 | *dst++ = *src++; |
212 | for (i = (u_intp_t)(len / SIZEOF_INT_P); i--; ) |
220 | for (i = (u_intp_t)(len / SIZEOF_INT_P); i--; ) |
213 | *((u_intp_t *)dst)++ = *((const u_intp_t *)src)++; |
221 | *((u_intp_t *)dst)++ = *((const u_intp_t *)src)++; |
214 | len &= (SIZEOF_INT_P - 1); |
222 | len &= (SIZEOF_INT_P - 1); |
215 | } |
223 | } |
216 | for ( ; len--; ) |
224 | for ( ; len--; ) |
217 | *dst++ = *src++; |
225 | *dst++ = *src++; |
218 | } else { |
226 | } |
219 | /* backwards */ |
227 | else |
|
|
228 | { |
|
|
229 | /* backwards */ |
220 | dst += len; |
230 | dst += len; |
221 | src += len; |
231 | src += len; |
222 | i = ((u_intp_t)dst) & (SIZEOF_INT_P - 1); |
232 | i = ((u_intp_t)dst) & (SIZEOF_INT_P - 1); |
223 | if (len >= 16 && i == (((u_intp_t)src) & (SIZEOF_INT_P - 1))) { |
233 | if (len >= 16 && i == (((u_intp_t)src) & (SIZEOF_INT_P - 1))) |
|
|
234 | { |
224 | /* speed up since src & dst are offset correctly */ |
235 | /* speed up since src & dst are offset correctly */ |
225 | len -= (size_t)i; |
236 | len -= (size_t)i; |
226 | for ( ; i--; ) |
237 | for ( ; i--; ) |
227 | *--dst = *--src; |
238 | *--dst = *--src; |
228 | for (i = (u_intp_t)(len / SIZEOF_INT_P); i--; ) |
239 | for (i = (u_intp_t)(len / SIZEOF_INT_P); i--; ) |
229 | *--((u_intp_t *)dst) = *--((const u_intp_t *)src); |
240 | *--((u_intp_t *)dst) = *--((const u_intp_t *)src); |
230 | len &= (SIZEOF_INT_P - 1); |
241 | len &= (SIZEOF_INT_P - 1); |
|
|
242 | } |
|
|
243 | for ( ; len--; ) |
|
|
244 | *--dst = *--src; |
|
|
245 | } |
231 | } |
246 | } |
232 | for ( ; len--; ) |
|
|
233 | *--dst = *--src; |
|
|
234 | } |
|
|
235 | } |
|
|
236 | return d; |
247 | return d; |
237 | } |
248 | } |
238 | |
249 | |
239 | /*--------------------------------------------------------------------------* |
250 | /*--------------------------------------------------------------------------* |
240 | * Possibly faster memset() by Geoff Wing <mason@primenet.com.au> |
251 | * Possibly faster memset() by Geoff Wing <mason@primenet.com.au> |
241 | * presumptions: |
252 | * presumptions: |
… | |
… | |
244 | *--------------------------------------------------------------------------*/ |
255 | *--------------------------------------------------------------------------*/ |
245 | |
256 | |
246 | void |
257 | void |
247 | bzero(void *b, size_t len) |
258 | bzero(void *b, size_t len) |
248 | { |
259 | { |
249 | MEMSET(b, 0, len); |
260 | MEMSET(b, 0, len); |
250 | } |
261 | } |
251 | |
262 | |
252 | void * |
263 | void * |
253 | memset(void *p, int c1, size_t len) |
264 | memset(void *p, int c1, size_t len) |
254 | { |
265 | { |
255 | u_intp_t i, val; |
266 | u_intp_t i, val; |
256 | unsigned char c = (unsigned char) c1; |
267 | unsigned char c = (unsigned char) c1; |
257 | unsigned char *lp = (unsigned char *) p; |
268 | unsigned char *lp = (unsigned char *) p; |
258 | |
269 | |
259 | if (len) { |
270 | if (len) |
260 | if (len >= 16) { /* < 16 probably not worth all the calculations */ |
271 | { |
|
|
272 | if (len >= 16) |
|
|
273 | { /* < 16 probably not worth all the calculations */ |
261 | /* write out preceding characters so we align on an integer boundary */ |
274 | /* write out preceding characters so we align on an integer boundary */ |
262 | if ((i = ((-(u_intp_t)p) & (SIZEOF_INT_P - 1)))) { |
275 | if ((i = ((-(u_intp_t)p) & (SIZEOF_INT_P - 1)))) |
263 | len -= (size_t)i; |
276 | { |
264 | for (; i--;) |
277 | len -= (size_t)i; |
265 | *lp++ = c; |
278 | for (; i--;) |
266 | } |
279 | *lp++ = c; |
|
|
280 | } |
267 | |
281 | |
268 | /* do the fast writing */ |
282 | /* do the fast writing */ |
269 | val = (c << 8) + c; |
283 | val = (c << 8) + c; |
270 | #if SIZEOF_INT_P >= 4 |
284 | #if SIZEOF_INT_P >= 4 |
271 | val |= (val << 16); |
285 | val |= (val << 16); |
272 | #endif |
286 | #endif |
273 | #if SIZEOF_INT_P >= 8 |
287 | #if SIZEOF_INT_P >= 8 |
274 | val |= (val << 32); |
288 | val |= (val << 32); |
275 | #endif |
289 | #endif |
276 | #if SIZEOF_INT_P == 16 |
290 | #if SIZEOF_INT_P == 16 |
277 | val |= (val << 64); |
291 | val |= (val << 64); |
278 | #endif |
292 | #endif |
279 | for (i = (u_intp_t)(len / SIZEOF_INT_P); i--;) |
293 | for (i = (u_intp_t)(len / SIZEOF_INT_P); i--;) |
280 | *((u_intp_t *)lp)++ = val; |
294 | *((u_intp_t *)lp)++ = val; |
281 | len &= (SIZEOF_INT_P - 1); |
295 | len &= (SIZEOF_INT_P - 1); |
282 | } |
296 | } |
283 | /* write trailing characters */ |
297 | /* write trailing characters */ |
284 | for (; len--;) |
298 | for (; len--;) |
285 | *lp++ = c; |
299 | *lp++ = c; |
286 | } |
300 | } |
287 | return p; |
301 | return p; |
288 | } |
302 | } |
289 | #endif |
303 | #endif |
290 | /*----------------------- end-of-file (C source) -----------------------*/ |
304 | /*----------------------- end-of-file (C source) -----------------------*/ |