You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Can't set universal variables with some special characters. #1225
I don't know what the original rationale was for the choice to make \x be ASCII and \X be Unicode. I think my personal preference is to make \x and \X identical, and presumably Unicode, so I think the fix is in the right direciton. However we should make that a conscious decision, and update the docs accordingly.
We should also understand where the errant \x is coming from. Is that from fishd?
I believe it's being generated in escape_string_internal method in common.cpp by fish process (and not fishd). fish sends it to fishd and then fishd fails to unescape it. \x seems to be correct since python too uses \x character to escape ä
Looks like it's actually being generated in full_escape in env_universal_common.cpp:
if (c < 256)
{
append_format(out, L"\\x%.2x", c);
}
This used to be 128, but I changed it in 51de269 to be 256.
I finally understand why it needs to be 128, and how these escapes are intended to work. \x and \X are both intended to produce a single byte of output. You use them to indicate that, when the given string gets converted to the ultimate narrow encoding, you want this particular byte in the output.
The difference is that \x escapes a character, and \X escapes a byte. \x is limited to ASCII because ASCII chars are the only ones that we are sure can be represented with a single byte in the narrow encoding, which we assume is a superset of ASCII. For example, if the narrow encoding is UTF-8, only ASCII characters use a single byte.
So how is ä represented? Is it the byte 0xE4? No! While that is its Unicode code point, it is not its representation in any encoding. Its encoding in UTF-16 is 00E4 and in UTF-8 is C3A4. It does not have a one-byte representation in any Unicode encoding, and therefore it cannot be represented using a single \x or \X escape, which only produce a single byte of output. (That's why there's the \U escape).
Because it has a two-byte representation, and I know my terminal encoding is UTF-8, I can represent it with two byte-escapes:
> echo \XC3\XA4
ä
This obviously is encoding-dependent, which is why the docs have the dire warning about "Only use this if you know what you are doing."
What I cannot do is represent it with one:
> echo \XE4
?
I just generated an invalid UTF-8 stream: the "invalid string" that the docs warned me about.
So I am now certain that the full_escape function's use of < 256 is quite broken. It needs to be < 128, because those are the characters whose code points match their byte values in the narrow encoding. Using <256 produces gibberish by reinterpreting code points as UTF-8 sequences.
I'm still unsure as to whether we want to extend \x to support arbitrary byte values. We have to decide whether we want those byte values to be specified as Unicode code points as \u does, or literal bytes as \X does. I'm going to look at how other shells and languages do that.
Here the u prefix means that the \xe4 is a code point (or UTF-16, same thing in this case).
The Python 3 docs say "In a bytes literal, hexadecimal and octal escapes denote the byte with the given value. In a string literal, these escapes denote a Unicode character with the given value." So in Python, \x specifies a Unicode code point.
In zsh, \x escapes a literal byte value, and \u can be used for Unicode. \X is ignored.
I am going to revert the wrong change to the full_escape function, which should fix this bug. I spun off #1352 to discuss whether/how to extend \x to non-ASCII byte values.
Activity
siteshwar commentedon Mar 16, 2014
Unicode representation of
whätiswh\xe4t(\xe4 = 228) which is not being decoded correctly in read_unquoted_escape method in common.cpp.Here is the relevant code snippet :
Value of max_val (ASCII_MAX = 127) is too strict for this case and it will cause following test to fail :
I changed case condition to :
and
set -U blah whät; echo $blahworks perfectly.If I have not missed anything here feel free to merge #1344
ridiculousfish commentedon Mar 16, 2014
I'm not sure about this. The fish docs are clear that \x escapes specifically ASCII strings, and \X escapes byte data.
It looks to me like the bug is that we are generating the string 'wh\xe4t' to represent that Unicode value. It should use \X, not \x.
siteshwar commentedon Mar 16, 2014
In python
unicode("whät", "utf-8")gives'wh\xe4t'.ridiculousfish commentedon Mar 16, 2014
I don't know what the original rationale was for the choice to make \x be ASCII and \X be Unicode. I think my personal preference is to make \x and \X identical, and presumably Unicode, so I think the fix is in the right direciton. However we should make that a conscious decision, and update the docs accordingly.
We should also understand where the errant \x is coming from. Is that from fishd?
siteshwar commentedon Mar 16, 2014
Yes, the problem was setting the variable in fishd. I have updated doc for \x through siteshwar@c8b6f58
siteshwar commentedon Mar 19, 2014
@ridiculousfish Should I merge #1344 ?
zanchey commentedon Mar 20, 2014
Perhaps we should add some tests as well.
siteshwar commentedon Mar 21, 2014
@zanchey Updated my pull request.
ridiculousfish commentedon Mar 22, 2014
Where's fishd generating the \x? Should it be \X instead?
siteshwar commentedon Mar 22, 2014
I believe it's being generated in escape_string_internal method in common.cpp by fish process (and not fishd). fish sends it to fishd and then fishd fails to unescape it.
\xseems to be correct since python too uses\xcharacter to escapeäridiculousfish commentedon Mar 22, 2014
Looks like it's actually being generated in
full_escapein env_universal_common.cpp:This used to be 128, but I changed it in 51de269 to be 256.
I finally understand why it needs to be 128, and how these escapes are intended to work. \x and \X are both intended to produce a single byte of output. You use them to indicate that, when the given string gets converted to the ultimate narrow encoding, you want this particular byte in the output.
The difference is that \x escapes a character, and \X escapes a byte. \x is limited to ASCII because ASCII chars are the only ones that we are sure can be represented with a single byte in the narrow encoding, which we assume is a superset of ASCII. For example, if the narrow encoding is UTF-8, only ASCII characters use a single byte.
So how is
ärepresented? Is it the byte 0xE4? No! While that is its Unicode code point, it is not its representation in any encoding. Its encoding in UTF-16 is00E4and in UTF-8 isC3A4. It does not have a one-byte representation in any Unicode encoding, and therefore it cannot be represented using a single \x or \X escape, which only produce a single byte of output. (That's why there's the \U escape).Because it has a two-byte representation, and I know my terminal encoding is UTF-8, I can represent it with two byte-escapes:
This obviously is encoding-dependent, which is why the docs have the dire warning about "Only use this if you know what you are doing."
What I cannot do is represent it with one:
I just generated an invalid UTF-8 stream: the "invalid string" that the docs warned me about.
So I am now certain that the
full_escapefunction's use of< 256is quite broken. It needs to be < 128, because those are the characters whose code points match their byte values in the narrow encoding. Using <256 produces gibberish by reinterpreting code points as UTF-8 sequences.I'm still unsure as to whether we want to extend \x to support arbitrary byte values. We have to decide whether we want those byte values to be specified as Unicode code points as \u does, or literal bytes as \X does. I'm going to look at how other shells and languages do that.
ridiculousfish commentedon Mar 22, 2014
Also, I should express my deep respect to axel for getting this right in the first place, and my shame at screwing it up!
ridiculousfish commentedon Mar 22, 2014
The Python code that siteshwar shows is funny:
Here the
uprefix means that the \xe4 is a code point (or UTF-16, same thing in this case).The Python 3 docs say "In a bytes literal, hexadecimal and octal escapes denote the byte with the given value. In a string literal, these escapes denote a Unicode character with the given value." So in Python, \x specifies a Unicode code point.
In zsh, \x escapes a literal byte value, and \u can be used for Unicode. \X is ignored.
bash doesn't seem to have any way to enter hex characters, except for using
echo -e.ridiculousfish commentedon Mar 22, 2014
I am going to revert the wrong change to the full_escape function, which should fix this bug. I spun off #1352 to discuss whether/how to extend \x to non-ASCII byte values.
siteshwar commentedon Mar 23, 2014
Thanks for explaining, I have closed my pull request.
Changes full_escape to not generate \x escapes for non-ASCII characters.