@@ -5,6 +5,8 @@ public class EscapeProcessor {
5
5
private static final char TAB_CHAR = '\t' ;
6
6
private static final char BREAK_LINE_CHAR = '\n' ;
7
7
private static final char SPACE_CHAR = ' ' ;
8
+ private static final char DOUBLE_QUOTE_CHAR = '\"' ;
9
+ private static final char ESCAPE_CHAR = '\\' ;
8
10
9
11
private final String inputString ;
10
12
private boolean inQuotes = false ;
@@ -20,7 +22,7 @@ public String process() {
20
22
inQuotes = false ;
21
23
for (int i = 0 ; i < inputString .length (); i ++) {
22
24
char currentChar = inputString .charAt (i );
23
- if (currentChar != '\"' ) {
25
+ if (currentChar != DOUBLE_QUOTE_CHAR ) {
24
26
handleNonQuoteCharacter (currentChar );
25
27
}
26
28
else {
@@ -45,15 +47,15 @@ private void handleQuoteCharacter(char currentChar, int i) {
45
47
handleQuoteNextToQuoteCase (currentChar , i );
46
48
return ;
47
49
}
48
- escapedJson .append ('\\' );
50
+ escapedJson .append (ESCAPE_CHAR );
49
51
escapedJson .append (currentChar );
50
52
}
51
53
52
54
private void handleQuoteNextToQuoteCase (char currentChar , int i ) {
53
- int nextQuotePosition = getNextValidCharPosition (i + 1 );
55
+ int nextQuotePosition = getNextNoneSpaceCharPosition (i + 1 );
54
56
// If next valid quote is a good close quote, then the current quote MUST be an escaped quote
55
57
if (isValidCloseQuote (nextQuotePosition )) {
56
- escapedJson .append ('\\' );
58
+ escapedJson .append (ESCAPE_CHAR );
57
59
escapedJson .append (currentChar );
58
60
}
59
61
else {
@@ -67,10 +69,10 @@ private void handleQuoteNextToQuoteCase(char currentChar, int i) {
67
69
}
68
70
69
71
private boolean hasNextQuoteRightAfterCurrentQuoteWithoutComma (int position ) {
70
- return findNextValidChar (position + 1 ) == '\"' ;
72
+ return findNextValidChar (position + 1 ) == DOUBLE_QUOTE_CHAR ;
71
73
}
72
74
73
- private int getNextValidCharPosition (int position ) {
75
+ private int getNextNoneSpaceCharPosition (int position ) {
74
76
for (int i = position ; i < inputString .length (); i ++) {
75
77
char currentChar = inputString .charAt (i );
76
78
if (currentChar != SPACE_CHAR && currentChar != BREAK_LINE_CHAR && currentChar != TAB_CHAR ) {
0 commit comments