]> gitweb.factorcode.org Git - factor.git/blob - extra/pcre/pcre.factor
Fixes #2966
[factor.git] / extra / pcre / pcre.factor
1 ! Copyright (C) 2013, 2016 Björn Lindqvist
2 ! See http://factorcode.org/license.txt for BSD license
3
4 USING: accessors alien alien.accessors alien.c-types alien.data
5 alien.enums alien.strings arrays assocs combinators
6 io.encodings.string io.encodings.utf8 kernel literals math
7 math.bitwise math.parser pcre.ffi regexp sequences
8 sequences.extras splitting strings ;
9 IN: pcre
10
11 ERROR: bad-option what ;
12
13 ERROR: malformed-regexp expr error ;
14
15 ERROR: pcre-error value ;
16
17 : version ( -- f )
18     pcre_version " -" splitting:split first string>number ;
19
20 <PRIVATE
21
22 : replace-all ( seq subseqs new -- seq )
23     swapd '[ _ replace ] reduce ;
24
25 : split-subseqs ( seq subseqs -- seqs )
26     dup first [ replace-all ] keep split-subseq [ >string ] map harvest ;
27
28 : utf8-start-byte? ( byte -- ? )
29     0xc0 bitand 0x80 = not ;
30
31 : next-utf8-char ( byte-array pos -- pos' )
32     1 + 2dup swap ?nth [
33         utf8-start-byte? [ nip ] [ next-utf8-char ] if
34     ] [ 2drop f ] if* ;
35
36 : check-bad-option ( err value what -- value )
37     rot 0 = [ drop ] [ bad-option ] if ;
38
39 : pcre-config ( what -- value )
40     [
41         dup {
42             PCRE_CONFIG_MATCH_LIMIT
43             PCRE_CONFIG_MATCH_LIMIT_RECURSION
44         } member? [
45             { long } [ pcre_config ] with-out-parameters
46         ] [
47             { int } [ pcre_config ] with-out-parameters
48         ] if
49     ] keep check-bad-option ;
50
51 : pcre-fullinfo ( pcre extra what -- obj )
52     [ { int } [ pcre_fullinfo ] with-out-parameters ] keep
53     check-bad-option ;
54
55 : pcre-substring-list ( subject match-array count -- alien )
56     { void* } [ pcre_get_substring_list drop ] with-out-parameters ;
57
58 : name-count ( pcre extra -- n )
59     PCRE_INFO_NAMECOUNT pcre-fullinfo ;
60
61 : name-table ( pcre extra -- addr )
62     [ drop alien-address 32 on-bits unmask ]
63     ! On at least win64, the pointer is returned as an int and is
64     ! negative. Cast it to a uint and everything works.
65     [ PCRE_INFO_NAMETABLE pcre-fullinfo int <ref> uint deref ] 2bi + ;
66
67 : name-entry-size ( pcre extra -- size )
68     PCRE_INFO_NAMEENTRYSIZE pcre-fullinfo ;
69
70 : name-table-entry ( addr -- group-index group-name )
71     [ <alien> 1 alien-unsigned-1 ]
72     [ 2 + <alien> utf8 alien>string ] bi ;
73
74 : name-table-entries ( pcre extra -- addrs )
75     [ name-table ] [ name-entry-size ] [ name-count ] 2tri
76     <iota> [ * + name-table-entry 2array ] 2with map ;
77
78 : options ( pcre -- opts )
79     f PCRE_INFO_OPTIONS pcre-fullinfo ;
80
81 : default-opts ( -- opts )
82     PCRE_UTF8 version 8.10 >= [ PCRE_UCP bitor ] when ;
83
84 : (pcre) ( expr -- pcre err-message err-offset )
85     default-opts { c-string int } [ f pcre_compile ] with-out-parameters ;
86
87 : <pcre> ( expr -- pcre )
88     dup (pcre) 2array swap [ 2nip ] [ malformed-regexp ] if* ;
89
90 : <pcre-extra> ( pcre -- pcre-extra )
91     0 { c-string } [ pcre_study ] with-out-parameters drop ;
92
93 : exec ( pcre extra subject ofs opts -- count match-data )
94     [ dup length ] 2dip 30 int <c-array> 30 [ pcre_exec ] keepd ;
95
96 TUPLE: matcher pcre extra subject ofs exec-opts ;
97
98 : <matcher> ( subject compiled-pcre -- matcher )
99     [ utf8 encode ] dip [ pcre>> ] [ extra>> ] bi rot 0 0 matcher boa ;
100
101 CONSTANT: empty-match-opts flags{ PCRE_NOTEMPTY_ATSTART PCRE_ANCHORED }
102
103 : findnext ( matcher -- matcher match/f )
104     dup {
105         [ pcre>> ]
106         [ extra>> ]
107         [ subject>> ]
108         [ ofs>> ]
109         [ exec-opts>> ]
110     } cleave exec over dup -1 < [
111         PCRE_ERRORS number>enum pcre-error
112     ] [
113         -1 = [
114             2drop dup exec-opts>> 0 =
115             [ f ]
116             [
117                 dup [ subject>> ] [ ofs>> ] bi next-utf8-char
118                 [ >>ofs 0 >>exec-opts findnext ] [ f ] if*
119             ] if
120         ] [
121             [
122                 nip
123                 [ first2 = [ empty-match-opts ] [ 0 ] if >>exec-opts ]
124                 [ second >>ofs ] bi
125             ] [
126                 2array
127             ] 2bi
128         ] if
129     ] if ;
130
131 : parse-match ( subject nametable match-data -- match )
132     swapd first2 swap [ pcre-substring-list ] keep void* <c-direct-array>
133     [ utf8 alien>string ] { } map-as [ of swap 2array ] with map-index ;
134
135 PRIVATE>
136
137 TUPLE: compiled-pcre pcre extra nametable ;
138
139 : <compiled-pcre> ( expr -- compiled-pcre )
140     <pcre> dup <pcre-extra> 2dup name-table-entries compiled-pcre boa ;
141
142 : has-option? ( compiled-pcre option -- ? )
143     [ pcre>> options ] dip bitand 0 > ;
144
145 GENERIC: findall ( subject obj -- matches )
146
147 M: compiled-pcre findall
148     [ <matcher> [ findnext ] loop>array nip ]
149     [ nametable>> rot [ parse-match ] 2with { } map-as ] 2bi ;
150
151 M: string findall
152     <compiled-pcre> findall ;
153
154 M: regexp:regexp findall
155     raw>> findall ;
156
157 : matches? ( subject obj -- ? )
158     dupd findall [ nip length 1 = ] [ ?first ?first ?last = ] 2bi and ;
159
160 : split ( subject obj -- strings )
161     dupd findall [ first second ] map split-subseqs ;