1 |
pcg |
1.1 |
/*--------------------------------*-C-*---------------------------------* |
2 |
|
|
* File: strings.c |
3 |
|
|
*----------------------------------------------------------------------* |
4 |
pcg |
1.3 |
* $Id: strings.C,v 1.2 2003/11/24 17:31:28 pcg Exp $ |
5 |
pcg |
1.1 |
* |
6 |
|
|
* All portions of code are copyright by their respective author/s. |
7 |
|
|
* Copyright (c) 1997-2001 Geoff Wing <gcw@pobox.com> |
8 |
|
|
* |
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 |
11 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
12 |
|
|
* (at your option) any later version. |
13 |
|
|
* |
14 |
|
|
* This program is distributed in the hope that it will be useful, |
15 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 |
|
|
* GNU General Public License for more details. |
18 |
|
|
* |
19 |
|
|
* You should have received a copy of the GNU General Public License |
20 |
|
|
* along with this program; if not, write to the Free Software |
21 |
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
22 |
|
|
*----------------------------------------------------------------------*/ |
23 |
|
|
|
24 |
|
|
#include "../config.h" /* NECESSARY */ |
25 |
|
|
#include "rxvt.h" /* NECESSARY */ |
26 |
|
|
#include "strings.intpro" /* PROTOS for internal routines */ |
27 |
|
|
|
28 |
|
|
#ifndef NO_STRINGS |
29 |
|
|
/*----------------------------------------------------------------------*/ |
30 |
|
|
/* |
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 |
33 |
|
|
*/ |
34 |
|
|
int |
35 |
|
|
strcasecmp(const char *s1, const char *s2) |
36 |
|
|
{ |
37 |
|
|
for ( ; tolower(*s1) == tolower(*s2); s1++, s2++) |
38 |
|
|
if (!*s1) |
39 |
|
|
return 0; |
40 |
|
|
return (int)(tolower(*s1) - tolower(*s2)); |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
int |
44 |
|
|
strncasecmp(const char *s1, const char *s2, size_t n) |
45 |
|
|
{ |
46 |
|
|
for ( ; n-- && (tolower(*s1) == tolower(*s2)); s1++, s2++) |
47 |
|
|
if (!*s1) |
48 |
|
|
return 0; |
49 |
|
|
if (n == 0) |
50 |
|
|
return 0; |
51 |
|
|
return (int)(tolower(*s1) - tolower(*s2)); |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
char * |
55 |
|
|
strcpy(char *d, const char *s) |
56 |
|
|
{ |
57 |
|
|
char *r = d; |
58 |
|
|
|
59 |
|
|
for ( ; (*r++ = *s++) != '\0'; ) ; |
60 |
|
|
return d; |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
char * |
64 |
|
|
strncpy(char *d, const char *s, size_t len) |
65 |
|
|
{ |
66 |
|
|
char *r = d; |
67 |
|
|
|
68 |
|
|
if (len) |
69 |
|
|
for ( ; len; len--) |
70 |
|
|
if ((*r++ = *s++) == '\0') { |
71 |
|
|
for ( ; --len; ) |
72 |
|
|
*r++ = '\0'; |
73 |
|
|
break; |
74 |
|
|
} |
75 |
|
|
return d; |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
int |
79 |
|
|
strcmp(const char *s1, const char *s2) |
80 |
|
|
{ |
81 |
|
|
for ( ; (*s1 == *s2++); ) |
82 |
|
|
if (*s1++ == '\0') |
83 |
|
|
return 0; |
84 |
|
|
return (int) ((unsigned char) *s1 - (unsigned char) *--s2); |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
int |
88 |
|
|
strncmp(const char *s1, const char *s2, size_t len) |
89 |
|
|
{ |
90 |
|
|
if (len) { |
91 |
|
|
for ( ; len-- && (*s1++ == *s2++); ) ; |
92 |
|
|
if (++len) |
93 |
|
|
return (int) ((unsigned char) *--s1 - (unsigned char) *--s2); |
94 |
|
|
} |
95 |
|
|
return 0; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
char * |
99 |
|
|
strcat(char *s1, const char *s2) |
100 |
|
|
{ |
101 |
|
|
char *r = s1; |
102 |
|
|
|
103 |
|
|
if (*r != '\0') |
104 |
|
|
for ( ; *++r != '\0'; ) ; |
105 |
|
|
for ( ; (*r++ = *s2++) != '\0'; ) ; |
106 |
|
|
|
107 |
|
|
return s1; |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
char * |
111 |
|
|
strncat(char *s1, const char *s2, size_t len) |
112 |
|
|
{ |
113 |
|
|
char *r = s1; |
114 |
|
|
|
115 |
|
|
if (*r != '\0') |
116 |
|
|
for ( ; *++r != '\0'; ) ; |
117 |
|
|
for ( ; len-- && ((*r++ = *s2++) != '\0'); ) ; |
118 |
|
|
*r = '\0'; |
119 |
|
|
|
120 |
|
|
return s1; |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
size_t |
124 |
|
|
strlen(const char *s) |
125 |
|
|
{ |
126 |
|
|
size_t len = 0; |
127 |
|
|
|
128 |
|
|
for ( ; *s++ != '\0'; len++) ; |
129 |
|
|
return len; |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
char * |
133 |
|
|
strdup(const char *s) |
134 |
|
|
{ |
135 |
|
|
size_t len = STRLEN(s) + 1; |
136 |
|
|
char *c; |
137 |
|
|
|
138 |
|
|
if ((c = malloc(len)) != NULL) |
139 |
|
|
MEMCPY(c, s, len); |
140 |
|
|
return c; |
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
char * |
144 |
|
|
index(const char *s, int c) |
145 |
|
|
{ |
146 |
|
|
return STRCHR(s, c); |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
char * |
150 |
|
|
strchr(const char *s, int c) |
151 |
|
|
{ |
152 |
|
|
char *p = NULL; |
153 |
|
|
|
154 |
|
|
for (;;) { |
155 |
|
|
if (*s == (char)c) { |
156 |
|
|
p = (char *)s; |
157 |
|
|
break; |
158 |
|
|
} |
159 |
|
|
if (*s++ == '\0') |
160 |
|
|
break; |
161 |
|
|
} |
162 |
|
|
return p; |
163 |
|
|
|
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
char * |
167 |
|
|
rindex(const char *s, int c) |
168 |
|
|
{ |
169 |
|
|
return STRRCHR(s, c); |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
char * |
173 |
|
|
strrchr(const char *s, int c) |
174 |
|
|
{ |
175 |
|
|
char *p = NULL; |
176 |
|
|
|
177 |
|
|
for (;;) { |
178 |
|
|
if (*s == (char)c) |
179 |
|
|
p = (char *)s; |
180 |
|
|
if (*s++ == '\0') |
181 |
|
|
break; |
182 |
|
|
} |
183 |
|
|
return p; |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
void * |
187 |
|
|
memcpy(void *s1, const void *s2, size_t len) |
188 |
|
|
{ |
189 |
|
|
/* has extra stack and time but less code space */ |
190 |
|
|
return MEMMOVE(s1, s2, len); |
191 |
|
|
} |
192 |
|
|
|
193 |
|
|
/*--------------------------------------------------------------------------* |
194 |
|
|
* Possibly faster memmove() by Geoff Wing <mason@primenet.com.au> |
195 |
|
|
*--------------------------------------------------------------------------*/ |
196 |
|
|
void * |
197 |
|
|
memmove(void *d, const void *s, size_t len) |
198 |
|
|
{ |
199 |
|
|
u_intp_t i; |
200 |
|
|
unsigned char *dst = (unsigned char *)d; |
201 |
|
|
const unsigned char *src = (const unsigned char *)s; |
202 |
|
|
|
203 |
|
|
if (len && d != s) { |
204 |
|
|
if ((u_intp_t)d < (u_intp_t)s) { |
205 |
|
|
/* forwards */ |
206 |
|
|
i = (-(u_intp_t)dst) & (SIZEOF_INT_P - 1); |
207 |
|
|
if (len >= 16 && i == ((-(u_intp_t)src) & (SIZEOF_INT_P - 1))) { |
208 |
|
|
/* speed up since src & dst are offset correctly */ |
209 |
|
|
len -= (size_t)i; |
210 |
|
|
for ( ; i--; ) |
211 |
|
|
*dst++ = *src++; |
212 |
|
|
for (i = (u_intp_t)(len / SIZEOF_INT_P); i--; ) |
213 |
|
|
*((u_intp_t *)dst)++ = *((const u_intp_t *)src)++; |
214 |
|
|
len &= (SIZEOF_INT_P - 1); |
215 |
|
|
} |
216 |
|
|
for ( ; len--; ) |
217 |
|
|
*dst++ = *src++; |
218 |
|
|
} else { |
219 |
|
|
/* backwards */ |
220 |
|
|
dst += len; |
221 |
|
|
src += len; |
222 |
|
|
i = ((u_intp_t)dst) & (SIZEOF_INT_P - 1); |
223 |
|
|
if (len >= 16 && i == (((u_intp_t)src) & (SIZEOF_INT_P - 1))) { |
224 |
|
|
/* speed up since src & dst are offset correctly */ |
225 |
|
|
len -= (size_t)i; |
226 |
|
|
for ( ; i--; ) |
227 |
|
|
*--dst = *--src; |
228 |
|
|
for (i = (u_intp_t)(len / SIZEOF_INT_P); i--; ) |
229 |
|
|
*--((u_intp_t *)dst) = *--((const u_intp_t *)src); |
230 |
|
|
len &= (SIZEOF_INT_P - 1); |
231 |
|
|
} |
232 |
|
|
for ( ; len--; ) |
233 |
|
|
*--dst = *--src; |
234 |
|
|
} |
235 |
|
|
} |
236 |
|
|
return d; |
237 |
|
|
} |
238 |
|
|
|
239 |
|
|
/*--------------------------------------------------------------------------* |
240 |
|
|
* Possibly faster memset() by Geoff Wing <mason@primenet.com.au> |
241 |
|
|
* presumptions: |
242 |
|
|
* 1) intp_t write the best |
243 |
|
|
* 2) SIZEOF_INT_P == power of 2 |
244 |
|
|
*--------------------------------------------------------------------------*/ |
245 |
|
|
|
246 |
|
|
void |
247 |
|
|
bzero(void *b, size_t len) |
248 |
|
|
{ |
249 |
|
|
MEMSET(b, 0, len); |
250 |
|
|
} |
251 |
|
|
|
252 |
|
|
void * |
253 |
|
|
memset(void *p, int c1, size_t len) |
254 |
|
|
{ |
255 |
|
|
u_intp_t i, val; |
256 |
|
|
unsigned char c = (unsigned char) c1; |
257 |
|
|
unsigned char *lp = (unsigned char *) p; |
258 |
|
|
|
259 |
|
|
if (len) { |
260 |
|
|
if (len >= 16) { /* < 16 probably not worth all the calculations */ |
261 |
|
|
/* write out preceding characters so we align on an integer boundary */ |
262 |
|
|
if ((i = ((-(u_intp_t)p) & (SIZEOF_INT_P - 1)))) { |
263 |
|
|
len -= (size_t)i; |
264 |
|
|
for (; i--;) |
265 |
|
|
*lp++ = c; |
266 |
|
|
} |
267 |
|
|
|
268 |
|
|
/* do the fast writing */ |
269 |
|
|
val = (c << 8) + c; |
270 |
|
|
#if SIZEOF_INT_P >= 4 |
271 |
|
|
val |= (val << 16); |
272 |
|
|
#endif |
273 |
|
|
#if SIZEOF_INT_P >= 8 |
274 |
|
|
val |= (val << 32); |
275 |
|
|
#endif |
276 |
|
|
#if SIZEOF_INT_P == 16 |
277 |
|
|
val |= (val << 64); |
278 |
|
|
#endif |
279 |
|
|
for (i = (u_intp_t)(len / SIZEOF_INT_P); i--;) |
280 |
|
|
*((u_intp_t *)lp)++ = val; |
281 |
|
|
len &= (SIZEOF_INT_P - 1); |
282 |
|
|
} |
283 |
|
|
/* write trailing characters */ |
284 |
|
|
for (; len--;) |
285 |
|
|
*lp++ = c; |
286 |
|
|
} |
287 |
|
|
return p; |
288 |
|
|
} |
289 |
|
|
#endif |
290 |
|
|
/*----------------------- end-of-file (C source) -----------------------*/ |