Skip to content

Commit 2dd0262

Browse files
committed
[GR-18163] Update String#lstrip! to remove leading null characters
PullRequest: truffleruby/3217
2 parents 0e73061 + b82662f commit 2dd0262

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Compatibility:
3838
* Update `String#rindex` to only accept `Regexp` or objects convertable to `String` as the first parameter (#2608, @bjfish).
3939
* Update `String#<<` to require one argument (#2609, @bjfish).
4040
* Update `String#split` to raise `TypeError` when false is given (#2606, @bjfish).
41+
* Update `String#lstrip!` to remove leading null characters (#2607, @bjfish).
4142

4243
Performance:
4344

spec/ruby/core/string/lstrip_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"hello".lstrip.should == "hello"
1313
end
1414

15-
ruby_version_is '3.1' do
15+
ruby_version_is '3.0' do
1616
it "strips leading \\0" do
1717
"\x00hello".lstrip.should == "hello"
1818
"\000 \000hello\000 \000".lstrip.should == "hello\000 \000"
@@ -35,7 +35,7 @@
3535
a.should == "hello "
3636
end
3737

38-
ruby_version_is '3.1' do
38+
ruby_version_is '3.0' do
3939
it "strips leading \\0" do
4040
a = "\000 \000hello\000 \000"
4141
a.lstrip!

spec/ruby/core/string/strip_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"\tgoodbye\r\v\n".strip.should == "goodbye"
1212
end
1313

14-
ruby_version_is '3.1' do
14+
ruby_version_is '3.0' do
1515
it "returns a copy of self without leading and trailing NULL bytes and whitespace" do
1616
" \x00 goodbye \x00 ".strip.should == "goodbye"
1717
end
@@ -43,7 +43,7 @@
4343
a.should == "hello"
4444
end
4545

46-
ruby_version_is '3.1' do
46+
ruby_version_is '3.0' do
4747
it "removes leading and trailing NULL bytes and whitespace" do
4848
a = "\000 goodbye \000"
4949
a.strip!

src/main/java/org/truffleruby/core/string/StringNodes.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1804,15 +1804,15 @@ protected Object lstripBangSingleByte(RubyString string,
18041804
// Check the first code point to see if it's a space. In the case of strings without leading spaces,
18051805
// this check can avoid having to materialize the entire byte[] (a potentially expensive operation
18061806
// for ropes) and can avoid having to compile the while loop.
1807-
if (noopProfile.profile(!StringSupport.isAsciiSpace(firstCodePoint))) {
1807+
if (noopProfile.profile(!isSpace(firstCodePoint))) {
18081808
return nil;
18091809
}
18101810

18111811
final int end = rope.byteLength();
18121812
final byte[] bytes = bytesNode.execute(rope);
18131813

18141814
int p = 0;
1815-
while (p < end && StringSupport.isAsciiSpace(bytes[p])) {
1815+
while (p < end && isSpace(bytes[p])) {
18161816
p++;
18171817
}
18181818

@@ -1838,7 +1838,7 @@ protected Object lstripBang(RubyString string,
18381838
int p = s;
18391839
while (p < end) {
18401840
int c = getCodePointNode.executeGetCodePoint(enc, rope, p);
1841-
if (!ASCIIEncoding.INSTANCE.isSpace(c)) {
1841+
if (!(c == 0 || ASCIIEncoding.INSTANCE.isSpace(c))) {
18421842
break;
18431843
}
18441844
p += StringSupport.codeLength(enc.jcoding, c);
@@ -1852,6 +1852,10 @@ protected Object lstripBang(RubyString string,
18521852

18531853
return nil;
18541854
}
1855+
1856+
private boolean isSpace(int c) {
1857+
return c == 0 || StringSupport.isAsciiSpace(c);
1858+
}
18551859
}
18561860

18571861
@CoreMethod(names = "ord")

0 commit comments

Comments
 (0)