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. |
… | |
… | |
21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
22 | *----------------------------------------------------------------------*/ |
22 | *----------------------------------------------------------------------*/ |
23 | |
23 | |
24 | #include "../config.h" /* NECESSARY */ |
24 | #include "../config.h" /* NECESSARY */ |
25 | #include "rxvt.h" /* NECESSARY */ |
25 | #include "rxvt.h" /* NECESSARY */ |
26 | #include "strings.intpro" /* PROTOS for internal routines */ |
|
|
27 | |
26 | |
28 | #ifndef NO_STRINGS |
27 | #ifndef NO_STRINGS |
29 | /*----------------------------------------------------------------------*/ |
28 | /*----------------------------------------------------------------------*/ |
30 | /* |
29 | /* |
31 | * a replacement for strcasecmp() to avoid linking an entire library. |
30 | * 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 |
31 | * Mark Olesen added this in 2.15 but for which OS & library? - Geoff Wing |
33 | */ |
32 | */ |
34 | int |
33 | int |
35 | strcasecmp(const char *s1, const char *s2) |
34 | strcasecmp (const char *s1, const char *s2) |
36 | { |
35 | { |
37 | for ( ; tolower(*s1) == tolower(*s2); s1++, s2++) |
36 | for ( ; tolower (*s1) == tolower (*s2); s1++, s2++) |
38 | if (!*s1) |
37 | if (!*s1) |
39 | return 0; |
38 | return 0; |
40 | return (int)(tolower(*s1) - tolower(*s2)); |
39 | return (int) (tolower (*s1) - tolower (*s2)); |
41 | } |
40 | } |
42 | |
41 | |
43 | int |
42 | int |
44 | strncasecmp(const char *s1, const char *s2, size_t n) |
43 | strncasecmp (const char *s1, const char *s2, size_t n) |
45 | { |
44 | { |
46 | for ( ; n-- && (tolower(*s1) == tolower(*s2)); s1++, s2++) |
45 | for ( ; n-- && (tolower (*s1) == tolower (*s2)); s1++, s2++) |
47 | if (!*s1) |
46 | if (!*s1) |
48 | return 0; |
47 | return 0; |
49 | if (n == 0) |
48 | if (n == 0) |
50 | return 0; |
49 | return 0; |
51 | return (int)(tolower(*s1) - tolower(*s2)); |
50 | return (int) (tolower (*s1) - tolower (*s2)); |
52 | } |
51 | } |
53 | |
52 | |
54 | char * |
53 | char * |
55 | strcpy(char *d, const char *s) |
54 | strcpy (char *d, const char *s) |
56 | { |
55 | { |
57 | char *r = d; |
56 | char *r = d; |
58 | |
57 | |
59 | for ( ; (*r++ = *s++) != '\0'; ) ; |
58 | for ( ; (*r++ = *s++) != '\0'; ) ; |
60 | return d; |
59 | return d; |
61 | } |
60 | } |
62 | |
61 | |
63 | char * |
62 | char * |
64 | strncpy(char *d, const char *s, size_t len) |
63 | strncpy (char *d, const char *s, size_t len) |
65 | { |
64 | { |
66 | char *r = d; |
65 | char *r = d; |
67 | |
66 | |
68 | if (len) |
67 | if (len) |
69 | for ( ; len; len--) |
68 | for ( ; len; len--) |
… | |
… | |
75 | } |
74 | } |
76 | return d; |
75 | return d; |
77 | } |
76 | } |
78 | |
77 | |
79 | int |
78 | int |
80 | strcmp(const char *s1, const char *s2) |
79 | strcmp (const char *s1, const char *s2) |
81 | { |
80 | { |
82 | for ( ; (*s1 == *s2++); ) |
81 | for ( ; (*s1 == *s2++); ) |
83 | if (*s1++ == '\0') |
82 | if (*s1++ == '\0') |
84 | return 0; |
83 | return 0; |
85 | return (int) ((unsigned char) *s1 - (unsigned char) *--s2); |
84 | return (int) ((unsigned char) *s1 - (unsigned char) *--s2); |
86 | } |
85 | } |
87 | |
86 | |
88 | int |
87 | int |
89 | strncmp(const char *s1, const char *s2, size_t len) |
88 | strncmp (const char *s1, const char *s2, size_t len) |
90 | { |
89 | { |
91 | if (len) |
90 | if (len) |
92 | { |
91 | { |
93 | for ( ; len-- && (*s1++ == *s2++); ) ; |
92 | for ( ; len-- && (*s1++ == *s2++); ) ; |
94 | if (++len) |
93 | if (++len) |
… | |
… | |
96 | } |
95 | } |
97 | return 0; |
96 | return 0; |
98 | } |
97 | } |
99 | |
98 | |
100 | char * |
99 | char * |
101 | strcat(char *s1, const char *s2) |
100 | strcat (char *s1, const char *s2) |
102 | { |
101 | { |
103 | char *r = s1; |
102 | char *r = s1; |
104 | |
103 | |
105 | if (*r != '\0') |
104 | if (*r != '\0') |
106 | for ( ; *++r != '\0'; ) ; |
105 | for ( ; *++r != '\0'; ) ; |
… | |
… | |
108 | |
107 | |
109 | return s1; |
108 | return s1; |
110 | } |
109 | } |
111 | |
110 | |
112 | char * |
111 | char * |
113 | strncat(char *s1, const char *s2, size_t len) |
112 | strncat (char *s1, const char *s2, size_t len) |
114 | { |
113 | { |
115 | char *r = s1; |
114 | char *r = s1; |
116 | |
115 | |
117 | if (*r != '\0') |
116 | if (*r != '\0') |
118 | for ( ; *++r != '\0'; ) ; |
117 | for ( ; *++r != '\0'; ) ; |
… | |
… | |
121 | |
120 | |
122 | return s1; |
121 | return s1; |
123 | } |
122 | } |
124 | |
123 | |
125 | size_t |
124 | size_t |
126 | strlen(const char *s) |
125 | strlen (const char *s) |
127 | { |
126 | { |
128 | size_t len = 0; |
127 | size_t len = 0; |
129 | |
128 | |
130 | for ( ; *s++ != '\0'; len++) ; |
129 | for ( ; *s++ != '\0'; len++) ; |
131 | return len; |
130 | return len; |
132 | } |
131 | } |
133 | |
132 | |
134 | char * |
133 | char * |
135 | strdup(const char *s) |
134 | strdup (const char *s) |
136 | { |
135 | { |
137 | size_t len = STRLEN(s) + 1; |
136 | size_t len = STRLEN (s) + 1; |
138 | char *c; |
137 | char *c; |
139 | |
138 | |
140 | if ((c = malloc(len)) != NULL) |
139 | if ((c = malloc (len)) != NULL) |
141 | MEMCPY(c, s, len); |
140 | MEMCPY (c, s, len); |
142 | return c; |
141 | return c; |
143 | } |
142 | } |
144 | |
143 | |
145 | char * |
144 | char * |
146 | index(const char *s, int c) |
145 | index (const char *s, int c) |
147 | { |
146 | { |
148 | return STRCHR(s, c); |
147 | return STRCHR (s, c); |
149 | } |
148 | } |
150 | |
149 | |
151 | char * |
150 | char * |
152 | strchr(const char *s, int c) |
151 | strchr (const char *s, int c) |
153 | { |
152 | { |
154 | char *p = NULL; |
153 | char *p = NULL; |
155 | |
154 | |
156 | for (;;) |
155 | for (;;) |
157 | { |
156 | { |
… | |
… | |
166 | return p; |
165 | return p; |
167 | |
166 | |
168 | } |
167 | } |
169 | |
168 | |
170 | char * |
169 | char * |
171 | rindex(const char *s, int c) |
170 | rindex (const char *s, int c) |
172 | { |
171 | { |
173 | return STRRCHR(s, c); |
172 | return STRRCHR (s, c); |
174 | } |
173 | } |
175 | |
174 | |
176 | char * |
175 | char * |
177 | strrchr(const char *s, int c) |
176 | strrchr (const char *s, int c) |
178 | { |
177 | { |
179 | char *p = NULL; |
178 | char *p = NULL; |
180 | |
179 | |
181 | for (;;) |
180 | for (;;) |
182 | { |
181 | { |
… | |
… | |
187 | } |
186 | } |
188 | return p; |
187 | return p; |
189 | } |
188 | } |
190 | |
189 | |
191 | void * |
190 | void * |
192 | memcpy(void *s1, const void *s2, size_t len) |
191 | memcpy (void *s1, const void *s2, size_t len) |
193 | { |
192 | { |
194 | /* has extra stack and time but less code space */ |
193 | /* has extra stack and time but less code space */ |
195 | return MEMMOVE(s1, s2, len); |
194 | return MEMMOVE (s1, s2, len); |
196 | } |
195 | } |
197 | |
196 | |
198 | /*--------------------------------------------------------------------------* |
197 | /*--------------------------------------------------------------------------* |
199 | * Possibly faster memmove() by Geoff Wing <mason@primenet.com.au> |
198 | * Possibly faster memmove () by Geoff Wing <mason@primenet.com.au> |
200 | *--------------------------------------------------------------------------*/ |
199 | *--------------------------------------------------------------------------*/ |
201 | void * |
200 | void * |
202 | memmove(void *d, const void *s, size_t len) |
201 | memmove (void *d, const void *s, size_t len) |
203 | { |
202 | { |
204 | u_intp_t i; |
203 | u_intp_t i; |
205 | unsigned char *dst = (unsigned char *)d; |
204 | unsigned char *dst = (unsigned char *)d; |
206 | const unsigned char *src = (const unsigned char *)s; |
205 | const unsigned char *src = (const unsigned char *)s; |
207 | |
206 | |
208 | if (len && d != s) |
207 | if (len && d != s) |
209 | { |
208 | { |
210 | if ((u_intp_t)d < (u_intp_t)s) |
209 | if ((u_intp_t)d < (u_intp_t)s) |
211 | { |
210 | { |
212 | /* forwards */ |
211 | /* forwards */ |
213 | i = (-(u_intp_t)dst) & (SIZEOF_INT_P - 1); |
212 | i = (- (u_intp_t)dst) & (SIZEOF_INT_P - 1); |
214 | if (len >= 16 && i == ((-(u_intp_t)src) & (SIZEOF_INT_P - 1))) |
213 | if (len >= 16 && i == ((- (u_intp_t)src) & (SIZEOF_INT_P - 1))) |
215 | { |
214 | { |
216 | /* speed up since src & dst are offset correctly */ |
215 | /* speed up since src & dst are offset correctly */ |
217 | len -= (size_t)i; |
216 | len -= (size_t)i; |
218 | for ( ; i--; ) |
217 | for ( ; i--; ) |
219 | *dst++ = *src++; |
218 | *dst++ = *src++; |
220 | for (i = (u_intp_t)(len / SIZEOF_INT_P); i--; ) |
219 | for (i = (u_intp_t) (len / SIZEOF_INT_P); i--; ) |
221 | *((u_intp_t *)dst)++ = *((const u_intp_t *)src)++; |
220 | * ((u_intp_t *)dst)++ = * ((const u_intp_t *)src)++; |
222 | len &= (SIZEOF_INT_P - 1); |
221 | len &= (SIZEOF_INT_P - 1); |
223 | } |
222 | } |
224 | for ( ; len--; ) |
223 | for ( ; len--; ) |
225 | *dst++ = *src++; |
224 | *dst++ = *src++; |
226 | } |
225 | } |
… | |
… | |
234 | { |
233 | { |
235 | /* speed up since src & dst are offset correctly */ |
234 | /* speed up since src & dst are offset correctly */ |
236 | len -= (size_t)i; |
235 | len -= (size_t)i; |
237 | for ( ; i--; ) |
236 | for ( ; i--; ) |
238 | *--dst = *--src; |
237 | *--dst = *--src; |
239 | for (i = (u_intp_t)(len / SIZEOF_INT_P); i--; ) |
238 | for (i = (u_intp_t) (len / SIZEOF_INT_P); i--; ) |
240 | *--((u_intp_t *)dst) = *--((const u_intp_t *)src); |
239 | *-- ((u_intp_t *)dst) = *-- ((const u_intp_t *)src); |
241 | len &= (SIZEOF_INT_P - 1); |
240 | len &= (SIZEOF_INT_P - 1); |
242 | } |
241 | } |
243 | for ( ; len--; ) |
242 | for ( ; len--; ) |
244 | *--dst = *--src; |
243 | *--dst = *--src; |
245 | } |
244 | } |
246 | } |
245 | } |
247 | return d; |
246 | return d; |
248 | } |
247 | } |
249 | |
248 | |
250 | /*--------------------------------------------------------------------------* |
249 | /*--------------------------------------------------------------------------* |
251 | * Possibly faster memset() by Geoff Wing <mason@primenet.com.au> |
250 | * Possibly faster memset () by Geoff Wing <mason@primenet.com.au> |
252 | * presumptions: |
251 | * presumptions: |
253 | * 1) intp_t write the best |
252 | * 1) intp_t write the best |
254 | * 2) SIZEOF_INT_P == power of 2 |
253 | * 2) SIZEOF_INT_P == power of 2 |
255 | *--------------------------------------------------------------------------*/ |
254 | *--------------------------------------------------------------------------*/ |
256 | |
255 | |
257 | void |
256 | void |
258 | bzero(void *b, size_t len) |
257 | bzero (void *b, size_t len) |
259 | { |
258 | { |
260 | MEMSET(b, 0, len); |
259 | MEMSET (b, 0, len); |
261 | } |
260 | } |
262 | |
261 | |
263 | void * |
262 | void * |
264 | memset(void *p, int c1, size_t len) |
263 | memset (void *p, int c1, size_t len) |
265 | { |
264 | { |
266 | u_intp_t i, val; |
265 | u_intp_t i, val; |
267 | unsigned char c = (unsigned char) c1; |
266 | unsigned char c = (unsigned char) c1; |
268 | unsigned char *lp = (unsigned char *) p; |
267 | unsigned char *lp = (unsigned char *) p; |
269 | |
268 | |
270 | if (len) |
269 | if (len) |
271 | { |
270 | { |
272 | if (len >= 16) |
271 | if (len >= 16) |
273 | { /* < 16 probably not worth all the calculations */ |
272 | { /* < 16 probably not worth all the calculations */ |
274 | /* write out preceding characters so we align on an integer boundary */ |
273 | /* write out preceding characters so we align on an integer boundary */ |
275 | if ((i = ((-(u_intp_t)p) & (SIZEOF_INT_P - 1)))) |
274 | if ((i = ((- (u_intp_t)p) & (SIZEOF_INT_P - 1)))) |
276 | { |
275 | { |
277 | len -= (size_t)i; |
276 | len -= (size_t)i; |
278 | for (; i--;) |
277 | for (; i--;) |
279 | *lp++ = c; |
278 | *lp++ = c; |
280 | } |
279 | } |
… | |
… | |
288 | val |= (val << 32); |
287 | val |= (val << 32); |
289 | #endif |
288 | #endif |
290 | #if SIZEOF_INT_P == 16 |
289 | #if SIZEOF_INT_P == 16 |
291 | val |= (val << 64); |
290 | val |= (val << 64); |
292 | #endif |
291 | #endif |
293 | for (i = (u_intp_t)(len / SIZEOF_INT_P); i--;) |
292 | for (i = (u_intp_t) (len / SIZEOF_INT_P); i--;) |
294 | *((u_intp_t *)lp)++ = val; |
293 | * ((u_intp_t *)lp)++ = val; |
295 | len &= (SIZEOF_INT_P - 1); |
294 | len &= (SIZEOF_INT_P - 1); |
296 | } |
295 | } |
297 | /* write trailing characters */ |
296 | /* write trailing characters */ |
298 | for (; len--;) |
297 | for (; len--;) |
299 | *lp++ = c; |
298 | *lp++ = c; |