]> gitweb.factorcode.org Git - factor.git/blobdiff - basis/xmode/marker/marker.factor
xmode.marker: more correct faster update-match-group
[factor.git] / basis / xmode / marker / marker.factor
index 05e93b61548447f417e21c6a99e6a8d1837e2acf..a93283e29020935417bdf1faae8991ee2e4832f8 100644 (file)
@@ -102,7 +102,7 @@ M: regexp text-matches?
 : match-group-regexp ( regexp n -- skip-regexp match-regexp )
     [ [ options>> options>string ] [ raw>> ] bi ] dip
     CHAR: ( pick nth-index cut CHAR: ) over index 1 + head
-    rot '[ _ <optioned-regexp> ] bi@ ;
+    rot '[ _ H{ } [ <optioned-regexp> ] 2cache ] bi@ ;
 
 : skip-first-match ( match regexp -- tailseq )
     first-match [ seq>> ] [ to>> ] bi tail ;
@@ -110,8 +110,12 @@ M: regexp text-matches?
 : nth-match ( match regexp n -- slice/f )
     match-group-regexp [ skip-first-match ] [ first-match ] bi* ;
 
-: update-match-group ( str match regexp n -- str' )
-    [ nth-match ] [ CHAR: 1 + "$%c" sprintf ] bi swap replace ;
+:: update-match-group ( str match regexp n -- str' )
+    n H{ } [ CHAR: 1 + CHAR: $ swap "" 2sequence ] cache :> x
+    x str subseq-range :> ( from to )
+    from [
+        to str snip-slice match regexp n nth-match glue
+    ] [ str ] if* ;
 
 : update-match-groups ( str match regexp -- str' )
     [ >string ] dip
@@ -171,14 +175,14 @@ PRIVATE>
 
 DEFER: get-rules
 
-: get-always-rules ( vector/f ruleset -- vector/f )
-    f swap rules>> at ?push-all ;
+: get-always-rules ( ruleset -- vector/f )
+    f swap rules>> at ;
 
-: get-char-rules ( vector/f char ruleset -- vector/f )
-    [ ch>upper ] dip rules>> at ?push-all ;
+: get-char-rules ( char ruleset -- vector/f )
+    [ ch>upper ] dip rules>> at ;
 
 : get-rules ( char ruleset -- seq )
-    [ f ] 2dip [ get-char-rules ] keep get-always-rules ;
+    [ get-char-rules ] [ get-always-rules ] bi [ append ] when* ;
 
 GENERIC: handle-rule-start ( match-count rule -- )