diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2021-06-11 12:26:58 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2021-06-11 15:14:17 +0900 |
| commit | 49af1fca03173f415c5634dafdce3ee81cc01401 (patch) | |
| tree | eb1a8ddfe0f2fcc5b10e6666322efd0964189b30 /mrbgems/mruby-sprintf | |
| parent | 14a69c6ffd08842a896066c3610c5fabcae3925f (diff) | |
| download | mruby-49af1fca03173f415c5634dafdce3ee81cc01401.tar.gz mruby-49af1fca03173f415c5634dafdce3ee81cc01401.zip | |
readint.c: add new function `mrb_int_read`.
Difference from `strtoul(3)`:
* reads `mrb_int` based on configuration
* specifies the end of the string
* no sign interpretation
* base 10 only
Diffstat (limited to 'mrbgems/mruby-sprintf')
| -rw-r--r-- | mrbgems/mruby-sprintf/src/sprintf.c | 25 |
1 files changed, 5 insertions, 20 deletions
diff --git a/mrbgems/mruby-sprintf/src/sprintf.c b/mrbgems/mruby-sprintf/src/sprintf.c index 2ae982a16..ea9d74d95 100644 --- a/mrbgems/mruby-sprintf/src/sprintf.c +++ b/mrbgems/mruby-sprintf/src/sprintf.c @@ -245,7 +245,6 @@ check_name_arg(mrb_state *mrb, int posarg, const char *name, size_t len) #define GETASTER(num) do { \ mrb_value tmp_v; \ t = p++; \ - n = 0; \ GETNUM(n, val); \ if (*p == '$') { \ tmp_v = GETPOSARG(n); \ @@ -260,22 +259,11 @@ check_name_arg(mrb_state *mrb, int posarg, const char *name, size_t len) static const char * get_num(mrb_state *mrb, const char *p, const char *end, int *valp) { - mrb_int next_n = (int)*valp; - for (; p < end && ISDIGIT(*p); p++) { - if (mrb_int_mul_overflow(10, next_n, &next_n)) { - return NULL; - } - if (MRB_INT_MAX - (*p - '0') < next_n) { - return NULL; - } - next_n += *p - '0'; - } - if (next_n > INT_MAX || next_n < 0) return NULL; - if (p >= end) { - mrb_raise(mrb, E_ARGUMENT_ERROR, "malformed format string - %%*[0-9]"); - } - *valp = (int)next_n; - return p; + char *e; + mrb_int n = mrb_int_read(p, end, &e); + if (e == NULL || n > INT_MAX) return NULL; + *valp = (int)n; + return e; } static void @@ -665,7 +653,6 @@ retry: case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': - n = 0; GETNUM(n, width); if (*p == '$') { if (!mrb_undef_p(nextvalue)) { @@ -722,7 +709,6 @@ retry: } flags |= FPREC|FPREC0; - prec = 0; p++; if (*p == '*') { GETASTER(prec); @@ -732,7 +718,6 @@ retry: p++; goto retry; } - GETNUM(prec, precision); goto retry; |
