Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/libsa/string.c

1/*
2 * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999-2003 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 2.0 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/* string operations */
25
26#include "libsa.h"
27
28static char *STRDUP(const char *string);
29
30#if 0
31/*
32 * Abstract:
33 * strcmp (s1, s2) compares the strings "s1" and "s2".
34 * It returns 0 if the strings are identical. It returns
35 * > 0 if the first character that differs in the two strings
36 * is larger in s1 than in s2 or if s1 is longer than s2 and
37 * the contents are identical up to the length of s2.
38 * It returns < 0 if the first differing character is smaller
39 * in s1 than in s2 or if s1 is shorter than s2 and the
40 * contents are identical upto the length of s1.
41 * Deprecation Warning:
42 *strcmp() is being deprecated. Please use strncmp() instead.
43 */
44
45int
46strcmp(
47 const char *s1,
48 const char *s2)
49{
50unsigned int a, b;
51
52do {
53a = *s1++;
54b = *s2++;
55if (a != b)
56return a-b; /* includes case when
57 'a' is zero and 'b' is not zero
58 or vice versa */
59} while (a != '\0');
60
61return 0; /* both are zero */
62}
63
64/*
65 * Abstract:
66 * strncmp (s1, s2, n) compares the strings "s1" and "s2"
67 * in exactly the same way as strcmp does. Except the
68 * comparison runs for at most "n" characters.
69 */
70
71int
72strncmp(
73 const char *s1,
74 const char *s2,
75 size_t n)
76{
77unsigned int a, b;
78
79while (n != 0) {
80a = *s1++;
81b = *s2++;
82if (a != b)
83return a-b; /* includes case when
84 'a' is zero and 'b' is not zero
85 or vice versa */
86if (a == '\0')
87return 0; /* both are zero */
88n--;
89}
90
91return 0;
92}
93
94/*
95 * Abstract:
96 * strcpy copies the contents of the string "from" including
97 * the null terminator to the string "to". A pointer to "to"
98 * is returned.
99 * Deprecation Warning:
100 *strcpy() is being deprecated. Please use strlcpy() instead.
101 */
102char *
103strcpy(
104 char *to,
105 const char *from)
106{
107char *ret = to;
108
109while ((*to++ = *from++) != '\0')
110continue;
111
112return ret;
113}
114
115/*
116 * Abstract:
117 * strncpy copies "count" characters from the "from" string to
118 * the "to" string. If "from" contains less than "count" characters
119 * "to" will be padded with null characters until exactly "count"
120 * characters have been written. The return value is a pointer
121 * to the "to" string.
122 */
123
124char *
125strncpy(
126char *s1,
127const char *s2,
128size_t n)
129{
130char *os1 = s1;
131unsigned long i;
132
133for (i = 0; i < n;)
134if ((*s1++ = *s2++) == '\0')
135for (i++; i < n; i++)
136*s1++ = '\0';
137else
138i++;
139return (os1);
140}
141
142/*
143 * Copy src to string dst of size siz. At most siz-1 characters
144 * will be copied. Always NUL terminates (unless siz == 0).
145 * Returns strlen(src); if retval >= siz, truncation occurred.
146 */
147size_t
148strlcpy(char *dst, const char *src, size_t siz)
149{
150char *d = dst;
151const char *s = src;
152size_t n = siz;
153
154/* Copy as many bytes as will fit */
155if (n != 0 && --n != 0) {
156do {
157if ((*d++ = *s++) == 0)
158break;
159} while (--n != 0);
160}
161
162/* Not enough room in dst, add NUL and traverse rest of src */
163if (n == 0) {
164if (siz != 0)
165*d = '\0';/* NUL-terminate dst */
166while (*s++)
167;
168}
169
170return(s - src - 1);/* count does not include NUL */
171}
172#endif
173
174/*
175 * History:
176 * 2002-01-24 gvdlInitial implementation of strstr
177 */
178
179const char *
180strstr(const char *in, const char *str)
181{
182 char c;
183 size_t len;
184
185 c = *str++;
186 if (!c)
187 return (const char *) in;// Trivial empty string case
188
189 len = strlen(str);
190 do {
191 char sc;
192
193 do {
194 sc = *in++;
195 if (!sc)
196 return (char *) 0;
197 } while (sc != c);
198 } while (strncmp(in, str, len) != 0);
199
200 return (const char *) (in - 1);
201}
202
203#if 0
204void *
205memmove(void *dst, const void *src, size_t ulen)
206{
207 bcopy(src, dst, ulen);
208 return dst;
209}
210#endif
211
212#if UNUSED
213int
214ptol(const char *str)
215{
216register int c = *str;
217
218if (c <= '7' && c >= '0')
219c -= '0';
220else if (c <= 'h' && c >= 'a')
221c -= 'a';
222else c = 0;
223return c;
224}
225#endif
226/*
227 * atoi:
228 *
229 * This function converts an ascii string into an integer.
230 *
231 * input : string
232 * output : a number
233 */
234
235int
236atoi(const char *cp)
237{
238int number;
239
240for (number = 0; ('0' <= *cp) && (*cp <= '9'); cp++)
241number = (number * 10) + (*cp - '0');
242
243return( number );
244}
245#if UNUSED
246/*
247 * convert an integer to an ASCII string.
248 * inputs:
249 *numinteger to be converted
250 *strstring pointer.
251 *
252 * outputs:
253 *pointer to string start.
254 */
255
256char *
257itoa(
258 intnum,
259 char*str)
260{
261char digits[11];
262char *dp;
263char *cp = str;
264
265if (num == 0) {
266*cp++ = '0';
267}
268else {
269dp = digits;
270while (num) {
271*dp++ = '0' + num % 10;
272num /= 10;
273}
274while (dp != digits) {
275*cp++ = *--dp;
276}
277}
278*cp++ = '\0';
279
280return str;
281}
282#endif
283
284#if 0
285/*
286 * Appends src to string dst of size siz (unlike strncat, siz is the
287 * full size of dst, not space left). At most siz-1 characters
288 * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
289 * Returns strlen(src) + MIN(siz, strlen(initial dst)).
290 * If retval >= siz, truncation occurred.
291 */
292size_t
293strlcat(char *dst, const char *src, size_t siz)
294{
295char *d = dst;
296const char *s = src;
297size_t n = siz;
298size_t dlen;
299
300/* Find the end of dst and adjust bytes left but don't go past end */
301while (n-- != 0 && *d != '\0')
302d++;
303dlen = d - dst;
304n = siz - dlen;
305
306if (n == 0)
307return(dlen + strlen(s));
308while (*s != '\0') {
309if (n != 1) {
310*d++ = *s;
311n--;
312}
313s++;
314}
315*d = '\0';
316
317return(dlen + (s - src)); /* count does not include NUL */
318}
319#endif
320
321/*
322 *
323 */
324
325char *
326strncat(char *s1, const char *s2, unsigned long n)
327{
328char *os1;
329int i = n;
330
331os1 = s1;
332while (*s1++)
333;
334--s1;
335while ((*s1++ = *s2++))
336if (--i < 0) {
337*--s1 = '\0';
338break;
339}
340return(os1);
341}
342
343#if 0
344static int _mach_strlen(const char *str);
345static int
346_mach_strlen(const char *str)
347{
348const char *p;
349for (p = str; p; p++) {
350if (*p == '\0') {
351return (p - str);
352}
353}
354/* NOTREACHED */
355return 0;
356}
357
358size_t strlen(const char * str)
359{
360return (size_t)_mach_strlen(str);
361}
362#endif
363
364#if UNUSED
365/*
366 * Does the same thing as strlen, except only looks up
367 * to max chars inside the buffer.
368 * Taken from archive/kern-stuff/sbf_machine.c in
369 * seatbelt.
370 * inputs:
371 * sstring whose length is to be measured
372 *maxmaximum length of string to search for null
373 * outputs:
374 *length of s or max; whichever is smaller
375 */
376size_t
377strnlen(const char *s, size_t max) {
378const char *es = s + max, *p = s;
379while(*p && p != es)
380p++;
381
382return p - s;
383}
384#endif
385/*
386 * Deprecation Warning:
387 *strcat() is being deprecated. Please use strlcat() instead.
388 */
389char *
390strcat(
391 char *dest,
392 const char *src)
393{
394char *old = dest;
395
396while (*dest)
397++dest;
398while ((*dest++ = *src++))
399;
400return (old);
401}
402
403/*
404 * STRDUP
405 *
406 * Description: The STRDUP function allocates sufficient memory for a copy
407 * of the string "string", does the copy, and returns a pointer
408 * it. The pointer may subsequently be used as an argument to
409 * the macro FREE().
410 *
411 * Parameters: stringString to be duplicated
412 *
413 * Returns: char * A pointer to the newly allocated string with
414 * duplicated contents in it.
415 *
416 * NULLIf MALLOC() fails.
417 *
418 */
419
420static char *
421STRDUP(const char *string)
422{
423size_t len;
424char *copy;
425
426len = strlen(string) + 1;
427copy = malloc(len);
428if (copy == NULL)
429return (NULL);
430bcopy(string, copy, len);
431return (copy);
432}
433
434char *strdup(const char *string)
435{
436if (string) {
437return STRDUP(string);
438}
439return (NULL);
440}
441
442#if STRNCASECMP
443
444//
445// Lame implementation just for use by strcasecmp/strncasecmp
446//
447static int
448tolower(unsigned char ch)
449{
450 if (ch >= 'A' && ch <= 'Z')
451ch = 'a' + (ch - 'A');
452
453 return ch;
454}
455
456int
457strcasecmp(const char *s1, const char *s2)
458{
459 const unsigned char *us1 = (const u_char *)s1,
460*us2 = (const u_char *)s2;
461
462 while (tolower(*us1) == tolower(*us2++))
463if (*us1++ == '\0')
464return (0);
465 return (tolower(*us1) - tolower(*--us2));
466}
467
468int
469strncasecmp(const char *s1, const char *s2, size_t n)
470{
471 if (n != 0) {
472const unsigned char *us1 = (const u_char *)s1,
473*us2 = (const u_char *)s2;
474
475do {
476if (tolower(*us1) != tolower(*us2++))
477return (tolower(*us1) - tolower(*--us2));
478if (*us1++ == '\0')
479break;
480} while (--n != 0);
481 }
482 return (0);
483}
484#endif
485
486/*
487 *
488 */
489
490char *strchr(const char *str, int ch)
491{
492 do {
493if (*str == ch)
494return(__CAST_AWAY_QUALIFIER(str, const, char *));
495 } while (*str++);
496 return ((char *) 0);
497}
498
499char* strbreak(const char *str, char **next, long *len)
500{
501 char *start = (char*)str, *end;
502 bool quoted = false;
503
504 if ( !start || !len )
505 return 0;
506
507 *len = 0;
508
509 while ( isspace(*start) )
510 start++;
511
512 if (*start == '"')
513 {
514 start++;
515
516 end = strchr(start, '"');
517 if(end)
518 quoted = true;
519 else
520 end = strchr(start, '\0');
521 }
522 else
523 {
524 for ( end = start; *end && !isspace(*end); end++ )
525 {}
526 }
527
528 *len = end - start;
529
530 if(next)
531 *next = quoted ? end+1 : end;
532
533 return start;
534}
535
536unsigned long
537adler32( unsigned char * buffer, long length )
538{
539 long cnt;
540 unsigned long result, lowHalf, highHalf;
541
542 lowHalf = 1;
543 highHalf = 0;
544
545for ( cnt = 0; cnt < length; cnt++ )
546 {
547 if ((cnt % 5000) == 0)
548 {
549 lowHalf %= 65521L;
550 highHalf %= 65521L;
551 }
552
553 lowHalf += buffer[cnt];
554 highHalf += lowHalf;
555 }
556
557lowHalf %= 65521L;
558highHalf %= 65521L;
559
560result = (highHalf << 16) | lowHalf;
561
562return result;
563}
564
565/*-
566 * For memcmp, bsearch.
567 * Copyright (c) 1990, 1993
568 *The Regents of the University of California. All rights reserved.
569 *
570 * This code is derived from software contributed to Berkeley by
571 * Chris Torek.
572 *
573 * Redistribution and use in source and binary forms, with or without
574 * modification, are permitted provided that the following conditions
575 * are met:
576 * 1. Redistributions of source code must retain the above copyright
577 * notice, this list of conditions and the following disclaimer.
578 * 2. Redistributions in binary form must reproduce the above copyright
579 * notice, this list of conditions and the following disclaimer in the
580 * documentation and/or other materials provided with the distribution.
581 * 4. Neither the name of the University nor the names of its contributors
582 * may be used to endorse or promote products derived from this software
583 * without specific prior written permission.
584 *
585 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
586 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
587 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
588 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
589 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
590 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
591 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
592 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
593 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
594 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
595 * SUCH DAMAGE.
596 */
597
598#if 0
599/*
600 * Compare memory regions.
601 */
602int
603memcmp(const void *s1, const void *s2, size_t n)
604{
605if (n != 0) {
606const unsigned char *p1 = s1, *p2 = s2;
607
608do {
609if (*p1++ != *p2++)
610return (*--p1 - *--p2);
611} while (--n != 0);
612}
613return (0);
614}
615#endif
616/*
617 * Perform a binary search.
618 *
619 * The code below is a bit sneaky. After a comparison fails, we
620 * divide the work in half by moving either left or right. If lim
621 * is odd, moving left simply involves halving lim: e.g., when lim
622 * is 5 we look at item 2, so we change lim to 2 so that we will
623 * look at items 0 & 1. If lim is even, the same applies. If lim
624 * is odd, moving right again involes halving lim, this time moving
625 * the base up one item past p: e.g., when lim is 5 we change base
626 * to item 3 and make lim 2 so that we will look at items 3 and 4.
627 * If lim is even, however, we have to shrink it by one before
628 * halving: e.g., when lim is 4, we still looked at item 2, so we
629 * have to make lim 3, then halve, obtaining 1, so that we will only
630 * look at item 3.
631 */
632void *
633bsearch(key, base0, nmemb, size, compar)
634register const void *key;
635const void *base0;
636size_t nmemb;
637register size_t size;
638register int (*compar)(const void *, const void *);
639{
640register const char *base = base0;
641register size_t lim;
642register int cmp;
643register const void *p;
644
645for (lim = nmemb; lim != 0; lim >>= 1) {
646p = base + (lim >> 1) * size;
647cmp = (*compar)(key, p);
648if (cmp == 0)
649return ((void *)(uintptr_t)p);
650if (cmp > 0) {/* key > p: move right */
651base = (const char *)p + size;
652lim--;
653}/* else move left */
654}
655return (NULL);
656}
657

Archive Download this file

Revision: 2112