Avoid using `snprintf(3)` in case `MRB_DISABLE_STDIO`; fix #3714

parent 455f3954
......@@ -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++;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment