summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2020-01-06 15:47:19 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2020-01-06 15:52:39 +0900
commitccf28775b896e7cf772657eacd8727de25c3a0f4 (patch)
tree442be2b2e6b5e51abc4a7996c022c131e1f9d6d1
parent2532e625edc2457447369e36e2ecf7882d872ef9 (diff)
downloadmruby-ccf28775b896e7cf772657eacd8727de25c3a0f4.tar.gz
mruby-ccf28775b896e7cf772657eacd8727de25c3a0f4.zip
Fix `mrb_str_len_to_dbl` to support Hexadecimal like `0x10`.
-rw-r--r--mrbgems/mruby-kernel-ext/test/kernel.rb1
-rw-r--r--src/string.c11
2 files changed, 10 insertions, 2 deletions
diff --git a/mrbgems/mruby-kernel-ext/test/kernel.rb b/mrbgems/mruby-kernel-ext/test/kernel.rb
index f782510aa..cf78faaad 100644
--- a/mrbgems/mruby-kernel-ext/test/kernel.rb
+++ b/mrbgems/mruby-kernel-ext/test/kernel.rb
@@ -80,6 +80,7 @@ assert('Kernel#Float') do
assert_operator(12.34, :eql?, Float('1_2.3_4'))
assert_operator(0.9, :eql?, Float('.9'))
assert_operator(0.9, :eql?, Float(" \t\r\n\f\v.9 \t\r\n\f\v"))
+ assert_operator(16.0, :eql?, Float("0x10"))
assert_raise(TypeError) { Float(nil) }
assert_raise(ArgumentError) { Float("1. 5") }
assert_raise(ArgumentError) { Float("1.5a") }
diff --git a/src/string.c b/src/string.c
index 15e52fb3c..7b6a11a6f 100644
--- a/src/string.c
+++ b/src/string.c
@@ -2503,8 +2503,15 @@ mrb_str_len_to_dbl(mrb_state *mrb, const char *s, size_t len, mrb_bool badcheck)
while (ISSPACE(*s)) s++;
p = s;
- if (!badcheck && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
- return 0.0;
+ if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
+ mrb_value x;
+
+ x = mrb_str_len_to_inum(mrb, p, pend-p, 0, badcheck);
+ if (mrb_fixnum_p(x))
+ d = (double)mrb_fixnum(x);
+ else /* if (mrb_float_p(x)) */
+ d = mrb_float(x);
+ return d;
}
while (p < pend) {
if (!*p) {