raw string tail escape revisited

Bengt Richter bokr at oz.net
Sat Aug 9 13:44:45 EDT 2003


On 9 Aug 2003 15:33:39 GMT, bokr at oz.net (Bengt Richter) wrote:

>Why wouldn't quote-stuffing solve the problem, and let you treat \ as
>an ordinary character? In a raw string, it's no good for preventing
>end-of-quoting anyway, unless you want the literal \ in front of the quote
>you are escaping.
>
>Quote-stuffing is a variation on the old quote-doubling, extended to
>deal with triple quotes as well (which makes it a little like HDLC bit stuffing).
>
>IOW, treat \ as an ordinary character, and then if you don't want the
>string to end, just stuff one quote character of the starting kind after
>the otherwise terminating sequence. You could do this with single quoting
>or triple quoting, where of course you'd need it less for triple quotes.
>E.g., using uppercase R as a prefix for this kind of raw string syntax,
>
>    R'\'   # just fine
>    R'C:\' # one of the motivations 
>    R''''  # dumb way to do "'"
Really dumb ;-/ That makes an un-terminated triple quoted string
starting with one quote. D'oh. The logic doesn't start until the beginning
delimiter - single or triple - has been passed and established. So if you
perversely wanted to use only single quotes to quote one single quote,
you couldn't. Is there one you couldn't do at all? I don't think so, since
you could always do single-quote doubling and choose the opposite quote of a leading
quote in the data. E.g., R'"""''''''' Would be a painful R'"""'+R"'''"
Actually, that could be triple quoted as R"""""""'''""", but putting an ending '"'
in that data would make a problem. Nope, R'''"""''''"''' would handle that.
But what if we add another "'"? Then the data would be ["""'''"'] Still ok,
looks like we can always start with a triple quote opposite to the end of the data:
R"""""""'''"'""" would do it. Is there an impossible case I'm missing that would have
to be split into two adjacent (thus concatenated) string representations?

Is there a reasonable use case that is messed up as the price of getting R'\' ?
 
Otherwise I guess it should be ok. Woke up too early and not enough ;-)


>    R""" <just about anything> ->[""""]<-makes 3 quotes, and we end with \"""
>    R""" ->[""""""""]<-two stuffing-extended triple quotes make 6 quotes."""
>
>The tokenizer would recognize a stuffed quote mark and just discard it if present,
>otherwise recognize end of string.
>
>Just had this idea. Do I need more coffee? What did I forget?
>
>Regards,
>Bengt Richter

Regards,
Bengt Richter




More information about the Python-list mailing list