diff options
author | Eric Andersen <andersen@codepoet.org> | 2000-07-06 07:16:59 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2000-07-06 07:16:59 +0000 |
commit | afb85e9d6ca1de8f1ecb267e8c30b88ba4382820 (patch) | |
tree | e6b9c1e2d2b9ec890cf832dc0973231dace674c2 | |
parent | 6811c064139574e2e2e07fdeb10218428cd68b36 (diff) |
Rework all the string handling. Make const stuff be constified.
-Erik
-rw-r--r-- | include/string.h | 92 | ||||
-rw-r--r-- | include/utime.h | 2 | ||||
-rw-r--r-- | libc/misc/time/tm_conv.c | 2 | ||||
-rw-r--r-- | libc/string/Makefile | 4 | ||||
-rw-r--r-- | libc/string/strcasecmp.c | 4 | ||||
-rw-r--r-- | libc/string/strcspn.c | 72 | ||||
-rw-r--r-- | libc/string/string.c | 18 | ||||
-rw-r--r-- | libc/string/strncasecmp.c | 4 | ||||
-rw-r--r-- | libc/string/strpbrk.c | 4 | ||||
-rw-r--r-- | libc/string/strsep.c | 3 | ||||
-rw-r--r-- | libc/string/strspn.c | 42 | ||||
-rw-r--r-- | libc/string/strstr.c | 155 | ||||
-rw-r--r-- | libc/string/strtok.c | 2 |
13 files changed, 271 insertions, 133 deletions
diff --git a/include/string.h b/include/string.h index eb448492e..923005de0 100644 --- a/include/string.h +++ b/include/string.h @@ -6,28 +6,56 @@ #include <stddef.h> /* Basic string functions */ -extern size_t strlen __P ((__const char* __str)); -extern char * strcat __P ((char*, __const char*)); -extern char * strcpy __P ((char*, __const char*)); -extern int strcmp __P ((__const char*, __const char*)); +/* Return the length of S. */ +extern size_t strlen __P ((__const char *__s)); +/* Append SRC onto DEST. */ +extern char *strcat __P ((char *__restrict __dest, + __const char *__restrict __src)); +/* Append no more than N characters from SRC onto DEST. */ +extern char *strncat __P ((char *__restrict __dest, + __const char *__restrict __src, size_t __n)); -extern char * strncat __P ((char*, __const char*, size_t)); -extern char * strncpy __P ((char*, __const char*, size_t)); -extern int strncmp __P ((__const char*, __const char*, size_t)); +/* Copy SRC to DEST. */ +extern char *strcpy __P ((char *__restrict __dest, + __const char *__restrict __src)); +/* Copy no more than N characters of SRC to DEST. */ +extern char *strncpy __P ((char *__restrict __dest, + __const char *__restrict __src, size_t __n)); -extern char * strchr __P ((char*, int)); -extern char * strrchr __P ((char*, int)); -extern char * strdup __P ((__const char*)); +/* Compare S1 and S2. */ +extern int strcmp __P ((__const char *__s1, __const char *__s2)); +/* Compare N characters of S1 and S2. */ +extern int strncmp __P ((__const char *__s1, __const char *__s2, size_t __n)); + +/* Find the first occurrence of C in S. */ +extern char *strchr __P ((__const char *__s, int __c)); +/* Find the last occurrence of C in S. */ +extern char *strrchr __P ((__const char *__s, int __c)); +/* Duplicate S, returning an identical malloc'd string. */ +extern char *strdup __P ((__const char *__s)); /* Basic mem functions */ -extern void * memcpy __P ((void*, __const void*, size_t)); -extern void * memccpy __P ((void*, void*, int, size_t)); -extern void * memchr __P ((__const void*, __const int, size_t)); -extern void * memset __P ((void*, int, size_t)); -extern int memcmp __P ((__const void*, __const void*, size_t)); -extern void * memmove __P ((void*, void*, size_t)); +/* Copy N bytes of SRC to DEST. */ +extern __ptr_t memcpy __P ((__ptr_t __restrict __dest, + __const __ptr_t __restrict __src, size_t __n)); +/* Copy no more than N bytes of SRC to DEST, stopping when C is found. + Return the position in DEST one byte past where C was copied, + or NULL if C was not found in the first N bytes of SRC. */ +extern __ptr_t memccpy __P ((__ptr_t __dest, __const __ptr_t __src, + int __c, size_t __n)); +/* Search N bytes of S for C. */ +extern __ptr_t memchr __P ((__const __ptr_t __s, int __c, size_t __n)); +/* Set N bytes of S to C. */ +extern __ptr_t memset __P ((__ptr_t __s, int __c, size_t __n)); +/* Compare N bytes of S1 and S2. */ +extern int memcmp __P ((__const __ptr_t __s1, __const __ptr_t __s2, + size_t __n)); +/* Copy N bytes of SRC to DEST, guaranteeing + correct behavior for overlapping strings. */ +extern __ptr_t memmove __P ((__ptr_t __dest, __const __ptr_t __src, + size_t __n)); /* Minimal (very!) locale support */ #define strcoll strcmp @@ -38,14 +66,30 @@ extern void * memmove __P ((void*, void*, size_t)); #define rindex strrchr /* Other common BSD functions */ -extern int strcasecmp __P ((char*, char*)); -extern int strncasecmp __P ((char*, char*, size_t)); -char *strpbrk __P ((char *, char *)); -char *strsep __P ((char **, char *)); -char *strstr __P ((char *, char *)); -char *strtok __P ((char *, char *)); -size_t strcspn __P ((char *, char *)); -size_t strspn __P ((char *, char *)); + +/* Compare S1 and S2, ignoring case. */ +extern int strcasecmp __P ((__const char *__s1, __const char *__s2)); +/* Compare no more than N chars of S1 and S2, ignoring case. */ +extern int strncasecmp __P ((__const char *__s1, __const char *__s2, + size_t __n)); +/* Find the first occurrence in S of any character in ACCEPT. */ +extern char *strpbrk __P ((__const char *__s, __const char *__accept)); +/* Return the next DELIM-delimited token from *STRINGP, + terminating it with a '\0', and update *STRINGP to point past it. */ +extern char *strsep __P ((char **__restrict __stringp, + __const char *__restrict __delim)); +/* Find the first occurrence of NEEDLE in HAYSTACK. */ +extern char *strstr __P ((__const char *__haystack, __const char *__needle)); +/* Divide S into tokens separated by characters in DELIM. */ +extern char *strtok __P ((char *__restrict __s, + __const char *__restrict __delim)); +/* Return the length of the initial segment of S which + consists entirely of characters not in REJECT. */ +extern size_t strcspn __P ((__const char *__s, __const char *__reject)); +/* Return the length of the initial segment of S which + consists entirely of characters in ACCEPT. */ +extern size_t strspn __P ((__const char *__s, __const char *__accept)); + /* More BSD compatabilty */ #define bcmp memcmp diff --git a/include/utime.h b/include/utime.h index 7f82b9fa6..687e1348e 100644 --- a/include/utime.h +++ b/include/utime.h @@ -9,7 +9,7 @@ struct utimbuf { time_t modtime; }; -extern int utime __P ((char *__filename, struct utimbuf *__utimebuf)); +extern int utime __P ((const char *__filename, struct utimbuf *__utimebuf)); #endif diff --git a/libc/misc/time/tm_conv.c b/libc/misc/time/tm_conv.c index ffd524c4b..ec0073294 100644 --- a/libc/misc/time/tm_conv.c +++ b/libc/misc/time/tm_conv.c @@ -91,7 +91,7 @@ time_t offset; { long days, rem; register int y; - register unsigned short int *ip; + register const unsigned short int *ip; days = *t / SECS_PER_DAY; rem = *t % SECS_PER_DAY; diff --git a/libc/string/Makefile b/libc/string/Makefile index 251948183..b190adb0f 100644 --- a/libc/string/Makefile +++ b/libc/string/Makefile @@ -12,12 +12,12 @@ SOBJ=strlen.o strcat.o strcpy.o strcmp.o strncat.o strncpy.o strncmp.o \ strchr.o strrchr.o strdup.o memcpy.o memccpy.o memchr.o memset.o \ memcmp.o memmove.o movedata.o -OBJ=$(SOBJ) strpbrk.o strsep.o strstr.o strtok.o strcspn.o \ +OBJ=strpbrk.o strsep.o strstr.o strtok.o strcspn.o \ strspn.o strcasecmp.o strncasecmp.o config.o all: $(LIBC) -$(LIBC): $(LIBC)($(OBJ)) +$(LIBC): $(LIBC)($(SOBJ)) $(OBJ) $(LIBC)($(SOBJ)): $(SSRC) $(CC) $(CFLAGS) -DL_$* $< -c -o $*.o diff --git a/libc/string/strcasecmp.c b/libc/string/strcasecmp.c index 0e7b0388f..c6144ff23 100644 --- a/libc/string/strcasecmp.c +++ b/libc/string/strcasecmp.c @@ -8,8 +8,8 @@ int strcasecmp(s, d) -char *s; -char *d; +const char *s; +const char *d; { for(;;) { diff --git a/libc/string/strcspn.c b/libc/string/strcspn.c index 619c8be6b..3630a1333 100644 --- a/libc/string/strcspn.c +++ b/libc/string/strcspn.c @@ -1,32 +1,50 @@ -/* strcspn.c */ +/* Copyright (C) 1991, 1994, 1996, 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. -/* from Schumacher's Atari library, improved */ + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. -#include <string.h> + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. -size_t strcspn(string, set) -register char *string; -char *set; -/* - * Return the length of the sub-string of <string> that consists - * entirely of characters not found in <set>. The terminating '\0' - * in <set> is not considered part of the match set. If the first - * character if <string> is in <set>, 0 is returned. - */ + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#if HAVE_CONFIG_H +# include <config.h> +#endif + +#if defined _LIBC || HAVE_STRING_H +# include <string.h> +#else +# include <strings.h> +# ifndef strchr +# define strchr index +# endif +#endif + +#undef strcspn + +/* Return the length of the maximum initial segment of S + which contains no characters from REJECT. */ +size_t +strcspn (s, reject) + const char *s; + const char *reject; { - register char *setptr; - char *start; - - start = string; - while (*string) - { - setptr = set; - do - if (*setptr == *string) - goto break2; - while (*setptr++); - ++string; - } -break2: - return string - start; + size_t count = 0; + + while (*s != '\0') + if (strchr (reject, *s++) == NULL) + ++count; + else + return count; + + return count; } diff --git a/libc/string/string.c b/libc/string/string.c index 664929b22..0025cec2b 100644 --- a/libc/string/string.c +++ b/libc/string/string.c @@ -270,7 +270,7 @@ lp4: #ifdef L_strchr char * strchr(s, c) -char * s; +const char * s; int c; { #ifdef BCC_AX_ASM @@ -307,7 +307,7 @@ got_it: register char ch; for(;;) { - if( (ch= *s) == c ) return s; + if( (ch= *s) == c ) return (char*)s; if( ch == 0 ) return 0; s++; } @@ -319,11 +319,11 @@ got_it: #ifdef L_strrchr char * strrchr(s, c) -char * s; +const char * s; int c; { register char * prev = 0; - register char * p = s; + register char * p = (char*)s; /* For null it's just like strlen */ if( c == '\0' ) return p+strlen(p); @@ -414,11 +414,12 @@ size_t l; #ifdef L_memccpy void * memccpy(d, s, c, l) /* Do we need a fast one ? */ -void *s, *d; +void *d; +const void *s; int c; size_t l; { - register char *s1=d, *s2=s; + register char *s1=d, *s2=(char*)s; while(l-- > 0) if((*s1++ = *s2++) == c ) return s1; @@ -598,10 +599,11 @@ xit: #ifdef L_memmove void * memmove(d, s, l) -void *d, *s; +void *d; +const void *s; size_t l; { - register char *s1=d, *s2=s; + register char *s1=d, *s2=(char*)s; /* This bit of sneakyness c/o Glibc, it assumes the test is unsigned */ if( s1-s2 >= l ) return memcpy(d,s,l); diff --git a/libc/string/strncasecmp.c b/libc/string/strncasecmp.c index 561f72a76..c55f259d4 100644 --- a/libc/string/strncasecmp.c +++ b/libc/string/strncasecmp.c @@ -8,8 +8,8 @@ int strncasecmp(s, d, l) -char *s; -char *d; +const char *s; +const char *d; size_t l; { while(l>0) diff --git a/libc/string/strpbrk.c b/libc/string/strpbrk.c index 3fc27ec70..7e6b1a4a2 100644 --- a/libc/string/strpbrk.c +++ b/libc/string/strpbrk.c @@ -8,8 +8,8 @@ /* This uses strchr, strchr should be in assembler */ char *strpbrk(str, set) -register char *str; -char *set; +register const char *str; +const char *set; { while (*str != '\0') if (strchr(set, *str) == 0) diff --git a/libc/string/strsep.c b/libc/string/strsep.c index 703fe5c69..f4fdf503e 100644 --- a/libc/string/strsep.c +++ b/libc/string/strsep.c @@ -18,10 +18,11 @@ Cambridge, MA 02139, USA. */ #include <string.h> + char * strsep(pp, delim) char **pp; -char *delim; +const char *delim; { char *p, *q; diff --git a/libc/string/strspn.c b/libc/string/strspn.c index 2094caa86..5f64a6f75 100644 --- a/libc/string/strspn.c +++ b/libc/string/strspn.c @@ -1,33 +1,35 @@ -/* Copyright (C) 1992, 1993 Free Software Foundation, Inc. -This file is part of the GNU C Library. +/* Copyright (C) 1991, 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. -The GNU C Library is free software; you can redistribute it and/or -modify it under the terms of the GNU Library General Public License as -published by the Free Software Foundation; either version 2 of the -License, or (at your option) any later version. + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. -The GNU C Library is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -Library General Public License for more details. + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. -You should have received a copy of the GNU Library General Public -License along with the GNU C Library; see the file COPYING.LIB. If -not, write to the Free Software Foundation, Inc., 675 Mass Ave, -Cambridge, MA 02139, USA. */ + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ #include <string.h> +#undef strspn + /* Return the length of the maximum initial segment of S which contains only characters in ACCEPT. */ size_t -strspn(s, accept) -char *s; -char *accept; +strspn (s, accept) + const char *s; + const char *accept; { - register char *p; - register char *a; - register size_t count = 0; + const char *p; + const char *a; + size_t count = 0; for (p = s; *p != '\0'; ++p) { diff --git a/libc/string/strstr.c b/libc/string/strstr.c index 3c853ac52..03d6c8e5f 100644 --- a/libc/string/strstr.c +++ b/libc/string/strstr.c @@ -1,54 +1,125 @@ -/* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk> - * This file is part of the Linux-8086 C library and is distributed - * under the GNU Library General Public License. - */ +/* Return the offset of one string within another. + Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc. + This file is part of the GNU C Library. -#include <string.h> + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. -#if 1 -/* We've now got a nice fast strchr and memcmp use them */ + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +/* + * My personal strstr() implementation that beats most other algorithms. + * Until someone tells me otherwise, I assume that this is the + * fastest implementation of strstr() in C. + * I deliberately chose not to comment it. You should have at least + * as much fun trying to understand it, as I had to write it :-). + * + * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de */ + +#if HAVE_CONFIG_H +# include <config.h> +#endif + +#if defined _LIBC || defined HAVE_STRING_H +# include <string.h> +#endif + +typedef unsigned chartype; + +#undef strstr char * -strstr(s1, s2) -char *s1; char *s2; +strstr (phaystack, pneedle) + const char *phaystack; + const char *pneedle; { - register int l = strlen(s2); - register char * p = s1; - - if( l==0 ) return p; - - while ((p = strchr(p, *s2))) - { - if( memcmp(p, s2, l) == 0 ) - return p; - p++; - } - return (char *) 0; -} + register const unsigned char *haystack, *needle; + register chartype b, c; -#else -/* This is a nice simple self contained strstr, - now go and work out why the GNU one is faster :-) */ + haystack = (const unsigned char *) phaystack; + needle = (const unsigned char *) pneedle; -char *strstr(str1, str2) -char *str1, *str2; -{ - register char *Sptr, *Tptr; - int len = strlen(str1) -strlen(str2) + 1; + b = *needle; + if (b != '\0') + { + haystack--; /* possible ANSI violation */ + do + { + c = *++haystack; + if (c == '\0') + goto ret0; + } + while (c != b); - if (*str2) - for (; len > 0; len--, str1++){ - if (*str1 != *str2) - continue; + c = *++needle; + if (c == '\0') + goto foundneedle; + ++needle; + goto jin; - for (Sptr = str1, Tptr = str2; *Tptr != '\0'; Sptr++, Tptr++) - if (*Sptr != *Tptr) - break; + for (;;) + { + register chartype a; + register const unsigned char *rhaystack, *rneedle; - if (*Tptr == '\0') - return (char*) str1; - } + do + { + a = *++haystack; + if (a == '\0') + goto ret0; + if (a == b) + break; + a = *++haystack; + if (a == '\0') + goto ret0; +shloop: } + while (a != b); + +jin: a = *++haystack; + if (a == '\0') + goto ret0; + + if (a != c) + goto shloop; - return (char*)0; + rhaystack = haystack-- + 1; + rneedle = needle; + a = *rneedle; + + if (*rhaystack == a) + do + { + if (a == '\0') + goto foundneedle; + ++rhaystack; + a = *++needle; + if (*rhaystack != a) + break; + if (a == '\0') + goto foundneedle; + ++rhaystack; + a = *++needle; + } + while (*rhaystack == a); + + needle = rneedle; /* took the register-poor approach */ + + if (a == '\0') + break; + } + } +foundneedle: + return (char*) haystack; +ret0: + return 0; } -#endif diff --git a/libc/string/strtok.c b/libc/string/strtok.c index 27d8f25b6..c23c5b85e 100644 --- a/libc/string/strtok.c +++ b/libc/string/strtok.c @@ -33,7 +33,7 @@ static char *olds = 0; char * strtok(s, delim) register char *s; -register char *delim; +register const char *delim; { char *token; |