-
Notifications
You must be signed in to change notification settings - Fork 251
/
CSVParser.java
136 lines (119 loc) · 4.69 KB
/
CSVParser.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package csv_parser;
import java.util.*;
import org.junit.*;
import static org.junit.Assert.*;
public class CSVParser {
/*
CSV Parser
AirBnB Interview Question
http://creativyst.com/Doc/Articles/CSV/CSV01.htm#EmbedBRs
*/
public class Solution {
public String parseCSV(String str) {
List<String> res = new ArrayList<>();
boolean inQuote = false;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
if (inQuote) {
if (str.charAt(i) == '\"') {
if (i < str.length() - 1 && str.charAt(i + 1) == '\"') {
sb.append("\"");
i++;
} else {
inQuote = false;
}
} else {
sb.append(str.charAt(i));
}
} else {
if (str.charAt(i) == '\"') {
inQuote = true;
} else if (str.charAt(i) == ',') {
res.add(sb.toString());
sb.setLength(0);
} else {
sb.append(str.charAt(i));
}
}
}
if (sb.length() > 0) {
res.add(sb.toString());
}
return String.join("|", res);
}
}
/*
CSV Parser
AirBnB Interview Question
http://creativyst.com/Doc/Articles/CSV/CSV01.htm#EmbedBRs
*/
public class Solution_2 {
public String parseCSV(String str) {
if (str == null || str.isEmpty()) return null;
List<String> res = new ArrayList<>();
StringBuilder curr = new StringBuilder();
boolean inQuote = false;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (inQuote) {
if (c == '\"') {
if (i < str.length() - 1 && str.charAt(i + 1) == '\"') {
curr.append("\"");
i++;
} else {
inQuote = false;
}
} else {
curr.append(c);
}
} else {
if (c == '\"') {
inQuote = true;
} else if (c == ',') {
res.add(curr.toString());
curr.setLength(0);
} else {
curr.append(c);
}
}
}
if (curr.length() > 0)
res.add(curr.toString());
return String.join("|", res);
}
}
public static class UnitTest {
@Test
public void test1() {
Solution sol = new CSVParser().new Solution();
String test = "John,Smith,john.smith@gmail.com,Los Angeles,1";
String expected = "John|Smith|john.smith@gmail.com|Los Angeles|1";
assertEquals(expected, sol.parseCSV(test));
test = "Jane,Roberts,janer@msn.com,\"San Francisco, CA\",0";
expected = "Jane|Roberts|janer@msn.com|San Francisco, CA|0";
assertEquals(expected, sol.parseCSV(test));
test = "\"Alexandra \"\"Alex\"\"\",Menendez,alex.menendez@gmail.com,Miami,1";
expected = "Alexandra \"Alex\"|Menendez|alex.menendez@gmail.com|Miami|1";
assertEquals(expected, sol.parseCSV(test));
test = "\"\"\"Alexandra Alex\"\"\"";
expected = "\"Alexandra Alex\"";
assertEquals(expected, sol.parseCSV(test));
}
@Test
public void test2() {
Solution_2 sol = new CSVParser().new Solution_2();
String test = "John,Smith,john.smith@gmail.com,Los Angeles,1";
String expected = "John|Smith|john.smith@gmail.com|Los Angeles|1";
assertEquals(expected, sol.parseCSV(test));
test = "Jane,Roberts,janer@msn.com,\"San Francisco, CA\",0";
expected = "Jane|Roberts|janer@msn.com|San Francisco, CA|0";
assertEquals(expected, sol.parseCSV(test));
test = "\"Alexandra \"\"Alex\"\"\",Menendez,alex.menendez@gmail.com,Miami,1";
expected = "Alexandra \"Alex\"|Menendez|alex.menendez@gmail.com|Miami|1";
assertEquals(expected, sol.parseCSV(test));
test = "\"\"\"Alexandra Alex\"\"\"";
expected = "\"Alexandra Alex\"";
assertEquals(expected, sol.parseCSV(test));
}
}
}