Output of surrogates seems to repeat following character
See the following output:
% encoding system
utf-8
% puts A\uD800B
ABB
% puts A\uD800C
ACC
The parsing of the u sequence seems correct as is the encoder:
% binary encode hex [encoding convertto [encoding system] A\uDC00B]
41edb08042
which seems correct.
It is not clear what should happen on output of a surrogate but this repeating of following character hints at a bug.
/Ashok
07e42778fcFix [c5bcbf3494]: Output of surrogates seems to repeat following character check-in2023-07-08 20:25:28ef717df5a5Proposed fix for [c5bcbf3494]: Output of surrogates seems to repeat following character. Also use more ***_SURROGATE() macro's check-in2023-07-06 15:07:48
I cannot reproduce this. On MacOS:
$ tclsh8.7 % puts A\uD800B A???B % puts A\uD800C A???C % fconfigure stdout -strict 1 % puts A\uD800B error writing "stdout": illegal byte sequence A%
My guess is that this depends on the console. Tcl just outputs the (illegal) 3-byte UTF-8 sequence. In Tcl 9.0 it will (properly) throw an exception
It's certainly possible it's platform or console dependent. However consider the following code which hints at dependency on the encoding used by the channel driver.
% puts A\uD800B
ABB
% puts [regexp -inline .* A\uD800B]
error writing "stdout": illegal byte sequence
The regexp output should be same as its arg. Why the difference in result? Further investigating,
% string equal A\uD800B [regexp -inline .* A\uD800B]
0
% codepoints A\uD800B
U+00000041 U+0000D800 U+00000042
% codepoints [regexp -inline .* A\uD800B]
U+00000041 U+000000F0 U+00000042
So it seems there is clearly a issue there. Note 9.0 correctly shows the two strings as equal.
It's possible this is also a result of the Tcl_UnicharToUtf (or was it Tcl_UtfToUnichar?) bug you mentioned in the other ticket but something smells off.
Further, I'm surprised that the puts raises and error even though -nocomplainencoding is 1 (default for Tcl8)
% fconfigure stdout
-blocking 1 -buffering line -buffersize 4096 -encoding utf-16 -eofchar {} -nocomplainencoding 1 -strictencoding 0 -translation crlf -winsize {120 30}
% puts [regexp -inline .* A\uD800B]
error writing "stdout": illegal byte sequence
As an aside, I'm having a bit of hard time with test cases because when results are not as expected, the puts that prints the result raises an exception as above and the entire test run aborts on a failure to print to stdout. That's a separate issue though that I'm fixing in tcltest.
/Ashok
By the way Jan, I'll investigate this further if you don't get to it first and the Tcl_UnichartoUtf fix does not help here. I just need to finish my test suite for the encoding profiles first.
/Ashok
Finally got back to looking at this issue. It appears the surrogate handling in Tcl 8.7 is the cause (9.0 does not have the problem.)
The regexp correctly returns a Tcl_Obj containing a list with a single Tcl_Obj containing 0x00000041, 0x0000D800, 0x00000042.
When this list is printed, the Tcl_Obj.bytes is generated for it in ExtendStringRepWithUnicode in this loop.
The problem is Tcl_UnicharToUtf remembers the presence of the D800 by storing the utf sequence in buf[]. However, on the next iteration (for 0x00000042) this is ignored (there is no way for this to be detected as far as I can see). Consequently, the 0xF0 fragment remains as the remnant for the uD800 transform to utf8.
Jan, assigning to you as I do not grok the whole surrogate handling business in 8.7. In my view it is a valiant attempt to support in 8.7 but keeps throwing up bugs.
Fix attempt [here]. Testing ....
Should be fixed [here]