mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
readint.c (mrb_int_read): new function.
We no longer use `mrb_read_int` which is kinda compatible with `strtol`.
This commit is contained in:
+9
-11
@@ -1,16 +1,15 @@
|
||||
#include <mruby.h>
|
||||
#include <mruby/numeric.h>
|
||||
#include <errno.h>
|
||||
|
||||
/* mrb_int_read(): read mrb_int from a string (base 10 only) */
|
||||
/* mrb_read_int(): read mrb_int from a string (base 10 only) */
|
||||
/* const char *p - string to read */
|
||||
/* const char *e - end of string */
|
||||
/* char **endp - end of parsed integer */
|
||||
|
||||
/* if integer overflows, errno will be set to ERANGE */
|
||||
/* also endp will be set to NULL on overflow */
|
||||
MRB_API mrb_int
|
||||
mrb_int_read(const char *p, const char *e, char **endp)
|
||||
/* mrb_int *np - variable to save the result */
|
||||
/* returns TRUE if read succeeded */
|
||||
/* if integer overflows, returns FALSE */
|
||||
MRB_API mrb_bool
|
||||
mrb_read_int(const char *p, const char *e, char **endp, mrb_int *np)
|
||||
{
|
||||
mrb_int n = 0;
|
||||
int ch;
|
||||
@@ -19,12 +18,11 @@ mrb_int_read(const char *p, const char *e, char **endp)
|
||||
ch = *p - '0';
|
||||
if (mrb_int_mul_overflow(n, 10, &n) ||
|
||||
mrb_int_add_overflow(n, ch, &n)) {
|
||||
if (endp) *endp = NULL;
|
||||
errno = ERANGE;
|
||||
return MRB_INT_MAX;
|
||||
return FALSE;
|
||||
}
|
||||
p++;
|
||||
}
|
||||
if (endp) *endp = (char*)p;
|
||||
return n;
|
||||
*np = n;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user