2013-07-18 22:24:37 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2015-05-13 16:26:24 +00:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2011, 2013, 2014, 2015.
|
2013-07-18 22:24:37 +00:00
|
|
|
|
|
|
|
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/>.
|
|
|
|
|
2016-02-28 11:11:02 +00:00
|
|
|
stdlib/strtol.c
|
2013-07-18 22:24:37 +00:00
|
|
|
Converts integers represented as strings to binary representation.
|
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
#ifndef STRTOL
|
|
|
|
#define STRTOL strtol
|
2013-07-19 20:36:11 +00:00
|
|
|
#define STRTOL_CHAR char
|
2015-05-13 16:26:24 +00:00
|
|
|
#define STRTOL_UCHAR unsigned char
|
2013-07-19 20:36:11 +00:00
|
|
|
#define STRTOL_L(x) x
|
|
|
|
#define STRTOL_ISSPACE isspace
|
2013-07-18 22:24:37 +00:00
|
|
|
#define STRTOL_INT long
|
2013-07-19 17:58:21 +00:00
|
|
|
#define STRTOL_UNSIGNED_INT unsigned long
|
|
|
|
#define STRTOL_INT_MIN LONG_MIN
|
|
|
|
#define STRTOL_INT_MAX LONG_MAX
|
2013-07-18 22:24:37 +00:00
|
|
|
#define STRTOL_INT_IS_UNSIGNED false
|
|
|
|
#endif
|
|
|
|
|
2013-07-19 17:58:21 +00:00
|
|
|
#include <assert.h>
|
2013-07-18 22:24:37 +00:00
|
|
|
#include <ctype.h>
|
2013-07-19 17:58:21 +00:00
|
|
|
#include <errno.h>
|
2013-07-18 22:24:37 +00:00
|
|
|
#include <inttypes.h>
|
2016-02-28 11:11:02 +00:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdbool.h>
|
2013-07-18 22:24:37 +00:00
|
|
|
#include <stddef.h>
|
2013-07-19 17:58:21 +00:00
|
|
|
#include <stdint.h>
|
2013-07-18 22:24:37 +00:00
|
|
|
#include <stdlib.h>
|
2013-07-19 20:36:11 +00:00
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
2013-07-18 22:24:37 +00:00
|
|
|
|
2013-07-19 17:58:21 +00:00
|
|
|
// Convert a character into a digit.
|
2013-10-21 17:46:38 +00:00
|
|
|
static int debase(STRTOL_CHAR c)
|
2013-07-18 22:24:37 +00:00
|
|
|
{
|
2013-07-19 20:36:11 +00:00
|
|
|
if ( STRTOL_L('0') <= c && c <= STRTOL_L('9') )
|
|
|
|
return c - STRTOL_L('0');
|
|
|
|
if ( STRTOL_L('a') <= c && c <= STRTOL_L('z') )
|
|
|
|
return 10 + c - STRTOL_L('a');
|
|
|
|
if ( STRTOL_L('A') <= c && c <= STRTOL_L('Z') )
|
|
|
|
return 10 + c - STRTOL_L('A');
|
2013-07-18 22:24:37 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-07-19 20:36:11 +00:00
|
|
|
STRTOL_INT STRTOL(const STRTOL_CHAR* restrict str,
|
2014-09-14 18:03:16 +00:00
|
|
|
STRTOL_CHAR** restrict end_ptr,
|
2013-07-19 20:36:11 +00:00
|
|
|
int base)
|
2013-07-18 22:24:37 +00:00
|
|
|
{
|
2013-07-19 17:58:21 +00:00
|
|
|
// Reject bad bases.
|
2014-09-14 18:03:16 +00:00
|
|
|
if ( base < 0 || base == 1 || 36 < base )
|
2013-07-18 22:24:37 +00:00
|
|
|
{
|
2014-09-14 18:03:16 +00:00
|
|
|
if ( end_ptr )
|
|
|
|
*end_ptr = (STRTOL_CHAR*) str;
|
2013-07-19 17:58:21 +00:00
|
|
|
return errno = EINVAL, 0;
|
2013-07-18 22:24:37 +00:00
|
|
|
}
|
2013-07-19 17:58:21 +00:00
|
|
|
|
2014-09-14 18:03:16 +00:00
|
|
|
const STRTOL_CHAR* original_str = str;
|
|
|
|
|
|
|
|
// Skip any leading white space.
|
2015-05-13 16:26:24 +00:00
|
|
|
while ( STRTOL_ISSPACE((STRTOL_UCHAR) *str) )
|
2014-09-14 18:03:16 +00:00
|
|
|
str++;
|
|
|
|
|
2013-07-18 22:24:37 +00:00
|
|
|
bool negative = false;
|
2013-07-19 20:36:11 +00:00
|
|
|
STRTOL_CHAR c = *str;
|
2013-07-19 17:58:21 +00:00
|
|
|
|
|
|
|
// Handle a leading sign character.
|
2013-07-19 20:36:11 +00:00
|
|
|
if ( c == STRTOL_L('-') )
|
2013-07-18 22:24:37 +00:00
|
|
|
str++, negative = true;
|
2014-03-09 18:57:49 +00:00
|
|
|
else if ( c == STRTOL_L('+') )
|
2013-07-18 22:24:37 +00:00
|
|
|
str++, negative = false;
|
2013-07-19 17:58:21 +00:00
|
|
|
|
2014-09-14 18:03:16 +00:00
|
|
|
bool actually_negative = !STRTOL_INT_IS_UNSIGNED && negative;
|
|
|
|
|
|
|
|
// Autodetect base if requested.
|
|
|
|
if ( base == 0 )
|
2013-07-18 22:24:37 +00:00
|
|
|
{
|
2014-09-14 18:03:16 +00:00
|
|
|
if ( str[0] == STRTOL_L('0') &&
|
|
|
|
(str[1] == STRTOL_L('x') || str[1] == STRTOL_L('X')) &&
|
|
|
|
(0 <= debase(str[2]) && debase(str[2]) < 16) )
|
2013-07-18 22:24:37 +00:00
|
|
|
str += 2, base = 16;
|
2014-09-14 18:03:16 +00:00
|
|
|
else if ( str[0] == STRTOL_L('0') &&
|
|
|
|
0 <= debase(str[1]) && debase(str[1]) < 8 )
|
2013-07-18 22:24:37 +00:00
|
|
|
str++, base = 8;
|
2014-09-14 18:03:16 +00:00
|
|
|
else
|
|
|
|
base = 10;
|
2013-07-18 22:24:37 +00:00
|
|
|
}
|
2013-07-19 17:58:21 +00:00
|
|
|
|
|
|
|
// Skip the leading '0x' prefix in base 16 for hexadecimal integers.
|
2014-09-14 18:03:16 +00:00
|
|
|
else if ( base == 16 )
|
|
|
|
{
|
|
|
|
if ( str[0] == STRTOL_L('0') &&
|
|
|
|
(str[1] == STRTOL_L('x') || str[1] == STRTOL_L('X')) &&
|
|
|
|
(0 <= debase(str[2]) && debase(str[2]) < 16) )
|
|
|
|
str += 2;
|
|
|
|
}
|
2013-07-19 17:58:21 +00:00
|
|
|
|
|
|
|
// Determine what value will be returned on overflow/underflow.
|
2014-09-14 18:03:16 +00:00
|
|
|
STRTOL_INT overflow_value =
|
|
|
|
actually_negative ? STRTOL_INT_MIN : STRTOL_INT_MAX;
|
2013-07-19 17:58:21 +00:00
|
|
|
|
|
|
|
// Convert a single character at a time.
|
|
|
|
STRTOL_INT result = 0;
|
2014-09-14 18:03:16 +00:00
|
|
|
size_t num_converted_chars = 0;
|
2013-07-19 17:58:21 +00:00
|
|
|
bool overflow_occured = false;
|
2013-07-18 22:24:37 +00:00
|
|
|
while ( (c = *str ) )
|
|
|
|
{
|
2014-09-14 18:03:16 +00:00
|
|
|
// Stop if we encounter a character that doesn't fit in this base.
|
2013-07-19 17:58:21 +00:00
|
|
|
int val = debase(c);
|
|
|
|
if ( val < 0 || base <= val )
|
2013-07-18 22:24:37 +00:00
|
|
|
break;
|
2013-07-19 17:58:21 +00:00
|
|
|
|
2014-09-14 18:03:16 +00:00
|
|
|
str++;
|
|
|
|
num_converted_chars++;
|
|
|
|
|
|
|
|
if ( overflow_occured )
|
|
|
|
continue;
|
|
|
|
|
2013-07-19 17:58:21 +00:00
|
|
|
// Attempt to multiply the accumulator with the current base.
|
2016-02-28 11:11:02 +00:00
|
|
|
if ( __builtin_mul_overflow(result, base, &result) )
|
2014-09-14 18:03:16 +00:00
|
|
|
{
|
|
|
|
overflow_occured = true;
|
|
|
|
result = overflow_value;
|
|
|
|
errno = ERANGE;
|
|
|
|
continue;
|
|
|
|
}
|
2013-07-19 17:58:21 +00:00
|
|
|
|
2014-09-14 18:03:16 +00:00
|
|
|
// Nothing needs to be added if we are encountered a zero digit.
|
|
|
|
if ( val == 0 )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-07-19 17:58:21 +00:00
|
|
|
// Attempt to add the latest digit to the accumulator (positive).
|
2014-09-14 18:03:16 +00:00
|
|
|
else if ( !actually_negative &&
|
|
|
|
(STRTOL_INT) val <= (STRTOL_INT) (STRTOL_INT_MAX - result) )
|
|
|
|
{
|
2013-07-19 17:58:21 +00:00
|
|
|
result += (STRTOL_INT) val;
|
2014-09-14 18:03:16 +00:00
|
|
|
}
|
|
|
|
|
2013-07-19 17:58:21 +00:00
|
|
|
|
|
|
|
// Attempt to subtract the latest digit to the accumulator (negative).
|
2014-09-14 18:03:16 +00:00
|
|
|
else if ( actually_negative &&
|
|
|
|
(STRTOL_UNSIGNED_INT) val <
|
|
|
|
((STRTOL_UNSIGNED_INT) result -
|
|
|
|
(STRTOL_UNSIGNED_INT) STRTOL_INT_MIN) )
|
|
|
|
{
|
2013-07-19 17:58:21 +00:00
|
|
|
result -= (STRTOL_INT) val;
|
2014-09-14 18:03:16 +00:00
|
|
|
}
|
2013-07-19 17:58:21 +00:00
|
|
|
|
2014-09-14 18:03:16 +00:00
|
|
|
// Otherwise, the addition/subtract would overflow/underflow.
|
2013-07-19 17:58:21 +00:00
|
|
|
else
|
2014-09-14 18:03:16 +00:00
|
|
|
{
|
|
|
|
overflow_occured = true;
|
|
|
|
result = overflow_value;
|
|
|
|
errno = ERANGE;
|
|
|
|
continue;
|
|
|
|
}
|
2013-07-18 22:24:37 +00:00
|
|
|
}
|
2013-07-19 17:58:21 +00:00
|
|
|
|
2014-09-14 18:03:16 +00:00
|
|
|
// If no characters were successfully converted, rewind to the start.
|
|
|
|
if ( !num_converted_chars )
|
|
|
|
{
|
|
|
|
errno = EINVAL;
|
|
|
|
str = original_str;
|
|
|
|
}
|
2013-07-19 17:58:21 +00:00
|
|
|
|
|
|
|
// Let the caller know where we got to.
|
2014-09-14 18:03:16 +00:00
|
|
|
if ( end_ptr )
|
|
|
|
*end_ptr = (STRTOL_CHAR*) str;
|
2013-07-18 22:24:37 +00:00
|
|
|
|
2013-07-19 17:58:21 +00:00
|
|
|
// Handle the special case where we are creating an unsigned integer and the
|
2014-09-14 18:03:16 +00:00
|
|
|
// string was negative. The result is the negation assuming no overflow.
|
|
|
|
if ( STRTOL_INT_IS_UNSIGNED && negative )
|
|
|
|
{
|
|
|
|
if ( overflow_occured )
|
|
|
|
result = STRTOL_INT_MAX;
|
|
|
|
else
|
|
|
|
result = -result;
|
|
|
|
}
|
2013-07-19 17:58:21 +00:00
|
|
|
|
|
|
|
return result;
|
2013-07-18 22:24:37 +00:00
|
|
|
}
|