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

Add raw strings literals to GDScript #5362

Closed
dalexeev opened this issue Sep 7, 2022 · 2 comments · Fixed by godotengine/godot#74995
Closed

Add raw strings literals to GDScript #5362

dalexeev opened this issue Sep 7, 2022 · 2 comments · Fixed by godotengine/godot#74995
Milestone

Comments

@dalexeev
Copy link
Member

dalexeev commented Sep 7, 2022

Describe the project you are working on

Various projects.

Describe the problem or limitation you are having in your project

When working with regular expressions containing backslashes, you need to escape each backslash, and the readability of the regular expression is noticeably reduced.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Add raw strings literals, where only the opening quote character must be escaped. You can escape the backslash, but you don't have to if it's followed by a character that doesn't match the opening quote. EDIT: Backslashes and quotes cannot be really escaped in Python r-strings, since they stay in the result. Also, escape sequences like \n and \t do not work in r-strings (they remain unchanged). This is how r-strings work in Python, and there is a proposal for f-strings.

On the other hand, if you want \\, then even in r-strings you must use \\\\ (r-strings only get rid of doubling a single \, like \d). EDIT: I was wrong, see above. Perhaps some other option would be better, such as doubling the quote to escape (as in SQL and Pascal). Or regular expression literals like in JavaScript. EDIT: Given that string literals are already Python-like in GDScript (triple quotes for multiline strings), r- and f-strings probably should be the same as in Python.


LAST EDIT: Here are the "escaping" rules in r-strings:

\" -> \" (or \' -> \' if in r'...')
\\ -> \\ (this allows you \\\")
\ -> \

Unlike regular strings, r-strings converts special sequences to themselves.


Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

'\\d+(?:\\.\\d+)?|\\+|-|\\*\\*?|/|\\(|\\)|(.)'
r'\d+(?:\.\d+)?|\+|-|\*\*?|/|\(|\)|(.)'

Python r-strings:
print(r'" \' \ \\') # " \' \ \\
print(r"\" ' \ \\") # \" ' \ \\

You cannot represent a string containing an even number of backslashes in a row before a quote.

Also multiline string literals (r"""string""" and r'''string''').

If this enhancement will not be used often, can it be worked around with a few lines of script?

This is about GDScript syntax.

Is there a reason why this should be core and not an add-on in the asset library?

This cannot be done with an add-on.

@HenryLoenwind
Copy link

Keyword: regex

So this can be found with the fulltext search.

@dalexeev
Copy link
Member Author

See also godotengine/godot#74995 (comment).

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

Successfully merging a pull request may close this issue.

4 participants