! Copyright (C) 2005, 2009 Daniel Ehrenberg
! See http://factorcode.org/license.txt for BSD license.
-USING: namespaces xml.state kernel sequences accessors
-xml.char-classes xml.errors math io sbufs fry strings ascii
-xml.entities assocs splitting math.parser
-locals combinators combinators.short-circuit arrays hints ;
+USING: accessors ascii assocs combinators
+combinators.short-circuit hints io kernel math math.parser
+namespaces sbufs sequences splitting strings xml.char-classes
+xml.entities xml.errors xml.state ;
IN: xml.tokenize
! * Basic utility words
spot get (skip-until) ; inline
: take-until ( ... quot: ( ... char -- ... ? ) -- ... string )
- #! Take the substring of a string starting at spot
- #! from code until the quotation given is true and
- #! advance spot to after the substring.
+ ! Take the substring of a string starting at spot
+ ! from code until the quotation given is true and
+ ! advance spot to after the substring.
10 <sbuf> [
'[ _ keep over [ drop ] [ _ push ] if ] skip-until
] keep "" like ; inline
'[ _ member? ] take-until ; inline
: pass-blank ( -- )
- #! Advance code past any whitespace, including newlines
+ ! Advance code past any whitespace, including newlines
[ blank? not ] skip-until ;
: next-matching ( pos ch str -- pos' )
- [ over ] dip nth eq? [ 1 + ] [ drop 0 ] if ; inline
+ overd nth eq? [ 1 + ] [ drop 0 ] if ; inline
: string-matcher ( str -- quot: ( pos char -- pos ? ) )
dup length 1 - '[ _ next-matching dup _ > ] ; inline
+:: (take-string) ( match spot -- sbuf matched? )
+ 10 <sbuf> f [
+ spot char>> [
+ nip over push
+ spot next*
+ dup match tail? dup not
+ ] [ f ] if*
+ ] loop ; inline
+
: take-string ( match -- string )
- [ 0 swap string-matcher take-until nip ] keep
- dupd [ length ] bi@ 1 - - head
- get-char [ missing-close ] unless next ;
+ [ spot get (take-string) [ missing-close ] unless ]
+ [ dupd [ length ] bi@ - over shorten "" like ] bi ;
: expect ( string -- )
dup length spot get '[ _ [ char>> ] keep next* ] "" replicate-as