ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/strings.C
(Generate patch)

Comparing rxvt-unicode/src/strings.C (file contents):
Revision 1.2 by pcg, Mon Nov 24 17:31:28 2003 UTC vs.
Revision 1.6 by pcg, Thu Apr 8 20:31:45 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines