1996-11-02 10:02:27 -08:00
|
|
|
#if !macintosh
|
1995-09-07 05:31:26 -07:00
|
|
|
#include <sys/types.h>
|
1996-11-02 10:02:27 -08:00
|
|
|
#else
|
|
|
|
#include <SizeTDef.h>
|
|
|
|
#endif
|
1997-06-13 08:54:18 -07:00
|
|
|
#include <string.h>
|
1995-09-07 05:31:26 -07:00
|
|
|
#include <regex.h>
|
|
|
|
#include <mlvalues.h>
|
|
|
|
#include <alloc.h>
|
2000-02-10 06:04:59 -08:00
|
|
|
#include <custom.h>
|
1995-09-07 05:31:26 -07:00
|
|
|
#include <fail.h>
|
|
|
|
#include <memory.h>
|
|
|
|
|
|
|
|
struct regexp_struct {
|
2000-02-10 06:04:59 -08:00
|
|
|
struct custom_operations * ops;
|
1995-09-07 05:31:26 -07:00
|
|
|
struct re_pattern_buffer re;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct regexp_struct * regexp;
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static void free_regexp(value vexpr)
|
1995-09-07 05:31:26 -07:00
|
|
|
{
|
1997-04-25 09:15:59 -07:00
|
|
|
regexp expr = (regexp) Bp_val(vexpr);
|
|
|
|
expr->re.translate = NULL;
|
2000-03-24 08:15:44 -08:00
|
|
|
re_free(&(expr->re));
|
1995-09-07 05:31:26 -07:00
|
|
|
}
|
|
|
|
|
2000-02-10 06:04:59 -08:00
|
|
|
static struct custom_operations regexp_ops = {
|
|
|
|
"_regexp",
|
|
|
|
free_regexp,
|
|
|
|
custom_compare_default,
|
|
|
|
custom_hash_default,
|
|
|
|
custom_serialize_default,
|
|
|
|
custom_deserialize_default
|
|
|
|
};
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
static regexp alloc_regexp(void)
|
1995-09-07 05:31:26 -07:00
|
|
|
{
|
1997-04-08 02:10:11 -07:00
|
|
|
value res =
|
2000-02-10 06:04:59 -08:00
|
|
|
alloc_custom(®exp_ops, sizeof(struct regexp_struct), 1, 10000);
|
1995-09-07 05:31:26 -07:00
|
|
|
return (regexp) res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define RE_SYNTAX RE_SYNTAX_EMACS
|
|
|
|
|
1997-04-25 09:15:59 -07:00
|
|
|
static char * case_fold_table = NULL;
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value str_compile_regexp(value src, value fold) /* ML */
|
1995-09-07 05:31:26 -07:00
|
|
|
{
|
|
|
|
regexp expr;
|
|
|
|
char * msg;
|
|
|
|
|
1997-05-26 10:16:31 -07:00
|
|
|
Begin_root(src);
|
1995-09-07 05:31:26 -07:00
|
|
|
expr = alloc_regexp();
|
1997-05-26 10:16:31 -07:00
|
|
|
End_roots();
|
1995-09-07 05:31:26 -07:00
|
|
|
re_syntax_options = RE_SYNTAX;
|
1997-04-25 09:15:59 -07:00
|
|
|
if (Bool_val(fold) && case_fold_table == NULL) {
|
1995-09-07 05:31:26 -07:00
|
|
|
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;
|
|
|
|
}
|
1997-04-25 09:15:59 -07:00
|
|
|
expr->re.translate = Bool_val(fold) ? case_fold_table : NULL;
|
1995-09-07 05:31:26 -07:00
|
|
|
expr->re.fastmap = stat_alloc(256);
|
|
|
|
expr->re.buffer = NULL;
|
|
|
|
expr->re.allocated = 0;
|
1996-11-02 10:02:27 -08:00
|
|
|
msg = (char *) re_compile_pattern(String_val(src), string_length(src),
|
|
|
|
&(expr->re));
|
1995-09-07 05:31:26 -07:00
|
|
|
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 };
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value str_string_match(regexp expr, value text, value pos) /* ML */
|
1995-09-07 05:31:26 -07:00
|
|
|
{
|
1998-10-20 05:49:18 -07:00
|
|
|
int len = string_length(text);
|
|
|
|
int start = Int_val(pos);
|
|
|
|
if (start < 0 || start > len)
|
1995-09-07 05:31:26 -07:00
|
|
|
invalid_argument("Str.string_match");
|
1998-10-20 05:49:18 -07:00
|
|
|
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");
|
1995-09-07 05:31:26 -07:00
|
|
|
case -1:
|
|
|
|
return Val_false;
|
|
|
|
default:
|
|
|
|
return Val_true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value str_search_forward(regexp expr, value text, value pos) /* ML */
|
1995-09-07 05:31:26 -07:00
|
|
|
{
|
1998-10-20 05:49:18 -07:00
|
|
|
int res;
|
1995-09-07 05:31:26 -07:00
|
|
|
int len = string_length(text);
|
|
|
|
int start = Int_val(pos);
|
1998-10-20 05:49:18 -07:00
|
|
|
if (start < 0 || start > len)
|
|
|
|
invalid_argument("Str.search_forward");
|
|
|
|
res = re_search(&(expr->re), String_val(text), len, start, len-start,
|
|
|
|
&match_regs);
|
1995-09-07 05:31:26 -07:00
|
|
|
switch(res) {
|
|
|
|
case -2:
|
1998-10-20 05:49:18 -07:00
|
|
|
failwith("Str.search_forward");
|
1995-09-07 05:31:26 -07:00
|
|
|
case -1:
|
|
|
|
raise_not_found();
|
|
|
|
default:
|
|
|
|
return Val_int(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value str_search_backward(regexp expr, value text, value pos) /* ML */
|
1995-09-07 05:31:26 -07:00
|
|
|
{
|
1998-10-20 05:49:18 -07:00
|
|
|
int res;
|
1995-09-07 05:31:26 -07:00
|
|
|
int len = string_length(text);
|
|
|
|
int start = Int_val(pos);
|
1998-10-20 05:49:18 -07:00
|
|
|
if (start < 0 || start > len)
|
|
|
|
invalid_argument("Str.search_backward");
|
|
|
|
res = re_search(&(expr->re), String_val(text), len, start, -start-1,
|
|
|
|
&match_regs);
|
1995-09-07 05:31:26 -07:00
|
|
|
switch(res) {
|
|
|
|
case -2:
|
1998-10-20 05:49:18 -07:00
|
|
|
failwith("Str.search_backward");
|
1995-09-07 05:31:26 -07:00
|
|
|
case -1:
|
|
|
|
raise_not_found();
|
|
|
|
default:
|
|
|
|
return Val_int(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value str_beginning_group(value ngroup) /* ML */
|
1995-09-07 05:31:26 -07:00
|
|
|
{
|
|
|
|
return Val_int(start_regs[Int_val(ngroup)]);
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value str_end_group(value ngroup) /* ML */
|
1995-09-07 05:31:26 -07:00
|
|
|
{
|
|
|
|
return Val_int(end_regs[Int_val(ngroup)]);
|
|
|
|
}
|
|
|
|
|
1997-09-02 05:55:01 -07:00
|
|
|
value str_replacement_text(value repl, value orig) /* ML */
|
1995-09-07 05:31:26 -07:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1997-05-26 10:16:31 -07:00
|
|
|
Begin_roots2(orig,repl);
|
1995-09-07 05:31:26 -07:00
|
|
|
res = alloc_string(len);
|
1997-05-26 10:16:31 -07:00
|
|
|
End_roots();
|
1995-09-07 05:31:26 -07:00
|
|
|
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];
|
2000-11-23 05:45:03 -08:00
|
|
|
memmove (q, &Byte(orig, start_regs[c]), len);
|
1995-09-07 05:31:26 -07:00
|
|
|
q += len;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*q++ = '\\'; *q++ = c; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|