ktcc/libc: add floating-point parsing to strtod family and scanf

- add strntold(): bounded string-to-long-double parser (the core)
- implement strtod(), strtof(), strtold() on top of it; rework strtod()
- vsscanf: add %e %E %f %F %g %G (field width, EOF/error, float/double/long double)
- fix itoa(): __reverse() called strlen() on a not-yet-NUL-terminated buffer — pass length explicitly, drop <string.h>
- export new symbols (libc.def, stdlib.h, libc.c)
- update libc_test.c

---------

Co-authored-by: Burer <burer@kolibrios.org>
Co-authored-by: Zverrr <zverrr@skbkontur.ru>
Reviewed-on: #407
Reviewed-by: Егор <y.yarin@inbox.ru>
Reviewed-by: Mikhail Frolov <mixa.frolov2003@gmail.com>
Co-authored-by: Zverev Dmitriy <dvzverrr@gmail.com>
Co-committed-by: Zverev Dmitriy <dvzverrr@gmail.com>
This commit was merged in pull request #407.
This commit is contained in:
2026-06-10 16:46:49 +00:00
committed by Burer
co-authored by Burer Zverrr
parent 4cd1bfda63
commit 0381abade7
10 changed files with 340 additions and 180 deletions
+2
View File
@@ -66,7 +66,9 @@ strtol
srand
rand
qsort
strtof
strtod
strtold
__assert_fail
;____STRING____
memccpy
@@ -66,6 +66,8 @@ DLLAPI void __assert_fail(const char* expr, const char* file, int line, const ch
DLLAPI void qsort(void* base0, size_t n, size_t size, int (*compar)(const void*, const void*));
DLLAPI double strtod(const char* s, char** sret);
DLLAPI long double strtold(const char* s, char** sret);
DLLAPI float strtof(const char* s, char** sret);
DLLAPI double atof(const char* ascii);
DLLAPI int atoi(const char* s);
@@ -1,90 +1,159 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ksys.h>
#include <time.h>
int comp(void* a, void* b)
{
return *(int*)a - *(int*)b;
}
int main()
{
puts("Start testing.");
assert(NULL == ((void*)0));
assert(RAND_MAX == 65535);
assert(min(3, 10) == 3);
assert(max(3, 10) == 10);
assert(atof("12.4") == 12.4);
assert(atoi("-123") == -123);
assert(atol("-2146483647") == -2146483647L);
assert(atoll("-9223372036854775806") == -9223372036854775806LL);
assert(!strcmp("123", "123"));
char st1[32];
itoa(-2341, st1);
assert(!strcmp(st1, "-2341"));
assert(strlen("12345") == 5);
assert(abs(4) == 4);
assert(abs(-4) == 4);
assert(labs(1000000000) == 1000000000);
assert(labs(-1000000000) == 1000000000);
assert(llabs(100000000000) == 100000000000);
assert(llabs(-100000000000) == 100000000000);
div_t output1 = div(27, 4);
assert(output1.quot == 6);
assert(output1.rem == 3);
ldiv_t output2 = ldiv(27, 4);
assert(output2.quot == 6);
assert(output2.rem == 3);
lldiv_t output3 = lldiv(27, 4);
assert(output3.quot == 6);
assert(output3.rem == 3);
char* st2 = malloc(sizeof(char) * 2);
assert(st2 != NULL);
st2[0] = 'H';
st2[1] = 'i';
st2 = realloc(st2, sizeof(char) * 3);
st2[2] = '!';
assert(!strcmp(st2, "Hi!"));
free(st2);
st2 = calloc(2, sizeof(char));
assert(st2 != NULL);
st2[0] = 'H';
st2[1] = 'i';
assert(!strcmp(st2, "Hi"));
free(st2);
char* start = "100.00 Rub";
char* end;
assert(strtol(start, &end, 10) == 100L);
assert(!strcmp(end, ".00 Rub"));
end = NULL;
assert(strtod(start, &end) == 100.0);
assert(!strcmp(end, " Rub"));
char* st3 = "21.3e3Hello World!";
assert(atof(st3) == 21300.0);
int nums[10] = { 5, 3, 9, 1, 8, 4, 2, 0, 7, 6 };
qsort(nums, 10, sizeof(int), (int (*)(const void*, const void*))comp);
for (int i = 0; i < 10; i++) {
assert(nums[i] == i);
}
time_t libc_time = time(NULL);
struct tm* libc_tm = localtime(&libc_time);
printf(asctime(libc_tm));
puts("End testing.");
exit(0);
}
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ksys.h>
#include <time.h>
int comp(void* a, void* b)
{
return *(int*)a - *(int*)b;
}
int main()
{
puts("Start testing.");
assert(NULL == ((void*)0));
assert(RAND_MAX == 65535);
assert(min(3, 10) == 3);
assert(max(3, 10) == 10);
assert(atof("12.4") == 12.4);
assert(atoi("-123") == -123);
assert(atol("-2146483647") == -2146483647L);
assert(atoll("-9223372036854775806") == -9223372036854775806LL);
assert(!strcmp("123", "123"));
char st1[32];
itoa(-2341, st1);
assert(!strcmp(st1, "-2341"));
assert(strlen("12345") == 5);
assert(abs(4) == 4);
assert(abs(-4) == 4);
assert(labs(1000000000) == 1000000000);
assert(labs(-1000000000) == 1000000000);
assert(llabs(100000000000) == 100000000000);
assert(llabs(-100000000000) == 100000000000);
div_t output1 = div(27, 4);
assert(output1.quot == 6);
assert(output1.rem == 3);
ldiv_t output2 = ldiv(27, 4);
assert(output2.quot == 6);
assert(output2.rem == 3);
lldiv_t output3 = lldiv(27, 4);
assert(output3.quot == 6);
assert(output3.rem == 3);
char* st2 = malloc(sizeof(char) * 2);
assert(st2 != NULL);
st2[0] = 'H';
st2[1] = 'i';
st2 = realloc(st2, sizeof(char) * 3);
st2[2] = '!';
assert(!strcmp(st2, "Hi!"));
free(st2);
st2 = calloc(2, sizeof(char));
assert(st2 != NULL);
st2[0] = 'H';
st2[1] = 'i';
assert(!strcmp(st2, "Hi"));
free(st2);
char* start = "100.00 Rub";
char* end;
assert(strtol(start, &end, 10) == 100L);
assert(!strcmp(end, ".00 Rub"));
end = NULL;
double expected_d = 100.0;
double result_d = strtod(start, &end);
assert(result_d == expected_d);
assert(!strcmp(end, " Rub"));
end = NULL;
float expected_f = 100.0f;
float result_f = strtof(start, &end);
assert(result_f == expected_f);
assert(!strcmp(end, " Rub"));
end = NULL;
long double expected_ld = 100.0;
long double result_ld = strtold(start, &end);
assert(result_ld == expected_ld);
assert(!strcmp(end, " Rub"));
char* st3 = "21.3e3Hello World!";
assert(atof(st3) == 21300.0);
char* st4 = "12345";
float fpart;
int ipart;
sscanf(st4, "%3f%d", &fpart, &ipart);
assert(fpart == 123.0);
assert(ipart == 45);
char* st5 = "123.45";
float fval;
sscanf(st5, "%f", &fval);
assert(fval == 123.45f);
double dval;
sscanf(st5, "%lf", &dval);
assert(dval == 123.45);
long double ldval;
sscanf(st5, "%Lf", &ldval);
assert(ldval == 123.45);
float gval;
sscanf(st5, "%g", &gval);
assert(gval == 123.45f);
float eval;
sscanf(st5, "%e", &eval);
assert(eval == 123.45f);
/* Signed exponent must be parsed (regression guard for e+/e-). */
assert(strtod("12e+3", NULL) == 12000.0);
float epos;
sscanf("12E+3", "%f", &epos);
assert(epos == 12000.0f);
double eneg = strtod("3000e-3", NULL);
assert(eneg > 2.9 && eneg < 3.1);
/* Uppercase float specifiers behave like lowercase in scanf. */
float gup;
assert(sscanf("123.45", "%G", &gup) == 1);
assert(gup == 123.45f);
/* No valid number: value 0, endptr unchanged, scanf reports no match. */
char* sdot = ".";
char* edot;
assert(strtod(sdot, &edot) == 0.0);
assert(edot == sdot);
float fdot;
assert(sscanf(".", "%f", &fdot) == 0);
/* Malformed exponent: value ends before 'e', sign preserved. */
char* sbad = "-1.5e";
char* ebad;
assert(strtod(sbad, &ebad) == -1.5);
assert(*ebad == 'e');
int nums[10] = { 5, 3, 9, 1, 8, 4, 2, 0, 7, 6 };
qsort(nums, 10, sizeof(int), (int (*)(const void*, const void*))comp);
for (int i = 0; i < 10; i++) {
assert(nums[i] == i);
}
time_t libc_time = time(NULL);
struct tm* libc_tm = localtime(&libc_time);
printf(asctime(libc_tm));
puts("End testing.");
exit(0);
}
@@ -57,6 +57,8 @@
#include "stdio/vfprintf.c"
#include "stdio/vprintf.c"
#include "stdio/vsnprintf.c"
/* strntold() is used by vsscanf() below; include its definition first */
#include "stdlib/strntold.c"
#include "stdio/vsscanf.c"
#include "string/memccpy.c"
@@ -102,6 +104,8 @@
#include "stdlib/rand.c"
#include "stdlib/realloc.c"
#include "stdlib/strtod.c"
#include "stdlib/strtof.c"
#include "stdlib/strtold.c"
#include "stdlib/strtol.c"
#include "math/acosh.c"
@@ -189,6 +193,8 @@ ksys_dll_t EXPORTS[] = {
{ "rand", &rand },
{ "qsort", &qsort },
{ "strtod", &strtod },
{ "strtof", &strtof },
{ "strtold", &strtold },
{ "__assert_fail", &__assert_fail },
{ "memccpy", &memccpy},
{ "memchr", &memchr },
@@ -73,6 +73,7 @@ int vsscanf(const char* buffer, const char* format, va_list ap)
const char* q = buffer;
const char* qq;
uintmax_t val = 0;
long double val_ld = 0.0;
int rank = rank_int; /* Default rank */
unsigned int width = UINT_MAX;
int base;
@@ -220,6 +221,15 @@ int vsscanf(const char* buffer, const char* format, va_list ap)
case 'n': /* # of characters consumed */
val = (q - buffer);
goto set_integer;
case 'e': /* Float / double / long double */
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
sign = 1;
goto scan_float;
scan_int:
q = skipspace(q);
@@ -271,6 +281,40 @@ int vsscanf(const char* buffer, const char* format, va_list ap)
break;
}
}
break;
scan_float:
q = skipspace(q);
if (!*q) {
bail = bail_eof;
break;
}
val_ld = strntold(q, (char**)&qq, width);
if (qq == q) {
bail = bail_err;
break;
}
q = qq;
if (!(flags & FL_SPLAT))
converted++;
set_float:
if (!(flags & FL_SPLAT)) {
switch (rank) {
case rank_long:
*va_arg(ap, double*)
= val_ld;
break;
case rank_longlong:
*va_arg(ap, long double*)
= val_ld;
break;
case rank_int:
default:
*va_arg(ap, float*)
= val_ld;
break;
}
}
break;
case 'c': /* Character */
@@ -1,12 +1,9 @@
#include <string.h>
#include <sys/ksys.h>
char* __reverse(char* str)
char* __reverse(char* str, size_t len)
{
char tmp, *src, *dst;
size_t len;
if (str != NULL) {
len = strlen(str);
if (len > 1) {
src = str;
dst = src + len - 1;
@@ -36,7 +33,7 @@ void itoa(int n, char s[])
if (sign < 0)
s[i++] = '-';
__reverse(s);
__reverse(s, i);
s[i] = '\0';
return;
}
}
@@ -0,0 +1,110 @@
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
#include <stddef.h>
#ifndef unconst
#define unconst(__v, __t) __extension__({union { const __t __cp; __t __p; } __q; __q.__cp = __v; __q.__p; })
#endif
long double strntold(const char* s, char** sret, size_t n)
{
long double r; /* result */
int e; /* exponent */
long double d; /* scale */
int sign; /* +- 1.0 */
int esign;
int i;
int flags = 0;
const char* start = s; /* original ptr; endptr on no conversion (per strtod) */
r = 0.0;
sign = 1;
e = 0;
esign = 1;
while (((*s == ' ') || (*s == '\t')) && n)
{
s++;
n--;
}
if (n)
{
if (*s == '+')
{
s++;
n--;
}
else if (*s == '-') {
sign = -1;
s++;
n--;
}
}
while ((*s >= '0') && (*s <= '9') && n) {
flags |= 1;
r *= 10.0;
r += *s - '0';
s++;
n--;
}
if (*s == '.' && n) {
d = 0.1L;
s++;
n--;
while ((*s >= '0') && (*s <= '9') && n) {
flags |= 2;
r += d * (*s - '0');
s++;
d *= 0.1L;
n--;
}
}
if (flags == 0) {
if (sret)
*sret = unconst(start, char*);
return 0;
}
if (((*s == 'e') || (*s == 'E')) && n) {
const char* exp_pos = s; /* rewind here if the exponent is malformed */
s++;
n--;
if ((*s == '+') && n)
{
s++;
n--;
}
else if ((*s == '-') && n) {
s++;
esign = -1;
n--;
}
if ((*s < '0') || (*s > '9') || !n) {
if (sret)
*sret = unconst(exp_pos, char*);
return r * sign;
}
while ((*s >= '0') && (*s <= '9') && n) {
e *= 10;
e += *s - '0';
s++;
n--;
}
}
if (esign < 0)
for (i = 1; i <= e; i++)
r *= 0.1L;
else
for (i = 1; i <= e; i++)
r *= 10.0;
if (sret)
*sret = unconst(s, char*);
return r * sign;
}
@@ -1,89 +1,7 @@
/* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
#include <math.h>
#ifndef unconst
#define unconst(__v, __t) __extension__({union { const __t __cp; __t __p; } __q; __q.__cp = __v; __q.__p; })
#endif
#include <limits.h>
double strtod(const char* s, char** sret)
{
long double r; /* result */
int e; /* exponent */
long double d; /* scale */
int sign; /* +- 1.0 */
int esign;
int i;
int flags = 0;
r = 0.0;
sign = 1.0;
e = 0;
esign = 1;
while ((*s == ' ') || (*s == '\t'))
s++;
if (*s == '+')
s++;
else if (*s == '-') {
sign = -1;
s++;
}
while ((*s >= '0') && (*s <= '9')) {
flags |= 1;
r *= 10.0;
r += *s - '0';
s++;
}
if (*s == '.') {
d = 0.1L;
s++;
while ((*s >= '0') && (*s <= '9')) {
flags |= 2;
r += d * (*s - '0');
s++;
d *= 0.1L;
}
}
if (flags == 0) {
if (sret)
*sret = unconst(s, char*);
return 0;
}
if ((*s == 'e') || (*s == 'E')) {
s++;
if (*s == '+')
s++;
else if (*s == '-') {
s++;
esign = -1;
}
if ((*s < '0') || (*s > '9')) {
if (sret)
*sret = unconst(s, char*);
return r;
}
while ((*s >= '0') && (*s <= '9')) {
e *= 10;
e += *s - '0';
s++;
}
}
if (esign < 0)
for (i = 1; i <= e; i++)
r *= 0.1L;
else
for (i = 1; i <= e; i++)
r *= 10.0;
if (sret)
*sret = unconst(s, char*);
return r * sign;
return strntold(s, sret, UINT_MAX);
}
@@ -0,0 +1,6 @@
#include<limits.h>
float strtof(const char* s, char** sret)
{
return strntold(s, sret, UINT_MAX);
}
@@ -0,0 +1,6 @@
#include<limits.h>
long double strtold(const char* s, char** sret)
{
return strntold(s, sret, UINT_MAX);
}