Fix poor implementation of the strchr(3) family.

This commit is contained in:
Jonas 'Sortie' Termansen 2013-09-24 23:18:57 +02:00
parent e72b1c0ac1
commit d6c1e64628
3 changed files with 16 additions and 14 deletions

View File

@ -24,8 +24,8 @@
#include <string.h> #include <string.h>
extern "C" char* strchr(const char* str, int c) extern "C" char* strchr(const char* str, int uc)
{ {
char* ret = strchrnul(str, c); char* ret = strchrnul(str, uc);
return ret && c == ret[0] ? ret : NULL; return uc == ((unsigned char*) ret)[0] ? ret : NULL;
} }

View File

@ -24,10 +24,10 @@
#include <string.h> #include <string.h>
extern "C" char* strchrnul(const char* str, int c) extern "C" char* strchrnul(const char* str, int uc)
{ {
for ( ; *str != c; str++ ) const unsigned char* ustr = (const unsigned char*) str;
if ( !*str ) for ( size_t i = 0; true; i++)
return NULL; if ( ustr[i] == uc || !ustr[i] )
return (char*) str; return (char*) str + i;
} }

View File

@ -1,6 +1,6 @@
/******************************************************************************* /*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013.
This file is part of the Sortix C Library. This file is part of the Sortix C Library.
@ -24,14 +24,16 @@
#include <string.h> #include <string.h>
extern "C" char* strrchr(const char* str, int c) extern "C" char* strrchr(const char* str, int uc)
{ {
const unsigned char* ustr = (const unsigned char*) str;
const char* last = NULL; const char* last = NULL;
while ( *str ) for ( size_t i = 0; true; i++ )
{ {
if ( *str == c ) if ( ustr[i] == uc )
last = str; last = str + i;
str++; if ( !ustr[i] )
break;
} }
return (char*) last; return (char*) last;
} }