Commit 87c8120b authored by Jonas Termansen's avatar Jonas Termansen

Refactored libmaxsi/string.cpp into multiple files.

However, parts libmaxsi/string.cpp remains as the kernel and parts of the
standard library still rely on <libmaxsi/string.h>.
parent 01df9708
......@@ -93,6 +93,28 @@ truncate.o \
umask.o \
unlink.o \
write.o \
stpcpy.o \
strcasecmp.o \
strcat.o \
strchr.o \
strchrnul.o \
strcmp.o \
strcoll.o \
strcpy.o \
strcspn.o \
strdup.o \
strlen.o \
strncasecmp.o \
strncat.o \
strncmp.o \
strncpy.o \
strnlen.o \
strpbrk.o \
strrchr.o \
strspn.o \
strstr.o \
strtok.o \
strtok_r.o \
UNPROCHEADERDIRS:=$(shell find include -type d)
UNPROCHEADERS:=$(shell find include -type f)
......
......@@ -25,9 +25,9 @@
#include <libmaxsi/platform.h>
#include <libmaxsi/signal.h>
#include <libmaxsi/string.h>
#include <libmaxsi/io.h>
#include <libmaxsi/memory.h>
#include <string.h>
namespace Maxsi
{
......@@ -40,9 +40,7 @@ namespace Maxsi
extern "C" void initialize_standard_library(int argc, char* argv[])
{
if ( argc )
{
String::Copy(program_invocation_name, argv[0]);
}
strcpy(program_invocation_name, argv[0]);
// Initialize stuff such as errno.
init_error_functions();
......
......@@ -107,13 +107,10 @@ namespace Maxsi
return ParseInteger<unsigned long long int, true>(str, endptr, base);
}
// TODO: This conflicts with libmaxsi/string.cpp:Maxsi::ToInt().
#if 0
extern "C" int atoi(const char* str)
{
return strtol(str, (char **) NULL, 10);
return (int) strtol(str, (char **) NULL, 10);
}
#endif
extern "C" long atol(const char* str)
{
......
......@@ -23,7 +23,6 @@
*******************************************************************************/
#include <libmaxsi/platform.h>
#include <libmaxsi/string.h>
#include <libmaxsi/memory.h>
#include <libmaxsi/syscall.h>
#include <libmaxsi/process.h>
......
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi 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 Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
strcpy.cpp
Copy a string returning a pointer to its end.
*******************************************************************************/
#include <string.h>
extern "C" char* stpcpy(char* dest, const char* src)
{
while ( *src )
*dest++ = *src++;
*dest = '\0';
return dest;
}
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi 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 Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
strcasecmp.cpp
Compares two strings ignoring case.
*******************************************************************************/
#include <string.h>
#include <ctype.h>
extern "C" int strcasecmp(const char* a, const char* b)
{
while ( true )
{
char ac = tolower(*a++), bc = tolower(*b++);
if ( ac == '\0' && bc == '\0' )
return 0;
if ( ac < bc )
return -1;
if ( ac > bc )
return 1;
}
}
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi 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 Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
strcat.cpp
Appends a string onto another string.
*******************************************************************************/
#include <string.h>
extern "C" char* strcat(char* dest, const char* src)
{
strcpy(dest + strlen(dest), src);
return dest;
}
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi 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 Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
strchr.cpp
Searches a string for a specific character.
*******************************************************************************/
#include <string.h>
extern "C" char* strchr(const char* str, int c)
{
char* ret = strchrnul(str, c);
if ( !*ret )
return NULL;
return ret;
}
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi 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 Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
strchrnul.cpp
Searches a string for a specific character.
*******************************************************************************/
#include <string.h>
extern "C" char* strchrnul(const char* str, int c)
{
for ( ; *str && *str != c; str++ );
return (char*) str;
}
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi 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 Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
strcmp.cpp
Compares two strings.
*******************************************************************************/
#include <string.h>
extern "C" int strcmp(const char* a, const char* b)
{
while ( true )
{
char ac = *a++, bc = *b++;
if ( ac == '\0' && bc == '\0' )
return 0;
if ( ac < bc )
return -1;
if ( ac > bc )
return 1;
}
}
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi 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 Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
strcoll.cpp
Compare two strings using the current locale.
*******************************************************************************/
#include <string.h>
extern "C" int strcoll(const char* s1, const char* s2)
{
// TODO: Pay attention to locales.
return strcmp(s1, s2);
}
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi 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 Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
strcpy.cpp
Copies a string and returns dest.
*******************************************************************************/
#include <string.h>
extern "C" char* strcpy(char* dest, const char* src)
{
char* origdest = dest;
while ( *src )
*dest++ = *src++;
*dest = '\0';
return origdest;
}
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi 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 Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
strcspn.cpp
Search a string for a set of characters.
*******************************************************************************/
#include <string.h>
extern "C" size_t strcspn(const char* str, const char* reject)
{
size_t rejectlen = 0;
while ( reject[rejectlen] ) { rejectlen++; }
for ( size_t result = 0; true; result++ )
{
char c = str[result];
if ( !c ) { return result; }
bool matches = false;
for ( size_t i = 0; i < rejectlen; i++ )
{
if ( str[result] != reject[i] ) { continue; }
matches = true;
break;
}
if ( matches ) { return result; }
}
}
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi 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 Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
strdup.cpp
Creates a copy of a string.
*******************************************************************************/
#include <stdlib.h>
#include <string.h>
extern "C" char* strdup(const char* input)
{
size_t inputsize = strlen(input);
char* result = (char*) malloc(inputsize + 1);
if ( !result )
return NULL;
memcpy(result, input, inputsize + 1);
return result;
}
......@@ -33,7 +33,7 @@ namespace Maxsi
{
namespace String
{
DUAL_FUNCTION(size_t, strlen, Length, (const char* String))
size_t Length(const char* String)
{
size_t Result = 0;
......@@ -45,14 +45,7 @@ namespace Maxsi
return Result;
}
extern "C" size_t strnlen(const char* str, size_t maxlen)
{
size_t result;
for ( result = 0; maxlen - result && str[result]; result++ ) { }
return result;
}
DUAL_FUNCTION(char*, strcpy, Copy, (char* Dest, const char* Src))
char* Copy(char* Dest, const char* Src)
{
char* OriginalDest = Dest;
......@@ -67,15 +60,7 @@ namespace Maxsi
return OriginalDest;
}
extern "C" char* strncpy(char* dest, const char* src, size_t len)
{
size_t i;
for ( i = 0; i < len && src[i]; i++ ) { dest[i] = src[i]; }
for ( ; i < len; i++ ) { dest[i] = '\0'; }
return dest;
}
DUAL_FUNCTION(char*, strcat, Cat, (char* Dest, const char* Src))
char* Cat(char* Dest, const char* Src)
{
char* OriginalDest = Dest;
......@@ -92,20 +77,7 @@ namespace Maxsi
return OriginalDest;
}
extern "C" char* strncat(char* dest, const char* src, size_t len)
{
size_t destlen = Length(dest);
size_t i;
for ( i = 0; i < len && src[i]; i++ )
{
dest[destlen + i] = src[i];
}
dest[destlen+i] = 0;
return dest;
}
DUAL_FUNCTION(int, strcmp, Compare, (const char* A, const char* B))
int Compare(const char* A, const char* B)
{
while ( true )
{
......@@ -116,7 +88,7 @@ namespace Maxsi
}
}
DUAL_FUNCTION(int, strncmp, CompareN, (const char* A, const char* B, size_t MaxLength))
int CompareN(const char* A, const char* B, size_t MaxLength)
{
while ( MaxLength-- )
{
......@@ -129,39 +101,7 @@ namespace Maxsi
return 0;
}
#ifndef SORTIX_KERNEL
inline int charcasecmp(char a, char b)
{
return tolower(a) == tolower(b);
}
extern "C" int strcasecmp(const char* a, const char* b)
{
while ( true )
{
char achar = *a++;
char bchar = *b++;
if ( !achar && !bchar ) { return 0; }
int cmp = charcasecmp(achar, bchar);
if ( cmp ) { return cmp; }
}
}
extern "C" int strncasecmp(const char* a, const char* b, size_t n)
{
while ( n-- )
{
char achar = *a++;
char bchar = *b++;
if ( !achar && !bchar ) { return 0; }
int cmp = charcasecmp(achar, bchar);
if ( cmp ) { return cmp; }
}
return 0;
}
#endif
DUAL_FUNCTION(size_t, strspn, Accept, (const char* str, const char* accept))
size_t Accept(const char* str, const char* accept)
{
size_t acceptlen = 0;
while ( accept[acceptlen] ) { acceptlen++; }
......@@ -180,7 +120,7 @@ namespace Maxsi
}
}
DUAL_FUNCTION(size_t, strcspn, Reject, (const char* str, const char* reject))
size_t Reject(const char* str, const char* reject)
{
size_t rejectlen = 0;
while ( reject[rejectlen] ) { rejectlen++; }
......@@ -199,14 +139,7 @@ namespace Maxsi
}
}
extern "C" char* strpbrk(const char* str, const char* accept)
{
size_t rejectlen = Reject(str, accept);
if ( !str[rejectlen] ) { return NULL; }
return (char*) str + rejectlen;
}
DUAL_FUNCTION(char*, strtok_r, TokenizeR, (char* str, const char* delim, char** saveptr))
char* TokenizeR(char* str, const char* delim, char** saveptr)
{
if ( !str && !*saveptr ) { return NULL; }
if ( !str ) { str = *saveptr; }
......@@ -219,13 +152,13 @@ namespace Maxsi
return str;
}
DUAL_FUNCTION(char*, strtok, TokenizeR, (char* str, const char* delim))
char* Tokenize(char* str, const char* delim)
{
static char* lasttokensaveptr = NULL;
return TokenizeR(str, delim, &lasttokensaveptr);
}
DUAL_FUNCTION(char*, strchrnul, SeekNul, (const char* str, int c))
char* SeekNul(const char* str, int c)
{
while ( *str )
{
......@@ -235,7 +168,7 @@ namespace Maxsi
return (char*) str;
}
DUAL_FUNCTION(char*, strrchr, SeekReverse, (const char* str, int c))
char* SeekReverse(const char* str, int c)
{
const char* last = NULL;
while ( *str )
......@@ -246,7 +179,7 @@ namespace Maxsi
return (char*) last;
}
DUAL_FUNCTION(char*, strchr, Seek, (const char* str, int c))
char* Seek(const char* str, int c)
{
char* result = SeekNul(str, c);
if ( !*result ) { return NULL; }
......@@ -277,7 +210,7 @@ namespace Maxsi
return dest;
}
DUAL_FUNCTION(int, atoi, ToInt, (const char* str))
int ToInt(const char* str)
{
bool negative = ( *str == '-' );
if ( negative ) { str++; }
......@@ -291,49 +224,6 @@ namespace Maxsi
return (negative) ? -result : result;
}
extern "C" int strcoll(const char* s1, const char* s2)
{
// TODO: Pay attention to locales.
return Compare(s1, s2);
}
extern "C" char* stpcpy(char* s1, const char* s2)
{
char* result = Copy(s1, s2);
result += Length(s1);
return result;
}
// TODO: This simple and hacky implementation runs in O(N^2) even though
// this problem can be solved in O(N).
extern "C" char* strstr(const char* haystack, const char* needle)
{
if ( !needle[0] ) { return (char*) haystack; }
for ( size_t i = 0; haystack[i]; i++ )
{
bool diff = false;
for ( size_t j = 0; needle[j]; j++ )
{
if ( haystack[i+j] != needle[j] ) { diff = true; break; }
}
if ( diff ) { continue; }
return (char*) haystack + i;
}
return NULL;
}
#ifndef SORTIX_KERNEL
extern "C" char* strdup(const char* input)
{
size_t inputsize = strlen(input);
char* result = (char*) malloc(inputsize + 1);
if ( result == NULL ) { return NULL; }
memcpy(result, input, inputsize + 1);
return result;
}
#endif
#if 1
char* Combine(size_t NumParameters, ...)
{
va_list param_pt;
......@@ -376,6 +266,5 @@ namespace Maxsi
return Result;
}
#endif
}
}
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi 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 Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
strlen.cpp
Returns the length of a string.
*******************************************************************************/
#include <string.h>
extern "C" size_t strlen(const char* str)
{
size_t ret = 0;
while ( str[ret] )
ret++;
return ret;
}
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012.
This file is part of LibMaxsi.
LibMaxsi is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
LibMaxsi 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 Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
strncasecmp.cpp
Compares a prefix of two strings ignoring case.
*******************************************************************************/
#include <string.h>
#include <ctype.h>
extern "C" int strncasecmp(const char* a, const char* b, size_t maxcount)
{
while ( --maxcount )
{
char ac = tolower(*a++), bc = tolower(*b++);