Skip to content

Commit 7b8a3ea

Browse files
committed
Use RopeOperations.decodeAscii() instead of new String() without a Charset
1 parent 01cc700 commit 7b8a3ea

File tree

9 files changed

+34
-18
lines changed

9 files changed

+34
-18
lines changed

mx.truffleruby/spotbugs-filters.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<Bug pattern="MS_MUTABLE_COLLECTION" />
1111
<Bug pattern="SF_SWITCH_NO_DEFAULT" />
1212
<Bug pattern="RI_REDUNDANT_INTERFACES" />
13+
<Bug pattern="EQ_COMPARETO_USE_OBJECT_EQUALS" />
14+
<Bug pattern="SE_BAD_FIELD" />
1315
<!-- Intentional -->
1416
<Bug pattern="BC_UNCONFIRMED_CAST" />
1517
<Bug pattern="BC_UNCONFIRMED_CAST_OF_RETURN_VALUE" />
@@ -18,6 +20,7 @@
1820
<Bug pattern="EI_EXPOSE_REP" />
1921
<Bug pattern="EI_EXPOSE_REP2" />
2022
<Bug pattern="FE_FLOATING_POINT_EQUALITY" />
23+
<Bug pattern="NM_CLASS_NOT_EXCEPTION" />
2124
</Or>
2225
</Not>
2326
</Match>

src/main/java/org/truffleruby/core/encoding/EncodingConverterNodes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ protected Object initialize(
117117

118118
final byte[] segmentSource = transcoder.getSource();
119119
ret[retIndex++] = getSymbol(
120-
RopeOperations.decodeAscii(segmentSource, 0, segmentSource.length).toUpperCase());
120+
RopeOperations.decodeAscii(segmentSource).toUpperCase());
121121
}
122122

123123
final int retSize = retIndex + 1;
@@ -129,7 +129,7 @@ protected Object initialize(
129129

130130
final byte[] destinationName = destinationEncoding.getName();
131131
ret[retIndex] = getSymbol(
132-
RopeOperations.decodeAscii(destinationName, 0, destinationName.length).toUpperCase());
132+
RopeOperations.decodeAscii(destinationName).toUpperCase());
133133

134134
return createArray(ret);
135135
}

src/main/java/org/truffleruby/core/encoding/TranscodingManager.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
import org.jcodings.util.Hash;
4646

4747
import com.oracle.truffle.api.TruffleOptions;
48+
import org.truffleruby.core.rope.RopeOperations;
49+
import org.truffleruby.core.string.StringUtils;
4850

