-
Notifications
You must be signed in to change notification settings - Fork 76
/
UnescapeCellPreProcessor.java
55 lines (42 loc) · 1.51 KB
/
UnescapeCellPreProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package org.simpleflatmapper.csv.parser;
public class UnescapeCellPreProcessor extends CellPreProcessor {
private final TextFormat textFormat;
public UnescapeCellPreProcessor(TextFormat textFormat) {
this.textFormat = textFormat;
}
public final void newCell(char[] chars, int start, int end, CellConsumer cellConsumer) {
int strStart = start;
int strEnd = end;
if (strStart < strEnd && chars[strStart] == textFormat.escapeChar) {
strStart ++;
strEnd = unescape(chars, strStart, strEnd);
}
cellConsumer.newCell(chars, strStart, strEnd - strStart);
}
private int unescape(final char[] chars, final int start, final int end) {
for(int i = start; i < end - 1; i ++) {
if (chars[i] == textFormat.escapeChar) {
return removeEscapeChars(chars, end, i);
}
}
if (start < end && chars[end - 1] == textFormat.escapeChar) {
return end - 1;
}
return end;
}
private int removeEscapeChars(final char[] chars, final int end, final int firstEscapeChar) {
int j = firstEscapeChar;
boolean escaped = true;
for(int i = firstEscapeChar + 1;i < end; i++) {
escaped = chars[i] == textFormat.escapeChar && ! escaped;
if (!escaped) {
chars[j++] = chars[i];
}
}
return j;
}
@Override
public final boolean ignoreLeadingSpace() {
return false;
}
}