Skip to content

Commit

Permalink
[GR-18163] Fix String#split to return empty array when splitting all …
Browse files Browse the repository at this point in the history
…whitespace on whitespace

PullRequest: truffleruby/3115
  • Loading branch information
bjfish committed Jan 13, 2022
2 parents 754cfcb + c9279eb commit 6dd971c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Bug fixes:

* Guard against unterminated ranges in file matching patterns (#2556, @aardvark179).
* Fixed `rb_proc_new` to return a proc that will pass all required arguments to C (#2556, @aardvark179).
* Fixed `String#split` to return empty array when splitting all whitespace on whitespace (#2565, @bjfish).

Compatibility:

Expand Down
11 changes: 11 additions & 0 deletions spec/ruby/core/string/split_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,17 @@
end
end
end

it "returns an empty array when whitespace is split on whitespace" do
" ".split(" ").should == []
" \n ".split(" ").should == []
" ".split(" ").should == []
" \t ".split(" ").should == []
end

it "doesn't split on non-ascii whitespace" do
"a\u{2008}b".split(" ").should == ["a\u{2008}b"]
end
end

describe "String#split with Regexp" do
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/truffleruby/core/string/StringNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -3607,7 +3607,7 @@ protected Object stringAwkSplitSingleByte(Object string, int limit, Object block
}
}

if (trailingSubstringProfile.profile(substringStart != SUBSTRING_CREATED)) {
if (trailingSubstringProfile.profile(findingSubstringEnd)) {
final RubyString substring = substringNode
.executeSubstring(string, substringStart, bytes.length - substringStart);
ret = addSubstring(ret, storeIndex++, substring, block, executeBlockProfile, growArrayProfile);
Expand Down Expand Up @@ -3655,7 +3655,7 @@ protected Object stringAwkSplit(Object string, int limit, Object block,
p += StringSupport.characterLength(enc, cr, bytes, p, end, true);

if (skip) {
if (StringSupport.isSpace(enc, c)) {
if (StringSupport.isAsciiSpace(c)) {
b = p - ptr;
} else {
e = p - ptr;
Expand All @@ -3665,7 +3665,7 @@ protected Object stringAwkSplit(Object string, int limit, Object block,
}
}
} else {
if (StringSupport.isSpace(enc, c)) {
if (StringSupport.isAsciiSpace(c)) {
final RubyString substring = substringNode.executeSubstring(string, b, e - b);
ret = addSubstring(
ret,
Expand Down

0 comments on commit 6dd971c

Please sign in to comment.