1 ! Copyright (C) 2006, 2008 Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors byte-arrays math math.order kernel sequences
4 sbufs vectors growable io continuations namespaces io.encodings
14 : starts-2? ( char -- ? )
15 dup [ -6 shift BIN: 10 number= ] when ; inline
17 : append-nums ( stream byte -- stream char )
18 over stream-read1 dup starts-2?
19 [ swap 6 shift swap BIN: 111111 bitand bitor ]
20 [ 2drop replacement-char ] if ; inline
22 : double ( stream byte -- stream char )
23 BIN: 11111 bitand append-nums ; inline
25 : triple ( stream byte -- stream char )
26 BIN: 1111 bitand append-nums append-nums ; inline
28 : quadruple ( stream byte -- stream char )
29 BIN: 111 bitand append-nums append-nums append-nums ; inline
31 : begin-utf8 ( stream byte -- stream char )
33 { [ dup -7 shift zero? ] [ ] }
34 { [ dup -5 shift BIN: 110 = ] [ double ] }
35 { [ dup -4 shift BIN: 1110 = ] [ triple ] }
36 { [ dup -3 shift BIN: 11110 = ] [ quadruple ] }
37 [ drop replacement-char ]
40 : decode-utf8 ( stream -- char/f )
41 dup stream-read1 dup [ begin-utf8 ] when nip ; inline
44 drop decode-utf8 ; inline
48 : encoded ( stream char -- )
49 BIN: 111111 bitand BIN: 10000000 bitor swap stream-write1 ; inline
51 : char>utf8 ( char stream -- )
53 { [ dup -7 shift zero? ] [ swap stream-write1 ] }
54 { [ dup -11 shift zero? ] [
55 2dup -6 shift BIN: 11000000 bitor swap stream-write1
58 { [ dup -16 shift zero? ] [
59 2dup -12 shift BIN: 11100000 bitor swap stream-write1
64 2dup -18 shift BIN: 11110000 bitor swap stream-write1
65 2dup -12 shift encoded
77 [ [ char>utf8 ] curry each ]
78 [ [ >byte-array ] dip stream-write ] if ;
82 : code-point-length ( n -- x )
85 { [ dup 0 6 between? ] [ 1 ] }
86 { [ dup 7 10 between? ] [ 2 ] }
87 { [ dup 11 15 between? ] [ 3 ] }
88 { [ dup 16 20 between? ] [ 4 ] }
92 : code-point-offsets ( string -- indices )
93 0 [ code-point-length + ] accumulate swap suffix ;
95 : utf8-index> ( n string -- n' )
96 code-point-offsets [ <= ] with find drop ;
98 : >utf8-index ( n string -- n' )
99 code-point-offsets nth ;