-
Notifications
You must be signed in to change notification settings - Fork 162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Escape characters in CSV #397
Conversation
@AndyScherzinger can you review? |
@emartynov sure, I'll give the branch a test drive :) |
osw.append(i == values.length - 1 ? '\n' : ','); | ||
} | ||
} | ||
|
||
private String escapeCSV(String value) { | ||
if (containsSymbolsToEscape(value)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SYMBOLS_TO_ESCAPE
actually contains more symbols that get checked (and lead to a return true
) than the following line will actually escape.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you elaborate more? I don't get the comment, sorry.
Did I make some logical error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll try :) containsSymbolsToEscape checks for a certain set of characters while not all of them get escaped then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you be more specific? What characters are not escaped?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll try :)
if (containsSymbolsToEscape(value)) {
return "\"" + value.replace("\"", "\"\"") + "\"";
}
escaping is done for "
while the condition containsSymbolsToEscape(value)
needs to be met of course. Thus I expected the condition to check for the same pattern. But it actually checks for:
private boolean containsSymbolsToEscape(String value) {
for (char c : SYMBOLS_TO_ESCAPE) {
if (value.indexOf(c) > -1) {
return true;
}
}
return false;
}
while SYMBOLS_TO_ESCAPE
contains:
private static final char[] SYMBOLS_TO_ESCAPE = new char[]{',', '"', '\n', '\r'};
So I would have expected the actual ascaping to also then escape the other "symbols to escape".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
other than that also when \n
has been found the check return true and you are escaping for "
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If ,
or \n
or \r
is detected then escaping is wrapping the whole string in the single quotes "
. However, if the string contains single quote it should be escaped as double quote ""
. I hope it helps.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah 👍
I didn't think of that, now makes send. Thanks for the explanation and your patience 😃
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No issues, I also didn't know it by heart and should, of course, mention it in PR description.
Looks good to me code wise, just a minor comment. Will test the export later today after work. |
We don't have import, only export :( I was quickly checking https://tools.ietf.org/html/rfc4180. It looks like MS Excel is wrong with importing CSV. I will dive more :) |
I also send it to my gmail adress and opened it as preview within gmail and there it works fine... so yeah, maybe we just have to ignore Excel... |
Conclusion on this PR? |
@emartynov fine by me and as discussed excel has its cvs issues but that is excel not glucose :) |
Hi @emartynov I tried to explain it again, sorry for not getting it clearly explained :( Hope this time I got it right :) |
All fine by me. Any feedback @paolorotolo? |
I'll try it ASAP |
It is fix for #383.
Changes:
No changes here:
Please review and comment.