1 ! Copyright (C) 2005, 2009 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel namespaces xml.name io.encodings.utf8 xml.elements
4 io.encodings.utf16 xml.tokenize xml.state math ascii sequences
5 io.encodings.string io.encodings combinators accessors
6 xml.data io.encodings.iana ;
9 : continue-make-tag ( str -- tag )
10 parse-name-starting middle-tag end-tag ;
12 : start-utf16le ( -- tag )
17 : 10xxxxxx? ( ch -- ? )
18 -6 shift 3 bitand 2 = ;
20 : start<name ( ch -- tag )
21 ! This is unfortunate, and exists for the corner case
22 ! that the first letter of the document is < and second is
25 [ utf8 decode-input next make-tag ] [
27 [ get-next 10xxxxxx? not ] take-until
28 get-char suffix utf8 decode
29 utf8 decode-input next
33 : prolog-encoding ( prolog -- )
34 encoding>> dup "UTF-16" =
35 [ drop ] [ name>encoding [ decode-input ] when* ] if ;
37 : instruct-encoding ( instruct/prolog -- )
40 [ drop utf8 decode-input ] if ;
43 check utf8 decode-input next next ;
46 ! What if first letter of processing instruction is non-ASCII?
48 { 0 [ next next start-utf16le ] }
49 { CHAR: ? [ go-utf8 instruct dup instruct-encoding ] }
50 { CHAR: ! [ go-utf8 direct ] }
54 : skip-utf8-bom ( -- tag )
55 "\u0000bb\u0000bf" expect utf8 decode-input
56 "<" expect check make-tag ;
58 : decode-expecting ( encoding string -- tag )
59 [ decode-input next ] [ expect ] bi* check make-tag ;
61 : start-utf16be ( -- tag )
62 utf16be "<" decode-expecting ;
64 : skip-utf16le-bom ( -- tag )
65 utf16le "\u0000fe<" decode-expecting ;
67 : skip-utf16be-bom ( -- tag )
68 utf16be "\u0000ff<" decode-expecting ;
70 : start-document ( -- tag )
72 { CHAR: < [ start< ] }
73 { 0 [ start-utf16be ] }
74 { HEX: EF [ skip-utf8-bom ] }
75 { HEX: FF [ skip-utf16le-bom ] }
76 { HEX: FE [ skip-utf16be-bom ] }
77 [ drop utf8 decode-input check f ]