| 1 |
#include <mruby.h> |
| 2 |
#include <mruby/numeric.h> |
| 3 |
|
| 4 |
/* mrb_read_int(): read mrb_int from a string (base 10 only) */ |
| 5 |
/* const char *p - string to read */ |
| 6 |
/* const char *e - end of string */ |
| 7 |
/* char **endp - end of parsed integer */ |
| 8 |
/* mrb_int *np - variable to save the result */ |
| 9 |
/* returns TRUE if read succeeded */ |
| 10 |
/* if integer overflows, returns FALSE */ |
| 11 |
MRB_API mrb_bool |
| 12 |
mrb_read_int(const char *p, const char *e, char **endp, mrb_int *np) |
| 13 |
{ |
| 14 |
mrb_int n = 0; |
| 15 |
int ch; |
| 16 |
|
| 17 |
while ((e == NULL || p < e) && ISDIGIT(*p)) { |
| 18 |
ch = *p - '0'; |
| 19 |
if (mrb_int_mul_overflow(n, 10, &n) || |
| 20 |
mrb_int_add_overflow(n, ch, &n)) { |
| 21 |
return FALSE; |
| 22 |
} |
| 23 |
p++; |
| 24 |
} |
| 25 |
if (endp) *endp = (char*)p; |
| 26 |
*np = n; |
| 27 |
return TRUE; |
| 28 |
} |