 USING: accessors alien alien.accessors alien.c-types alien.data
 alien.enums alien.strings arrays assocs fry io.encodings.string
-io.encodings.utf8 kernel math math.bitwise mirrors pcre.ffi
-sequences sequences.generalizations splitting strings ;
+io.encodings.utf8 kernel literals math math.bitwise mirrors
+pcre.ffi sequences sequences.generalizations splitting strings ;
 QUALIFIED: regexp
 IN: pcre
 
+ERROR: malformed-regexp expr error ;
+
+ERROR: pcre-error value ;
+
 <PRIVATE
+
+: replace-all ( seq subseqs new -- seq )
+    swapd '[ _ replace ] reduce ;
+
+: split-subseqs ( seq subseqs -- seqs )
+    dup first [ replace-all ] keep split-subseq [ >string ] map harvest ;
 
 : 2with ( param1 param2 obj quot -- obj curry )
     [ -rot ] dip [ [ rot ] dip call ] 3curry ; inline

: matches? ( subject obj -- ? )
    dupd findall [ nip length 1 = ] [ ?first ?first ?last = ] 2bi and ;

<PRIVATE

: replace-all ( seq subseqs new -- seq )
    swapd '[ _ replace ] reduce ;

: split-subseqs ( seq subseqs -- seqs )
    dup first [ replace-all ] keep split-subseq [ >string ] map harvest ;

PRIVATE>

: split ( subject obj -- strings )
    dupd findall [ first second ] map split-subseqs ;

    swapd first2 swap [ substring-list ] keep void* <c-direct-array>
    [ utf8 alien>string ] { } map-as [ of swap 2array ] with map-index ;

PRIVATE>

TUPLE: compiled-pcre pcre extra nametable ;

: <compiled-pcre> ( expr -- compiled-pcre )
    <pcre> dup <pcre-extra> 2dup name-table-entries compiled-pcre boa ;

