Skip to content

Commit ccc9eb9

Browse files
committed
[GR-17457] Update ASCIIEncoding.INSTANCE.isSpace to StringSupport.isAsciiSpace
PullRequest: truffleruby/3237
2 parents 199ad95 + 3dcc6ed commit ccc9eb9

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

src/main/java/org/truffleruby/core/regexp/ClassicRegexp.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import java.util.Iterator;
4646

4747
import org.jcodings.Encoding;
48-
import org.jcodings.specific.ASCIIEncoding;
4948
import org.jcodings.specific.USASCIIEncoding;
5049
import org.joni.NameEntry;
5150
import org.joni.Option;
@@ -281,7 +280,7 @@ private static int unescapeUnicodeBmp(RopeBuilder to, byte[] bytes, int p, int e
281280

282281
private static int unescapeUnicodeList(RopeBuilder to, byte[] bytes, int p, int end,
283282
RubyEncoding[] encp, Rope str, RegexpSupport.ErrorMode mode) throws DeferredRaiseException {
284-
while (p < end && ASCIIEncoding.INSTANCE.isSpace(bytes[p] & 0xff)) {
283+
while (p < end && StringSupport.isAsciiSpace(bytes[p] & 0xff)) {
285284
p++;
286285
}
287286

@@ -300,7 +299,7 @@ private static int unescapeUnicodeList(RopeBuilder to, byte[] bytes, int p, int
300299
appendUtf8(to, code, encp, str, mode);
301300
}
302301
hasUnicode = true;
303-
while (p < end && ASCIIEncoding.INSTANCE.isSpace(bytes[p] & 0xff)) {
302+
while (p < end && StringSupport.isAsciiSpace(bytes[p] & 0xff)) {
304303
p++;
305304
}
306305
}

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

Lines changed: 6 additions & 9 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(!isSpace(firstCodePoint))) {
1807+
if (noopProfile.profile(!StringSupport.isAsciiSpaceOrNull(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 && isSpace(bytes[p])) {
1815+
while (p < end && StringSupport.isAsciiSpaceOrNull(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 (!(c == 0 || ASCIIEncoding.INSTANCE.isSpace(c))) {
1841+
if (!StringSupport.isAsciiSpaceOrNull(c)) {
18421842
break;
18431843
}
18441844
p += StringSupport.codeLength(enc.jcoding, c);
@@ -1853,9 +1853,6 @@ protected Object lstripBang(RubyString string,
18531853
return nil;
18541854
}
18551855

1856-
private boolean isSpace(int c) {
1857-
return c == 0 || StringSupport.isAsciiSpace(c);
1858-
}
18591856
}
18601857

18611858
@CoreMethod(names = "ord")
@@ -1935,7 +1932,7 @@ protected Object rstripBangSingleByte(RubyString string,
19351932
// Check the last code point to see if it's a space or NULL. In the case of strings without leading spaces,
19361933
// this check can avoid having to materialize the entire byte[] (a potentially expensive operation
19371934
// for ropes) and can avoid having to compile the while loop.
1938-
final boolean willStrip = lastCodePoint == 0x00 || StringSupport.isAsciiSpace(lastCodePoint);
1935+
final boolean willStrip = StringSupport.isAsciiSpaceOrNull(lastCodePoint);
19391936
if (noopProfile.profile(!willStrip)) {
19401937
return nil;
19411938
}
@@ -1944,7 +1941,7 @@ protected Object rstripBangSingleByte(RubyString string,
19441941
final byte[] bytes = bytesNode.execute(rope);
19451942

19461943
int endp = end - 1;
1947-
while (endp >= 0 && (bytes[endp] == 0 || StringSupport.isAsciiSpace(bytes[endp]))) {
1944+
while (endp >= 0 && StringSupport.isAsciiSpaceOrNull(bytes[endp])) {
19481945
endp--;
19491946
}
19501947

@@ -1979,7 +1976,7 @@ protected Object rstripBang(RubyString string,
19791976
int prev;
19801977
while ((prev = prevCharHead(enc.jcoding, bytes, start, endp, end)) != -1) {
19811978
int point = getCodePointNode.executeGetCodePoint(enc, rope, prev);
1982-
if (point != 0 && !ASCIIEncoding.INSTANCE.isSpace(point)) {
1979+
if (!StringSupport.isAsciiSpaceOrNull(point)) {
19831980
break;
19841981
}
19851982
endp = prev;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,6 +1670,10 @@ public static boolean isAsciiSpace(int c) {
16701670
return c == ' ' || ('\t' <= c && c <= '\r');
16711671
}
16721672

1673+
static boolean isAsciiSpaceOrNull(int c) {
1674+
return c == 0 || isAsciiSpace(c);
1675+
}
1676+
16731677
public static boolean isAsciiPrintable(int c) {
16741678
return c == ' ' || (c >= '!' && c <= '~');
16751679
}

0 commit comments

Comments
 (0)