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
|
#if !macintosh
#include <sys/types.h>
#else
#include <SizeTDef.h>
#endif
#include <string.h>
#include <regex.h>
#include <mlvalues.h>
#include <alloc.h>
#include <custom.h>
#include <fail.h>
#include <memory.h>
struct regexp_struct {
struct custom_operations * ops;
struct re_pattern_buffer re;
};
typedef struct regexp_struct * regexp;
static void free_regexp(value vexpr)
{
regexp expr = (regexp) Bp_val(vexpr);
expr->re.translate = NULL;
re_free(&(expr->re));
}
static struct custom_operations regexp_ops = {
"_regexp",
free_regexp,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default
};
static regexp alloc_regexp(void)
{
value res =
alloc_custom(®exp_ops, sizeof(struct regexp_struct), 1, 10000);
return (regexp) res;
}
#define RE_SYNTAX RE_SYNTAX_EMACS
static char * case_fold_table = NULL;
value str_compile_regexp(value src, value fold) /* ML */
{
regexp expr;
char * msg;
Begin_root(src);
expr = alloc_regexp();
End_roots();
re_syntax_options = RE_SYNTAX;
if (Bool_val(fold) && case_fold_table == NULL) {
int i;
case_fold_table = stat_alloc(256);
for (i = 0; i <= 255; i++) case_fold_table[i] = i;
for (i = 'A'; i <= 'Z'; i++) case_fold_table[i] = i + 32;
for (i = 192; i <= 214; i++) case_fold_table[i] = i + 32;
for (i = 216; i <= 222; i++) case_fold_table[i] = i + 32;
}
expr->re.translate = Bool_val(fold) ? case_fold_table : NULL;
expr->re.fastmap = stat_alloc(256);
expr->re.buffer = NULL;
expr->re.allocated = 0;
msg = (char *) re_compile_pattern(String_val(src), string_length(src),
&(expr->re));
if (msg != NULL) failwith(msg);
re_compile_fastmap(&(expr->re));
expr->re.regs_allocated = REGS_FIXED;
return (value) expr;
}
static regoff_t start_regs[10], end_regs[10];
static struct re_registers match_regs = { 10, start_regs, end_regs };
value str_string_match(regexp expr, value text, value pos) /* ML */
{
int len = string_length(text);
int start = Int_val(pos);
if (start < 0 || start > len)
invalid_argument("Str.string_match");
switch (re_match(&(expr->re), String_val(text), len,
start, &match_regs)) {
case -2:
failwith("Str.string_match");
case -1:
case -3:
return Val_false;
default:
return Val_true;
}
}
value str_string_partial_match(regexp expr, value text, value pos) /* ML */
{
int len = string_length(text);
int start = Int_val(pos);
if (start < 0 || start > len)
invalid_argument("Str.string_partial_match");
switch (re_match(&(expr->re), String_val(text), len,
start, &match_regs)) {
case -2:
failwith("Str.string_partial_match");
case -1:
return Val_false;
default:
return Val_true;
}
}
value str_search_forward(regexp expr, value text, value pos) /* ML */
{
int res;
int len = string_length(text);
int start = Int_val(pos);
if (start < 0 || start > len)
invalid_argument("Str.search_forward");
res = re_search(&(expr->re), String_val(text), len, start, len-start,
&match_regs);
switch(res) {
case -2:
failwith("Str.search_forward");
case -1:
raise_not_found();
default:
return Val_int(res);
}
}
value str_search_backward(regexp expr, value text, value pos) /* ML */
{
int res;
int len = string_length(text);
int start = Int_val(pos);
if (start < 0 || start > len)
invalid_argument("Str.search_backward");
res = re_search(&(expr->re), String_val(text), len, start, -start-1,
&match_regs);
switch(res) {
case -2:
failwith("Str.search_backward");
case -1:
raise_not_found();
default:
return Val_int(res);
}
}
value str_beginning_group(value ngroup) /* ML */
{
return Val_int(start_regs[Int_val(ngroup)]);
}
value str_end_group(value ngroup) /* ML */
{
return Val_int(end_regs[Int_val(ngroup)]);
}
value str_replacement_text(value repl, value orig) /* ML */
{
value res;
mlsize_t len, n;
char * p, * q;
int c;
len = 0;
p = String_val(repl);
n = string_length(repl);
while (n > 0) {
c = *p++; n--;
if(c != '\\')
len++;
else {
if (n == 0) failwith("Str.replace: illegal backslash sequence");
c = *p++; n--;
switch (c) {
case '\\':
len++; break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
c -= '0';
len += end_regs[c] - start_regs[c]; break;
default:
len += 2; break;
}
}
}
Begin_roots2(orig,repl);
res = alloc_string(len);
End_roots();
p = String_val(repl);
q = String_val(res);
n = string_length(repl);
while (n > 0) {
c = *p++; n--;
if(c != '\\')
*q++ = c;
else {
c = *p++; n--;
switch (c) {
case '\\':
*q++ = '\\'; break;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
c -= '0';
len = end_regs[c] - start_regs[c];
bcopy(&Byte(orig, start_regs[c]), q, len);
q += len;
break;
default:
*q++ = '\\'; *q++ = c; break;
}
}
}
return res;
}
|