From 21c4eb246c72a896ec5de2346a7232999a6fec09 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Mon, 11 Sep 2023 00:01:22 +0200 Subject: [PATCH] More specs for String#to_i with underscores Specifically for cases with leading zeros, which falls into logic in CRuby that ignores any number of non-consecutivee underscores but bails out on two or more consecutive underscores. --- spec/ruby/core/string/to_i_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spec/ruby/core/string/to_i_spec.rb b/spec/ruby/core/string/to_i_spec.rb index e4fa89aab3e..9931502baa2 100644 --- a/spec/ruby/core/string/to_i_spec.rb +++ b/spec/ruby/core/string/to_i_spec.rb @@ -10,6 +10,18 @@ "1_2_3asdf".to_i.should == 123 end + it "ignores multiple non-consecutive underscoes when the first digit is 0" do + (2..16).each do |base| + "0_0_010".to_i(base).should == base; + end + end + + it "bails out at the first double underscore if the first digit is 0" do + (2..16).each do |base| + "010__1".to_i(base).should == base; + end + end + it "ignores leading whitespaces" do [ " 123", " 123", "\r\n\r\n123", "\t\t123", "\r\n\t\n123", " \t\n\r\t 123"].each do |str|