4951
public class TranscodingManager {
5052

@@ -56,8 +58,8 @@ public class TranscodingManager {
5658
for (Hash.HashEntry<TranscoderDB.Entry> destinationEntry : sourceEntry.entryIterator()) {
5759
final TranscoderDB.Entry e = destinationEntry.value;
5860

59-
final String sourceName = new String(e.getSource()).toUpperCase();
60-
final String destinationName = new String(e.getDestination()).toUpperCase();
61+
final String sourceName = StringUtils.toUpperCase(RopeOperations.decodeAscii(e.getSource()));
62+
final String destinationName = StringUtils.toUpperCase(RopeOperations.decodeAscii(e.getDestination()));
6163

6264
if (TruffleOptions.AOT) {
6365
// Load the classes eagerly

src/main/java/org/truffleruby/core/format/format/FormatCharacterNode.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.truffleruby.core.format.convert.ToStringNodeGen;
1919
import org.truffleruby.core.format.exceptions.NoImplicitConversionException;
2020
import org.truffleruby.core.format.write.bytes.WriteByteNodeGen;
21+
import org.truffleruby.core.rope.RopeOperations;
2122
import org.truffleruby.core.string.StringUtils;
2223
import org.truffleruby.language.RubyGuards;
2324
import org.truffleruby.language.control.RaiseException;
@@ -83,7 +84,8 @@ protected String getCharString(VirtualFrame frame, Object value) {
8384
// TODO BJF check char length is > 0
8485
charString = Character.toString((char) charValue);
8586
} else {
86-
final String resultString = StringUtils.create((byte[]) toStrResult);
87+
byte[] bytes = (byte[]) toStrResult;
88+
final String resultString = RopeOperations.decodeAscii(bytes);
8789
final int size = resultString.length();
8890
if (size > 1) {
8991
throw new RaiseException(

src/main/java/org/truffleruby/core/rope/RopeOperations.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,17 @@ public static byte[] encodeAsciiBytes(String value) {
152152
return bytes;
153153
}
154154

155+
public static String decodeAscii(byte[] bytes) {
156+
return decodeAscii(bytes, 0, bytes.length);
157+
}
158+
155159
public static String decodeAscii(byte[] bytes, int byteOffset, int byteLength) {
156160
final char[] buffer = new char[byteLength];
157161

158162
for (int i = 0; i < byteLength; i++) {
159-
buffer[i] = (char) bytes[byteOffset + i];
163+
byte b = bytes[byteOffset + i];
164+
assert b >= 0;
165+
buffer[i] = (char) b;
160166
}
161167

162168
return newString(buffer);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.jcodings.ascii.AsciiTables;
3434
import org.jcodings.specific.ASCIIEncoding;
3535
import org.truffleruby.core.rope.CodeRange;
36+
import org.truffleruby.core.rope.RopeOperations;
3637

3738
public class EncodingUtils {
3839

@@ -80,7 +81,7 @@ public static List<String> encodingNames(byte[] name, int p, int end) {
8081
boolean isValid = false;
8182
if (s >= end) {
8283
isValid = true;
83-
names.add(new String(name, p, end));
84+
names.add(RopeOperations.decodeAscii(name, p, end));
8485
}
8586

8687
if (!isValid || hasLower) {
@@ -111,7 +112,7 @@ public static List<String> encodingNames(byte[] name, int p, int end) {
111112
}
112113
}
113114
if (hasUpper) {
114-
names.add(new String(constName, 0, constName.length));
115+
names.add(RopeOperations.decodeAscii(constName));
115116
}
116117
}
117118
if (hasLower) {
@@ -121,7 +122,7 @@ public static List<String> encodingNames(byte[] name, int p, int end) {
121122
constName[s] = AsciiTables.ToUpperCaseTable[code];
122123
}
123124
}
124-
names.add(new String(constName, 0, constName.length));
125+
names.add(RopeOperations.decodeAscii(constName));
125126
}
126127
}
127128

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,14 @@
1111

1212
import java.util.Locale;
1313

14-
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
1514
import org.truffleruby.core.rope.RopeOperations;
1615

16+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
17+
1718
public abstract class StringUtils {
1819

1920
public static final String[] EMPTY_STRING_ARRAY = new String[0];
2021

21-
@TruffleBoundary
22-
public static String create(byte[] bytes) {
23-
return new String(bytes);
24-
}
25-
2622
@TruffleBoundary
2723
public static String toString(Object value) {
2824
return value.toString();
@@ -57,4 +53,9 @@ public static String toLowerCase(String string) {
5753
return string.toLowerCase(Locale.ENGLISH);
5854
}
5955

56+
@TruffleBoundary
57+
public static String toUpperCase(String string) {
58+
return string.toUpperCase(Locale.ENGLISH);
59+
}
60+
6061
}

src/main/java/org/truffleruby/core/time/RubyTimeOutputFormatter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
***** END LICENSE BLOCK *****/
2727
package org.truffleruby.core.time;
2828

29+
import org.truffleruby.core.string.StringUtils;
2930
import org.truffleruby.core.time.RubyDateFormatter.FieldType;
3031

3132
/** Support for GNU-C output formatters, see:
@@ -84,9 +85,9 @@ public String format(String sequence, long value, FieldType type) {
8485
case '#': // change case
8586
char last = sequence.charAt(sequence.length() - 1);
8687
if (Character.isLowerCase(last)) {
87-
sequence = sequence.toUpperCase();
88+
sequence = StringUtils.toUpperCase(sequence);
8889
} else {
89-
sequence = sequence.toLowerCase();
90+
sequence = StringUtils.toLowerCase(sequence);
9091
}
9192
break;
9293
}

src/main/java/org/truffleruby/interop/ToJavaStringNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected String stringUncached(RubyString value,
6161
final byte[] bytes = bytesNode.execute(rope);
6262

6363
if (asciiOnlyProfile.profile(asciiOnlyNode.execute(rope))) {
64-
return RopeOperations.decodeAscii(bytes, 0, bytes.length);
64+
return RopeOperations.decodeAscii(bytes);
6565
} else {
6666
return RopeOperations.decodeNonAscii(rope.getEncoding(), bytes, 0, bytes.length);
6767
}

0 commit comments

Comments
 (0)