diff options
| -rw-r--r-- | mrbgems/mruby-string-ext/src/string.c | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/mrbgems/mruby-string-ext/src/string.c b/mrbgems/mruby-string-ext/src/string.c index 3b288f1db..0d834b6ef 100644 --- a/mrbgems/mruby-string-ext/src/string.c +++ b/mrbgems/mruby-string-ext/src/string.c @@ -602,17 +602,25 @@ mrb_str_upto(mrb_state *mrb, mrb_value beg) all_digits_p(RSTRING_PTR(beg), RSTRING_LEN(beg)) && all_digits_p(RSTRING_PTR(end), RSTRING_LEN(end))) { mrb_int min_width = RSTRING_LEN(beg); - mrb_int max_width = RSTRING_LEN(end); mrb_int bi = mrb_int(mrb, mrb_str_to_inum(mrb, beg, 10, FALSE)); mrb_int ei = mrb_int(mrb, mrb_str_to_inum(mrb, end, 10, FALSE)); - mrb_value str = mrb_str_new(mrb, NULL, max_width); int ai = mrb_gc_arena_save(mrb); - char *buf = RSTRING_PTR(str); while (bi <= ei) { + mrb_value ns, str; + if (excl && bi == ei) break; - snprintf(buf, max_width+1, "%.*" MRB_PRId, (int)min_width, bi); - mrb_yield(mrb, block, mrb_str_new(mrb, buf, strlen(buf))); + ns = mrb_format(mrb, "%S", mrb_fixnum_value(bi)); + if (min_width > RSTRING_LEN(ns)) { + str = mrb_str_new(mrb, NULL, min_width); + memset(RSTRING_PTR(str), '0', min_width-RSTRING_LEN(ns)); + memcpy(RSTRING_PTR(str)+min_width-RSTRING_LEN(ns), + RSTRING_PTR(ns), RSTRING_LEN(ns)); + } + else { + str = ns; + } + mrb_yield(mrb, block, str); mrb_gc_arena_restore(mrb, ai); bi++; } |
