-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Move to using %
as an escape in format!
#9161
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
Conversation
I believe that this is almost undeniably better because now to escape '{' it looks like "%{" instead of "\\{". Furthermore, in order to insert a '\' you only have to type "\\" (as one would expect), instead of "\\\\". At the same time, this also attempts to improve some error messages about expecting a character and not finding it.
How is this handled in other languages/libraries? |
For escaping, .Net doubles the {, so a literal '{' becomes '{{'. I think the only people this would look weird to are those coming from something like mustache or the django template language. In Shell, Perl, Groovy, Scala and I'm sure others, ${} indicates variable substitution, and Ruby uses #{}, so someone coming from those languages seeing %{ might guess that was indicating a sub, not escaping, but doesn't seem like an insurmountable surprise. C, C++, ObjC, D, Go, Python, Haskell, Java, Clojure, PHP, and R are all printf or derived (though Haskell has a .Net-like template package and PHP supports interpolation with $), and JavaScript punts to a billion and a half different templating libraries, so don't have much guidance to offer given we've already put printf syntax behind us. Ed: for the record, I'm fine w/ %, or whatever, really. I just want something that works and is easy to explain. |
I still don't really understand why |
Sorry about that, I should have put more information in the commit message. Here's the situation we're in: Right now we're using
And that's not even the worst part. If you want a literal So now that we've ruled out
Basically, we support methods where python does not (to support internationalization). What this means is that So now we're back to square one. The syntaxes that @jfager was mentioning were mostly for interpolation, and this is rather for escaping. I do agree though that @jfager, @sanxiyn, are you guys ok with that explanation? I wish there were a better solution, but I'm drawing blanks :( |
I understand the issue. I only brought up ${} interpolation to make the point about visual similarity. I disagree that people won't ever see %{} - if you're writing out a literal '{', the odds that you're going to write a literal '}' (which wouldn't need escaping, right?) soon after are pretty high - but as I said before I agree that it's a pretty minor speed bump. Also as I said before, I'm fine w/ %. I was just trying to answer the question about other langs. |
With the syntax that I'm proposing, you would always need the |
@alexcrichton, can you please explain in more detail what is the problem with "{0, select, other{test }} }}" ? If you want that to be a literal string, you simply double each { and } : "{{0, select, other{{test }}}} }}}}", no? |
In the case of For example, modify it slightly to
Those two strings are different, yet have the same format string, and we don't know which you specified. Also, if we resort to typing |
@alexcrichton: do we have a definitive format! syntax spec somewhere? I must be missing something about what's allowed inside a format string. |
We do indeed at http://static.rust-lang.org/doc/std/fmt.html |
I see. I would have simply used a different bracket type for select cases,- just to avoid creating new special characters that must be escaped inside format strings. I suppose that percent itself will now need to be escaped as "%%"? Or is that only inside the argument specifier? i.e. "{0, select, other{test %} %% } }" -> "test } %", but " {1} %" -> "nn %" ? |
A lone |
I mean, you only need it as an escape between { and }. Could allow single % in the rest of the format string... though, admittedly, this could be confusing. |
It seems to be that a simpler solution would be to implement "raw" string literals, which don't process escapes. I'm not seeing any proposals for this at the moment, but I know the idea has come up on IRC before. The benefit of this approach is "raw" string literals have other uses too. |
I, too, would like to see a raw-form of string literal, but I also don't consider this one use case as enough impetus for adding an entirely new string literal format to the language. |
@alexcrichton Regular expressions and Windows file paths are two more places that could benefit from raw string literals. |
Good point, I'm also writing rustdoc_web in rust and there's some huge HTML string literal which would greatly benefit from another form of string literal. |
I've sent an email about raw string literals to the ML |
I'm putting this on the backburner until the raw string discussion is resolved. I would rather not do this, and raw strings would mean that this would not necessarily have to be done. I'll reopen if raw strings don't become a thing. |
…ring_to_pedantic, r=flip1995 Move format_push_string to restriction Fixes rust-lang#9077 (kinda) by moving the lint to the restriction group. As I noted in that issue, I think the suggested change is too much and as the OP of the issue points out, the ramifications of the change are not necessarily easily understood. As such I don't think the lint should be enabled by default. changelog: [`format_push_string`]: moved to restriction (see rust-lang#9077).
I believe that this is almost undeniably better because now to escape '{' it
looks like "%{" instead of "{". Furthermore, in order to insert a '' you only
have to type "" (as one would expect), instead of "\".
At the same time, this also attempts to improve some error messages about
expecting a character and not finding it.