|
1 | 1 | package com.oracle.graal.python.builtins.modules.csv;
|
2 | 2 |
|
| 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 | + |
3 | 6 | import com.oracle.graal.python.builtins.PythonBuiltinClassType;
|
4 | 7 | import com.oracle.graal.python.builtins.objects.PNone;
|
5 | 8 | import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
|
|
13 | 16 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
|
14 | 17 | import com.oracle.truffle.api.object.Shape;
|
15 | 18 |
|
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 |
| - |
19 | 19 | public final class CSVWriter extends PythonBuiltinObject {
|
20 | 20 |
|
21 | 21 | Object write; /* write output lines to this file */
|
@@ -84,33 +84,36 @@ void joinField(Object field) {
|
84 | 84 | }
|
85 | 85 |
|
86 | 86 | 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; |
94 | 89 |
|
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 | + } |
106 | 108 |
|
107 |
| - if (!wantEscape) { |
108 |
| - needsQuotes = true; |
109 |
| - } |
| 109 | + if (!wantEscape) { |
| 110 | + needsQuotes = true; |
| 111 | + break; |
110 | 112 | }
|
111 |
| - offset += Character.charCount(c); |
112 | 113 | }
|
| 114 | + offset += Character.charCount(c); |
113 | 115 | }
|
| 116 | + |
114 | 117 | return needsQuotes;
|
115 | 118 | }
|
116 | 119 |
|
@@ -152,7 +155,7 @@ void joinAppend(Object field, boolean quoted) {
|
152 | 155 | if (c == dialect.delimiterCodePoint ||
|
153 | 156 | c == dialect.escapeCharCodePoint ||
|
154 | 157 | c == dialect.quoteCharCodePoint ||
|
155 |
| - dialect.lineTerminator.contains(new String(Character.toChars(c)))) { |
| 158 | + dialect.lineTerminator.codePoints().anyMatch(cp -> cp == c)) { |
156 | 159 |
|
157 | 160 | if (dialect.quoting == QUOTE_NONE) {
|
158 | 161 | wantEscape = true;
|
|
0 commit comments