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>.
This commit is contained in:
Jonas 'Sortie' Termansen 2012-07-26 14:17:56 +02:00
parent 01df97080e
commit 87c8120b95
28 changed files with 840 additions and 134 deletions

View File

@ -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)

View File

@ -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();

View File

@ -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)
{

View File

@ -23,7 +23,6 @@
*******************************************************************************/
#include <libmaxsi/platform.h>
#include <libmaxsi/string.h>
#include <libmaxsi/memory.h>
#include <libmaxsi/syscall.h>
#include <libmaxsi/process.h>

33
libmaxsi/stpcpy.cpp Normal file
View File

@ -0,0 +1,33 @@
/*******************************************************************************
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;
}

40
libmaxsi/strcasecmp.cpp Normal file
View File

@ -0,0 +1,40 @@
/*******************************************************************************
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;
}
}

31
libmaxsi/strcat.cpp Normal file
View File

@ -0,0 +1,31 @@
/*******************************************************************************
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;
}

33
libmaxsi/strchr.cpp Normal file
View File

@ -0,0 +1,33 @@
/*******************************************************************************
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;
}

31
libmaxsi/strchrnul.cpp Normal file
View File

@ -0,0 +1,31 @@
/*******************************************************************************
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;
}

39
libmaxsi/strcmp.cpp Normal file
View File

@ -0,0 +1,39 @@
/*******************************************************************************
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;
}
}

31
libmaxsi/strcoll.cpp Normal file
View File

@ -0,0 +1,31 @@
/*******************************************************************************
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);
}

34
libmaxsi/strcpy.cpp Normal file
View File

@ -0,0 +1,34 @@
/*******************************************************************************
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;
}

44
libmaxsi/strcspn.cpp Normal file
View File

@ -0,0 +1,44 @@
/*******************************************************************************
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; }
}
}

36
libmaxsi/strdup.cpp Normal file
View File

@ -0,0 +1,36 @@
/*******************************************************************************
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;
}

View File

@ -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
}
}

33
libmaxsi/strlen.cpp Normal file
View File

@ -0,0 +1,33 @@
/*******************************************************************************
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;
}

41
libmaxsi/strncasecmp.cpp Normal file
View File

@ -0,0 +1,41 @@
/*******************************************************************************
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++);
if ( ac == '\0' && bc == '\0' )
return 0;
if ( ac < bc )
return -1;
if ( ac > bc )
return 1;
}
return 0;
}

35
libmaxsi/strncat.cpp Normal file
View File

@ -0,0 +1,35 @@
/*******************************************************************************
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/>.
strncat.cpp
Appends parts of a string onto another string.
*******************************************************************************/
#include <string.h>
extern "C" char* strncat(char* dest, const char* src, size_t n)
{
size_t dest_len = strlen(dest);
size_t i;
for ( i = 0; i < n && src[i] != '\0'; i++ )
dest[dest_len + i] = src[i];
dest[dest_len + i] = '\0';
return dest;
}

40
libmaxsi/strncmp.cpp Normal file
View File

@ -0,0 +1,40 @@
/*******************************************************************************
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/>.
strncmp.cpp
Compares a prefix of two strings.
*******************************************************************************/
#include <string.h>
extern "C" int strncmp(const char* a, const char* b, size_t maxcount)
{
while ( --maxcount )
{
char ac = *a++, bc = *b++;
if ( ac == '\0' && bc == '\0' )
return 0;
if ( ac < bc )
return -1;
if ( ac > bc )
return 1;
}
return 0;
}

35
libmaxsi/strncpy.cpp Normal file
View File

@ -0,0 +1,35 @@
/*******************************************************************************
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/>.
strncpy.cpp
Copies a string into a fixed size buffer and returns dest.
*******************************************************************************/
#include <string.h>
extern "C" char* strncpy(char* dest, const char* src, size_t n)
{
size_t i;
for ( i = 0; i < n && src[i] != '\0'; i++ )
dest[i] = src[i];
for ( ; i < n; i++ )
dest[i] = '\0';
return dest;
}

33
libmaxsi/strnlen.cpp Normal file
View File

@ -0,0 +1,33 @@
/*******************************************************************************
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/>.
strnlen.cpp
Returns the length of a fixed length string.
*******************************************************************************/
#include <string.h>
extern "C" size_t strnlen(const char* str, size_t maxlen)
{
size_t ret = 0;
while ( ret < maxlen && str[ret] )
ret++;
return ret;
}

33
libmaxsi/strpbrk.cpp Normal file
View File

@ -0,0 +1,33 @@
/*******************************************************************************
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 any of a set of characters.
*******************************************************************************/
#include <string.h>
extern "C" char* strpbrk(const char* str, const char* accept)
{
size_t rejectlen = strcspn(str, accept);
if ( !str[rejectlen] )
return NULL;
return (char*) str + rejectlen;
}

37
libmaxsi/strrchr.cpp Normal file
View File

@ -0,0 +1,37 @@
/*******************************************************************************
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/>.
strrchr.cpp
Searches a string for a specific character.
*******************************************************************************/
#include <string.h>
extern "C" char* strrchr(const char* str, int c)
{
const char* last = NULL;
while ( *str )
{
if ( *str == c )
last = str;
str++;
}
return (char*) last;
}

44
libmaxsi/strspn.cpp Normal file
View File

@ -0,0 +1,44 @@
/*******************************************************************************
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/>.
strspn.cpp
Search a string for a set of characters.
*******************************************************************************/
#include <string.h>
extern "C" size_t strspn(const char* str, const char* accept)
{
size_t acceptlen = 0;
while ( accept[acceptlen] ) { acceptlen++; }
for ( size_t result = 0; true; result++ )
{
char c = str[result];
if ( !c ) { return result; }
bool matches = false;
for ( size_t i = 0; i < acceptlen; i++ )
{
if ( str[result] != accept[i] ) { continue; }
matches = true;
break;
}
if ( !matches ) { return result; }
}
}

45
libmaxsi/strstr.cpp Normal file
View File

@ -0,0 +1,45 @@
/*******************************************************************************
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/>.
strstr.cpp
Locate a substring.
*******************************************************************************/
#include <string.h>
// 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;
}

31
libmaxsi/strtok.cpp Normal file
View File

@ -0,0 +1,31 @@
/*******************************************************************************
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/>.
strtok.cpp
Extract tokens from strings.
*******************************************************************************/
#include <string.h>
extern "C" char* strtok(char* str, const char* delim)
{
static char* lasttokensaveptr = NULL;
return strtok_r(str, delim, &lasttokensaveptr);
}

43
libmaxsi/strtok_r.cpp Normal file
View File

@ -0,0 +1,43 @@
/*******************************************************************************
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/>.
strtok_r.cpp
Extract tokens from strings.
*******************************************************************************/
#include <string.h>
extern "C" char* strtok_r(char* str, const char* delim, char** saveptr)
{
if ( !str && !*saveptr )
return NULL;
if ( !str )
str = *saveptr;
str += strspn(str, delim); // Skip leading
if ( !*str )
return *saveptr = NULL;
size_t amount = strcspn(str, delim);
if ( str[amount] )
*saveptr = str + amount + 1;
else
*saveptr = NULL;
str[amount] = '\0';
return str;
}

View File

@ -23,7 +23,6 @@
*******************************************************************************/
#include <libmaxsi/platform.h>
#include <libmaxsi/string.h>
#include <libmaxsi/memory.h>
#include <libmaxsi/syscall.h>
#include <errno.h>