improved the speed somewhat

fixed bug with negative chars in comparisons
This commit is contained in:
eck 1990-08-28 13:54:46 +00:00
parent cd1f6c38a4
commit a41c51783a
17 changed files with 104 additions and 85 deletions

View file

@ -7,15 +7,17 @@
#include <string.h>
void *
memchr(const void *s, int c, register size_t n)
memchr(const void *s, register int c, register size_t n)
{
register const unsigned char *s1 = s;
unsigned char c1 = (unsigned char) c;
while (n > 0) {
n--;
if (*s1++ == c1)
return (void *) (s1 - 1);
c = (unsigned char) c;
if (n) {
n++;
while (--n > 0) {
if (*s1++ != c) continue;
return (void *) --s1;
}
}
return NULL;
}

View file

@ -11,10 +11,12 @@ memcmp(const void *s1, const void *s2, size_t n)
{
register const char *p1 = s1, *p2 = s2;
while (n > 0) {
n--;
if (*p1++ != *p2++)
if (n) {
n++;
while (--n > 0) {
if (*p1++ == *p2++) continue;
return *--p1 - *--p2;
}
}
return 0;
}

View file

@ -13,9 +13,11 @@ memcpy(void *s1, const void *s2, register size_t n)
register const char *p2 = s2;
while (n > 0) {
n--;
*p1++ = *p2++;
if (n) {
n++;
while (--n > 0) {
*p1++ = *p2++;
}
}
return s1;
}

View file

@ -12,18 +12,21 @@ memmove(void *s1, const void *s2, register size_t n)
register char *p1 = s1;
register const char *p2 = s2;
if (p2 <= p1 && p2 + (n-1) >= p1) {
/* overlap, copy backwards */
p1 += n;
p2 += n;
while (n > 0) {
n--;
*--p1 = *--p2;
}
} else
while (n > 0) {
n--;
*p1++ = *p2++;
if (n>0) {
if (p2 <= p1 && p2 + n > p1) {
/* overlap, copy backwards */
p1 += n;
p2 += n;
n++;
while (--n > 0) {
*--p1 = *--p2;
}
} else {
n++;
while (--n > 0) {
*p1++ = *p2++;
}
}
}
return s1;
}

View file

@ -7,13 +7,15 @@
#include <string.h>
void *
memset(void *s, int c, register size_t n)
memset(void *s, register int c, register size_t n)
{
register char *s1 = s;
while (n > 0) {
n--;
*s1++ = c;
if (n>0) {
n++;
while (--n > 0) {
*s1++ = c;
}
}
return s;
}

View file

@ -7,14 +7,12 @@
#include <string.h>
char *
strchr(register const char *s, int c)
strchr(register const char *s, register int c)
{
register char c1 = (char) c;
c = (char) c;
do {
if (*s == c1)
return (char *)s;
} while (*s++ != '\0');
return (char *)NULL;
while (c != *s) {
if (*s++ == '\0') return NULL;
}
return (char *)s;
}

View file

@ -9,11 +9,12 @@
int
strcmp(register const char *s1, register const char *s2)
{
for(;;) {
if (*s1 != *s2)
return *s1 - *s2;
if (*s1++ == '\0')
while (*s1 == *s2++) {
if (*s1++ == '\0') {
return 0;
s2++;
}
}
if (*s1 == '\0') return -1;
if (*--s2 == '\0') return 1;
return *s1 - *s2;
}

View file

@ -10,11 +10,10 @@
int
strcoll(register const char *s1, register const char *s2)
{
for(;;) {
if (*s1 != *s2)
return *s1 - *s2;
if (*s1++ == '\0')
while (*s1 == *s2++) {
if (*s1++ == '\0') {
return 0;
s2++;
}
}
return *s1 - *--s2;
}

View file

@ -6,6 +6,10 @@
#include <string.h>
/*
* I don't know why, but X3J11 says that strerror() should be in declared
* in <string.h>. That is why the function is defined here.
*/
char *
strerror(register int errnum)
{

View file

@ -14,5 +14,5 @@ strlen(const char *org)
while (*s++)
/* EMPTY */ ;
return (s - 1) - org;
return --s - org;
}

View file

@ -11,16 +11,15 @@ strncat(char *ret, register const char *s2, size_t n)
{
register char *s1 = ret;
if (n <= 0)
return s1;
while (*s1++)
/* EMPTY */ ;
s1--;
while (*s1++ = *s2++) {
if (--n == 0) {
if (n > 0) {
while (*s1++)
/* EMPTY */ ;
s1--;
while (*s1++ = *s2++) {
if (--n > 0) continue;
*s1 = '\0';
break;
}
}
return ret;
return ret;
} else return s1;
}

View file

@ -7,15 +7,20 @@
#include <string.h>
int
strncmp(register const char *s1, register const char *s2, size_t n)
strncmp(register const char *s1, register const char *s2, register size_t n)
{
while (n > 0) {
n--;
if (*s1 != *s2)
if (n) {
do {
if (*s1 != *s2++)
break;
if (*s1++ == '\0')
return 0;
} while (--n > 0);
if (n > 0) {
if (*s1 == '\0') return -1;
if (*--s2 == '\0') return 1;
return *s1 - *s2;
if (*s1++ == '\0')
return 0;
s2++;
}
}
return 0;
}

View file

@ -7,17 +7,18 @@
#include <string.h>
char *
strncpy(char *ret, register const char *s2, size_t n)
strncpy(char *ret, register const char *s2, register size_t n)
{
register char *s1 = ret;
while (*s2 && n > 0) {
n--;
*s1++ = *s2++;
}
while (n > 0) {
n--;
*s1++ = '\0';
if (n>0) {
while((*s1++ = *s2++) && --n > 0)
/* EMPTY */ ;
if ((*--s2 == '\0') && --n > 0) {
do {
*s1++ = '\0';
} while(--n > 0);
}
}
return ret;
}

View file

@ -9,14 +9,14 @@
char *
strrchr(register const char *s, int c)
{
register const char *result;
register char c1 = (char) c;
register const char *result = NULL;
c = (char) c;
result = (char *)NULL;
do {
if (*s == c1)
if (c == *s)
result = s;
} while (*s++);
} while (*s++ != '\0');
return (char *)result;
}

View file

@ -9,7 +9,7 @@
char *
strstr(register const char *s, register const char *wanted)
{
int len = strlen(wanted);
register const int len = strlen(wanted);
if (len == 0) return (char *)s;
while (*s != *wanted || strncmp(s, wanted, len))

View file

@ -12,18 +12,19 @@ strtok(register char *string, const char *separators)
register char *s1, *s2;
static char *savestring;
if (!string)
if (string == NULL) {
string = savestring;
if (string == NULL) return (char *)NULL;
}
if (!string)
return (char *)NULL;
if (*(s1 = string + strspn(string, separators)) == '\0') {
s1 = string + strspn(string, separators);
if (*s1 == '\0') {
savestring = NULL;
return (char *)NULL;
}
if (s2 = strpbrk(s1, separators))
s2 = strpbrk(s1, separators);
if (s2 != NULL)
*s2++ = '\0';
savestring = s2;
return s1;

View file

@ -18,7 +18,7 @@ strxfrm(register char *s1, register const char *save, register size_t n)
} else
s2++;
}
if (n >= 1)
if (n > 0)
*s1++ = '\0';
return s2 - save;
}