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>
extern "C" char* strchr(const char* str, int c)
extern "C" char* strchr(const char* str, int uc)
{
char* ret = strchrnul(str, c);
return ret && c == ret[0] ? ret : NULL;
char* ret = strchrnul(str, uc);
return uc == ((unsigned char*) ret)[0] ? ret : NULL;
}

View File

@ -24,10 +24,10 @@
#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++ )
if ( !*str )
return NULL;
return (char*) str;
const unsigned char* ustr = (const unsigned char*) str;
for ( size_t i = 0; true; i++)
if ( ustr[i] == uc || !ustr[i] )
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.
@ -24,14 +24,16 @@
#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;
while ( *str )
for ( size_t i = 0; true; i++ )
{
if ( *str == c )
last = str;
str++;
if ( ustr[i] == uc )
last = str + i;
if ( !ustr[i] )
break;
}
return (char*) last;
}