1 | /*--------------------------------*-C-*---------------------------------* |
1 | /*--------------------------------*-C-*---------------------------------* |
2 | * File: strings.c |
2 | * File: strings.C |
3 | *----------------------------------------------------------------------* |
3 | *----------------------------------------------------------------------* |
4 | * $Id: strings.C,v 1.4 2004/02/01 01:34:41 pcg Exp $ |
|
|
5 | * |
4 | * |
6 | * All portions of code are copyright by their respective author/s. |
5 | * All portions of code are copyright by their respective author/s. |
7 | * Copyright (c) 1997-2001 Geoff Wing <gcw@pobox.com> |
6 | * Copyright (c) 1997-2001 Geoff Wing <gcw@pobox.com> |
|
|
7 | * Copyright (c) 2004 Marc Lehmann <pcg@goof.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 |
10 | * it under the terms of the GNU General Public License as published by |
10 | * it under the terms of the GNU General Public License as published by |
11 | * the Free Software Foundation; either version 2 of the License, or |
11 | * the Free Software Foundation; either version 2 of the License, or |
12 | * (at your option) any later version. |
12 | * (at your option) any later version. |
… | |
… | |
26 | #include "strings.intpro" /* PROTOS for internal routines */ |
26 | #include "strings.intpro" /* PROTOS for internal routines */ |
27 | |
27 | |
28 | #ifndef NO_STRINGS |
28 | #ifndef NO_STRINGS |
29 | /*----------------------------------------------------------------------*/ |
29 | /*----------------------------------------------------------------------*/ |
30 | /* |
30 | /* |
31 | * a replacement for strcasecmp() to avoid linking an entire library. |
31 | * a replacement for strcasecmp () to avoid linking an entire library. |
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; |
48 | return 0; |
49 | if (n == 0) |
49 | if (n == 0) |
50 | return 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--) |
… | |
… | |
75 | } |
75 | } |
76 | return d; |
76 | return d; |
77 | } |
77 | } |
78 | |
78 | |
79 | int |
79 | int |
80 | strcmp(const char *s1, const char *s2) |
80 | strcmp (const char *s1, const char *s2) |
81 | { |
81 | { |
82 | for ( ; (*s1 == *s2++); ) |
82 | for ( ; (*s1 == *s2++); ) |
83 | if (*s1++ == '\0') |
83 | if (*s1++ == '\0') |
84 | return 0; |
84 | return 0; |
85 | return (int) ((unsigned char) *s1 - (unsigned char) *--s2); |
85 | return (int) ((unsigned char) *s1 - (unsigned char) *--s2); |
86 | } |
86 | } |
87 | |
87 | |
88 | int |
88 | int |
89 | strncmp(const char *s1, const char *s2, size_t len) |
89 | strncmp (const char *s1, const char *s2, size_t len) |
90 | { |
90 | { |
91 | if (len) |
91 | if (len) |
92 | { |
92 | { |
93 | for ( ; len-- && (*s1++ == *s2++); ) ; |
93 | for ( ; len-- && (*s1++ == *s2++); ) ; |
94 | if (++len) |
94 | if (++len) |
… | |
… | |
96 | } |
96 | } |
97 | return 0; |
97 | return 0; |
98 | } |
98 | } |
99 | |
99 | |
100 | char * |
100 | char * |
101 | strcat(char *s1, const char *s2) |
101 | strcat (char *s1, const char *s2) |
102 | { |
102 | { |
103 | char *r = s1; |
103 | char *r = s1; |
104 | |
104 | |
105 | if (*r != '\0') |
105 | if (*r != '\0') |
106 | for ( ; *++r != '\0'; ) ; |
106 | for ( ; *++r != '\0'; ) ; |
… | |
… | |
108 | |
108 | |
109 | return s1; |
109 | return s1; |
110 | } |
110 | } |
111 | |
111 | |
112 | char * |
112 | char * |
113 | strncat(char *s1, const char *s2, size_t len) |
113 | strncat (char *s1, const char *s2, size_t len) |
114 | { |
114 | { |
115 | char *r = s1; |
115 | char *r = s1; |
116 | |
116 | |
117 | if (*r != '\0') |
117 | if (*r != '\0') |
118 | for ( ; *++r != '\0'; ) ; |
118 | for ( ; *++r != '\0'; ) ; |
… | |
… | |
121 | |
121 | |
122 | return s1; |
122 | return s1; |
123 | } |
123 | } |
124 | |
124 | |
125 | size_t |
125 | size_t |
126 | strlen(const char *s) |
126 | strlen (const char *s) |
127 | { |
127 | { |
128 | size_t len = 0; |
128 | size_t len = 0; |
129 | |
129 | |
130 | for ( ; *s++ != '\0'; len++) ; |
130 | for ( ; *s++ != '\0'; len++) ; |
131 | return len; |
131 | return len; |
132 | } |
132 | } |
133 | |
133 | |
134 | char * |
134 | char * |
135 | strdup(const char *s) |
135 | strdup (const char *s) |
136 | { |
136 | { |
137 | size_t len = STRLEN(s) + 1; |
137 | size_t len = STRLEN (s) + 1; |
138 | char *c; |
138 | char *c; |
139 | |
139 | |
140 | if ((c = malloc(len)) != NULL) |
140 | if ((c = malloc (len)) != NULL) |
141 | MEMCPY(c, s, len); |
141 | MEMCPY (c, s, len); |
142 | return c; |
142 | return c; |
143 | } |
143 | } |
144 | |
144 | |
145 | char * |
145 | char * |
146 | index(const char *s, int c) |
146 | index (const char *s, int c) |
147 | { |
147 | { |
148 | return STRCHR(s, c); |
148 | return STRCHR (s, c); |
149 | } |
149 | } |
150 | |
150 | |
151 | char * |
151 | char * |
152 | strchr(const char *s, int c) |
152 | strchr (const char *s, int c) |
153 | { |
153 | { |
154 | char *p = NULL; |
154 | char *p = NULL; |
155 | |
155 | |
156 | for (;;) |
156 | for (;;) |
157 | { |
157 | { |
… | |
… | |
166 | return p; |
166 | return p; |
167 | |
167 | |
168 | } |
168 | } |
169 | |
169 | |
170 | char * |
170 | char * |
171 | rindex(const char *s, int c) |
171 | rindex (const char *s, int c) |
172 | { |
172 | { |
173 | return STRRCHR(s, c); |
173 | return STRRCHR (s, c); |
174 | } |
174 | } |
175 | |
175 | |
176 | char * |
176 | char * |
177 | strrchr(const char *s, int c) |
177 | strrchr (const char *s, int c) |
178 | { |
178 | { |
179 | char *p = NULL; |
179 | char *p = NULL; |
180 | |
180 | |
181 | for (;;) |
181 | for (;;) |
182 | { |
182 | { |
… | |
… | |
187 | } |
187 | } |
188 | return p; |
188 | return p; |
189 | } |
189 | } |
190 | |
190 | |
191 | void * |
191 | void * |
192 | memcpy(void *s1, const void *s2, size_t len) |
192 | memcpy (void *s1, const void *s2, size_t len) |
193 | { |
193 | { |
194 | /* has extra stack and time but less code space */ |
194 | /* has extra stack and time but less code space */ |
195 | return MEMMOVE(s1, s2, len); |
195 | return MEMMOVE (s1, s2, len); |
196 | } |
196 | } |
197 | |
197 | |
198 | /*--------------------------------------------------------------------------* |
198 | /*--------------------------------------------------------------------------* |
199 | * Possibly faster memmove() by Geoff Wing <mason@primenet.com.au> |
199 | * Possibly faster memmove () by Geoff Wing <mason@primenet.com.au> |
200 | *--------------------------------------------------------------------------*/ |
200 | *--------------------------------------------------------------------------*/ |
201 | void * |
201 | void * |
202 | memmove(void *d, const void *s, size_t len) |
202 | memmove (void *d, const void *s, size_t len) |
203 | { |
203 | { |
204 | u_intp_t i; |
204 | u_intp_t i; |
205 | unsigned char *dst = (unsigned char *)d; |
205 | unsigned char *dst = (unsigned char *)d; |
206 | const unsigned char *src = (const unsigned char *)s; |
206 | const unsigned char *src = (const unsigned char *)s; |
207 | |
207 | |
208 | if (len && d != s) |
208 | if (len && d != s) |
209 | { |
209 | { |
210 | if ((u_intp_t)d < (u_intp_t)s) |
210 | if ((u_intp_t)d < (u_intp_t)s) |
211 | { |
211 | { |
212 | /* forwards */ |
212 | /* forwards */ |
213 | i = (-(u_intp_t)dst) & (SIZEOF_INT_P - 1); |
213 | i = (- (u_intp_t)dst) & (SIZEOF_INT_P - 1); |
214 | 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 | { |
215 | { |
216 | /* speed up since src & dst are offset correctly */ |
216 | /* speed up since src & dst are offset correctly */ |
217 | len -= (size_t)i; |
217 | len -= (size_t)i; |
218 | for ( ; i--; ) |
218 | for ( ; i--; ) |
219 | *dst++ = *src++; |
219 | *dst++ = *src++; |
220 | for (i = (u_intp_t)(len / SIZEOF_INT_P); i--; ) |
220 | for (i = (u_intp_t) (len / SIZEOF_INT_P); i--; ) |
221 | *((u_intp_t *)dst)++ = *((const u_intp_t *)src)++; |
221 | * ((u_intp_t *)dst)++ = * ((const u_intp_t *)src)++; |
222 | len &= (SIZEOF_INT_P - 1); |
222 | len &= (SIZEOF_INT_P - 1); |
223 | } |
223 | } |
224 | for ( ; len--; ) |
224 | for ( ; len--; ) |
225 | *dst++ = *src++; |
225 | *dst++ = *src++; |
226 | } |
226 | } |
… | |
… | |
234 | { |
234 | { |
235 | /* speed up since src & dst are offset correctly */ |
235 | /* speed up since src & dst are offset correctly */ |
236 | len -= (size_t)i; |
236 | len -= (size_t)i; |
237 | for ( ; i--; ) |
237 | for ( ; i--; ) |
238 | *--dst = *--src; |
238 | *--dst = *--src; |
239 | for (i = (u_intp_t)(len / SIZEOF_INT_P); i--; ) |
239 | for (i = (u_intp_t) (len / SIZEOF_INT_P); i--; ) |
240 | *--((u_intp_t *)dst) = *--((const u_intp_t *)src); |
240 | *-- ((u_intp_t *)dst) = *-- ((const u_intp_t *)src); |
241 | len &= (SIZEOF_INT_P - 1); |
241 | len &= (SIZEOF_INT_P - 1); |
242 | } |
242 | } |
243 | for ( ; len--; ) |
243 | for ( ; len--; ) |
244 | *--dst = *--src; |
244 | *--dst = *--src; |
245 | } |
245 | } |
246 | } |
246 | } |
247 | return d; |
247 | return d; |
248 | } |
248 | } |
249 | |
249 | |
250 | /*--------------------------------------------------------------------------* |
250 | /*--------------------------------------------------------------------------* |
251 | * Possibly faster memset() by Geoff Wing <mason@primenet.com.au> |
251 | * Possibly faster memset () by Geoff Wing <mason@primenet.com.au> |
252 | * presumptions: |
252 | * presumptions: |
253 | * 1) intp_t write the best |
253 | * 1) intp_t write the best |
254 | * 2) SIZEOF_INT_P == power of 2 |
254 | * 2) SIZEOF_INT_P == power of 2 |
255 | *--------------------------------------------------------------------------*/ |
255 | *--------------------------------------------------------------------------*/ |
256 | |
256 | |
257 | void |
257 | void |
258 | bzero(void *b, size_t len) |
258 | bzero (void *b, size_t len) |
259 | { |
259 | { |
260 | MEMSET(b, 0, len); |
260 | MEMSET (b, 0, len); |
261 | } |
261 | } |
262 | |
262 | |
263 | void * |
263 | void * |
264 | memset(void *p, int c1, size_t len) |
264 | memset (void *p, int c1, size_t len) |
265 | { |
265 | { |
266 | u_intp_t i, val; |
266 | u_intp_t i, val; |
267 | unsigned char c = (unsigned char) c1; |
267 | unsigned char c = (unsigned char) c1; |
268 | unsigned char *lp = (unsigned char *) p; |
268 | unsigned char *lp = (unsigned char *) p; |
269 | |
269 | |
270 | if (len) |
270 | if (len) |
271 | { |
271 | { |
272 | if (len >= 16) |
272 | if (len >= 16) |
273 | { /* < 16 probably not worth all the calculations */ |
273 | { /* < 16 probably not worth all the calculations */ |
274 | /* write out preceding characters so we align on an integer boundary */ |
274 | /* write out preceding characters so we align on an integer boundary */ |
275 | if ((i = ((-(u_intp_t)p) & (SIZEOF_INT_P - 1)))) |
275 | if ((i = ((- (u_intp_t)p) & (SIZEOF_INT_P - 1)))) |
276 | { |
276 | { |
277 | len -= (size_t)i; |
277 | len -= (size_t)i; |
278 | for (; i--;) |
278 | for (; i--;) |
279 | *lp++ = c; |
279 | *lp++ = c; |
280 | } |
280 | } |
… | |
… | |
288 | val |= (val << 32); |
288 | val |= (val << 32); |
289 | #endif |
289 | #endif |
290 | #if SIZEOF_INT_P == 16 |
290 | #if SIZEOF_INT_P == 16 |
291 | val |= (val << 64); |
291 | val |= (val << 64); |
292 | #endif |
292 | #endif |
293 | for (i = (u_intp_t)(len / SIZEOF_INT_P); i--;) |
293 | for (i = (u_intp_t) (len / SIZEOF_INT_P); i--;) |
294 | *((u_intp_t *)lp)++ = val; |
294 | * ((u_intp_t *)lp)++ = val; |
295 | len &= (SIZEOF_INT_P - 1); |
295 | len &= (SIZEOF_INT_P - 1); |
296 | } |
296 | } |
297 | /* write trailing characters */ |
297 | /* write trailing characters */ |
298 | for (; len--;) |
298 | for (; len--;) |
299 | *lp++ = c; |
299 | *lp++ = c; |