[前][次][番号順一覧][スレッド一覧]

ruby-changes:45906

From: nobu <ko1@a...>
Date: Wed, 15 Mar 2017 17:15:42 +0900 (JST)
Subject: [ruby-changes:45906] nobu:r57979 (trunk): object.c: make String#to_f consistent with literal

nobu	2017-03-15 17:15:32 +0900 (Wed, 15 Mar 2017)

  New Revision: 57979

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=57979

  Log:
    object.c: make String#to_f consistent with literal
    
    * object.c (rb_cstr_to_dbl): stop at successive underscores, as
      well as Float literals.  [ruby-core:80098] [Bug #13105]
    
      * `_` should be within digits
      * only one `_` allowed between digits

  Modified files:
    trunk/object.c
    trunk/test/ruby/test_float.rb
Index: object.c
===================================================================
--- object.c	(revision 57978)
+++ object.c	(revision 57979)
@@ -2870,15 +2870,10 @@ rb_cstr_to_dbl(const char *p, int badche https://github.com/ruby/ruby/blob/trunk/object.c#L2870
 	while (p < end && n < e) prev = *n++ = *p++;
 	while (*p) {
 	    if (*p == '_') {
-		/* remove underscores between digits */
-		if (badcheck) {
-		    if (n == buf || !ISDIGIT(prev)) goto bad;
-		    ++p;
-		    if (!ISDIGIT(*p)) goto bad;
-		}
-		else {
-		    while (*++p == '_');
-		    continue;
+		/* remove an underscore between digits */
+		if (n == buf || !ISDIGIT(prev) || (++p, !ISDIGIT(*p))) {
+		    if (badcheck) goto bad;
+		    break;
 		}
 	    }
 	    prev = *p++;
Index: test/ruby/test_float.rb
===================================================================
--- test/ruby/test_float.rb	(revision 57978)
+++ test/ruby/test_float.rb	(revision 57979)
@@ -755,6 +755,12 @@ class TestFloat < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_float.rb#L755
   def test_Float
     assert_in_delta(0.125, Float("0.1_2_5"), 0.00001)
     assert_in_delta(0.125, "0.1_2_5__".to_f, 0.00001)
+    assert_in_delta(0.0, "0_.125".to_f, 0.00001)
+    assert_in_delta(0.0, "0._125".to_f, 0.00001)
+    assert_in_delta(0.1, "0.1__2_5".to_f, 0.00001)
+    assert_in_delta(0.1, "0.1_e10".to_f, 0.00001)
+    assert_in_delta(0.1, "0.1e_10".to_f, 0.00001)
+    assert_in_delta(1.0, "0.1e1__0".to_f, 0.00001)
     assert_equal(1, suppress_warning {Float(([1] * 10000).join)}.infinite?)
     assert_not_predicate(Float(([1] * 10000).join("_")), :infinite?) # is it really OK?
     assert_raise(ArgumentError) { Float("1.0\x001") }

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]