Visitar URL original
Use Byte.toUnsignedInt instead of masking with 0xff · jruby/jruby@d41c811 · GitHub
Skip to content

Commit d41c811

Browse files
Use Byte.toUnsignedInt instead of masking with 0xff
1 parent 21e5ecc commit d41c811

2 files changed

Lines changed: 37 additions & 37 deletions

File tree

‎core/src/main/java/org/jruby/RubyString.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6444,7 +6444,7 @@ private IRubyObject enumerateCodepoints(ThreadContext context, String name, Bloc
64446444

64456445
final boolean asciiCompat = enc.isAsciiCompatible();
64466446
while (ptr < end) {
6447-
int c = ptrBytes[ptr] & 0xff;
6447+
int c = Byte.toUnsignedInt(ptrBytes[ptr]);
64486448
int n = 1;
64496449
// an ASCII byte in an ASCII-compatible encoding is its own codepoint
64506450
if (!asciiCompat || !Encoding.isAscii(c)) {
@@ -7024,7 +7024,7 @@ else if (MBCLEN_CHARFOUND_P(ret)) {
70247024
cr = CR_VALID;
70257025
p += MBCLEN_CHARFOUND_LEN(ret);
70267026
// skip the ASCII run after a valid multibyte char
7027-
if (p < e && Encoding.isAscii(pBytes[p] & 0xff)) {
7027+
if (p < e && Encoding.isAscii(Byte.toUnsignedInt(pBytes[p]))) {
70287028
p = StringSupport.searchNonAscii(pBytes, p, e);
70297029
if (p == -1) {
70307030
p = e;

‎core/src/main/java/org/jruby/util/StringSupport.java‎

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ public static long getAscii(Encoding enc, byte[]bytes, int p, int end, int len)
638638
if (p >= end) return pack(-1, len);
639639

640640
if (enc.isAsciiCompatible()) {
641-
int c = bytes[p] & 0xff;
641+
int c = Byte.toUnsignedInt(bytes[p]);
642642
if (!Encoding.isAscii(c)) return pack(-1, len);
643643
return pack(c, len == 0 ? 0 : 1);
644644
} else {
@@ -764,7 +764,7 @@ public static int offset(RubyString str, int pos) {
764764
public static int caseCmp(byte[]bytes1, int p1, byte[]bytes2, int p2, int len) {
765765
int i = -1;
766766
for (; ++i < len && bytes1[p1 + i] == bytes2[p2 + i];) {}
767-
if (i < len) return (bytes1[p1 + i] & 0xff) > (bytes2[p2 + i] & 0xff) ? 1 : -1;
767+
if (i < len) return Byte.toUnsignedInt(bytes1[p1 + i]) > Byte.toUnsignedInt(bytes2[p2 + i]) ? 1 : -1;
768768
return 0;
769769
}
770770

@@ -775,7 +775,7 @@ public static int scanHex(byte[]bytes, int p, int len) {
775775
public static int scanHex(byte[]bytes, int p, int len, Encoding enc) {
776776
int v = 0;
777777
int c;
778-
while (len-- > 0 && enc.isXDigit(c = bytes[p++] & 0xff)) {
778+
while (len-- > 0 && enc.isXDigit(c = Byte.toUnsignedInt(bytes[p++]))) {
779779
v = (v << 4) + enc.xdigitVal(c);
780780
}
781781
return v;
@@ -787,7 +787,7 @@ public static int hexLength(byte[]bytes, int p, int len) {
787787

788788
public static int hexLength(byte[]bytes, int p, int len, Encoding enc) {
789789
int hlen = 0;
790-
while (len-- > 0 && enc.isXDigit(bytes[p++] & 0xff)) hlen++;
790+
while (len-- > 0 && enc.isXDigit(Byte.toUnsignedInt(bytes[p++]))) hlen++;
791791
return hlen;
792792
}
793793

@@ -798,7 +798,7 @@ public static int scanOct(byte[]bytes, int p, int len) {
798798
public static int scanOct(byte[]bytes, int p, int len, Encoding enc) {
799799
int v = 0;
800800
int c;
801-
while (len-- > 0 && enc.isDigit(c = bytes[p++] & 0xff) && c < '8') {
801+
while (len-- > 0 && enc.isDigit(c = Byte.toUnsignedInt(bytes[p++])) && c < '8') {
802802
v = (v << 3) + Encoding.digitVal(c);
803803
}
804804
return v;
@@ -811,7 +811,7 @@ public static int octLength(byte[]bytes, int p, int len) {
811811
public static int octLength(byte[]bytes, int p, int len, Encoding enc) {
812812
int olen = 0;
813813
int c;
814-
while (len-- > 0 && enc.isDigit(c = bytes[p++] & 0xff) && c < '8') olen++;
814+
while (len-- > 0 && enc.isDigit(c = Byte.toUnsignedInt(bytes[p++])) && c < '8') olen++;
815815
return olen;
816816
}
817817

@@ -899,7 +899,7 @@ public static int bytesToFixBrokenTrailingCharacter(byte[] bytes, int begin, int
899899
charHead -= begin;
900900

901901
// byte at char head
902-
byte byteHead = (byte)(bytes[begin + charHead] & 0xFF);
902+
byte byteHead = bytes[begin + charHead];
903903

904904
// total bytes we would need to complete character
905905
int extra = encoding.length(byteHead);
@@ -1038,7 +1038,7 @@ public static ByteList dumpCommon(Ruby runtime, ByteList byteList, boolean quote
10381038

10391039
int len = 2;
10401040
while (p < end) {
1041-
int c = bytes[p++] & 0xff;
1041+
int c = Byte.toUnsignedInt(bytes[p++]);
10421042

10431043
switch (c) {
10441044
case '"':case '\\':case '\n':case '\r':case '\t':case '\f':
@@ -1051,7 +1051,7 @@ public static ByteList dumpCommon(Ruby runtime, ByteList byteList, boolean quote
10511051
default:
10521052
if (ASCIIEncoding.INSTANCE.isPrint(c)) {
10531053
int run = p;
1054-
while (p < end && ASCII_NO_ESCAPE[bytes[p] & 0xff]) p++;
1054+
while (p < end && ASCII_NO_ESCAPE[Byte.toUnsignedInt(bytes[p])]) p++;
10551055
len += 1 + p - run;
10561056
} else {
10571057
includingsNonprintable = true;
@@ -1088,7 +1088,7 @@ public static ByteList dumpCommon(Ruby runtime, ByteList byteList, boolean quote
10881088

10891089
if (!quoteOnlyIfNeeded || includingsNonprintable) out[q++] = '"';
10901090
while (p < end) {
1091-
int c = bytes[p++] & 0xff;
1091+
int c = Byte.toUnsignedInt(bytes[p++]);
10921092
switch (c) {
10931093
case '"': case '\\':
10941094
out[q++] = '\\'; out[q++] = (byte)c; break;
@@ -1116,7 +1116,7 @@ public static ByteList dumpCommon(Ruby runtime, ByteList byteList, boolean quote
11161116
if (ASCIIEncoding.INSTANCE.isPrint(c)) {
11171117
out[q++] = (byte)c;
11181118
int run = p;
1119-
while (p < end && ASCII_NO_ESCAPE[bytes[p] & 0xff]) p++;
1119+
while (p < end && ASCII_NO_ESCAPE[Byte.toUnsignedInt(bytes[p])]) p++;
11201120
System.arraycopy(bytes, run, out, q, p - run);
11211121
q += p - run;
11221122
} else {
@@ -1149,7 +1149,7 @@ public static ByteList dumpCommon(Ruby runtime, ByteList byteList, boolean quote
11491149
}
11501150

11511151
public static boolean isEVStr(byte[] bytes, int p, int end) {
1152-
return p < end ? isEVStr(bytes[p] & 0xff) : false;
1152+
return p < end ? isEVStr(Byte.toUnsignedInt(bytes[p])) : false;
11531153
}
11541154

11551155
public static boolean isEVStr(int c) {
@@ -1166,7 +1166,7 @@ public static int strCount(ByteList str, boolean[] table, TrTables tables, Encod
11661166
int count = 0;
11671167
while (p < end) {
11681168
int c;
1169-
if (asciiCompat && (c = bytes[p] & 0xff) < 0x80) {
1169+
if (asciiCompat && (c = Byte.toUnsignedInt(bytes[p])) < 0x80) {
11701170
if (table[c]) count++;
11711171
p++;
11721172
} else {
@@ -1539,10 +1539,10 @@ public static ByteList succCommon(ByteList original) {
15391539
while ((s = enc.prevCharHead(bytes, p, s, end)) != -1) {
15401540
if (neighbor == NeighborChar.NOT_CHAR && lastAlnum != -1) {
15411541
ASCIIEncoding ascii = ASCIIEncoding.INSTANCE;
1542-
if (ascii.isAlpha(bytes[lastAlnum] & 0xff) ?
1543-
ascii.isDigit(bytes[s] & 0xff) :
1544-
ascii.isDigit(bytes[lastAlnum] & 0xff) ?
1545-
ascii.isAlpha(bytes[s] & 0xff) : false) {
1542+
if (ascii.isAlpha(Byte.toUnsignedInt(bytes[lastAlnum])) ?
1543+
ascii.isDigit(Byte.toUnsignedInt(bytes[s])) :
1544+
ascii.isDigit(Byte.toUnsignedInt(bytes[lastAlnum])) ?
1545+
ascii.isAlpha(Byte.toUnsignedInt(bytes[s])) : false) {
15461546
s = lastAlnum;
15471547
break;
15481548
}
@@ -1629,7 +1629,7 @@ public static NeighborChar succChar(Encoding enc, byte[] bytes, int p, int len)
16291629
int i = len - 1;
16301630
for (; i >= 0 && bytes[p + i] == (byte)0xff; i--) bytes[p + i] = 0;
16311631
if (i < 0) return NeighborChar.WRAPPED;
1632-
bytes[p + i] = (byte)((bytes[p + i] & 0xff) + 1);
1632+
bytes[p + i] = (byte)(Byte.toUnsignedInt(bytes[p + i]) + 1);
16331633
l = preciseLength(enc, bytes, p, p + len);
16341634
if (MBCLEN_CHARFOUND_P(l)) {
16351635
l = MBCLEN_CHARFOUND_LEN(l);
@@ -1749,7 +1749,7 @@ private static NeighborChar predChar(Encoding enc, byte[]bytes, int p, int len)
17491749
int i = len - 1;
17501750
for (; i >= 0 && bytes[p + i] == 0; i--) bytes[p + i] = (byte)0xff;
17511751
if (i < 0) return NeighborChar.WRAPPED;
1752-
bytes[p + i] = (byte)((bytes[p + i] & 0xff) - 1);
1752+
bytes[p + i] = (byte)(Byte.toUnsignedInt(bytes[p + i]) - 1);
17531753
l = preciseLength(enc, bytes, p, p + len);
17541754
if (MBCLEN_CHARFOUND_P(l)) {
17551755
l = MBCLEN_CHARFOUND_LEN(l);
@@ -1992,7 +1992,7 @@ static CodeRangeable strDeleteBang(CodeRangeable rubyString, boolean[] squeeze,
19921992
int cr = asciiCompatible ? CR_7BIT : CR_VALID;
19931993
while (s < send) {
19941994
int c;
1995-
if (asciiCompatible && Encoding.isAscii(c = bytes[s] & 0xff)) {
1995+
if (asciiCompatible && Encoding.isAscii(c = Byte.toUnsignedInt(bytes[s]))) {
19961996
if (squeeze[c]) {
19971997
modify = true;
19981998
} else {
@@ -2504,7 +2504,7 @@ public static CodeRangeable trTransHelper(CodeRangeable self, CodeRangeable srcS
25042504
self.getByteList().setRealSize(t);
25052505
} else if (enc.isSingleByte() || (singlebyte && hash == null)) {
25062506
while (s < send) {
2507-
c = sbytes[s] & 0xff;
2507+
c = Byte.toUnsignedInt(sbytes[s]);
25082508
if (trans[c] != -1) {
25092509
if (!cflag) {
25102510
c = trans[c];
@@ -2619,8 +2619,8 @@ public static int multiByteCasecmp(Encoding enc, ByteList value, ByteList otherV
26192619
while (p < end && op < oend) {
26202620
final int c, oc;
26212621
if (enc.isAsciiCompatible()) {
2622-
c = bytes[p] & 0xff;
2623-
oc = obytes[op] & 0xff;
2622+
c = Byte.toUnsignedInt(bytes[p]);
2623+
oc = Byte.toUnsignedInt(obytes[op]);
26242624
} else {
26252625
c = preciseCodePoint(enc, bytes, p, end);
26262626
oc = preciseCodePoint(enc, obytes, op, oend);
@@ -2661,7 +2661,7 @@ public static boolean singleByteSqueeze(ByteList value, boolean squeeze[]) {
26612661
int save = -1;
26622662

26632663
while (s < send) {
2664-
int c = bytes[s++] & 0xff;
2664+
int c = Byte.toUnsignedInt(bytes[s++]);
26652665
if (c != save || !squeeze[c]) bytes[t++] = (byte)(save = c);
26662666
}
26672667

@@ -2682,7 +2682,7 @@ public static boolean multiByteSqueeze(ByteList value, boolean squeeze[], TrTabl
26822682
int c;
26832683

26842684
while (s < send) {
2685-
if (enc.isAsciiCompatible() && (c = bytes[s] & 0xff) < 0x80) {
2685+
if (enc.isAsciiCompatible() && (c = Byte.toUnsignedInt(bytes[s])) < 0x80) {
26862686
if (c != save || (isArg && !squeeze[c])) bytes[t++] = (byte)(save = c);
26872687
s++;
26882688
} else {
@@ -2766,9 +2766,9 @@ private static int rb_memsearch_qs(byte[] xsBytes, int xs, int m, byte[] ysBytes
27662766
/* Preprocessing */
27672767
Arrays.fill(qstable, m + 1);
27682768
for (; x < xe; ++x)
2769-
qstable[xsBytes[x] & 0xFF] = xe - x;
2769+
qstable[Byte.toUnsignedInt(xsBytes[x])] = xe - x;
27702770
/* Searching */
2771-
for (; y + m <= ys + n; y += qstable[ysBytes[y + m] & 0xFF]) {
2771+
for (; y + m <= ys + n; y += qstable[Byte.toUnsignedInt(ysBytes[y + m])]) {
27722772
if (xsBytes[xs] == ysBytes[y] && ByteList.memcmp(xsBytes, xs, ysBytes, y, m) == 0)
27732773
return y - ys;
27742774
}
@@ -2779,7 +2779,7 @@ private static int rb_memsearch_qs_utf8_hash(byte[] xBytes, final int x) {
27792779
final int mix = 8353;
27802780
int h;
27812781
if (x != xBytes.length) {
2782-
h = xBytes[x] & 0xFF;
2782+
h = Byte.toUnsignedInt(xBytes[x]);
27832783
}
27842784
else {
27852785
h = '\0'; // (C) ary end - due y+m at rb_memsearch_qs_utf8
@@ -2789,27 +2789,27 @@ private static int rb_memsearch_qs_utf8_hash(byte[] xBytes, final int x) {
27892789
}
27902790
else if (h < 0xE0) {
27912791
h *= mix;
2792-
h += xBytes[x + 1] & 0xff;
2792+
h += Byte.toUnsignedInt(xBytes[x + 1]);
27932793
}
27942794
else if (h < 0xF0) {
27952795
h *= mix;
2796-
h += xBytes[x + 1] & 0xff;
2796+
h += Byte.toUnsignedInt(xBytes[x + 1]);
27972797
h *= mix;
2798-
h += xBytes[x + 2] & 0xff;
2798+
h += Byte.toUnsignedInt(xBytes[x + 2]);
27992799

28002800
}
28012801
else if (h < 0xF5) {
28022802
h *= mix;
2803-
h += xBytes[x + 1] & 0xff;
2803+
h += Byte.toUnsignedInt(xBytes[x + 1]);
28042804
h *= mix;
2805-
h += xBytes[x + 2] & 0xff;
2805+
h += Byte.toUnsignedInt(xBytes[x + 2]);
28062806
h *= mix;
2807-
h += xBytes[x + 3] & 0xff;
2807+
h += Byte.toUnsignedInt(xBytes[x + 3]);
28082808
}
28092809
else {
28102810
return h + 256;
28112811
}
2812-
return ((byte)h) & 0xff;
2812+
return Byte.toUnsignedInt((byte) h);
28132813
}
28142814

28152815
private static int rb_memsearch_qs_utf8(byte[] xsBytes, int xs, int m, byte[] ysBytes, int ys, int n) {

0 commit comments

Comments
 (0)