ciabatta/code/stdlib/conv.c

342 lines
8.2 KiB
C
Raw Normal View History

2022-06-03 09:31:16 +00:00
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
#include <limits.h>
#include <errno.h>
2022-06-05 22:02:54 +00:00
#include <math.h>
2022-06-03 09:31:16 +00:00
2022-06-05 22:02:54 +00:00
// TODO: strto*: locale-based parsing
// TODO: correctly rounded base 16 floats parsing
2022-06-03 09:31:16 +00:00
// Call me weak if you want but I'm actually too lazy to type
// them out every time, also they take up a lot of horiz space.
2022-06-06 00:26:32 +00:00
typedef long long intll;
typedef long intl;
typedef unsigned long long intull;
typedef unsigned long intul;
typedef unsigned intu;
2022-06-03 09:31:16 +00:00
#define inrange(start, c, end) ((start) <= (c) && (c) <= (end))
static bool isbase(int c, int base) {
int val;
if(isdigit(c)) {
val = c-'0';
}
else if(islower(c)) {
val = c-'a'+10;
}
else if(isupper(c)) {
val = c-'A'+10;
}
else {
return false;
}
return val < base;
}
2022-06-06 00:26:32 +00:00
static bool strprefix_i(char const *restrict str, char const *restrict prefix) {
2022-06-05 22:02:54 +00:00
while(*prefix != 0) {
if(*str == 0) break;
if(toupper(*str) != toupper(*prefix)) return false;
++prefix;
++str;
}
return true;
}
2022-06-03 09:31:16 +00:00
// Called only when isbase(c, base) for some base in range
2022-06-05 22:02:54 +00:00
static long todigit(int c) {
2022-06-03 09:31:16 +00:00
int val;
if(isdigit(c)) {
val = c-'0';
}
else if(islower(c)) {
val = c-'a'+10;
}
else if(isupper(c)) {
val = c-'A'+10;
}
return val;
}
2022-06-06 00:26:32 +00:00
static intull strtoi_generic(const char *restrict nptr,
char **restrict endptr,
int inbase,
intl *coefptr,
intull int_max) {
const char *restrict str = nptr;
intull value = 0;
int digits_read = 0;
bool is_signed = (coefptr != NULL);
// Find max{abs(int)}. Signed integers have negative,
// whose absolute value is 1 bigger than int_max.
intull int_abs_max = int_max;
if(is_signed) {
++int_abs_max;
}
2022-06-03 09:31:16 +00:00
if(!inrange(0, inbase, 36)) {
goto finish;
2022-06-03 09:31:16 +00:00
}
intull base = (intull)inbase;
2022-06-03 09:31:16 +00:00
// Skip space on the beginning
while(isspace(*str)) {
++str;
2022-06-03 09:31:16 +00:00
}
// Parse sign
intl coef = 1;
if(is_signed) {
if(*str == '-') {
coef = -1;
++str;
}
2022-06-03 09:31:16 +00:00
}
if(*str == '+') {
++str;
2022-06-03 09:31:16 +00:00
}
// See if we need to parse base in C-like format
// then set the base accordingly
2022-06-05 22:02:54 +00:00
if(strprefix_i(str, "0X")) {
++str;
2022-06-03 09:31:16 +00:00
if(base == 16 || base == 0) {
++str;
2022-06-03 09:31:16 +00:00
base = 16;
}
else {
goto finish;
2022-06-03 09:31:16 +00:00
}
}
else if(*str == '0') {
++str;
++digits_read;
2022-06-03 09:31:16 +00:00
if(base == 8 || base == 0) {
base = 8;
}
}
// Parse the string of digits in the given base. If the value
// exceeds abs(int_min) we exit with range error.
while(isbase(*str, (int)base)) {
2022-06-05 22:02:54 +00:00
intull digit = (intull)todigit(*str);
if(value > (int_abs_max - digit)/base) {
goto error_out_of_range;
2022-06-03 09:31:16 +00:00
}
value = base*value + digit;
++str;
++digits_read;
2022-06-03 09:31:16 +00:00
}
// We only allow the modulo of value equal to abs(int_min) if it is
// preceeded by the minus sign.
if(is_signed) {
if(value == int_abs_max && coef != -1) {
goto error_out_of_range;
}
2022-06-03 09:31:16 +00:00
}
goto finish;
2022-06-06 00:26:32 +00:00
error_out_of_range:
// Skip the remainder of the subject string
while(isbase(*str, (int)base)) {
++str;
}
errno = ERANGE;
value = int_max;
goto finish;
2022-06-06 00:26:32 +00:00
finish:
// If no conversion is performed we return the value of 0 and *endptr
// is set to the nptr.
bool conv_performed = (digits_read > 0);
if(!conv_performed) {
2022-06-03 09:31:16 +00:00
value = 0;
}
if(endptr != NULL) {
if(!conv_performed) {
*endptr = (char *)nptr;
2022-06-03 09:31:16 +00:00
}
else {
*endptr = (char *)str;
2022-06-03 09:31:16 +00:00
}
}
*coefptr = coef;
return value;
}
2022-06-06 00:26:32 +00:00
static double strtod_generic(const char *restrict nptr, char **restrict endptr) {
2022-06-05 22:02:54 +00:00
const char *restrict str = nptr;
bool conv_performed = false;
double value = 0.0;
double coef = 1.0;
// Skip space on the beginning
while(isspace(*str)) {
++str;
}
// Check for inf and nan
if(strprefix_i(str, "INF")) {
str += sizeof "INF"-1;
value = HUGE_VAL;
conv_performed = true;
goto finish;
}
if(strprefix_i(str, "INFINITY")) {
str += sizeof "INFINITY"-1;
value = HUGE_VAL;
conv_performed = true;
goto finish;
}
if(strprefix_i(str, "NAN")) {
str += sizeof "NAN"-1;
value = NAN;
conv_performed = true;
if(*str == '(') {
while(*str != ')') {
++str;
}
++str;
}
goto finish;
}
// Parse C float
if(*str == '+') {
++str;
}
else if(*str == '-') {
coef = -1.;
++str;
}
int base = 10;
if(strprefix_i(str, "0X")) {
str += sizeof "0X"-1;
base = 16;
}
// Parse the whole part
while(isbase(*str, base)) {
long digit = todigit(*str);
value = 10.0*value + (double)digit;
++str;
}
if(*str != '.') {
value = 0.0;
goto finish;
}
++str;
// Parse the fractional part
long exp = 1;
while(isbase(*str, base)) {
long digit = todigit(*str);
double fract = (double)digit;
long cexp = exp;
while(cexp-- != 0) {
fract /= (double)base;
}
value += fract;
++exp;
++str;
}
// Parse the exponent
if((base == 10 && strprefix_i(str, "E"))
2022-06-06 00:26:32 +00:00
|| (base == 16 && strprefix_i(str, "P")))
2022-06-05 22:02:54 +00:00
{
++str;
long exp = 0;
long exp_coef = 1;
if(*str == '+') {
++str;
}
else if(*str == '-') {
exp_coef = -1;
++str;
}
while(isdigit(*str)) {
exp = 10*exp + (long)(*str-'0');
++str;
}
if(exp_coef == 1) {
while(exp--!=0) value = value * base;
}
else if(exp_coef == -1) {
while(exp--!=0) value = value / base;
}
}
if(!isfinite(value)) {
errno = ERANGE;
value = coef*HUGE_VAL;
}
conv_performed = true;
2022-06-06 00:26:32 +00:00
finish:
2022-06-03 09:31:16 +00:00
if(endptr != NULL) {
2022-06-05 22:02:54 +00:00
if(conv_performed) {
*endptr = (char *)str;
}
else {
*endptr = (char *)nptr;
}
2022-06-03 09:31:16 +00:00
}
2022-06-05 22:02:54 +00:00
return coef*value;
2022-06-03 09:31:16 +00:00
}
intl strtol(const char *restrict nptr, char **restrict endptr, int base) {
intull int_max = (intull)LONG_MAX;
intl coef;
2022-06-05 22:02:54 +00:00
intull modulo = strtoi_generic(nptr, endptr, base, &coef, int_max);
intl value;
if(modulo == int_max) {
value = LONG_MIN;
2022-06-03 09:31:16 +00:00
}
else {
value = coef * (intl)modulo;
}
return value;
2022-06-03 09:31:16 +00:00
}
intll strtoll(const char *restrict nptr, char **restrict endptr, int base) {
intull int_max = (intull)LLONG_MAX;
intl coef;
2022-06-05 22:02:54 +00:00
intull modulo = strtoi_generic(nptr, endptr, base, &coef, int_max);
intll value;
if(modulo == int_max) {
value = LLONG_MIN;
}
else {
value = (intll)coef * (intll)modulo;
}
return value;
2022-06-03 09:31:16 +00:00
}
intul strtoul(const char *restrict nptr, char **restrict endptr, int base) {
intull int_max = (intull)ULONG_MAX;
2022-06-05 22:02:54 +00:00
intull value = strtoi_generic(nptr, endptr, base, NULL, int_max);
return (intul)value;
2022-06-03 09:31:16 +00:00
}
intull strtoull(const char *restrict nptr, char **restrict endptr, int base) {
intull int_max = (intull)ULLONG_MAX;
2022-06-05 22:02:54 +00:00
return strtoi_generic(nptr, endptr, base, NULL, int_max);
2022-06-03 09:31:16 +00:00
}
double atof(const char *nptr) {
return strtod(nptr, (char **)NULL);
}
int atoi(const char *nptr) {
return (int)strtol(nptr, (char **)NULL, 10);
}
long int atol(const char *nptr) {
return strtol(nptr, (char **)NULL, 10);
}
long long int atoll(const char *nptr) {
return strtoll(nptr, (char **)NULL, 10);
}
double strtod(const char *restrict nptr, char **restrict endptr) {
2022-06-05 22:02:54 +00:00
return strtod_generic(nptr, endptr);
2022-06-03 09:31:16 +00:00
}
float strtof(const char *restrict nptr, char **restrict endptr) {
2022-06-05 22:02:54 +00:00
return (float)strtod_generic(nptr, endptr);
2022-06-03 09:31:16 +00:00
}
long double strtold(const char *restrict nptr, char **restrict endptr) {
2022-06-05 22:02:54 +00:00
return (long double)strtod_generic(nptr, endptr);
2022-06-03 09:31:16 +00:00
}