ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/strings.C
Revision: 1.2
Committed: Mon Nov 24 17:31:28 2003 UTC (20 years, 6 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: rel-1-3, rel-1-2
Changes since 1.1: +0 -0 lines
Log Message:
*** empty log message ***

File Contents

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