Skip to content

Commit

Permalink
opt
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudroger committed Jun 20, 2016
1 parent 5d1c3f2 commit 1c143e5
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions sfm/src/main/java/org/sfm/csv/parser/StandardCsvCharConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public final class StandardCsvCharConsumer extends CsvCharConsumer {
private static final int TURN_OFF_IN_CR_MASK = ~IN_CR;
private static final int ALL_QUOTES = QUOTE | IN_QUOTE;

private static final char QUOTE_CHAR = '"';

private final CharBuffer csvBuffer;
private int _currentIndex;
private int _currentState = NONE;
Expand Down Expand Up @@ -48,7 +50,7 @@ private int consumeOneChar(CellConsumer cellConsumer, int currentIndex, int curr
return handleEndOfLineLF(currentIndex, currentState, cellConsumer);
case '\r':
return handleEndOfLineCR(currentIndex, currentState, cellConsumer);
case '"':
case QUOTE_CHAR:
return quote(currentState);
}
return currentState & TURN_OFF_IN_CR_MASK;
Expand Down Expand Up @@ -84,7 +86,7 @@ public boolean consumeToNextRow(CellConsumer cellConsumer) {
}
currentState &= TURN_OFF_NOTHING;
break;
case '"':
case QUOTE_CHAR:
currentState = quote(currentState);
break;
default:
Expand Down Expand Up @@ -128,17 +130,13 @@ private int endOfRow(int currentIndex, CellConsumer cellConsumer) {
}

private int quote(int currentState) {
if ((currentState & ALL_QUOTES) == 0) {
return (currentState ^ IN_QUOTE) & TURN_OFF_IN_CR_MASK;
} else {
return (currentState ^ ALL_QUOTES) & TURN_OFF_IN_CR_MASK;
}
return (currentState ^ ((currentState & ALL_QUOTES) == 0 ? IN_QUOTE : ALL_QUOTES)) & TURN_OFF_IN_CR_MASK;
}

private int newCell(int end, final CellConsumer cellConsumer) {
char[] charBuffer = csvBuffer.getCharBuffer();
int start = csvBuffer.getMark();
if (charBuffer[start] != '"') {
if (charBuffer[start] != QUOTE_CHAR) {
cellConsumer.newCell(charBuffer, start, end - start);
} else {
newQuotedCell(charBuffer, start, end, cellConsumer);
Expand All @@ -158,15 +156,15 @@ private void newQuotedCell(final char[] chars, final int offset, final int end,
int i = start;
for (; i < end - 1 ; i++) {
int correctedIndex = i - skipIndex;
escaped = '"' == chars[correctedIndex] && !escaped;
escaped = QUOTE_CHAR == chars[correctedIndex] && !escaped;
if (escaped) {
skipIndex ++;
System.arraycopy(chars, correctedIndex + 1, chars, correctedIndex, end - 1 - i);
}
}

// if last is not quote add to shifted char
if ('"' == chars[i] && !escaped) {
if (QUOTE_CHAR == chars[i] && !escaped) {
skipIndex ++;
}

Expand Down

0 comments on commit 1c143e5

Please sign in to comment.