Skip to content

Commit 9f074db

Browse files
committed
Refactor CSVWriter for readability and more performant contains checks.
Increase readibility of `needsQuotes()` method. Improve performance of check if the current character is contained in the lineterminator. Previously a new String object was created from the current characters code point, now the check is done on the code point Stream of the lineterminator.
1 parent 74fbbfc commit 9f074db

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

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

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.oracle.graal.python.builtins.modules.csv;
22

3+
import static com.oracle.graal.python.builtins.modules.csv.CSVModuleBuiltins.NOT_SET;
4+
import static com.oracle.graal.python.builtins.modules.csv.QuoteStyle.QUOTE_NONE;
5+
36
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
47
import com.oracle.graal.python.builtins.objects.PNone;
58
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
@@ -13,9 +16,6 @@
1316
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
1417
import com.oracle.truffle.api.object.Shape;
1518

16-
import static com.oracle.graal.python.builtins.modules.csv.CSVModuleBuiltins.NOT_SET;
17-
import static com.oracle.graal.python.builtins.modules.csv.QuoteStyle.QUOTE_NONE;
18-
1919
public final class CSVWriter extends PythonBuiltinObject {
2020

2121
Object write; /* write output lines to this file */
@@ -84,33 +84,36 @@ void joinField(Object field) {
8484
}
8585

8686
boolean needsQuotes(Object field) {
87-
boolean needsQuotes = false;
88-
if (field != null) {
89-
String fieldStr = field.toString();
90-
int strLen = fieldStr.length();
91-
92-
for (int offset = 0; offset < strLen; ) {
93-
boolean wantEscape = false;
87+
if (field == null)
88+
return false;
9489

95-
final int c = fieldStr.codePointAt(offset);
96-
if (c == dialect.delimiterCodePoint ||
97-
c == dialect.escapeCharCodePoint ||
98-
c == dialect.quoteCharCodePoint ||
99-
dialect.lineTerminator.contains(new String(Character.toChars(c)))) {
100-
101-
if (dialect.quoting == QUOTE_NONE ||
102-
c == dialect.quoteCharCodePoint && !dialect.doubleQuote ||
103-
c == dialect.escapeCharCodePoint) {
104-
wantEscape = true;
105-
}
90+
boolean needsQuotes = false;
91+
String fieldStr = field.toString();
92+
final int strLen = fieldStr.length();
93+
94+
for (int offset = 0; offset < strLen;) {
95+
boolean wantEscape = false;
96+
97+
final int c = fieldStr.codePointAt(offset);
98+
if (c == dialect.delimiterCodePoint ||
99+
c == dialect.escapeCharCodePoint ||
100+
c == dialect.quoteCharCodePoint ||
101+
dialect.lineTerminator.codePoints().anyMatch(cp -> cp == c)) {
102+
103+
if (dialect.quoting == QUOTE_NONE ||
104+
c == dialect.quoteCharCodePoint && !dialect.doubleQuote ||
105+
c == dialect.escapeCharCodePoint) {
106+
wantEscape = true;
107+
}
106108

107-
if (!wantEscape) {
108-
needsQuotes = true;
109-
}
109+
if (!wantEscape) {
110+
needsQuotes = true;
111+
break;
110112
}
111-
offset += Character.charCount(c);
112113
}
114+
offset += Character.charCount(c);
113115
}
116+
114117
return needsQuotes;
115118
}
116119

@@ -152,7 +155,7 @@ void joinAppend(Object field, boolean quoted) {
152155
if (c == dialect.delimiterCodePoint ||
153156
c == dialect.escapeCharCodePoint ||
154157
c == dialect.quoteCharCodePoint ||
155-
dialect.lineTerminator.contains(new String(Character.toChars(c)))) {
158+
dialect.lineTerminator.codePoints().anyMatch(cp -> cp == c)) {
156159

157160
if (dialect.quoting == QUOTE_NONE) {
158161
wantEscape = true;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.oracle.graal.python.lib;
2+
3+
public class PyDictSetItem {
4+
}

0 commit comments

Comments
 (0)