HEX: 10FFFF maximum-code-point ; inline
: begin-utf8 ( stream byte -- stream char )
- {
- { [ dup -7 shift zero? ] [ ] }
- { [ dup -5 shift BIN: 110 = ] [ double ] }
- { [ dup -4 shift BIN: 1110 = ] [ triple ] }
- { [ dup -3 shift BIN: 11110 = ] [ quadruple ] }
- [ drop replacement-char ]
- } cond ; inline
+ dup 127 > [
+ {
+ { [ dup -5 shift BIN: 110 = ] [ double ] }
+ { [ dup -4 shift BIN: 1110 = ] [ triple ] }
+ { [ dup -3 shift BIN: 11110 = ] [ quadruple ] }
+ [ drop replacement-char ]
+ } cond
+ ] when ; inline
: decode-utf8 ( stream -- char/f )
dup stream-read1 dup [ begin-utf8 ] when nip ; inline
BIN: 111111 bitand BIN: 10000000 bitor swap stream-write1 ; inline
: char>utf8 ( char stream -- )
- swap {
- { [ dup -7 shift zero? ] [ swap stream-write1 ] }
- { [ dup -11 shift zero? ] [
- 2dup -6 shift BIN: 11000000 bitor swap stream-write1
- encoded
- ] }
- { [ dup -16 shift zero? ] [
- 2dup -12 shift BIN: 11100000 bitor swap stream-write1
- 2dup -6 shift encoded
- encoded
- ] }
- [
- 2dup -18 shift BIN: 11110000 bitor swap stream-write1
- 2dup -12 shift encoded
- 2dup -6 shift encoded
- encoded
- ]
- } cond ; inline
+ over 127 <= [ stream-write1 ] [
+ swap {
+ { [ dup -11 shift zero? ] [
+ 2dup -6 shift BIN: 11000000 bitor swap stream-write1
+ encoded
+ ] }
+ { [ dup -16 shift zero? ] [
+ 2dup -12 shift BIN: 11100000 bitor swap stream-write1
+ 2dup -6 shift encoded
+ encoded
+ ] }
+ [
+ 2dup -18 shift BIN: 11110000 bitor swap stream-write1
+ 2dup -12 shift encoded
+ 2dup -6 shift encoded
+ encoded
+ ]
+ } cond
+ ] if ; inline
M: utf8 encode-char
drop char>utf8 ;