Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
Andrey Vetlugin
groovy-csv-exercise
Commits
81492bf1
Commit
81492bf1
authored
Jan 24, 2018
by
Andrey Vetlugin
Browse files
Strip first and last quote in each token
parent
045cce7f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
1 deletion
+18
-1
reader.groovy
reader.groovy
+18
-1
No files found.
reader.groovy
View file @
81492bf1
...
...
@@ -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
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment