Skip to content

Commit 00d0def

Browse files
committed
Replaced the unicode escaping code from the parser with a class implementing java.io.Reader
git-svn-id: https://svn.apache.org/repos/asf/commons/sandbox/csv/trunk@1298001 13f79535-47bb-0310-9956-ffa450edef68
1 parent 60709c2 commit 00d0def

2 files changed

Lines changed: 95 additions & 54 deletions

File tree

src/main/java/org/apache/commons/csv/CSVParser.java

Lines changed: 12 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ public class CSVParser implements Iterable<String[]> {
7676
private final List<String> record = new ArrayList<String>();
7777
private final Token reusableToken = new Token();
7878
private final CharBuffer wsBuf = new CharBuffer();
79-
private final CharBuffer code = new CharBuffer(4);
80-
8179

8280
/**
8381
* Token is an internal token representation.
@@ -137,6 +135,10 @@ public CSVParser(Reader input) {
137135
* @param format the CSVFormat used for CSV parsing
138136
*/
139137
public CSVParser(Reader input, CSVFormat format) {
138+
if (format.isUnicodeEscapesInterpreted()) {
139+
input = new UnicodeUnescapeReader(input);
140+
}
141+
140142
this.in = new ExtendedBufferedReader(input);
141143
this.format = format;
142144
}
@@ -404,9 +406,6 @@ private Token simpleTokenLexer(Token tkn, int c) throws IOException {
404406
tkn.type = TOKEN;
405407
tkn.isReady = true;
406408
break;
407-
} else if (c == '\\' && format.isUnicodeEscapesInterpreted() && in.lookAhead() == 'u') {
408-
// interpret unicode escaped chars (like \u0070 -> p)
409-
tkn.content.append((char) unicodeEscapeLexer(c));
410409
} else if (c == format.getEscape()) {
411410
tkn.content.append((char) readEscape(c));
412411
} else {
@@ -444,10 +443,8 @@ private Token encapsulatedTokenLexer(Token tkn, int c) throws IOException {
444443
// assert c == delimiter;
445444
for (; ;) {
446445
c = in.read();
447-
448-
if (c == '\\' && format.isUnicodeEscapesInterpreted() && in.lookAhead() == 'u') {
449-
tkn.content.append((char) unicodeEscapeLexer(c));
450-
} else if (c == format.getEscape()) {
446+
447+
if (c == format.getEscape()) {
451448
tkn.content.append((char) readEscape(c));
452449
} else if (c == format.getEncapsulator()) {
453450
if (in.lookAhead() == format.getEncapsulator()) {
@@ -487,62 +484,23 @@ private Token encapsulatedTokenLexer(Token tkn, int c) throws IOException {
487484
}
488485
}
489486

490-
491-
/**
492-
* Decodes Unicode escapes.
493-
* <p/>
494-
* Interpretation of "\\uXXXX" escape sequences where XXXX is a hex-number.
495-
*
496-
* @param c current char which is discarded because it's the "\\" of "\\uXXXX"
497-
* @return the decoded character
498-
* @throws IOException on wrong unicode escape sequence or read error
499-
*/
500-
private int unicodeEscapeLexer(int c) throws IOException {
501-
int ret = 0;
502-
// ignore 'u' (assume c==\ now) and read 4 hex digits
503-
c = in.read();
504-
code.clear();
505-
try {
506-
for (int i = 0; i < 4; i++) {
507-
c = in.read();
508-
if (isEndOfFile(c) || isEndOfLine(c)) {
509-
throw new NumberFormatException("number too short");
510-
}
511-
code.append((char) c);
512-
}
513-
ret = Integer.parseInt(code.toString(), 16);
514-
} catch (NumberFormatException e) {
515-
throw new IOException(
516-
"(line " + getLineNumber() + ") Wrong unicode escape sequence found '"
517-
+ code.toString() + "'" + e.toString());
518-
}
519-
return ret;
520-
}
521-
522487
private int readEscape(int c) throws IOException {
523488
// assume c is the escape char (normally a backslash)
524489
c = in.read();
525-
int out;
526490
switch (c) {
527491
case 'r':
528-
out = '\r';
529-
break;
492+
return '\r';
530493
case 'n':
531-
out = '\n';
532-
break;
494+
return '\n';
533495
case 't':
534-
out = '\t';
535-
break;
496+
return '\t';
536497
case 'b':
537-
out = '\b';
538-
break;
498+
return '\b';
539499
case 'f':
540-
out = '\f';
541-
break;
500+
return '\f';
542501
default:
543-
out = c;
502+
return c;
544503
}
545-
return out;
546504
}
547505

548506
/**
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.csv;
19+
20+
import java.io.IOException;
21+
import java.io.PushbackReader;
22+
import java.io.Reader;
23+
24+
/**
25+
* Reader transforming unicode escape sequences (i.e \u0065) in the provided
26+
* stream into the corresponding unicode character.
27+
*
28+
* @author Emmanuel Bourg
29+
* @version $Revision$, $Date$
30+
*/
31+
class UnicodeUnescapeReader extends Reader {
32+
private PushbackReader reader;
33+
34+
/** The buffer used to read unicode escape sequences. */
35+
private final char[] sequence = new char[5];
36+
37+
UnicodeUnescapeReader(Reader reader) {
38+
this.reader = new PushbackReader(reader, sequence.length);
39+
}
40+
41+
public int read(char[] cbuf, int off, int len) throws IOException {
42+
int count = 0;
43+
for (int i = 0; i < len; i++) {
44+
int c = reader.read();
45+
46+
if (c == -1) {
47+
return count == 0 ? -1 : count;
48+
}
49+
50+
if (c == '\\') {
51+
int l = reader.read(sequence);
52+
if (l == sequence.length
53+
&& 'u' == sequence[0]
54+
&& isHexadecimal(sequence[1])
55+
&& isHexadecimal(sequence[2])
56+
&& isHexadecimal(sequence[3])
57+
&& isHexadecimal(sequence[4])) {
58+
// unicode escape found
59+
c = Integer.parseInt(new String(sequence, 1, 4), 16);
60+
61+
} else if (l > 0) {
62+
// put the characters back in the stream
63+
reader.unread(sequence, 0, l);
64+
}
65+
}
66+
67+
cbuf[off + i] = (char) c;
68+
count++;
69+
}
70+
71+
return count;
72+
}
73+
74+
private boolean isHexadecimal(char c) {
75+
return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F');
76+
}
77+
78+
public void close() throws IOException {
79+
if (reader != null) {
80+
reader.close();
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)