Unverified Commit 8bca11a6 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto Committed by GitHub

Merge pull request #4800 from shuujii/set-MRB_STR_ASCII-flag-to-some-stringize-methods

Set `MRB_STR_ASCII` flag to some stringize methods
parents 14e325e4 ad6c5897
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include <mruby/data.h> #include <mruby/data.h>
#include <mruby/numeric.h> #include <mruby/numeric.h>
#include <mruby/time.h> #include <mruby/time.h>
#include <mruby/string.h>
#ifdef MRB_DISABLE_STDIO #ifdef MRB_DISABLE_STDIO
#include <string.h> #include <string.h>
...@@ -954,7 +955,9 @@ mrb_time_to_s(mrb_state *mrb, mrb_value self) ...@@ -954,7 +955,9 @@ mrb_time_to_s(mrb_state *mrb, mrb_value self)
struct mrb_time *tm = time_get_ptr(mrb, self); struct mrb_time *tm = time_get_ptr(mrb, self);
mrb_bool utc = tm->timezone == MRB_TIMEZONE_UTC; mrb_bool utc = tm->timezone == MRB_TIMEZONE_UTC;
size_t len = (utc ? time_to_s_utc : time_to_s_local)(mrb, tm, buf, sizeof(buf)); size_t len = (utc ? time_to_s_utc : time_to_s_local)(mrb, tm, buf, sizeof(buf));
return mrb_str_new(mrb, buf, len); mrb_value str = mrb_str_new(mrb, buf, len);
RSTR_SET_ASCII_FLAG(mrb_str_ptr(str));
return str;
} }
void void
......
...@@ -210,26 +210,30 @@ static mrb_value ...@@ -210,26 +210,30 @@ static mrb_value
flo_to_s(mrb_state *mrb, mrb_value flt) flo_to_s(mrb_state *mrb, mrb_value flt)
{ {
mrb_float f = mrb_float(flt); mrb_float f = mrb_float(flt);
mrb_value str;
if (isinf(f)) { if (isinf(f)) {
return f < 0 ? mrb_str_new_lit(mrb, "-Infinity") str = f < 0 ? mrb_str_new_lit(mrb, "-Infinity")
: mrb_str_new_lit(mrb, "Infinity"); : mrb_str_new_lit(mrb, "Infinity");
goto exit;
} }
else if (isnan(f)) { else if (isnan(f)) {
return mrb_str_new_lit(mrb, "NaN"); str = mrb_str_new_lit(mrb, "NaN");
goto exit;
} }
else { else {
char fmt[] = "%." MRB_STRINGIZE(FLO_TO_STR_PREC) "g"; char fmt[] = "%." MRB_STRINGIZE(FLO_TO_STR_PREC) "g";
mrb_value str = mrb_float_to_str(mrb, flt, fmt);
mrb_int len; mrb_int len;
char *begp, *p, *endp; char *begp, *p, *endp;
str = mrb_float_to_str(mrb, flt, fmt);
insert_dot_zero: insert_dot_zero:
begp = RSTRING_PTR(str); begp = RSTRING_PTR(str);
len = RSTRING_LEN(str); len = RSTRING_LEN(str);
for (p = begp, endp = p + len; p < endp; ++p) { for (p = begp, endp = p + len; p < endp; ++p) {
if (*p == '.') { if (*p == '.') {
return str; goto exit;
} }
else if (*p == 'e') { else if (*p == 'e') {
ptrdiff_t e_pos = p - begp; ptrdiff_t e_pos = p - begp;
...@@ -237,7 +241,7 @@ flo_to_s(mrb_state *mrb, mrb_value flt) ...@@ -237,7 +241,7 @@ flo_to_s(mrb_state *mrb, mrb_value flt)
p = RSTRING_PTR(str) + e_pos; p = RSTRING_PTR(str) + e_pos;
memmove(p + 2, p, len - e_pos); memmove(p + 2, p, len - e_pos);
memcpy(p, ".0", 2); memcpy(p, ".0", 2);
return str; goto exit;
} }
} }
...@@ -247,8 +251,12 @@ flo_to_s(mrb_state *mrb, mrb_value flt) ...@@ -247,8 +251,12 @@ flo_to_s(mrb_state *mrb, mrb_value flt)
goto insert_dot_zero; goto insert_dot_zero;
} }
return str; goto exit;
} }
exit:
RSTR_SET_ASCII_FLAG(mrb_str_ptr(str));
return str;
} }
/* 15.2.9.3.2 */ /* 15.2.9.3.2 */
...@@ -1383,6 +1391,7 @@ mrb_fixnum_to_str(mrb_state *mrb, mrb_value x, mrb_int base) ...@@ -1383,6 +1391,7 @@ mrb_fixnum_to_str(mrb_state *mrb, mrb_value x, mrb_int base)
char buf[MRB_INT_BIT+1]; char buf[MRB_INT_BIT+1];
char *b = buf + sizeof buf; char *b = buf + sizeof buf;
mrb_int val = mrb_fixnum(x); mrb_int val = mrb_fixnum(x);
mrb_value str;
if (base < 2 || 36 < base) { if (base < 2 || 36 < base) {
mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid radix %i", base); mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid radix %i", base);
...@@ -1403,7 +1412,9 @@ mrb_fixnum_to_str(mrb_state *mrb, mrb_value x, mrb_int base) ...@@ -1403,7 +1412,9 @@ mrb_fixnum_to_str(mrb_state *mrb, mrb_value x, mrb_int base)
} while (val /= base); } while (val /= base);
} }
return mrb_str_new(mrb, b, buf + sizeof(buf) - b); str = mrb_str_new(mrb, b, buf + sizeof(buf) - b);
RSTR_SET_ASCII_FLAG(mrb_str_ptr(str));
return str;
} }
/* 15.2.8.3.25 */ /* 15.2.8.3.25 */
......
...@@ -83,13 +83,17 @@ mrb_true(mrb_state *mrb, mrb_value obj) ...@@ -83,13 +83,17 @@ mrb_true(mrb_state *mrb, mrb_value obj)
static mrb_value static mrb_value
nil_to_s(mrb_state *mrb, mrb_value obj) nil_to_s(mrb_state *mrb, mrb_value obj)
{ {
return mrb_str_new_frozen(mrb, 0, 0); mrb_value str = mrb_str_new_frozen(mrb, 0, 0);
RSTR_SET_ASCII_FLAG(mrb_str_ptr(str));
return str;
} }
static mrb_value static mrb_value
nil_inspect(mrb_state *mrb, mrb_value obj) nil_inspect(mrb_state *mrb, mrb_value obj)
{ {
return mrb_str_new_lit_frozen(mrb, "nil"); mrb_value str = mrb_str_new_lit_frozen(mrb, "nil");
RSTR_SET_ASCII_FLAG(mrb_str_ptr(str));
return str;
} }
/*********************************************************************** /***********************************************************************
...@@ -150,7 +154,9 @@ true_xor(mrb_state *mrb, mrb_value obj) ...@@ -150,7 +154,9 @@ true_xor(mrb_state *mrb, mrb_value obj)
static mrb_value static mrb_value
true_to_s(mrb_state *mrb, mrb_value obj) true_to_s(mrb_state *mrb, mrb_value obj)
{ {
return mrb_str_new_lit_frozen(mrb, "true"); mrb_value str = mrb_str_new_lit_frozen(mrb, "true");
RSTR_SET_ASCII_FLAG(mrb_str_ptr(str));
return str;
} }
/* 15.2.5.3.4 */ /* 15.2.5.3.4 */
...@@ -257,7 +263,9 @@ false_or(mrb_state *mrb, mrb_value obj) ...@@ -257,7 +263,9 @@ false_or(mrb_state *mrb, mrb_value obj)
static mrb_value static mrb_value
false_to_s(mrb_state *mrb, mrb_value obj) false_to_s(mrb_state *mrb, mrb_value obj)
{ {
return mrb_str_new_lit_frozen(mrb, "false"); mrb_value str = mrb_str_new_lit_frozen(mrb, "false");
RSTR_SET_ASCII_FLAG(mrb_str_ptr(str));
return str;
} }
void void
......
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