readfloat.c (mrb_read_float): a new function.

We no longer use `mrb_float_read()` that depends on `errno`.
This commit is contained in:
Yukihiro "Matz" Matsumoto
2022-11-07 13:42:30 +09:00
parent 4e9773ae3d
commit 4a888a34bb
7 changed files with 1047 additions and 1061 deletions
+3 -1
View File
@@ -104,7 +104,9 @@ MRB_API mrb_bool mrb_read_int(const char *p, const char *e, char **endp, mrb_int
/* obsolete; do not use mrb_int_read() */
MRB_API mrb_int mrb_int_read(const char*, const char*, char**);
#ifndef MRB_NO_FLOAT
MRB_API double mrb_float_read(const char*, char**);
MRB_API mrb_bool mrb_read_float(const char *p, char **endp, double *fp);
/* obsolete; do not use mrb_float_read() */
MRB_API double mrb_float_read(const char *p, char **endp);
#ifdef MRB_USE_FLOAT32
typedef float mrb_float;
#else
+6 -4
View File
@@ -3318,8 +3318,9 @@ codegen(codegen_scope *s, node *tree, int val)
case NODE_FLOAT:
if (val) {
char *p = (char*)tree;
mrb_float f = mrb_float_read(p, NULL);
int off = new_lit_float(s, f);
double f;
mrb_read_float(p, NULL, &f);
int off = new_lit_float(s, (mrb_float)f);
genop_2(s, OP_LOADL, cursp(), off);
push();
@@ -3335,8 +3336,9 @@ codegen(codegen_scope *s, node *tree, int val)
case NODE_FLOAT:
if (val) {
char *p = (char*)tree->cdr;
mrb_float f = mrb_float_read(p, NULL);
int off = new_lit_float(s, -f);
double f;
mrb_read_float(p, NULL, &f);
int off = new_lit_float(s, (mrb_float)-f);
genop_2(s, OP_LOADL, cursp(), off);
push();
+1 -9
View File
@@ -12,7 +12,6 @@
#define YYSTACK_USE_ALLOCA 1
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <mruby.h>
#include <mruby/compile.h>
@@ -5973,17 +5972,10 @@ parser_yylex(parser_state *p)
return tINTEGER;
#else
double d;
char *endp;
errno = 0;
d = mrb_float_read(tok(p), &endp);
if (d == 0 && endp == tok(p)) {
if (!mrb_read_float(tok(p), NULL, &d)) {
yywarning_s(p, "corrupted float value", tok(p));
}
else if (errno == ERANGE) {
yywarning_s(p, "float out of range", tok(p));
errno = 0;
}
suffix = number_literal_suffix(p);
if (seen_e && (suffix & NUM_SUFFIX_R)) {
pushback(p, 'r');
File diff suppressed because it is too large Load Diff
-1
View File
@@ -12,7 +12,6 @@
#include "mruby/endian.h"
#include <ctype.h>
#include <errno.h>
#include <string.h>
#define INT_OVERFLOW_P(n) ((n) < MRB_INT_MIN || (n) > MRB_INT_MAX)
+24 -24
View File
@@ -12,8 +12,9 @@ The original code can be found in https://github.com/mattn/strtod
I modified the routine for mruby:
* renamed the function `vim_strtod` -> `mrb_float_read`
* renamed the function `vim_strtod` -> `mrb_read_float`
* simplified the code
* changed the API
My modifications in this file are also placed in the public domain.
@@ -22,10 +23,9 @@ Matz (Yukihiro Matsumoto)
#include <string.h>
#include <math.h>
#include <errno.h>
MRB_API double
mrb_float_read(const char *str, char **end)
MRB_API mrb_bool
mrb_read_float(const char *str, char **endp, double *fp)
{
double d = 0.0;
int sign;
@@ -34,21 +34,22 @@ mrb_float_read(const char *str, char **end)
a = p = str;
while (ISSPACE(*p))
++p;
p++;
/* decimal part */
sign = 1;
if (*p == '-') {
sign = -1;
++p;
} else if (*p == '+')
++p;
p++;
}
else if (*p == '+')
p++;
if (ISDIGIT(*p)) {
d = (double)(*p++ - '0');
while (*p && ISDIGIT(*p)) {
d = d * 10.0 + (double)(*p - '0');
++p;
++n;
p++;
n++;
}
a = p;
}
@@ -60,14 +61,14 @@ mrb_float_read(const char *str, char **end)
if (*p == '.') {
double f = 0.0;
double base = 0.1;
++p;
p++;
if (ISDIGIT(*p)) {
while (*p && ISDIGIT(*p)) {
f += base * (*p - '0') ;
base /= 10.0;
++p;
++n;
p++;
n++;
}
}
d += f * sign;
@@ -77,19 +78,19 @@ mrb_float_read(const char *str, char **end)
/* exponential part */
if ((*p == 'E') || (*p == 'e')) {
int e = 0;
++p;
p++;
sign = 1;
if (*p == '-') {
sign = -1;
++p;
p++;
}
else if (*p == '+')
++p;
p++;
if (ISDIGIT(*p)) {
while (*p == '0')
++p;
p++;
if (*p == '\0') --p;
e = (int)(*p++ - '0');
for (; *p && ISDIGIT(*p); p++) {
@@ -99,22 +100,21 @@ mrb_float_read(const char *str, char **end)
e *= sign;
}
else if (!ISDIGIT(*(a-1))) {
a = str;
goto done;
return FALSE;
}
else if (*p == 0)
goto done;
d *= pow(10.0, (double) e);
d *= pow(10.0, (double)e);
a = p;
}
else if (p > str && !ISDIGIT(*(p-1))) {
a = str;
goto done;
}
done:
if (end)
*end = (char*)a;
return d;
*fp = d;
if (endp) *endp = (char*)a;
if (str == a) return FALSE;
return TRUE;
}
#endif
+1 -2
View File
@@ -2572,8 +2572,7 @@ mrb_str_len_to_dbl(mrb_state *mrb, const char *s, size_t len, mrb_bool badcheck)
p = buf;
pend = n;
nocopy:
d = mrb_float_read(p, &end);
if (p == end) {
if (mrb_read_float(p, &end, &d) == FALSE) {
if (badcheck) {
bad:
mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid string for float(%!s)", s);