Commit 20dad003 authored by Andrey Vetlugin's avatar Andrey Vetlugin

Handle comma in the value

parent d9a2bd46
......@@ -18,16 +18,25 @@ assert unescape('asdf') == 'asdf'
List<String> splitLine(String line) {
List<String> tokens = []
// state of the current token
String currentToken = ''
boolean isFirstCharacterInToken = true
List<String> tokenQuotes = []
line.each {
if (it == ',') {
// end of token
tokens << currentToken
currentToken = ''
tokenQuotes = []
isFirstCharacterInToken = true
if (tokenQuotes) {
// end of token
tokens << currentToken
// reset for the next token
currentToken = ''
tokenQuotes = []
isFirstCharacterInToken = true
} else {
currentToken += it
isFirstCharacterInToken = false
}
} else if (it == '"') {
if (!isFirstCharacterInToken) {
if (tokenQuotes.size() > 0) {
......@@ -37,6 +46,8 @@ List<String> splitLine(String line) {
tokenQuotes << it
isFirstCharacterInToken = false
}
} else {
isFirstCharacterInToken = false
}
} else {
// simple character
......@@ -51,10 +62,11 @@ List<String> splitLine(String line) {
return tokens
}
assert splitLine('a,b,c') == ['a', 'b', 'c']
assert splitLine('"a","b","c"') == ['a', 'b', 'c']
assert splitLine('"as""df"') == ['as"df']
assert splitLine('"as""df","qwer"""') == ['as"df', 'qwer"']
assert splitLine('"as,df","qwer,"""') == ['as,df', 'qwer,"']
assert splitLine('""",zxcv"') == ['",zxcv']
assert splitLine('"as,df","qwer,""",""",zxcv"') == ['as,df', 'qwer,"', '",zxcv']
List<String> lines = new File('data.csv').readLines()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment