18.6.1.2487.25 XC-SIZE x-c-size XCHAR
( xchar -- u )
u is the number of pchars used to encode xchar in memory.
Implementation:
: XC-SIZE ( xchar -- n )
DUP $80 U< IF DROP 1 EXIT THEN \ special case ASCII
$800 2 >R
BEGIN 2DUP U>= WHILE 5 LSHIFT R> 1+ >R DUP 0= UNTIL THEN
2DROP R>
;
DUP $80 U< IF DROP 1 EXIT THEN \ special case ASCII
$800 2 >R
BEGIN 2DUP U>= WHILE 5 LSHIFT R> 1+ >R DUP 0= UNTIL THEN
2DROP R>
;
Testing:
This test assumes UTF-8 encoding is being used.
HEX
T{ 0 XC-SIZE -> 1 }T
T{ 7f XC-SIZE -> 1 }T
T{ 80 XC-SIZE -> 2 }T
T{ 7ff XC-SIZE -> 2 }T
T{ 800 XC-SIZE -> 3 }T
T{ ffff XC-SIZE -> 3 }T
T{ 10000 XC-SIZE -> 4 }T
T{ 1fffff XC-SIZE -> 4 }T
T{ 0 XC-SIZE -> 1 }T
T{ 7f XC-SIZE -> 1 }T
T{ 80 XC-SIZE -> 2 }T
T{ 7ff XC-SIZE -> 2 }T
T{ 800 XC-SIZE -> 3 }T
T{ ffff XC-SIZE -> 3 }T
T{ 10000 XC-SIZE -> 4 }T
T{ 1fffff XC-SIZE -> 4 }T