Skip to content
Tcl Source Code View Ticket
Login

View Ticket

tip-389 branch: string range errors with code points greater than U+FFFF

Closed / Fixed11ae2be95dac941790db43dcfb265f8586072cd8Created 12 years agoUpdated 9 years ago
Descriptionplain2014-12-11 21:38:40
% string range a\U10000b 0 0
a
% string range a\U10000b 1 1
Segmentation fault
% string range a\U10000b 2 2
Segmentation fault
Referenced by1
  • de13f558fbFix [11ae2be95d]: tip-389 branch: string range errors with code points greater than U+FFFF check-in2018-01-12 10:03:58
jan.nijtmanswiki2014-12-12 15:10:19

Dup of [3408718]

jan.nijtmans Closed ticket [11ae2be95d]. [History diff]2014-12-12 15:10:43
dgpplain2018-01-10 15:03:16
Re-opening.

The segfaults are indeed gone but now on the tip-389 branch:

% string range a\U10000b 0 0
a
% string range a\U10000b 1 1
% string range a\U10000b 2 2
st

Especially for the last command, I'd expect the result "b"

I'm far from confident the middle command is doing the right thing too.
dgp Ticket [11ae2be95d]: 3 changes [History diff]2018-01-10 15:04:09
sebresplain2018-01-10 15:18:44
> @dgp Especially for the last command, I'd expect the result "b"

Just to be sure: is it a valid escape-sequence at all? 
I mean, do you not need 32-bit hex value there?

For example doing this in python will cause following error:

>>> 'a\U10000b'
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 1-8: truncated \UXXXXXXXX escape
dgpplain2018-01-10 15:24:55
To remove the abiguity about where \UHHHHHHHHH parsing ends....

% string index a\U10ffff! 2
pßÿ¿
% string index a-! 2
!
jan.nijtmansplain2018-01-10 15:45:28
I'll have a loot at "string range"  and "string index". In the current implementation, a character > \U0ffff occupies 2 locations in the string (= actually 'character array'). So we must implement a precaution that we can never point in the middle of a upper/lower surrogate pair. That's what goes wrong here.

The best thing to do is - I think - to round up any index that points in the middle of a surrogate pair. So, what I would expect:

% string range a\U10000b 0 0
a
% string range a\U10000b 1 1
\U10000 (however that would be displayed ...)
% string range a\U10000b 2 2

% string range a\U10000b 3 3
b

Indeed, there's still something to be done here.

Thanks!
dgpplain2018-01-10 15:54:11
Aarrggh. used the wrong number of 0's in an earlier comment.

lets try again....

% string range a\U100000b 0 0
a
% string range a\U100000b 1 1
% string range a\U100000b 2 2
st
% string range a\U100000b 3 3
b

Ok, that's a somewhat different kettle of fish.
I thought we were getting just wrong answers.

Instead it's a more subtle issue of the "two surrogates"
instead of "one Unicode codepoint" leaking through the
abstraction.  This isn't ideal, but might be the tradeoff
that's in place.

Sorry about the botched demo.
sebreswiki2018-01-11 14:53:33

Hi Jan,

really printf in StringIndexCmd?.. :)

Do we need always use `int` instead of `Tcl_UniChar` now by call of Tcl_GetUniChar?... (if/after TIP get merged).

jan.nijtmansplain2018-01-11 15:28:39
> really printf in StringIndexCmd?.. :)

Work in progress ... will be fixed.

> Do we need always use `int` instead of `Tcl_UniChar` now by call of Tcl_GetUniChar?... (if/after TIP get merged).

Yes, at least if you want to make use of characters >U+FFFF. If you decide to keep using Tcl_UniChar, that's OK too, but then characters in other Unicode planes will be mapped to characters in the BMP. Since Tcl <= 8.6 cannot handle characters >U+FFFF anyway, I consider that an improvement ;-)
jan.nijtmanswiki2018-01-12 10:05:45

Fixed in: [de13f558fb5352c9]. Closing.

Log in (anonymous is fine) to add a comment.