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