From afb85e9d6ca1de8f1ecb267e8c30b88ba4382820 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Thu, 6 Jul 2000 07:16:59 +0000 Subject: Rework all the string handling. Make const stuff be constified. -Erik --- libc/misc/time/tm_conv.c | 2 +- libc/string/Makefile | 4 +- libc/string/strcasecmp.c | 4 +- libc/string/strcspn.c | 72 +++++++++++++-------- libc/string/string.c | 18 +++--- libc/string/strncasecmp.c | 4 +- libc/string/strpbrk.c | 4 +- libc/string/strsep.c | 3 +- libc/string/strspn.c | 42 +++++++------ libc/string/strstr.c | 155 +++++++++++++++++++++++++++++++++------------- libc/string/strtok.c | 2 +- 11 files changed, 202 insertions(+), 108 deletions(-) (limited to 'libc') 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 + 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 that consists - * entirely of characters not found in . The terminating '\0' - * in is not considered part of the match set. If the first - * character if is in , 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 +#endif + +#if defined _LIBC || HAVE_STRING_H +# include +#else +# include +# 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 + 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 +#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 - * 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 + 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 +#endif + +#if defined _LIBC || defined HAVE_STRING_H +# include +#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; -- cgit v1.2.3