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.3 by pcg, Sat Jan 31 00:20:21 2004 UTC vs.
Revision 1.7 by root, Sun Aug 15 00:37:05 2004 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines