Skip to content

Commit

Permalink
More specs for String#to_i with underscores
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
headius committed Sep 10, 2023
1 parent 79a7535 commit 21c4eb2
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions spec/ruby/core/string/to_i_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down

0 comments on commit 21c4eb2

Please sign in to comment.