Commit 81492bf1 authored by Andrey Vetlugin's avatar Andrey Vetlugin

Strip first and last quote in each token

parent 045cce7f
......@@ -19,21 +19,38 @@ assert unescape('asdf') == 'asdf'
List<String> splitLine(String line) {
List<String> tokens = []
String currentToken = ''
boolean isFirstCharacterInToken = true
List<String> tokenQuotes = []
line.each {
if (it == ',') {
// end of token
tokens << currentToken
currentToken = ''
tokenQuotes = []
isFirstCharacterInToken = true
} else if (it == '"') {
if (!isFirstCharacterInToken) {
tokenQuotes << it
isFirstCharacterInToken = false
}
} else {
// simple character
currentToken += tokenQuotes.join('')
tokenQuotes = []
currentToken += it
isFirstCharacterInToken = false
}
}
// add last token, because it didn't have a trailing comma
tokens << currentToken
return tokens
}
assert splitLine('a,b,c') == ['a', 'b', 'c']
assert splitLine('"a","b","c"') == ['a', 'b', 'c']
assert splitLine('"as""df","qwer""') == ['as"df', 'qwer"']
assert splitLine('"as""df"') == ['as"df']
assert splitLine('"as""df","qwer"""') == ['as"df', 'qwer"']
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