Skip to content
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

Fix handling of newlines when converting quotes #554

merged 3 commits into from Oct 11, 2019


Copy link

@krlmlr krlmlr commented Oct 11, 2019

I remember using deparse(parse()) to avoid edge cases like '\\\''. The non-obvious gsub() really seems like the simplest solution to ensure we always match one character after each backslash.

Closes #551.

Copy link

Awesome, thanks. This is much more elegant and even more general than #553. I am always afraid of gsub() / grep() with escape characters 😨. Maybe now is the time to overcome that? Then maybe you can tell me why do all three paterns match?

grepl("\n", "\n")
#> [1] TRUE
grepl("\\n", "\n")
#> [1] TRUE
grepl("\\\n", "\n")
#> [1] TRUE

Created on 2019-10-11 by the reprex package (v0.3.0)

@lorenzwalthert lorenzwalthert merged commit 236c957 into master Oct 11, 2019
@lorenzwalthert lorenzwalthert deleted the b-551-quote-newlines branch October 11, 2019 15:49
Copy link

Thank you!!!

Copy link
Member Author

krlmlr commented Oct 13, 2019

"\n" matches because it's a literal newline character.

"\\n" matches because backslashes are treated specially in regex -- and backslash + n happens to match a newline.

"\\\n" -- 🤕 it seems that backslash + \n also matches a newline. It's true for both perl = TRUE and perl = FALSE, so seems reliable. Also this means that we can seek for answers in a broader context.

Luckily, it breaks with the fourth backslash.

grepl("\\\\n", "\n")
#> [1] FALSE

Created on 2019-10-13 by the reprex package (v0.3.0)

Copy link

Thanks Kirill.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

Successfully merging this pull request may close these issues.

Styler does not preserve new line characters
3 participants