1
0
mirror of https://gitlab.os-k.eu/os-k-team/os-k.git synced 2023-08-25 14:03:10 +02:00
os-k/kaleid/libc/strtol.c

161 lines
4.7 KiB
C
Raw Normal View History

2019-01-21 15:00:04 +01:00
//----------------------------------------------------------------------------//
2020-09-27 17:33:48 +02:00
// OS on Kaleid //
2019-01-21 15:00:04 +01:00
// //
2019-02-16 23:36:33 +01:00
// Desc: strto*l() family //
2019-01-21 15:00:04 +01:00
// //
2019-02-16 23:36:33 +01:00
// //
2021-02-18 19:54:35 +01:00
// Copyright © 2018-2021 The OS/K Team //
2019-02-16 23:36:33 +01:00
// //
// This file is part of OS/K. //
// //
// OS/K is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// any later version. //
// //
// OS/K 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 General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
2019-01-21 15:00:04 +01:00
//----------------------------------------------------------------------------//
2019-05-13 23:22:27 +02:00
#include <libc.h>
2020-02-19 22:19:58 +01:00
#include <libbuf.h>
2019-01-21 15:00:04 +01:00
long strtol(const char *str, char **endp, int base) {
2019-12-07 17:54:48 +01:00
ulong n;
bool neg = 0;
const char *save, *start = str;
2020-01-20 20:01:35 +01:00
assert(str != NULL);
char c = *str;
2019-12-07 17:54:48 +01:00
// Ignore leading spaces
2020-01-20 20:01:35 +01:00
while (isspace(c)) {
2019-12-07 17:54:48 +01:00
c = *str++;
2020-01-20 20:01:35 +01:00
}
2019-12-07 17:54:48 +01:00
// Accept any +/-'s, whatever the base
// In particular we accept things like "-0xF"
if (c == '+')
c = *str++;
else if (c == '-') {
c = *str++;
neg = 1;
}
// Accept 0/0b/0B/0x/0X and guess base if needed
if (c == '0') {
c = *str++;
if ((base == 0 || base == 16) && (c == 'x' || c == 'X')) {
c = *str++;
base = 16;
}
else if ((base == 0 || base == 2) && (c == 'b' || c == 'B')) {
c = *str++;
base = 2;
}
else if (base == 0)
base = 8;
}
// base==0 but no leading '0' - assume it's decimal
if (base == 0)
base = 10;
//
// Extract the number from the string
// We do not check whether the number actually fits
// That's why our accumulator 'n' needs to be unsigned, otherwise
// containing a number too large would cause a signed overflow and UB
//
save = str;
for (n = 0;; c = *str) {
if (isdigit(c))
c -= '0';
else if (isalpha(c))
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
else
break;
if (c >= base)
break;
else {
str++;
n = (n * base) + c;
}
}
// Save where we stopped in *endp (optional)
// If 'str' is still '== save', no digit were consummed
if (endp != NULL)
*endp = (char *)(str == save ? start : str);
return neg ? -(long)n : (long)n;
2019-01-21 15:00:04 +01:00
}
2019-12-07 17:54:48 +01:00
//
// Made from the code above by removing "neg"
//
2019-01-21 15:00:04 +01:00
ulong strtoul(const char *str, char **endp, int base) {
2019-12-07 17:54:48 +01:00
ulong n;
const char *save, *start = str;
2020-01-20 20:01:35 +01:00
assert(str != NULL);
char c = *str;
while (isspace(c)) {
c = *str++;
}
2019-12-07 17:54:48 +01:00
2020-01-20 20:01:35 +01:00
if (c == '+')
2019-12-07 17:54:48 +01:00
c = *str++;
if (c == '0') {
c = *str++;
if ((base == 0 || base == 16) && (c == 'x' || c == 'X')) {
c = *str++;
base = 16;
}
else if ((base == 0 || base == 2) && (c == 'b' || c == 'B')) {
c = *str++;
base = 2;
}
else if (base == 0)
base = 8;
}
if (base == 0)
base = 10;
save = str;
for (n = 0;; c = *str) {
if (isdigit(c))
c -= '0';
else if (isalpha(c))
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
else
break;
if (c >= base)
break;
else {
str++;
n = (n * base) + c;
}
}
if (endp != NULL)
*endp = (char *)(str == save ? start : str);
2020-01-20 20:01:35 +01:00
return (long)n;
2019-01-21 15:00:04 +01:00
}