]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/xml/tokenize/tokenize.factor
factor: trim using lists
[factor.git] / basis / xml / tokenize / tokenize.factor
index a85c48a022731267eea84d001a226ac400f47220..77f5f8d917d5d16a53383b81a8325149ceb0c0f9 100644 (file)
@@ -1,9 +1,9 @@
 ! 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
@@ -72,9 +72,9 @@ HINTS: next* { spot } ;
     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
@@ -83,19 +83,27 @@ HINTS: next* { spot } ;
     '[ _ 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