1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
-module(cord_regexp).
-compile(export_all).
%%-export([Function/Arity, ...]).
first_match(RE, Cord) ->
first_match(RE, Cord, 1).
first_match(RE, Cord, Pos) ->
first_match(RE, Cord, Pos, forward).
%% Find the first match of RE in Cord starting from Pos.
first_match(RE, Cord, Pos, Dir) ->
case first_match_c(RE, Cord, Pos, Dir) of
{{match, Start, Len}, WalkerOut} ->
{match, Start, Len};
X ->
X
end.
first_match_c(RE, Cord) ->
first_match_c(RE, Cord, 1).
first_match_c(RE, Cord, Pos) ->
first_match_c(RE, Cord, Pos, forward).
first_match_c(RE, Cord, Pos, forward) ->
{_, Region} = cord:split(Cord, Pos-1),
first_match1(RE, cord:walker(Region, forward), Pos);
first_match_c(RE, Cord, Pos, backward) ->
case regexp:parse(RE) of
{ok, REP} ->
{Region,_} = cord:split(Cord, Pos),
RREP = reverse(REP),
case first_match1(RREP, cord:walker(Region, backward), Pos) of
{{match, S, L}, C} ->
{{match, S, L}, C};
X ->
X
end;
{error, Rsn} ->
{error, Rsn}
end.
continue_match(RE, {W, N}) ->
case first_match1(RE, W, N) of
{{match, S, L}, C} ->
{{match, S, L}, {C, S+L}};
X ->
X
end.
first_match1(RE, W, Pos) when is_list(RE) ->
case regexp:parse(RE) of
{ok, REP} ->
first_match1(optimise(REP), W, Pos);
X = {error, Rsn} ->
X
end;
first_match1(RE, Walker, Pos) ->
case cord:walker_at_end(Walker) of
true ->
nomatch;
false ->
Start = case cord:walker_direction(Walker) of
forward -> 1;
backward -> Pos
end,
case apply_regexp(RE, Start, Walker) of
nomatch ->
%% it would be more efficient to use Walker to get
%% a new continuation to recurse with
{_, W1} = cord:walker_next(Walker),
first_match1(RE, W1, advance(Pos, W1));
{match, EndPos, WalkerOut} ->
Len = case cord:walker_direction(Walker) of
forward -> EndPos;
backward -> Pos - EndPos - 1
end,
{{match, Pos, Len}, WalkerOut}
end
end.
apply_regexp(RE, Pos, W) ->
{Ch, W1} = cord:walker_next(W),
re_apply(RE, [], Ch, Pos, W1).
%% re_apply(RE, More, ThisChar, Pos, InputCont) => {match, Len} | nomatch
%% FIXME: Handling of bos (^) and eos ($) need thinking about when it
%% comes to backwards-searching. Right now, ^ will match the end when
%% going backwards.
re_apply(epsilon, More, Ch, P, C) ->
re_apply_more(More, P, push(Ch, C));
re_apply(eos, More, done, P, C) ->
case cord:walker_direction(C) of
forward ->
re_apply_more(More, P, C);
backward ->
nomatch
end;
re_apply(eos, More, $\n, P, C) ->
re_apply_more(More, P, push($\n, C)); % \n isn't consumed
re_apply(bos, More, done, P, C) ->
case cord:walker_direction(C) of
forward -> nomatch;
backward -> re_apply_more(More, P, C)
end;
re_apply({'or', RE1, RE2}, More, Ch, P, C) ->
re_apply_or({apply, RE1, More, Ch, P, C},
{apply, RE2, More, Ch, P, C});
re_apply({concat, RE1, RE2}, More, Ch, P, C) ->
re_apply(RE1, [RE2|More], Ch, P, C);
re_apply({kclosure, CE}, More, Ch, P, C) ->
re_apply_or({apply_more, More, P, push(Ch, C)},
{apply, CE, [{kclosure, CE}|More], Ch, P, C});
re_apply({pclosure, CE}, More, Ch, P, C) ->
re_apply(CE, [{kclosure, CE}|More], Ch, P, C);
re_apply({optional, CE}, More, Ch, P, C) ->
re_apply_or({apply_more, More, P, push(Ch, C)},
{apply, CE, More, Ch, P, C});
re_apply(bos, More, Ch, 1, C) ->
case cord:walker_direction(C) of
forward -> re_apply_more(More, 1, push(Ch, C));
backward -> nomatch
end;
re_apply(bos, More, $\n, P, C) ->
re_apply_more(More, 1, push($\n, C)); % \n isn't consumed
re_apply({char_class, Cc}, More, Ch, P, C) ->
case in_char_class(Ch, Cc) of
true -> re_apply_more(More, advance(P, C), C);
false -> nomatch
end;
re_apply({comp_class, Cc}, More, Ch, P, C) ->
case in_char_class(Ch, Cc) of
true -> nomatch;
false -> re_apply_more(More, advance(P, C), C)
end;
re_apply(Ch, More, Ch, P, C) when is_integer(Ch) ->
re_apply_more(More, advance(P, C), C);
re_apply(_, _, _, _, _) ->
nomatch.
advance(Pos, Walker) ->
case cord:walker_direction(Walker) of
forward -> Pos + 1;
backward -> Pos - 1
end.
re_apply_more([H|T], P, C) ->
{Next, C1} = cord:walker_next(C),
re_apply(H, T, Next, P, C1);
re_apply_more([], P, C) ->
%% -1 isn't used in 'regexp' module, not sure what's different..
{match, P-1, C}.
re_apply_or(A, B) ->
case re_apply_or_operand(A) of
nomatch ->
re_apply_or_operand(B);
X ->
X
end.
re_apply_or_operand({apply_more, More, P, C}) ->
re_apply_more(More, P, C);
re_apply_or_operand({apply, RE, More, Ch, P, C}) ->
re_apply(RE, More, Ch, P, C).
% re_apply_or({match,P1}, {match,P2}) when P1 >= P2 -> {match,P1};
% re_apply_or({match,P1}, {match,P2}) -> {match,P2};
% re_apply_or(nomatch, R2) -> R2;
% re_apply_or(R1, nomatch) -> R1.
in_char_class(C, [{C1,C2}|Cc]) when C >= C1, C =< C2 -> true;
in_char_class(C, [C|Cc]) -> true;
in_char_class(C, [_|Cc]) -> in_char_class(C, Cc);
in_char_class(C, []) -> false.
push(Ch, Cont) ->
cord:walker_push(Ch, Cont).
%% reverse(RE)
%% Returns an equivalent regexp which takes its input in reverse order.
%% RE should come from regexp:parse/1
reverse({'or', A, B}) ->
{'or', reverse(B), reverse(A)};
reverse({concat, A, B}) ->
{concat, reverse(B), reverse(A)};
reverse({optional, RE}) ->
{optional, reverse(RE)};
reverse({kclosure, RE}) ->
{kclosure, reverse(RE)};
reverse({pclosure, RE}) ->
{pclosure, reverse(RE)};
reverse(Other) ->
Other.
%% Just trivial stuff for starters.
%%
%% Rearrange concat
optimise({concat, {concat, A, B}, C}) ->
optimise({concat, A, {concat, B, C}});
%% recurse
optimise({'or', A, B}) ->
{'or', optimise(A), optimise(B)};
optimise({kclosure, A}) ->
{kclosure, optimise(A)};
optimise({pclosure, A}) ->
{pclosure, optimise(A)};
optimise({optional, A}) ->
{optional, optimise(A)};
optimise(X) ->
X.
test_exprs() ->
["foo",
"a|b",
"(foo)|(bar)",
"a+"].
test_inputs() ->
["foo", "a", "b", "c", "foo", "bar", "baz", "aaaaa"].
test() ->
case [{Inp, RE} || RE <- test_exprs(), Inp <- test_inputs(),
not same(Inp, RE)] of
[] ->
ok;
%% BadOnes is where the match result was different. BUT!
%% sometimes that is OK. At time of writing, the code seems
%% correct but has some BadOnes.
BadOnes ->
lists:foreach(fun complain/1, BadOnes)
end.
same(Inp, REStr) ->
forwards(Inp, REStr) == backwards(Inp, REStr).
forwards(Inp, RE) ->
regexp:match(Inp, RE).
backwards(Inp, REStr) ->
{ok, RE} = regexp:parse(REStr),
BackwardsResult = regexp:match(lists:reverse(Inp), reverse(RE)).
complain({Inp, REStr}) ->
{ok, RE} = regexp:parse(REStr),
ForwardsResult = regexp:match(Inp, RE),
BackwardsResult = regexp:match(lists:reverse(Inp), reverse(RE)),
io:format("match(~p, ~p):~n Fwds = ~p~n Bwds = ~p~n",
[Inp, REStr, ForwardsResult, BackwardsResult]).
%% Compare speed of this module on a cord to the speed of the 'regexp'
%% module on a string.
bench(Regexp, Cord) ->
List = cord:to_list(Cord),
{CordSpeed,_} = timer:tc(?MODULE, first_match, [Regexp, Cord]),
{ListSpeed, _} = timer:tc(regexp, first_match, [List, Regexp]),
io:format("Cord takes ~p% time of List (~pms vs ~pms).~n",
[round(CordSpeed * 100 / ListSpeed),
round(CordSpeed/1000),
round(ListSpeed/1000)]).
escape([H|T]) ->
case lists:member(H, specials()) of
true ->
[$\\,H|escape(T)];
false ->
[H|escape(T)]
end;
escape([]) ->
[].
specials() -> "()^$[]*+?.\\|".
|