Skip to content

Commit 1e1b213

Browse files
committed
Refactor: don't use Stream API functionality or String formatting outside Truffle boundaries.
Concrete refactorings: - Add self implemented `strToCodePointArray` method to be able to store the line terminator of a csv file as a code point array with out relying on the `str.codePoints()` method that produces an `IntStream` of code points. - Add (somewhat) redundant error message for strings displayed with quotes in the error message instead of using string concatenation when calling the Error message.
1 parent 8907c84 commit 1e1b213

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/csv/CSVDialect.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ public CSVDialect(Object cls, Shape instanceShape, String delimiter, boolean dou
8484
this.delimiterCodePoint = this.delimiter == NOT_SET ? NOT_SET_CODEPOINT : this.delimiter.codePointAt(0);
8585
this.escapeCharCodePoint = this.escapeChar == NOT_SET ? NOT_SET_CODEPOINT : this.escapeChar.codePointAt(0);
8686
this.quoteCharCodePoint = quoteChar.codePointAt(0); // quote char cannot be NOT_SET
87-
this.lineTerminatorCodePoints = this.lineTerminator.codePoints().toArray();
87+
this.lineTerminatorCodePoints = strToCodePointArray(this.lineTerminator);
88+
}
89+
90+
private static int[] strToCodePointArray(String str) {
91+
final int strLen = str.length();
92+
final int codePointCount = str.codePointCount(0, strLen);
93+
int[] codePoints = new int[codePointCount];
94+
95+
for (int offset = 0, index = 0; offset < strLen; index++) {
96+
final int c = str.codePointAt(offset);
97+
codePoints[index] = c;
98+
offset += Character.charCount(c);
99+
}
100+
return codePoints;
88101
}
89102
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/csv/CSVModuleBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ private String getString(String attribute, Object valueObj, String defaultValue,
572572
try {
573573
value = castToJavaStringNode.execute(valueObj);
574574
} catch (CannotCastException e) {
575-
throw raise(TypeError, ErrorMessages.MUST_BE_STRING, "\"" + attribute + "\"");
575+
throw raise(TypeError, ErrorMessages.MUST_BE_STRING_QUOTED, attribute);
576576
}
577577

578578
return value;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/ErrorMessages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ public abstract class ErrorMessages {
441441
public static final String MUST_BE_S_OR_S = "%s must be %s or %s";
442442
public static final String MUST_BE_SET_TO_S_OBJ = "%s must be set to a %s object";
443443
public static final String MUST_BE_STRING = "%s must be a string";
444+
public static final String MUST_BE_STRING_QUOTED = "\"%s\" must be a string";
444445
public static final String MUST_BE_STRINGS = "%s must be strings";
445446
public static final String MUST_BE_STRINGS_NOT_P = "%s must be strings, not %p";
446447
public static final String MUST_BE_TUPLE_OF_CLASSES_NOT_P = "%s.%s must be tuple of classes, not '%p'";

0 commit comments

Comments
 (0)