ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/strings.C
Revision: 1.9
Committed: Fri Jan 6 05:37:59 2006 UTC (18 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-7_0
Changes since 1.8: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

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