-
Notifications
You must be signed in to change notification settings - Fork 101
Fix escaping bugs in UseTextBlocks #215
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 escaping bugs in UseTextBlocks #215
Conversation
update comment to indicate the part of code responsible for the handling
// escape backslashes | ||
content = content.replace("\\", "\\\\"); | ||
// escape triple quotes | ||
content = content.replace("\"\"\"", "\"\"\\\""); |
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.
do you know why the triple quotes are transformed to \"\"\\\"
but not \\\"\"\"
, the difference is the 1st or 3rd quote is escaped.
I found for an 8 quotes case:
String s1 = "\"\"\"\"\"\"\"\"";
String s2 = """
""\"""\"""\
""";
String s3 = """
\"""\"""\""\
""";
s1 and s2 are equivalent, however,
String s3 = """
\"""\"""""\
""";
doesn't compile unless change it to
String s3 = """
\"""\"""\""\
""";
which makes me think to escape the 3rd quote instead of 1st is like magic.
Do you have any insight of this?
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.
@kunli2 I went for escaping the third quote instead of the first to optimize for the number of escapes needed in case of n number of continuous quotes (For example: 2 escapes instead of 3 in case of 8 quotes). Another thing I noticed was that IDEA by default escapes the third quote as well, for instance if you copy-paste a series of quotes inside a text block.
In your examples, I can't see any difference between s2 & s3 (both), did GitHub formatting probably make them look the same? Could you share it in some other form such as a gist so that I can try to understand it better, maybe then I could answer it better?.
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.
hi @aksh1618 , yes, I also found IDE by default escapes the 3rd quote.
what I noticed is below, the first statement in the code doesn't compile (\"""\"""""
doesn't compile but \"""\"""\""\
does). I guess that is the reason why IDEA escapes the 3rd quote instead of 1st.
String s3 = """
\"""\"""""\
""";
String s3 = """
\"""\"""\""\
""";
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.
sorry for the confusion in my 1st comment, I have revised the code in the comment.
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.
@kunli2 thanks for clarifying, it's a very good example! So to summarize, in this case if we're replacing programmatically successively, escaping the first one would not work as it leads to code that doesn't compile due to 4 quotes left together. So now it makes sense why they went with the third one as well instead of having to write a more complex replacement algorithm for handling leftover quotes 👍
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.
@aksh1618 yeah, it makes sense! thanks for fixing and contribution again!
I recently happened upon this amazing project, kudos to all involved, I absolutely love the idea and implementation! I decided to use it for one of my projects, and it worked very well except for some minor cases, for which I decided to submit fixes:
Thanks again for making this wonderful project, let me know if any further changes required here!