From 4fab0695b8f76f72e024167cbfc92a81ddc49fed Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 22 Sep 2008 02:28:10 -0500 Subject: [PATCH] Add some words --- unfinished/regexp/regexp.factor | 39 ++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/unfinished/regexp/regexp.factor b/unfinished/regexp/regexp.factor index 85bdccc2f4..c4ab2675d2 100644 --- a/unfinished/regexp/regexp.factor +++ b/unfinished/regexp/regexp.factor @@ -1,10 +1,9 @@ ! Copyright (C) 2008 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors combinators kernel math math.ranges -sequences regexp.backend regexp.utils memoize sets -regexp.parser regexp.nfa regexp.dfa regexp.traversal -regexp.transition-tables assocs prettyprint.backend -make lexer namespaces parser ; +USING: accessors combinators kernel math math.ranges sequences +sets assocs prettyprint.backend make lexer namespaces parser +arrays fry regexp.backend regexp.utils regexp.parser regexp.nfa +regexp.dfa regexp.traversal regexp.transition-tables ; IN: regexp : default-regexp ( string -- regexp ) @@ -47,6 +46,33 @@ IN: regexp [ 3drop drop f f ] [ drop [ 1+ ] dip match-range ] if ] if ; +: first-match ( string regexp -- pair/f ) + 0 swap match-range dup [ 2array ] [ 2drop f ] if ; + +: re-cut ( string regexp -- end/f start ) + dupd first-match + [ [ second tail-slice ] [ first head ] 2bi ] + [ "" like f swap ] + if* ; + +: re-split ( string regexp -- seq ) + [ dup ] swap '[ _ re-cut ] [ ] produce nip ; + +: re-replace ( string regexp replacement -- result ) + [ re-split ] dip join ; + +: next-match ( string regexp -- end/f match/f ) + dupd first-match dup + [ [ second tail-slice ] keep ] + [ 2drop f f ] + if ; + +: all-matches ( string regexp -- seq ) + [ dup ] swap '[ _ next-match ] [ ] produce nip ; + +: count-matches ( string regexp -- n ) + all-matches length 1- ; + : initial-option ( regexp option -- regexp' ) over options>> conjoin ; @@ -102,8 +128,6 @@ IN: regexp : option? ( option regexp -- ? ) options>> key? ; -USE: multiline -/* M: regexp pprint* [ [ @@ -112,4 +136,3 @@ M: regexp pprint* case-insensitive swap option? [ "i" % ] when ] "" make ] keep present-text ; -*/