3 ms·
Since you had to pick a contrived example, that suggests the problem isn't particularly common. If it was "real" enough, you wouldn't need a contrived example.
by eridius 11y ago
Since you had to pick a contrived example, that suggests the problem isn't particularly common. If it was "real" enough, you wouldn't need a contrived example.
> and, in the article, is directly responsible for a bug.
Nope! It was not responsible for anything whatsoever. Yeah, the backslashes got escaped, but they were already broken, so it didn't actually break anything. All it did was make "btest" match instead of "\x08test" match (both of which are equally buggy, though "btest" is more likely to be triggered in the wild).
> Are you really arguing that we could make C less confusing by switching printf("%d", x) to printf("\\d", x)?
No. I did not say that at all, and I don't appreciate straw man arguments.
> I'd say that C strings and C format directives use different escape characters specifically because they're likely to be used together
I doubt it. Neither of us was present (I'm assuming) when the printf format spec was decided upon, but I'd wager that % was chosen because this has a semantically different meaning than backslash-escapes. Backslash escapes are just that: escapes. They're either stand-ins for certain un-typeable characters, or they disable interpretation of the following character. But % in printf isn't used as a character escape, it's used as a token. It's a stand-in for a more complicated representation of a separate argument. And the fact that it behaves differently than \ does warrants it using a different character.
Whereas in other languages, \ typically has the exact same semantic meaning as \ does in C strings (the set of character escapes may vary slightly, although most languages tend to copy C's character escapes verbatim). And that's why they use \. It's the same reason most languages use "" to denote strings, or [] to denote subscripting. It's common punctuation that behaves the same across multiple languages, which makes it easy for the programmer to remember and work with.
- thaumasiotes 11y agoThere is also a non-contrived example in my original comment, String.split("\\s+"). And the advice that regexes in python should always be constructed from r"" strings is a direct response to this problem specifically. I picked a contrived example to make clear that sharing escape characters causes exponential growth as you add layers. >> Are you really arguing that we could make C less confusing by switching printf("%d", x) to printf("\\d", x)? > No. I did not say that at all, and I don't appreciate straw man arguments. This is an argument in a very standard form, reduction to absurdity. I see printf escapes and C string backslash-based escapes as semantically identical; they are both ways of giving the parser instructions directly rather than giving it a literal representation of what you want (this analysis also nicely explains why they're called "escapes"). But since there is a good amount of evidence that a lot of other programmers feel the same way you do, let me ask this: HTML escapes (with &) have exactly the semantics you attribute to \. Do you think HTML would be better if it used backslashes instead?