mirror of
https://gitlab.com/sortix/sortix.git
synced 2023-02-13 20:55:38 -05:00
Refactor strtol functions.
This commit is contained in:
parent
cdc5a9673e
commit
1df749498a
9 changed files with 253 additions and 102 deletions
|
@ -25,6 +25,8 @@ dirent/alphasort.o \
|
|||
dirent/dir.o \
|
||||
dirent/versionsort.o \
|
||||
errno/errno.o \
|
||||
inttypes/strtoimax.o \
|
||||
inttypes/strtoumax.o \
|
||||
signal/sigaddset.o \
|
||||
signal/sigdelset.o \
|
||||
signal/sigemptyset.o \
|
||||
|
@ -86,7 +88,6 @@ stdlib/bsearch.o \
|
|||
stdlib/calloc.o \
|
||||
stdlib/div.o \
|
||||
stdlib/heap.o \
|
||||
stdlib/integer.o \
|
||||
stdlib/ldiv.o \
|
||||
stdlib/lldiv.o \
|
||||
stdlib/mblen.o \
|
||||
|
@ -96,6 +97,10 @@ stdlib/qsort.o \
|
|||
stdlib/strtod.o \
|
||||
stdlib/strtof.o \
|
||||
stdlib/strtold.o \
|
||||
stdlib/strtoll.o \
|
||||
stdlib/strtol.o \
|
||||
stdlib/strtoull.o \
|
||||
stdlib/strtoul.o \
|
||||
stdlib/wcstombs.o \
|
||||
stdlib/wctomb.o \
|
||||
string/ffsll.o \
|
||||
|
|
29
libc/inttypes/strtoimax.cpp
Normal file
29
libc/inttypes/strtoimax.cpp
Normal file
|
@ -0,0 +1,29 @@
|
|||
/*******************************************************************************
|
||||
|
||||
Copyright(C) Jonas 'Sortie' Termansen 2013.
|
||||
|
||||
This file is part of the Sortix C Library.
|
||||
|
||||
The Sortix C Library 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.
|
||||
|
||||
The Sortix C Library 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 the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
inttypes/strtoimax.cpp
|
||||
Converts integers represented as strings to binary representation.
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#define STRTOL strtoimax
|
||||
#define STRTOL_INT intmax_t
|
||||
#define STRTOL_INT_IS_UNSIGNED false
|
||||
|
||||
#include "../stdlib/strtol.cpp"
|
29
libc/inttypes/strtoumax.cpp
Normal file
29
libc/inttypes/strtoumax.cpp
Normal file
|
@ -0,0 +1,29 @@
|
|||
/*******************************************************************************
|
||||
|
||||
Copyright(C) Jonas 'Sortie' Termansen 2013.
|
||||
|
||||
This file is part of the Sortix C Library.
|
||||
|
||||
The Sortix C Library 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.
|
||||
|
||||
The Sortix C Library 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 the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
inttypes/strtoumax.cpp
|
||||
Converts integers represented as strings to binary representation.
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#define STRTOL strtoumax
|
||||
#define STRTOL_INT uintmax_t
|
||||
#define STRTOL_INT_IS_UNSIGNED true
|
||||
|
||||
#include "../stdlib/strtol.cpp"
|
0
libc/sortix/inttypes/.gitignore
vendored
Normal file
0
libc/sortix/inttypes/.gitignore
vendored
Normal file
|
@ -1,101 +0,0 @@
|
|||
/*******************************************************************************
|
||||
|
||||
Copyright(C) Jonas 'Sortie' Termansen 2011, 2013.
|
||||
|
||||
This file is part of the Sortix C Library.
|
||||
|
||||
The Sortix C Library 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.
|
||||
|
||||
The Sortix C Library 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 the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
stdlib/integer.cpp
|
||||
Converts integers represented as strings to binary representation.
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <inttypes.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static int Debase(char c)
|
||||
{
|
||||
if ( '0' <= c && c <= '9' ) { return c - '0'; }
|
||||
if ( 'a' <= c && c <= 'z' ) { return 10 + c - 'a'; }
|
||||
if ( 'A' <= c && c <= 'Z' ) { return 10 + c - 'A'; }
|
||||
return -1;
|
||||
}
|
||||
|
||||
template <class INT, bool UNSIGNED> INT ParseInteger(const char* str, char** endptr, int base)
|
||||
{
|
||||
const char* origstr = str;
|
||||
int origbase = base;
|
||||
while ( isspace(*str) ) { str++; }
|
||||
if ( base < 0 || 36 < base ) { if ( endptr ) { *endptr = (char*) str; } return 0; }
|
||||
INT result = 0;
|
||||
bool negative = false;
|
||||
char c = *str;
|
||||
if ( !UNSIGNED && c == '-' ) { str++; negative = true; }
|
||||
if ( !UNSIGNED && c == '+' ) { str++; negative = false; }
|
||||
if ( !base && str[0] == '0' )
|
||||
{
|
||||
if ( str[1] == 'x' || str[1] == 'X' ) { str += 2; base = 16; }
|
||||
else if ( 0 <= Debase(str[1]) && Debase(str[1]) < 8 ) { str++; base = 8; }
|
||||
}
|
||||
if ( !base ) { base = 10; }
|
||||
if ( origbase == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X') ) { str += 2; }
|
||||
size_t numconvertedchars = 0;
|
||||
while ( (c = *str ) )
|
||||
{
|
||||
int val = Debase(c);
|
||||
if ( val < 0 ) { break; }
|
||||
if ( base <= val ) { break; }
|
||||
if ( !UNSIGNED && negative ) { val = -val; }
|
||||
// TODO: Detect overflow!
|
||||
result = result * (INT) base + (INT) val;
|
||||
str++;
|
||||
numconvertedchars++;
|
||||
}
|
||||
if ( !numconvertedchars ) { str = origstr; result = 0; }
|
||||
if ( endptr ) { *endptr = (char*) str; }
|
||||
return result;
|
||||
}
|
||||
|
||||
extern "C" long strtol(const char* str, char** endptr, int base)
|
||||
{
|
||||
return ParseInteger<long, false>(str, endptr, base);
|
||||
}
|
||||
|
||||
extern "C" long long strtoll(const char* str, char** endptr, int base)
|
||||
{
|
||||
return ParseInteger<long long, false>(str, endptr, base);
|
||||
}
|
||||
|
||||
extern "C" intmax_t strtoimax(const char* str, char** endptr, int base)
|
||||
{
|
||||
return ParseInteger<intmax_t, false>(str, endptr, base);
|
||||
}
|
||||
|
||||
extern "C" unsigned long strtoul(const char* str, char** endptr, int base)
|
||||
{
|
||||
return ParseInteger<unsigned long, true>(str, endptr, base);
|
||||
}
|
||||
|
||||
extern "C" unsigned long long strtoull(const char* str, char** endptr, int base)
|
||||
{
|
||||
return ParseInteger<unsigned long long, true>(str, endptr, base);
|
||||
}
|
||||
|
||||
extern "C" uintmax_t strtoumax(const char* str, char** endptr, int base)
|
||||
{
|
||||
return ParseInteger<uintmax_t, true>(str, endptr, base);
|
||||
}
|
102
libc/stdlib/strtol.cpp
Normal file
102
libc/stdlib/strtol.cpp
Normal file
|
@ -0,0 +1,102 @@
|
|||
/*******************************************************************************
|
||||
|
||||
Copyright(C) Jonas 'Sortie' Termansen 2011, 2013.
|
||||
|
||||
This file is part of the Sortix C Library.
|
||||
|
||||
The Sortix C Library 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.
|
||||
|
||||
The Sortix C Library 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 the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
stdlib/strtol.cpp
|
||||
Converts integers represented as strings to binary representation.
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef STRTOL
|
||||
#define STRTOL strtol
|
||||
#define STRTOL_INT long
|
||||
#define STRTOL_INT_IS_UNSIGNED false
|
||||
#endif
|
||||
|
||||
#include <ctype.h>
|
||||
#include <inttypes.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static int Debase(char c)
|
||||
{
|
||||
if ( '0' <= c && c <= '9' )
|
||||
return c - '0';
|
||||
if ( 'a' <= c && c <= 'z' )
|
||||
return 10 + c - 'a';
|
||||
if ( 'A' <= c && c <= 'Z' )
|
||||
return 10 + c - 'A';
|
||||
return -1;
|
||||
}
|
||||
|
||||
template <class INT, bool UNSIGNED>
|
||||
INT ParseInteger(const char* str, char** endptr, int base)
|
||||
{
|
||||
const char* origstr = str;
|
||||
int origbase = base;
|
||||
while ( isspace(*str) ) { str++; }
|
||||
if ( base < 0 || 36 < base )
|
||||
{
|
||||
if ( endptr )
|
||||
*endptr = (char*) str;
|
||||
return 0;
|
||||
}
|
||||
INT result = 0;
|
||||
bool negative = false;
|
||||
char c = *str;
|
||||
if ( !UNSIGNED && c == '-' )
|
||||
str++, negative = true;
|
||||
if ( !UNSIGNED && c == '+' )
|
||||
str++, negative = false;
|
||||
if ( !base && str[0] == '0' )
|
||||
{
|
||||
if ( str[1] == 'x' || str[1] == 'X' )
|
||||
str += 2, base = 16;
|
||||
else if ( 0 <= Debase(str[1]) && Debase(str[1]) < 8 )
|
||||
str++, base = 8;
|
||||
}
|
||||
if ( !base )
|
||||
base = 10;
|
||||
if ( origbase == 16 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X') )
|
||||
str += 2;
|
||||
size_t numconvertedchars = 0;
|
||||
while ( (c = *str ) )
|
||||
{
|
||||
int val = Debase(c);
|
||||
if ( val < 0 )
|
||||
break;
|
||||
if ( base <= val )
|
||||
break;
|
||||
if ( !UNSIGNED && negative )
|
||||
val = -val;
|
||||
// TODO: Detect overflow!
|
||||
result = result * (INT) base + (INT) val;
|
||||
str++;
|
||||
numconvertedchars++;
|
||||
}
|
||||
if ( !numconvertedchars )
|
||||
str = origstr, result = 0;
|
||||
if ( endptr )
|
||||
*endptr = (char*) str;
|
||||
return result;
|
||||
}
|
||||
|
||||
extern "C" STRTOL_INT STRTOL(const char* str, char** endptr, int base)
|
||||
{
|
||||
return ParseInteger<STRTOL_INT, STRTOL_INT_IS_UNSIGNED>(str, endptr, base);
|
||||
}
|
29
libc/stdlib/strtoll.cpp
Normal file
29
libc/stdlib/strtoll.cpp
Normal file
|
@ -0,0 +1,29 @@
|
|||
/*******************************************************************************
|
||||
|
||||
Copyright(C) Jonas 'Sortie' Termansen 2013.
|
||||
|
||||
This file is part of the Sortix C Library.
|
||||
|
||||
The Sortix C Library 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.
|
||||
|
||||
The Sortix C Library 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 the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
stdlib/strtoll.cpp
|
||||
Converts integers represented as strings to binary representation.
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#define STRTOL strtoll
|
||||
#define STRTOL_INT long long
|
||||
#define STRTOL_INT_IS_UNSIGNED true
|
||||
|
||||
#include "strtol.cpp"
|
29
libc/stdlib/strtoul.cpp
Normal file
29
libc/stdlib/strtoul.cpp
Normal file
|
@ -0,0 +1,29 @@
|
|||
/*******************************************************************************
|
||||
|
||||
Copyright(C) Jonas 'Sortie' Termansen 2013.
|
||||
|
||||
This file is part of the Sortix C Library.
|
||||
|
||||
The Sortix C Library 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.
|
||||
|
||||
The Sortix C Library 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 the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
stdlib/strtoul.cpp
|
||||
Converts integers represented as strings to binary representation.
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#define STRTOL strtoul
|
||||
#define STRTOL_INT unsigned long
|
||||
#define STRTOL_INT_IS_UNSIGNED true
|
||||
|
||||
#include "strtol.cpp"
|
29
libc/stdlib/strtoull.cpp
Normal file
29
libc/stdlib/strtoull.cpp
Normal file
|
@ -0,0 +1,29 @@
|
|||
/*******************************************************************************
|
||||
|
||||
Copyright(C) Jonas 'Sortie' Termansen 2013.
|
||||
|
||||
This file is part of the Sortix C Library.
|
||||
|
||||
The Sortix C Library 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.
|
||||
|
||||
The Sortix C Library 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 the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
stdlib/strtoull.cpp
|
||||
Converts integers represented as strings to binary representation.
|
||||
|
||||
*******************************************************************************/
|
||||
|
||||
#define STRTOL strtoull
|
||||
#define STRTOL_INT unsigned long long
|
||||
#define STRTOL_INT_IS_UNSIGNED true
|
||||
|
||||
#include "strtol.cpp"
|
Loading…
Add table
Reference in a new issue