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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines