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 ;
8 : continue-make-tag ( str -- tag )
9 parse-name-starting middle-tag end-tag ;
11 : start-utf16le ( -- tag )
12 utf16le decode-input-if
14 0 expect check instruct ;
16 : 10xxxxxx? ( ch -- ? )
17 -6 shift 3 bitand 2 = ;
19 : start<name ( ch -- tag )
21 [ utf8 decode-input-if next make-tag ] [
23 [ get-next 10xxxxxx? not ] take-until
24 get-char suffix utf8 decode
25 utf8 decode-input-if next
31 { 0 [ next next start-utf16le ] }
32 { CHAR: ? [ check next next instruct ] } ! XML prolog parsing sets the encoding
33 { CHAR: ! [ check utf8 decode-input next next direct ] }
37 : skip-utf8-bom ( -- tag )
38 "\u0000bb\u0000bf" expect utf8 decode-input
39 CHAR: < expect check make-tag ;
41 : decode-expecting ( encoding string -- tag )
42 [ decode-input-if next ] [ expect-string ] bi* check make-tag ;
44 : start-utf16be ( -- tag )
45 utf16be "<" decode-expecting ;
47 : skip-utf16le-bom ( -- tag )
48 utf16le "\u0000fe<" decode-expecting ;
50 : skip-utf16be-bom ( -- tag )
51 utf16be "\u0000ff<" decode-expecting ;
53 : start-document ( -- tag )
55 { CHAR: < [ start< ] }
56 { 0 [ start-utf16be ] }
57 { HEX: EF [ skip-utf8-bom ] }
58 { HEX: FF [ skip-utf16le-bom ] }
59 { HEX: FE [ skip-utf16be-bom ] }
61 [ drop utf8 decode-input-if f ]
62 ! Same problem as with <e`>, in the case of XML chunks?