-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
Call escape_raw_string to make parsing more predictable #34
Conversation
Thanks! I wonder, why isn't this fixed by just adding julia> styled"\\"
"\\"
julia> styled"\\\\"
"\\\\"
julia> styled"\\\\\\"
"\\\\\\"
julia> styled"\\\\\\\\"
"\\\\\\\\"
julia> styled"\a\b\e\u200b"
"\a\b\e\u200b" |
That applies it always, rather than just at the end of a string |
Ah, so the bug you're mentioning in the original commit only affects a terminal Also, perhaps I'm missing the obvious - would you mind providing an example of how adding These are the changes I'm considering: diff --git a/src/stylemacro.jl b/src/stylemacro.jl
index b030569..9abccb4 100644
--- a/src/stylemacro.jl
+++ b/src/stylemacro.jl
@@ -107,7 +107,7 @@ macro styled_str(raw_content::String)
end =#
# Instead we'll just use a `NamedTuple`
- state = let content = unescape_string(raw_content, ('{', '}', '$', '\n', '\r'))
+ state = let content = unescape_string(raw_content, ('\\', '{', '}', '$', '\n', '\r'))
(; content, bytes = Vector{UInt8}(content),
s = Iterators.Stateful(pairs(content)),
parts = Any[],
@@ -635,10 +635,10 @@ macro styled_str(raw_content::String)
function run_state_machine!(state)
# Run the state machine
for (i, char) in state.s
- if char == '\\'
- state.escape[] = true
- elseif state.escape[]
+ if state.escape[]
escaped!(state, i, char)
+ elseif char == '\\'
+ state.escape[] = true
elseif char == '$'
interpolated!(state, i, char)
elseif char == '{' |
These should return the same styled string
|
You can attempt to combine escape_string and unescape_raw_string, but it can make the implementation much more confusing and likely to have bugs since they cannot be tested separately. It doesn't seem like that change you are considering implements any part of |
Right. Thanks for clarifying. One more question I'd like to ask is regarding this code comment in the PR
I'm not too concerned about the p.s. if there are any relevant tests you think it would be good to have, more tests would be nice, though not required. |
IIRC, |
Bump? |
Ah yes. Mind if I force push to your branch with some minor changes (moving a comment, adding some a test or two)? |
Go for it |
Due to the initial parsing of the string before the macro sees it, followed by the use of `unescape_string`, you currently need to double up trailing backslashes. We can fix this by calling escape_raw_string in advance. This change may cause undesirable side-effects with less comply used macro forms (e.g. `@styled_str "\\"`), but we'll deal with that if and when it comes up.
9ca429e
to
0625bce
Compare
Done. The tests I just added failed before the change, but now pass. I also expanded on the commit message a bit. |
The advantage here is that you don't escape it twice, so that
styled"\\"
doesn't need double the number of\\
each time to get one more\
at the end (the downside is that less common forms such as@styled_str "\\"
still apply theunescape_string
call twice and we cannot catch that)