From 64c0082cd17d7e32d97e490819075c859326c900 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Thu, 11 Aug 2022 17:05:43 -0700 Subject: [PATCH] xmode.marker: ignore non-capturing groups --- basis/xmode/marker/marker.factor | 38 ++++++++++++++++---------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/basis/xmode/marker/marker.factor b/basis/xmode/marker/marker.factor index a93283e290..204e108f3f 100644 --- a/basis/xmode/marker/marker.factor +++ b/basis/xmode/marker/marker.factor @@ -93,33 +93,33 @@ M: regexp text-matches? ! XXX: Terrible inefficient regexp match group support -: #match-groups ( regexp -- n/f ) - raw>> [ CHAR: ( = ] count [ f ] when-zero ; +! XXX: support named-capturing groups? -: nth-index ( n obj seq -- i ) - [ = dup [ drop 1 - dup 0 < ] when ] with find drop nip ; +: group-start ( i raw -- n/f ) + [ CHAR: ( -rot index-from ] keep 2dup + { [ drop ] [ [ 1 + ] dip ?nth CHAR: ? = ] } 2&& + [ [ 1 + ] dip group-start ] [ drop ] if ; + +: nth-group-start ( n raw -- n ) + [ -1 ] 2dip '[ dup [ 1 + _ group-start ] when ] times ; + +: nth-group ( n raw -- before nth ) + [ nth-group-start ] keep swap cut CHAR: ) over index 1 + head ; : 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 '[ _ H{ } [ ] 2cache ] bi@ ; + [ [ options>> options>string ] [ raw>> ] bi ] dip swap + nth-group rot '[ _ H{ } [ ] 2cache ] bi@ ; : skip-first-match ( match regexp -- tailseq ) - first-match [ seq>> ] [ to>> ] bi tail ; + [ >string ] dip first-match [ seq>> ] [ to>> ] bi tail ; : nth-match ( match regexp n -- slice/f ) - match-group-regexp [ skip-first-match ] [ first-match ] bi* ; - -:: 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* ; + match-group-regexp [ skip-first-match ] [ match-start ] bi* ; : update-match-groups ( str match regexp -- str' ) - [ >string ] dip - dup #match-groups [ update-match-group ] 2with each-integer ; + pick CHAR: $ swap index [ + R/ [$]\d/ [ second CHAR: 0 - nth-match ] 2with re-replace-with + ] [ 2drop ] if ; GENERIC: fixup-end ( match regexp end -- end' ) @@ -137,7 +137,7 @@ M: regexp fixup-end [ options>> options>string ] bi ; : fixup-end? ( text -- ? ) - { [ regexp? ] [ #match-groups ] } 1&& ; + { [ regexp? ] [ 0 swap raw>> group-start ] } 1&& ; : fixup-end/text-matches? ( string regexp rule -- match-count/f ) [ >string ] 2dip [ [ match-start dup ] keep ] dip pick [ -- 2.34.1