2000-05-01 13:42:38 +04:00
|
|
|
/**********************************************************************
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
re.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
created at: Mon Aug 9 18:24:49 JST 1993
|
|
|
|
|
2001-01-29 08:10:42 +03:00
|
|
|
Copyright (C) 1993-2001 Yukihiro Matsumoto
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-05-01 13:42:38 +04:00
|
|
|
**********************************************************************/
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
#include "ruby.h"
|
|
|
|
#include "re.h"
|
2001-05-16 13:05:54 +04:00
|
|
|
#include <ctype.h>
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-08-28 13:53:42 +04:00
|
|
|
static VALUE rb_eRegexpError;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
#define BEG(no) regs->beg[no]
|
|
|
|
#define END(no) regs->end[no]
|
|
|
|
|
|
|
|
#if 'a' == 97 /* it's ascii */
|
1999-08-13 09:45:20 +04:00
|
|
|
static const char casetable[] = {
|
1998-01-16 15:13:05 +03:00
|
|
|
'\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
|
|
|
|
'\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
|
|
|
|
'\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
|
|
|
|
'\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
|
|
|
|
/* ' ' '!' '"' '#' '$' '%' '&' ''' */
|
|
|
|
'\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
|
|
|
|
/* '(' ')' '*' '+' ',' '-' '.' '/' */
|
|
|
|
'\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
|
|
|
|
/* '0' '1' '2' '3' '4' '5' '6' '7' */
|
|
|
|
'\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
|
|
|
|
/* '8' '9' ':' ';' '<' '=' '>' '?' */
|
|
|
|
'\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
|
|
|
|
/* '@' 'A' 'B' 'C' 'D' 'E' 'F' 'G' */
|
|
|
|
'\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
|
|
|
|
/* 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' */
|
|
|
|
'\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
|
|
|
|
/* 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' */
|
|
|
|
'\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
|
|
|
|
/* 'X' 'Y' 'Z' '[' '\' ']' '^' '_' */
|
|
|
|
'\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
|
|
|
|
/* '`' 'a' 'b' 'c' 'd' 'e' 'f' 'g' */
|
|
|
|
'\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
|
|
|
|
/* 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' */
|
|
|
|
'\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
|
|
|
|
/* 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' */
|
|
|
|
'\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
|
|
|
|
/* 'x' 'y' 'z' '{' '|' '}' '~' */
|
|
|
|
'\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
|
|
|
|
'\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
|
|
|
|
'\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
|
|
|
|
'\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
|
|
|
|
'\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
|
|
|
|
'\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
|
|
|
|
'\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
|
|
|
|
'\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
|
|
|
|
'\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
|
|
|
|
'\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
|
|
|
|
'\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317',
|
|
|
|
'\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327',
|
|
|
|
'\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
|
|
|
|
'\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
|
|
|
|
'\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
|
|
|
|
'\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
|
|
|
|
'\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
>>> "You lose. You will need a translation table for your character set." <<<
|
|
|
|
#endif
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
#define MIN(a,b) (((a)>(b))?(b):(a))
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
int
|
2001-07-24 13:07:33 +04:00
|
|
|
rb_memcicmp(p1, p2, len)
|
1998-01-16 15:13:05 +03:00
|
|
|
char *p1, *p2;
|
2000-11-20 10:31:55 +03:00
|
|
|
long len;
|
2000-06-14 09:30:29 +04:00
|
|
|
{
|
|
|
|
int tmp;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-06-14 09:30:29 +04:00
|
|
|
while (len--) {
|
|
|
|
if (tmp = casetable[(unsigned)*p1++] - casetable[(unsigned)*p2++])
|
|
|
|
return tmp;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2000-06-14 09:30:29 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-07-24 13:07:33 +04:00
|
|
|
int
|
|
|
|
rb_memcmp(p1, p2, len)
|
|
|
|
char *p1, *p2;
|
|
|
|
long len;
|
|
|
|
{
|
|
|
|
int tmp;
|
|
|
|
|
|
|
|
if (!ruby_ignorecase) {
|
|
|
|
return memcmp(p1, p2, len);
|
|
|
|
}
|
|
|
|
return rb_memcicmp(p1, p2, len);
|
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
#define REG_CASESTATE FL_USER0
|
1998-01-16 15:13:05 +03:00
|
|
|
#define KCODE_NONE 0
|
2000-05-22 11:09:55 +04:00
|
|
|
#define KCODE_EUC FL_USER1
|
|
|
|
#define KCODE_SJIS FL_USER2
|
|
|
|
#define KCODE_UTF8 FL_USER3
|
|
|
|
#define KCODE_FIXED FL_USER4
|
1999-01-20 07:59:39 +03:00
|
|
|
#define KCODE_MASK (KCODE_EUC|KCODE_SJIS|KCODE_UTF8)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static int reg_kcode = DEFAULT_KCODE;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
static void
|
2000-05-22 11:09:55 +04:00
|
|
|
kcode_euc(re)
|
|
|
|
struct RRegexp *re;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-05-22 11:09:55 +04:00
|
|
|
FL_UNSET(re, KCODE_MASK);
|
|
|
|
FL_SET(re, KCODE_EUC);
|
|
|
|
FL_SET(re, KCODE_FIXED);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-05-22 11:09:55 +04:00
|
|
|
kcode_sjis(re)
|
|
|
|
struct RRegexp *re;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-05-22 11:09:55 +04:00
|
|
|
FL_UNSET(re, KCODE_MASK);
|
|
|
|
FL_SET(re, KCODE_SJIS);
|
|
|
|
FL_SET(re, KCODE_FIXED);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static void
|
2000-05-22 11:09:55 +04:00
|
|
|
kcode_utf8(re)
|
|
|
|
struct RRegexp *re;
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2000-05-22 11:09:55 +04:00
|
|
|
FL_UNSET(re, KCODE_MASK);
|
|
|
|
FL_SET(re, KCODE_UTF8);
|
|
|
|
FL_SET(re, KCODE_FIXED);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static void
|
2000-05-22 11:09:55 +04:00
|
|
|
kcode_none(re)
|
|
|
|
struct RRegexp *re;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-05-22 11:09:55 +04:00
|
|
|
FL_UNSET(re, KCODE_MASK);
|
|
|
|
FL_SET(re, KCODE_FIXED);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static int curr_kcode;
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static void
|
2000-05-22 11:09:55 +04:00
|
|
|
kcode_set_option(re)
|
|
|
|
VALUE re;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-05-22 11:09:55 +04:00
|
|
|
if (!FL_TEST(re, KCODE_FIXED)) return;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-05-22 11:09:55 +04:00
|
|
|
curr_kcode = RBASIC(re)->flags & KCODE_MASK;
|
1999-01-20 07:59:39 +03:00
|
|
|
if (reg_kcode == curr_kcode) return;
|
|
|
|
switch (curr_kcode) {
|
1998-01-16 15:13:05 +03:00
|
|
|
case KCODE_NONE:
|
1999-01-20 07:59:39 +03:00
|
|
|
re_mbcinit(MBCTYPE_ASCII);
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
case KCODE_EUC:
|
1999-01-20 07:59:39 +03:00
|
|
|
re_mbcinit(MBCTYPE_EUC);
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
case KCODE_SJIS:
|
1999-01-20 07:59:39 +03:00
|
|
|
re_mbcinit(MBCTYPE_SJIS);
|
|
|
|
break;
|
|
|
|
case KCODE_UTF8:
|
|
|
|
re_mbcinit(MBCTYPE_UTF8);
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static void
|
1998-01-16 15:13:05 +03:00
|
|
|
kcode_reset_option()
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
if (reg_kcode == curr_kcode) return;
|
1998-01-16 15:13:05 +03:00
|
|
|
switch (reg_kcode) {
|
|
|
|
case KCODE_NONE:
|
1999-01-20 07:59:39 +03:00
|
|
|
re_mbcinit(MBCTYPE_ASCII);
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
case KCODE_EUC:
|
1999-01-20 07:59:39 +03:00
|
|
|
re_mbcinit(MBCTYPE_EUC);
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
case KCODE_SJIS:
|
1999-01-20 07:59:39 +03:00
|
|
|
re_mbcinit(MBCTYPE_SJIS);
|
|
|
|
break;
|
|
|
|
case KCODE_UTF8:
|
|
|
|
re_mbcinit(MBCTYPE_UTF8);
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-11-04 11:39:57 +03:00
|
|
|
int
|
2000-02-08 11:54:01 +03:00
|
|
|
rb_reg_mbclen2(c, re)
|
2000-05-01 13:42:38 +04:00
|
|
|
unsigned int c;
|
1999-11-04 11:39:57 +03:00
|
|
|
VALUE re;
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (!FL_TEST(re, KCODE_FIXED))
|
|
|
|
return mbclen(c);
|
|
|
|
kcode_set_option(re);
|
|
|
|
len = mbclen(c);
|
|
|
|
kcode_reset_option();
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2000-05-22 11:09:55 +04:00
|
|
|
static void
|
|
|
|
rb_reg_check(re)
|
|
|
|
VALUE re;
|
|
|
|
{
|
|
|
|
if (!RREGEXP(re)->ptr || !RREGEXP(re)->str) {
|
|
|
|
rb_raise(rb_eTypeError, "uninitialized Regexp");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
extern int ruby_in_compile;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static void
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_reg_expr_str(str, s, len)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE str;
|
1999-08-13 09:45:20 +04:00
|
|
|
const char *s;
|
1998-01-16 15:13:05 +03:00
|
|
|
int len;
|
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
const char *p, *pend;
|
2000-05-30 08:24:17 +04:00
|
|
|
int need_escape = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
p = s; pend = p + len;
|
|
|
|
while (p<pend) {
|
2000-05-30 08:24:17 +04:00
|
|
|
if (*p == '/' || (!ISPRINT(*p) && !ismbchar(*p))) {
|
|
|
|
need_escape = 1;
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
2000-05-30 08:24:17 +04:00
|
|
|
if (!need_escape) {
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(str, s, len);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
p = s;
|
|
|
|
while (p<pend) {
|
|
|
|
if (*p == '/') {
|
|
|
|
char c = '\\';
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(str, &c, 1);
|
|
|
|
rb_str_buf_cat(str, p, 1);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2000-05-30 08:24:17 +04:00
|
|
|
else if (ismbchar(*p)) {
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(str, p, mbclen(*p));
|
2000-05-30 08:24:17 +04:00
|
|
|
p += mbclen(*p);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (ISPRINT(*p)) {
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(str, p, 1);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2000-05-30 08:24:17 +04:00
|
|
|
else {
|
|
|
|
char b[8];
|
|
|
|
switch (*p) {
|
|
|
|
case '\r':
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(str, "\\r", 2);
|
2000-05-30 08:24:17 +04:00
|
|
|
break;
|
|
|
|
case '\n':
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(str, "\\n", 2);
|
2000-05-30 08:24:17 +04:00
|
|
|
break;
|
|
|
|
case '\t':
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(str, "\\t", 2);
|
2000-05-30 08:24:17 +04:00
|
|
|
break;
|
|
|
|
case '\f':
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(str, "\\f", 2);
|
2000-05-30 08:24:17 +04:00
|
|
|
break;
|
|
|
|
case 007:
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(str, "\\a", 2);
|
2000-05-30 08:24:17 +04:00
|
|
|
break;
|
|
|
|
case 013:
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(str, "\\v", 2);
|
2000-05-30 08:24:17 +04:00
|
|
|
break;
|
|
|
|
case 033:
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(str, "\\e", 2);
|
2000-05-30 08:24:17 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sprintf(b, "\\%03o", *p & 0377);
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(str, b, 4);
|
2000-05-30 08:24:17 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_reg_desc(s, len, re)
|
1999-08-13 09:45:20 +04:00
|
|
|
const char *s;
|
1998-01-16 15:19:22 +03:00
|
|
|
int len;
|
|
|
|
VALUE re;
|
|
|
|
{
|
2001-05-30 13:12:34 +04:00
|
|
|
VALUE str = rb_str_buf_new2("/");
|
2000-05-22 11:09:55 +04:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_reg_expr_str(str, s, len);
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat2(str, "/");
|
1998-01-16 15:13:05 +03:00
|
|
|
if (re) {
|
2000-05-24 11:36:46 +04:00
|
|
|
rb_reg_check(re);
|
|
|
|
/* /p is obsolete; to be removed */
|
|
|
|
if ((RREGEXP(re)->ptr->options & RE_OPTION_POSIXLINE) == RE_OPTION_POSIXLINE)
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat2(str, "p");
|
2001-01-13 13:47:12 +03:00
|
|
|
else if (RREGEXP(re)->ptr->options & RE_OPTION_MULTILINE)
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat2(str, "m");
|
2000-05-22 11:09:55 +04:00
|
|
|
if (RREGEXP(re)->ptr->options & RE_OPTION_IGNORECASE)
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat2(str, "i");
|
2000-05-22 11:09:55 +04:00
|
|
|
if (RREGEXP(re)->ptr->options & RE_OPTION_EXTENDED)
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat2(str, "x");
|
2000-05-22 11:09:55 +04:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (FL_TEST(re, KCODE_FIXED)) {
|
1998-01-16 15:19:22 +03:00
|
|
|
switch ((RBASIC(re)->flags & KCODE_MASK)) {
|
|
|
|
case KCODE_NONE:
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat2(str, "n");
|
1998-01-16 15:19:22 +03:00
|
|
|
break;
|
|
|
|
case KCODE_EUC:
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat2(str, "e");
|
1998-01-16 15:19:22 +03:00
|
|
|
break;
|
|
|
|
case KCODE_SJIS:
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat2(str, "s");
|
1999-01-20 07:59:39 +03:00
|
|
|
break;
|
|
|
|
case KCODE_UTF8:
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat2(str, "u");
|
1998-01-16 15:19:22 +03:00
|
|
|
break;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
2000-02-23 08:23:12 +03:00
|
|
|
OBJ_INFECT(str, re);
|
1998-01-16 15:13:05 +03:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_reg_source(re)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE re;
|
|
|
|
{
|
2000-05-22 11:09:55 +04:00
|
|
|
VALUE str;
|
|
|
|
|
|
|
|
rb_reg_check(re);
|
|
|
|
str = rb_str_new(RREGEXP(re)->str,RREGEXP(re)->len);
|
1999-12-01 12:24:48 +03:00
|
|
|
if (OBJ_TAINTED(re)) OBJ_TAINT(str);
|
|
|
|
return str;
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_reg_inspect(re)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE re;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-05-22 11:09:55 +04:00
|
|
|
rb_reg_check(re);
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_reg_desc(RREGEXP(re)->str, RREGEXP(re)->len, re);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_reg_raise(s, len, err, re)
|
1999-08-13 09:45:20 +04:00
|
|
|
const char *s;
|
1998-01-16 15:13:05 +03:00
|
|
|
int len;
|
1999-08-13 09:45:20 +04:00
|
|
|
const char *err;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE re;
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE desc = rb_reg_desc(s, len, re);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (ruby_in_compile)
|
|
|
|
rb_compile_error("%s: %s", err, RSTRING(desc)->ptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
else
|
2000-08-28 13:53:42 +04:00
|
|
|
rb_raise(rb_eRegexpError, "%s: %s", err, RSTRING(desc)->ptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_reg_casefold_p(re)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE re;
|
|
|
|
{
|
2000-05-22 11:09:55 +04:00
|
|
|
rb_reg_check(re);
|
|
|
|
if (RREGEXP(re)->ptr->options & RE_OPTION_IGNORECASE) return Qtrue;
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qfalse;
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
|
|
|
|
2001-02-08 12:19:27 +03:00
|
|
|
static VALUE
|
|
|
|
rb_reg_options_m(re)
|
|
|
|
VALUE re;
|
|
|
|
{
|
|
|
|
rb_reg_check(re);
|
|
|
|
return INT2NUM(RREGEXP(re)->ptr->options);
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static VALUE
|
2000-01-05 07:41:21 +03:00
|
|
|
rb_reg_kcode_m(re)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE re;
|
|
|
|
{
|
2000-06-22 12:29:58 +04:00
|
|
|
char *kcode;
|
1998-01-16 15:19:22 +03:00
|
|
|
|
|
|
|
if (FL_TEST(re, KCODE_FIXED)) {
|
|
|
|
switch (RBASIC(re)->flags & KCODE_MASK) {
|
|
|
|
case KCODE_NONE:
|
|
|
|
kcode = "none"; break;
|
|
|
|
case KCODE_EUC:
|
|
|
|
kcode = "euc"; break;
|
|
|
|
case KCODE_SJIS:
|
|
|
|
kcode = "sjis"; break;
|
1999-01-20 07:59:39 +03:00
|
|
|
case KCODE_UTF8:
|
|
|
|
kcode = "utf8"; break;
|
1998-01-16 15:19:22 +03:00
|
|
|
default:
|
2000-06-22 12:29:58 +04:00
|
|
|
rb_bug("unknow kcode - should not happen");
|
1998-01-16 15:19:22 +03:00
|
|
|
break;
|
|
|
|
}
|
2000-06-22 12:29:58 +04:00
|
|
|
return rb_str_new2(kcode);
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
2000-06-22 12:29:58 +04:00
|
|
|
return Qnil;
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static Regexp*
|
2001-02-02 14:38:20 +03:00
|
|
|
make_regexp(s, len, flags)
|
1999-08-13 09:45:20 +04:00
|
|
|
const char *s;
|
2001-02-02 14:38:20 +03:00
|
|
|
int len, flags;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
Regexp *rp;
|
|
|
|
char *err;
|
|
|
|
|
|
|
|
/* Handle escaped characters first. */
|
|
|
|
|
|
|
|
/* Build a copy of the string (in dest) with the
|
|
|
|
escaped characters translated, and generate the regex
|
|
|
|
from that.
|
|
|
|
*/
|
|
|
|
|
|
|
|
rp = ALLOC(Regexp);
|
|
|
|
MEMZERO((char *)rp, Regexp, 1);
|
|
|
|
rp->buffer = ALLOC_N(char, 16);
|
|
|
|
rp->allocated = 16;
|
|
|
|
rp->fastmap = ALLOC_N(char, 256);
|
2001-02-02 14:38:20 +03:00
|
|
|
if (flags) {
|
|
|
|
rp->options = flags;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
err = re_compile_pattern(s, len, rp);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (err != NULL) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_reg_raise(s, len, err, 0);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return rp;
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE rb_cMatch;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
static VALUE
|
|
|
|
match_alloc()
|
|
|
|
{
|
|
|
|
NEWOBJ(match, struct RMatch);
|
1999-01-20 07:59:39 +03:00
|
|
|
OBJSETUP(match, rb_cMatch, T_MATCH);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
match->str = 0;
|
1999-01-20 07:59:39 +03:00
|
|
|
match->regs = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
match->regs = ALLOC(struct re_registers);
|
|
|
|
MEMZERO(match->regs, struct re_registers, 1);
|
|
|
|
|
|
|
|
return (VALUE)match;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static VALUE
|
2000-01-05 07:41:21 +03:00
|
|
|
match_clone(match)
|
|
|
|
VALUE match;
|
1998-01-16 15:19:22 +03:00
|
|
|
{
|
2000-01-05 07:41:21 +03:00
|
|
|
NEWOBJ(clone, struct RMatch);
|
2000-05-22 11:09:55 +04:00
|
|
|
CLONESETUP(clone, match);
|
1998-01-16 15:19:22 +03:00
|
|
|
|
2000-01-05 07:41:21 +03:00
|
|
|
clone->str = RMATCH(match)->str;
|
|
|
|
clone->regs = 0;
|
1998-01-16 15:19:22 +03:00
|
|
|
|
2000-01-05 07:41:21 +03:00
|
|
|
clone->regs = ALLOC(struct re_registers);
|
|
|
|
clone->regs->allocated = 0;
|
|
|
|
re_copy_registers(clone->regs, RMATCH(match)->regs);
|
1998-01-16 15:19:22 +03:00
|
|
|
|
2000-01-05 07:41:21 +03:00
|
|
|
return (VALUE)clone;
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static VALUE
|
|
|
|
match_size(match)
|
|
|
|
VALUE match;
|
|
|
|
{
|
|
|
|
return INT2FIX(RMATCH(match)->regs->num_regs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
match_offset(match, n)
|
|
|
|
VALUE match, n;
|
|
|
|
{
|
|
|
|
int i = NUM2INT(n);
|
|
|
|
|
|
|
|
if (i < 0 || RMATCH(match)->regs->num_regs <= i)
|
|
|
|
rb_raise(rb_eIndexError, "index %d out of matches", i);
|
|
|
|
|
|
|
|
if (RMATCH(match)->regs->beg[i] < 0)
|
|
|
|
return rb_assoc_new(Qnil, Qnil);
|
|
|
|
|
|
|
|
return rb_assoc_new(INT2FIX(RMATCH(match)->regs->beg[i]),
|
|
|
|
INT2FIX(RMATCH(match)->regs->end[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
match_begin(match, n)
|
|
|
|
VALUE match, n;
|
|
|
|
{
|
|
|
|
int i = NUM2INT(n);
|
|
|
|
|
|
|
|
if (i < 0 || RMATCH(match)->regs->num_regs <= i)
|
|
|
|
rb_raise(rb_eIndexError, "index %d out of matches", i);
|
|
|
|
|
|
|
|
if (RMATCH(match)->regs->beg[i] < 0)
|
|
|
|
return Qnil;
|
|
|
|
|
|
|
|
return INT2FIX(RMATCH(match)->regs->beg[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
match_end(match, n)
|
|
|
|
VALUE match, n;
|
|
|
|
{
|
|
|
|
int i = NUM2INT(n);
|
|
|
|
|
|
|
|
if (i < 0 || RMATCH(match)->regs->num_regs <= i)
|
|
|
|
rb_raise(rb_eIndexError, "index %d out of matches", i);
|
|
|
|
|
|
|
|
if (RMATCH(match)->regs->beg[i] < 0)
|
|
|
|
return Qnil;
|
|
|
|
|
|
|
|
return INT2FIX(RMATCH(match)->regs->end[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MATCH_BUSY FL_USER2
|
|
|
|
|
|
|
|
void
|
2000-02-08 11:54:01 +03:00
|
|
|
rb_match_busy(match)
|
1999-08-13 09:45:20 +04:00
|
|
|
VALUE match;
|
|
|
|
{
|
2000-02-08 11:54:01 +03:00
|
|
|
FL_SET(match, MATCH_BUSY);
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
int ruby_ignorecase;
|
|
|
|
static int may_need_recompile;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static void
|
2000-05-22 11:09:55 +04:00
|
|
|
rb_reg_prepare_re(re)
|
|
|
|
VALUE re;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
int need_recompile = 0;
|
2000-06-13 13:42:40 +04:00
|
|
|
int state;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-05-22 11:09:55 +04:00
|
|
|
rb_reg_check(re);
|
2000-06-13 13:42:40 +04:00
|
|
|
state = FL_TEST(re, REG_CASESTATE);
|
|
|
|
/* ignorecase status */
|
|
|
|
if (ruby_ignorecase && !state) {
|
|
|
|
FL_SET(re, REG_CASESTATE);
|
|
|
|
RREGEXP(re)->ptr->options |= RE_OPTION_IGNORECASE;
|
|
|
|
need_recompile = 1;
|
|
|
|
}
|
|
|
|
if (!ruby_ignorecase && state) {
|
|
|
|
FL_UNSET(re, REG_CASESTATE);
|
|
|
|
RREGEXP(re)->ptr->options &= ~RE_OPTION_IGNORECASE;
|
|
|
|
need_recompile = 1;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2000-05-22 11:09:55 +04:00
|
|
|
if (!FL_TEST(re, KCODE_FIXED) &&
|
|
|
|
(RBASIC(re)->flags & KCODE_MASK) != reg_kcode) {
|
1998-01-16 15:13:05 +03:00
|
|
|
need_recompile = 1;
|
2000-05-22 11:09:55 +04:00
|
|
|
RBASIC(re)->flags &= ~KCODE_MASK;
|
|
|
|
RBASIC(re)->flags |= reg_kcode;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (need_recompile) {
|
|
|
|
char *err;
|
|
|
|
|
2000-05-22 11:09:55 +04:00
|
|
|
if (FL_TEST(re, KCODE_FIXED))
|
|
|
|
kcode_set_option(re);
|
|
|
|
rb_reg_check(re);
|
|
|
|
RREGEXP(re)->ptr->fastmap_accurate = 0;
|
|
|
|
err = re_compile_pattern(RREGEXP(re)->str, RREGEXP(re)->len, RREGEXP(re)->ptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (err != NULL) {
|
2000-05-22 11:09:55 +04:00
|
|
|
rb_reg_raise(RREGEXP(re)->str, RREGEXP(re)->len, err, re);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
int
|
2000-05-22 11:09:55 +04:00
|
|
|
rb_reg_adjust_startpos(re, str, pos, reverse)
|
|
|
|
VALUE re, str;
|
1999-12-14 09:50:43 +03:00
|
|
|
int pos, reverse;
|
|
|
|
{
|
|
|
|
int range;
|
|
|
|
|
2000-05-22 11:09:55 +04:00
|
|
|
rb_reg_check(re);
|
|
|
|
if (may_need_recompile) rb_reg_prepare_re(re);
|
1999-12-14 09:50:43 +03:00
|
|
|
|
2000-05-22 11:09:55 +04:00
|
|
|
if (FL_TEST(re, KCODE_FIXED))
|
|
|
|
kcode_set_option(re);
|
1999-12-14 09:50:43 +03:00
|
|
|
else if (reg_kcode != curr_kcode)
|
|
|
|
kcode_reset_option();
|
|
|
|
|
|
|
|
if (reverse) {
|
|
|
|
range = -pos;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
range = RSTRING(str)->len - pos;
|
|
|
|
}
|
2000-05-22 11:09:55 +04:00
|
|
|
return re_adjust_startpos(RREGEXP(re)->ptr,
|
1999-12-14 09:50:43 +03:00
|
|
|
RSTRING(str)->ptr, RSTRING(str)->len,
|
|
|
|
pos, range);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-05-22 11:09:55 +04:00
|
|
|
rb_reg_search(re, str, pos, reverse)
|
|
|
|
VALUE re, str;
|
1999-08-13 09:45:20 +04:00
|
|
|
int pos, reverse;
|
1998-01-16 15:19:22 +03:00
|
|
|
{
|
|
|
|
int result;
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE match;
|
2000-02-08 11:54:01 +03:00
|
|
|
static struct re_registers regs;
|
1999-01-20 07:59:39 +03:00
|
|
|
int range;
|
1998-01-16 15:19:22 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
if (pos > RSTRING(str)->len) return -1;
|
1998-01-16 15:19:22 +03:00
|
|
|
|
2000-05-22 11:09:55 +04:00
|
|
|
rb_reg_check(re);
|
|
|
|
if (may_need_recompile) rb_reg_prepare_re(re);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2000-05-22 11:09:55 +04:00
|
|
|
if (FL_TEST(re, KCODE_FIXED))
|
|
|
|
kcode_set_option(re);
|
1999-01-20 07:59:39 +03:00
|
|
|
else if (reg_kcode != curr_kcode)
|
|
|
|
kcode_reset_option();
|
1998-01-16 15:19:22 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (reverse) {
|
1999-08-13 09:45:20 +04:00
|
|
|
range = -pos;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
else {
|
1999-08-13 09:45:20 +04:00
|
|
|
range = RSTRING(str)->len - pos;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2000-05-22 11:09:55 +04:00
|
|
|
result = re_search(RREGEXP(re)->ptr,RSTRING(str)->ptr,RSTRING(str)->len,
|
2000-02-08 11:54:01 +03:00
|
|
|
pos, range, ®s);
|
1999-11-04 11:39:57 +03:00
|
|
|
|
2000-05-22 11:09:55 +04:00
|
|
|
if (FL_TEST(re, KCODE_FIXED))
|
1999-08-13 09:45:20 +04:00
|
|
|
kcode_reset_option();
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (result == -2) {
|
2000-05-22 11:09:55 +04:00
|
|
|
rb_reg_raise(RREGEXP(re)->str, RREGEXP(re)->len,
|
2000-06-12 11:48:31 +04:00
|
|
|
"Stack overflow in regexp matcher", re);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2000-02-08 11:54:01 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
if (result < 0) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_backref_set(Qnil);
|
2000-02-08 11:54:01 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rb_thread_scope_shared_p()) {
|
|
|
|
match = Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
else {
|
2000-02-08 11:54:01 +03:00
|
|
|
match = rb_backref_get();
|
|
|
|
}
|
|
|
|
if (NIL_P(match) || FL_TEST(match, MATCH_BUSY)) {
|
|
|
|
match = match_alloc();
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2000-02-08 11:54:01 +03:00
|
|
|
re_copy_registers(RMATCH(match)->regs, ®s);
|
|
|
|
RMATCH(match)->str = rb_str_new4(str);
|
|
|
|
rb_backref_set(match);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-05-22 11:09:55 +04:00
|
|
|
OBJ_INFECT(match, re);
|
2000-01-05 07:41:21 +03:00
|
|
|
OBJ_INFECT(match, str);
|
1998-01-16 15:13:05 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_reg_nth_defined(nth, match)
|
1998-01-16 15:13:05 +03:00
|
|
|
int nth;
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE match;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
if (NIL_P(match)) return Qnil;
|
1998-01-16 15:19:22 +03:00
|
|
|
if (nth >= RMATCH(match)->regs->num_regs) {
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
if (RMATCH(match)->BEG(nth) == -1) return Qfalse;
|
|
|
|
return Qtrue;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_reg_nth_match(nth, match)
|
1998-01-16 15:13:05 +03:00
|
|
|
int nth;
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE match;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-01-05 07:41:21 +03:00
|
|
|
VALUE str;
|
1998-01-16 15:13:05 +03:00
|
|
|
int start, end, len;
|
|
|
|
|
|
|
|
if (NIL_P(match)) return Qnil;
|
1998-01-16 15:19:22 +03:00
|
|
|
if (nth >= RMATCH(match)->regs->num_regs) {
|
1998-01-16 15:13:05 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
start = RMATCH(match)->BEG(nth);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (start == -1) return Qnil;
|
1998-01-16 15:19:22 +03:00
|
|
|
end = RMATCH(match)->END(nth);
|
1998-01-16 15:13:05 +03:00
|
|
|
len = end - start;
|
2000-01-05 07:41:21 +03:00
|
|
|
str = rb_str_new(RSTRING(RMATCH(match)->str)->ptr + start, len);
|
|
|
|
if (OBJ_TAINTED(match)) OBJ_TAINT(str);
|
|
|
|
return str;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_reg_last_match(match)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE match;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_reg_nth_match(0, match);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_reg_match_pre(match)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE match;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
if (NIL_P(match)) return Qnil;
|
1998-01-16 15:19:22 +03:00
|
|
|
if (RMATCH(match)->BEG(0) == -1) return Qnil;
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_str_new(RSTRING(RMATCH(match)->str)->ptr, RMATCH(match)->BEG(0));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_reg_match_post(match)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE match;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
if (NIL_P(match)) return Qnil;
|
1998-01-16 15:19:22 +03:00
|
|
|
if (RMATCH(match)->BEG(0) == -1) return Qnil;
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_str_new(RSTRING(RMATCH(match)->str)->ptr+RMATCH(match)->END(0),
|
1999-08-13 09:45:20 +04:00
|
|
|
RSTRING(RMATCH(match)->str)->len-RMATCH(match)->END(0));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_reg_match_last(match)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE match;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (NIL_P(match)) return Qnil;
|
1998-01-16 15:19:22 +03:00
|
|
|
if (RMATCH(match)->BEG(0) == -1) return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
for (i=RMATCH(match)->regs->num_regs-1; RMATCH(match)->BEG(i) == -1 && i > 0; i--)
|
1998-01-16 15:13:05 +03:00
|
|
|
;
|
|
|
|
if (i == 0) return Qnil;
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_reg_nth_match(i, match);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
last_match_getter()
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_reg_last_match(rb_backref_get());
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
prematch_getter()
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_reg_match_pre(rb_backref_get());
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
postmatch_getter()
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_reg_match_post(rb_backref_get());
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
last_paren_match_getter()
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_reg_match_last(rb_backref_get());
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
match_to_a(match)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE match;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1998-01-16 15:19:22 +03:00
|
|
|
struct re_registers *regs = RMATCH(match)->regs;
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE ary = rb_ary_new2(regs->num_regs);
|
1998-01-16 15:19:22 +03:00
|
|
|
char *ptr = RSTRING(RMATCH(match)->str)->ptr;
|
1998-01-16 15:13:05 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i<regs->num_regs; i++) {
|
1999-01-20 07:59:39 +03:00
|
|
|
if (regs->beg[i] == -1) rb_ary_push(ary, Qnil);
|
|
|
|
else rb_ary_push(ary, rb_str_new(ptr+regs->beg[i],
|
1998-01-16 15:13:05 +03:00
|
|
|
regs->end[i]-regs->beg[i]));
|
|
|
|
}
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static VALUE
|
|
|
|
match_aref(argc, argv, match)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE match;
|
|
|
|
{
|
|
|
|
VALUE idx, rest;
|
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &idx, &rest);
|
|
|
|
|
|
|
|
if (!NIL_P(rest) || !FIXNUM_P(idx) || FIX2INT(idx) < 0) {
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_ary_aref(argc, argv, match_to_a(match));
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
2000-09-01 13:18:11 +04:00
|
|
|
return rb_reg_nth_match(FIX2INT(idx), match);
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
|
|
|
match_to_s(match)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE match;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE str = rb_reg_last_match(match);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-12-01 12:24:48 +03:00
|
|
|
if (NIL_P(str)) str = rb_str_new(0,0);
|
|
|
|
if (OBJ_TAINTED(match)) OBJ_TAINT(str);
|
2000-01-05 07:41:21 +03:00
|
|
|
if (OBJ_TAINTED(RMATCH(match)->str)) OBJ_TAINT(str);
|
1998-01-16 15:13:05 +03:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static VALUE
|
|
|
|
match_string(match)
|
|
|
|
VALUE match;
|
|
|
|
{
|
1999-10-15 12:52:18 +04:00
|
|
|
return RMATCH(match)->str; /* str is frozen */
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE rb_cRegexp;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-05-22 11:09:55 +04:00
|
|
|
static void
|
|
|
|
rb_reg_initialize(obj, s, len, options)
|
|
|
|
VALUE obj;
|
1999-08-13 09:45:20 +04:00
|
|
|
const char *s;
|
|
|
|
int len;
|
1999-01-20 07:59:39 +03:00
|
|
|
int options; /* CASEFOLD = 1 */
|
|
|
|
/* EXTENDED = 2 */
|
2000-05-17 10:33:50 +04:00
|
|
|
/* MULTILINE = 4 */
|
2000-05-23 19:36:43 +04:00
|
|
|
/* CODE_NONE = 16 */
|
|
|
|
/* CODE_EUC = 32 */
|
|
|
|
/* CODE_SJIS = 48 */
|
|
|
|
/* CODE_UTF8 = 64 */
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-05-22 11:09:55 +04:00
|
|
|
struct RRegexp *re = RREGEXP(obj);
|
|
|
|
|
|
|
|
if (re->ptr) re_free_pattern(re->ptr);
|
2000-07-01 10:47:47 +04:00
|
|
|
if (re->str) free(re->str);
|
1999-01-20 07:59:39 +03:00
|
|
|
re->ptr = 0;
|
|
|
|
re->str = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-05-23 19:36:43 +04:00
|
|
|
switch (options & ~0xf) {
|
1998-01-16 15:13:05 +03:00
|
|
|
case 0:
|
|
|
|
default:
|
|
|
|
FL_SET(re, reg_kcode);
|
|
|
|
break;
|
2000-05-22 11:29:50 +04:00
|
|
|
case 16:
|
1998-01-16 15:13:05 +03:00
|
|
|
kcode_none(re);
|
|
|
|
break;
|
2000-05-22 11:29:50 +04:00
|
|
|
case 32:
|
1998-01-16 15:13:05 +03:00
|
|
|
kcode_euc(re);
|
|
|
|
break;
|
2000-05-22 11:29:50 +04:00
|
|
|
case 48:
|
1998-01-16 15:13:05 +03:00
|
|
|
kcode_sjis(re);
|
|
|
|
break;
|
2000-05-22 11:29:50 +04:00
|
|
|
case 64:
|
1999-01-20 07:59:39 +03:00
|
|
|
kcode_utf8(re);
|
|
|
|
break;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2000-05-22 11:29:50 +04:00
|
|
|
if (options & ~0xf) {
|
1999-01-20 07:59:39 +03:00
|
|
|
kcode_set_option((VALUE)re);
|
|
|
|
}
|
|
|
|
if (ruby_ignorecase) {
|
|
|
|
options |= RE_OPTION_IGNORECASE;
|
|
|
|
FL_SET(re, REG_CASESTATE);
|
|
|
|
}
|
2000-05-22 11:29:50 +04:00
|
|
|
re->ptr = make_regexp(s, len, options & 0xf);
|
1998-01-16 15:13:05 +03:00
|
|
|
re->str = ALLOC_N(char, len+1);
|
|
|
|
memcpy(re->str, s, len);
|
|
|
|
re->str[len] = '\0';
|
|
|
|
re->len = len;
|
2000-05-22 11:29:50 +04:00
|
|
|
if (options & ~0xf) {
|
1999-01-20 07:59:39 +03:00
|
|
|
kcode_reset_option();
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_reg_new(s, len, options)
|
1999-08-13 09:45:20 +04:00
|
|
|
const char *s;
|
|
|
|
long len;
|
1999-01-20 07:59:39 +03:00
|
|
|
int options;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2000-05-22 11:09:55 +04:00
|
|
|
NEWOBJ(re, struct RRegexp);
|
|
|
|
OBJSETUP(re, rb_cRegexp, T_REGEXP);
|
|
|
|
|
2000-07-01 10:47:47 +04:00
|
|
|
re->ptr = 0; re->len = 0; re->str = 0;
|
2000-05-22 11:09:55 +04:00
|
|
|
rb_reg_initialize(re, s, len, options);
|
|
|
|
return (VALUE)re;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static int case_cache;
|
|
|
|
static int kcode_cache;
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE reg_cache;
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_reg_regcomp(str)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE str;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1998-01-16 15:19:22 +03:00
|
|
|
if (reg_cache && RREGEXP(reg_cache)->len == RSTRING(str)->len
|
1999-01-20 07:59:39 +03:00
|
|
|
&& case_cache == ruby_ignorecase
|
|
|
|
&& kcode_cache == reg_kcode
|
1998-01-16 15:19:22 +03:00
|
|
|
&& memcmp(RREGEXP(reg_cache)->str, RSTRING(str)->ptr, RSTRING(str)->len) == 0)
|
1998-01-16 15:13:05 +03:00
|
|
|
return reg_cache;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
case_cache = ruby_ignorecase;
|
|
|
|
kcode_cache = reg_kcode;
|
|
|
|
return reg_cache = rb_reg_new(RSTRING(str)->ptr, RSTRING(str)->len,
|
|
|
|
ruby_ignorecase);
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_reg_cur_kcode(re)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE re;
|
|
|
|
{
|
|
|
|
if (FL_TEST(re, KCODE_FIXED)) {
|
|
|
|
return RBASIC(re)->flags & KCODE_MASK;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_reg_equal(re1, re2)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE re1, re2;
|
|
|
|
{
|
|
|
|
int min;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (re1 == re2) return Qtrue;
|
|
|
|
if (TYPE(re2) != T_REGEXP) return Qfalse;
|
2000-05-22 11:09:55 +04:00
|
|
|
rb_reg_check(re1); rb_reg_check(re2);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (RREGEXP(re1)->len != RREGEXP(re2)->len) return Qfalse;
|
1998-01-16 15:19:22 +03:00
|
|
|
min = RREGEXP(re1)->len;
|
|
|
|
if (min > RREGEXP(re2)->len) min = RREGEXP(re2)->len;
|
|
|
|
if (memcmp(RREGEXP(re1)->str, RREGEXP(re2)->str, min) == 0 &&
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_reg_cur_kcode(re1) == rb_reg_cur_kcode(re2) &&
|
2001-02-08 12:19:27 +03:00
|
|
|
RREGEXP(re1)->ptr->options == RREGEXP(re2)->ptr->options) {
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qtrue;
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_reg_match(re, str)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE re, str;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
int start;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if (NIL_P(str)) return Qnil;
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(str);
|
1999-01-20 07:59:39 +03:00
|
|
|
start = rb_reg_search(re, str, 0, 0);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (start < 0) {
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
return INT2FIX(start);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_reg_match2(re)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE re;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
int start;
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE line = rb_lastline_get();
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (TYPE(line) != T_STRING)
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
start = rb_reg_search(re, line, 0, 0);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (start < 0) {
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
return INT2FIX(start);
|
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static VALUE
|
2000-01-05 07:41:21 +03:00
|
|
|
rb_reg_match_m(re, str)
|
1999-08-13 09:45:20 +04:00
|
|
|
VALUE re, str;
|
|
|
|
{
|
|
|
|
VALUE result = rb_reg_match(re, str);
|
|
|
|
|
|
|
|
if (NIL_P(result)) return Qnil;
|
2000-07-14 11:18:58 +04:00
|
|
|
result = rb_backref_get();
|
|
|
|
rb_match_busy(result);
|
|
|
|
return result;
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2000-05-22 11:09:55 +04:00
|
|
|
rb_reg_initialize_m(argc, argv, self)
|
1998-01-16 15:13:05 +03:00
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE self;
|
|
|
|
{
|
|
|
|
VALUE src;
|
2001-02-02 14:38:20 +03:00
|
|
|
int flags = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (argc == 0 || argc > 3) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eArgError, "wrong # of argument");
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
if (argc >= 2) {
|
2001-02-02 14:38:20 +03:00
|
|
|
if (FIXNUM_P(argv[1])) flags = FIX2INT(argv[1]);
|
|
|
|
else if (RTEST(argv[1])) flags = RE_OPTION_IGNORECASE;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
if (argc == 3) {
|
2001-05-02 08:22:21 +04:00
|
|
|
char *kcode = StringValuePtr(argv[2]);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
switch (kcode[0]) {
|
1998-01-16 15:13:05 +03:00
|
|
|
case 'n': case 'N':
|
2001-02-02 14:38:20 +03:00
|
|
|
flags |= 16;
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
case 'e': case 'E':
|
2001-02-02 14:38:20 +03:00
|
|
|
flags |= 32;
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
case 's': case 'S':
|
2001-02-02 14:38:20 +03:00
|
|
|
flags |= 48;
|
1999-08-13 09:45:20 +04:00
|
|
|
break;
|
|
|
|
case 'u': case 'U':
|
2001-02-02 14:38:20 +03:00
|
|
|
flags |= 64;
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
src = argv[0];
|
1999-01-20 07:59:39 +03:00
|
|
|
if (TYPE(src) == T_REGEXP) {
|
2000-05-22 11:09:55 +04:00
|
|
|
rb_reg_check(src);
|
2001-02-02 14:38:20 +03:00
|
|
|
rb_reg_initialize(self, RREGEXP(src)->str, RREGEXP(src)->len, flags);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
else {
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(src);
|
|
|
|
rb_reg_initialize(self, RSTRING(src)->ptr, RSTRING(src)->len, flags);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2001-01-15 10:01:00 +03:00
|
|
|
return self;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2000-05-22 11:09:55 +04:00
|
|
|
static VALUE
|
|
|
|
rb_reg_s_new(argc, argv, klass)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE klass;
|
|
|
|
{
|
|
|
|
NEWOBJ(re, struct RRegexp);
|
|
|
|
OBJSETUP(re, klass, T_REGEXP);
|
2000-07-01 10:47:47 +04:00
|
|
|
re->ptr = 0; re->len = 0; re->str = 0;
|
2000-05-22 11:09:55 +04:00
|
|
|
rb_obj_call_init((VALUE)re, argc, argv);
|
|
|
|
return (VALUE)re;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_reg_s_quote(argc, argv)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
VALUE str, kcode;
|
|
|
|
int kcode_saved = reg_kcode;
|
|
|
|
char *s, *send, *t;
|
2000-10-10 11:03:36 +04:00
|
|
|
VALUE tmp;
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
rb_scan_args(argc, argv, "11", &str, &kcode);
|
|
|
|
if (!NIL_P(kcode)) {
|
2001-05-02 08:22:21 +04:00
|
|
|
rb_set_kcode(StringValuePtr(kcode));
|
1999-08-13 09:45:20 +04:00
|
|
|
curr_kcode = reg_kcode;
|
|
|
|
reg_kcode = kcode_saved;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(str);
|
|
|
|
s = RSTRING(str)->ptr;
|
|
|
|
send = s + RSTRING(str)->len;
|
|
|
|
tmp = rb_str_new(0, RSTRING(str)->len*2);
|
2000-10-10 11:03:36 +04:00
|
|
|
t = RSTRING(tmp)->ptr;
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2000-07-01 10:47:47 +04:00
|
|
|
for (; s < send; s++) {
|
1999-08-13 09:45:20 +04:00
|
|
|
if (ismbchar(*s)) {
|
2000-11-20 10:31:55 +03:00
|
|
|
int n = mbclen(*s);
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2000-07-01 10:47:47 +04:00
|
|
|
while (n-- && s < send)
|
1999-08-13 09:45:20 +04:00
|
|
|
*t++ = *s++;
|
|
|
|
s--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*s == '[' || *s == ']'
|
|
|
|
|| *s == '{' || *s == '}'
|
|
|
|
|| *s == '(' || *s == ')'
|
1999-09-16 13:40:33 +04:00
|
|
|
|| *s == '|' || *s == '-'
|
1999-08-13 09:45:20 +04:00
|
|
|
|| *s == '*' || *s == '.' || *s == '\\'
|
|
|
|
|| *s == '?' || *s == '+'
|
|
|
|
|| *s == '^' || *s == '$') {
|
|
|
|
*t++ = '\\';
|
|
|
|
}
|
|
|
|
*t++ = *s;
|
|
|
|
}
|
|
|
|
kcode_reset_option();
|
2000-10-10 11:03:36 +04:00
|
|
|
rb_str_resize(tmp, t - RSTRING(tmp)->ptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-10-10 11:03:36 +04:00
|
|
|
return tmp;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_kcode()
|
|
|
|
{
|
|
|
|
switch (reg_kcode) {
|
|
|
|
case KCODE_EUC:
|
|
|
|
return MBCTYPE_EUC;
|
|
|
|
case KCODE_SJIS:
|
|
|
|
return MBCTYPE_SJIS;
|
1999-08-13 09:45:20 +04:00
|
|
|
case KCODE_UTF8:
|
|
|
|
return MBCTYPE_UTF8;
|
1999-01-20 07:59:39 +03:00
|
|
|
case KCODE_NONE:
|
|
|
|
return MBCTYPE_ASCII;
|
|
|
|
}
|
|
|
|
rb_bug("wrong reg_kcode value (0x%x)", reg_kcode);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static int
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_reg_get_kcode(re)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE re;
|
|
|
|
{
|
|
|
|
switch (RBASIC(re)->flags & KCODE_MASK) {
|
|
|
|
case KCODE_NONE:
|
2000-05-24 08:34:26 +04:00
|
|
|
return 16;
|
1999-01-20 07:59:39 +03:00
|
|
|
case KCODE_EUC:
|
2000-05-24 08:34:26 +04:00
|
|
|
return 32;
|
1998-01-16 15:19:22 +03:00
|
|
|
case KCODE_SJIS:
|
2000-05-24 08:34:26 +04:00
|
|
|
return 48;
|
1999-08-13 09:45:20 +04:00
|
|
|
case KCODE_UTF8:
|
2000-05-24 08:34:26 +04:00
|
|
|
return 64;
|
1998-01-16 15:19:22 +03:00
|
|
|
default:
|
2000-05-24 08:34:26 +04:00
|
|
|
return 0;
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
int
|
|
|
|
rb_reg_options(re)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE re;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
int options = 0;
|
1998-01-16 15:19:22 +03:00
|
|
|
|
2000-05-22 11:09:55 +04:00
|
|
|
rb_reg_check(re);
|
|
|
|
if (RREGEXP(re)->ptr->options & RE_OPTION_IGNORECASE)
|
1999-01-20 07:59:39 +03:00
|
|
|
options |= RE_OPTION_IGNORECASE;
|
2001-01-13 13:47:12 +03:00
|
|
|
if ((RREGEXP(re)->ptr->options & RE_OPTION_POSIXLINE) == RE_OPTION_POSIXLINE)
|
|
|
|
options |= RE_OPTION_POSIXLINE;
|
|
|
|
else if (RREGEXP(re)->ptr->options & RE_OPTION_MULTILINE)
|
|
|
|
options |= RE_OPTION_MULTILINE;
|
|
|
|
if (RREGEXP(re)->ptr->options & RE_OPTION_EXTENDED)
|
|
|
|
options |= RE_OPTION_EXTENDED;
|
1998-01-16 15:13:05 +03:00
|
|
|
if (FL_TEST(re, KCODE_FIXED)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
options |= rb_reg_get_kcode(re);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2000-05-22 11:09:55 +04:00
|
|
|
rb_reg_clone(re)
|
|
|
|
VALUE re;
|
2000-01-05 07:41:21 +03:00
|
|
|
{
|
2000-05-22 11:09:55 +04:00
|
|
|
NEWOBJ(clone, struct RRegexp);
|
|
|
|
CLONESETUP(clone, re);
|
|
|
|
rb_reg_check(re);
|
2000-07-01 10:47:47 +04:00
|
|
|
clone->ptr = 0; clone->len = 0; clone->str = 0;
|
2000-05-22 11:09:55 +04:00
|
|
|
rb_reg_initialize(clone, RREGEXP(re)->str, RREGEXP(re)->len,
|
|
|
|
rb_reg_options(re));
|
|
|
|
return (VALUE)re;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_reg_regsub(str, src, regs)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE str, src;
|
1998-01-16 15:13:05 +03:00
|
|
|
struct re_registers *regs;
|
|
|
|
{
|
|
|
|
VALUE val = 0;
|
|
|
|
char *p, *s, *e, c;
|
|
|
|
int no;
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
p = s = RSTRING(str)->ptr;
|
|
|
|
e = s + RSTRING(str)->len;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
while (s < e) {
|
|
|
|
char *ss = s;
|
|
|
|
|
|
|
|
c = *s++;
|
1999-01-20 07:59:39 +03:00
|
|
|
if (ismbchar(c)) {
|
1999-08-13 09:45:20 +04:00
|
|
|
s += mbclen(c) - 1;
|
1999-01-20 07:59:39 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c != '\\' || s == e) continue;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-05-30 13:12:34 +04:00
|
|
|
if (!val) {
|
|
|
|
val = rb_str_buf_new(ss-p);
|
|
|
|
rb_str_buf_cat(val, p, ss-p);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_str_buf_cat(val, p, ss-p);
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
c = *s++;
|
|
|
|
p = s;
|
|
|
|
switch (c) {
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
|
|
|
no = c - '0';
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
no = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '`':
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(val, RSTRING(src)->ptr, BEG(0));
|
1998-01-16 15:13:05 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case '\'':
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(val, RSTRING(src)->ptr+END(0), RSTRING(src)->len-END(0));
|
1998-01-16 15:13:05 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case '+':
|
|
|
|
no = regs->num_regs-1;
|
|
|
|
while (BEG(no) == -1 && no > 0) no--;
|
|
|
|
if (no == 0) continue;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\\':
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(val, s-1, 1);
|
1998-01-16 15:13:05 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
default:
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(val, s-2, 2);
|
1998-01-16 15:13:05 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (no >= 0) {
|
2000-02-08 11:54:01 +03:00
|
|
|
if (no >= regs->num_regs) continue;
|
1998-01-16 15:13:05 +03:00
|
|
|
if (BEG(no) == -1) continue;
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(val, RSTRING(src)->ptr+BEG(no), END(no)-BEG(no));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p < e) {
|
2001-05-30 13:12:34 +04:00
|
|
|
if (!val) {
|
|
|
|
val = rb_str_buf_new(e-p);
|
|
|
|
rb_str_buf_cat(val, p, e-p);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_str_buf_cat(val, p, e-p);
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
if (!val) return str;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
const char*
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_get_kcode()
|
1998-01-16 15:19:22 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
switch (reg_kcode) {
|
|
|
|
case KCODE_SJIS:
|
|
|
|
return "SJIS";
|
|
|
|
case KCODE_EUC:
|
|
|
|
return "EUC";
|
|
|
|
case KCODE_UTF8:
|
|
|
|
return "UTF8";
|
|
|
|
default:
|
|
|
|
return "NONE";
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
|
|
|
kcode_getter()
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_str_new2(rb_get_kcode());
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_set_kcode(code)
|
1999-08-13 09:45:20 +04:00
|
|
|
const char *code;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
if (code == 0) goto set_no_conversion;
|
|
|
|
|
|
|
|
switch (code[0]) {
|
|
|
|
case 'E':
|
|
|
|
case 'e':
|
|
|
|
reg_kcode = KCODE_EUC;
|
1999-01-20 07:59:39 +03:00
|
|
|
re_mbcinit(MBCTYPE_EUC);
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
case 's':
|
|
|
|
reg_kcode = KCODE_SJIS;
|
1999-01-20 07:59:39 +03:00
|
|
|
re_mbcinit(MBCTYPE_SJIS);
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
case 'u':
|
|
|
|
reg_kcode = KCODE_UTF8;
|
|
|
|
re_mbcinit(MBCTYPE_UTF8);
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case 'N':
|
|
|
|
case 'n':
|
1999-01-20 07:59:39 +03:00
|
|
|
case 'A':
|
|
|
|
case 'a':
|
1998-01-16 15:13:05 +03:00
|
|
|
set_no_conversion:
|
|
|
|
reg_kcode = KCODE_NONE;
|
1999-01-20 07:59:39 +03:00
|
|
|
re_mbcinit(MBCTYPE_ASCII);
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
kcode_setter(val)
|
2001-05-02 08:22:21 +04:00
|
|
|
VALUE val;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
may_need_recompile = 1;
|
2001-05-02 08:22:21 +04:00
|
|
|
rb_set_kcode(StringValuePtr(val));
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
ignorecase_getter()
|
|
|
|
{
|
|
|
|
return ruby_ignorecase?Qtrue:Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-07-24 13:07:33 +04:00
|
|
|
ignorecase_setter(val, id)
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE val;
|
2001-07-24 13:07:33 +04:00
|
|
|
ID id;
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2001-07-24 13:07:33 +04:00
|
|
|
rb_warn("modifying %s is deperecated", rb_id2name(id));
|
1999-01-20 07:59:39 +03:00
|
|
|
may_need_recompile = 1;
|
|
|
|
ruby_ignorecase = RTEST(val);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
match_getter()
|
|
|
|
{
|
1999-08-13 09:45:20 +04:00
|
|
|
VALUE match = rb_backref_get();
|
|
|
|
|
|
|
|
if (NIL_P(match)) return Qnil;
|
2000-02-08 11:54:01 +03:00
|
|
|
rb_match_busy(match);
|
|
|
|
return match;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
match_setter(val)
|
|
|
|
VALUE val;
|
|
|
|
{
|
|
|
|
Check_Type(val, T_MATCH);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_backref_set(val);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2001-01-15 10:01:00 +03:00
|
|
|
static VALUE
|
|
|
|
rb_reg_s_last_match(argc, argv)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
|
|
|
VALUE nth;
|
|
|
|
|
|
|
|
if (rb_scan_args(argc, argv, "01", &nth) == 1) {
|
|
|
|
return rb_reg_nth_match(NUM2INT(nth), rb_backref_get());
|
|
|
|
}
|
|
|
|
return match_getter();
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
void
|
|
|
|
Init_Regexp()
|
|
|
|
{
|
2000-08-28 13:53:42 +04:00
|
|
|
rb_eRegexpError = rb_define_class("RegexpError", rb_eStandardError);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
re_set_casetable(casetable);
|
1999-08-13 09:45:20 +04:00
|
|
|
#if DEFAULT_KCODE == KCODE_EUC
|
1999-01-20 07:59:39 +03:00
|
|
|
re_mbcinit(MBCTYPE_EUC);
|
|
|
|
#else
|
1999-08-13 09:45:20 +04:00
|
|
|
#if DEFAULT_KCODE == KCODE_SJIS
|
1999-01-20 07:59:39 +03:00
|
|
|
re_mbcinit(MBCTYPE_SJIS);
|
|
|
|
#else
|
1999-08-13 09:45:20 +04:00
|
|
|
#if DEFAULT_KCODE == KCODE_UTF8
|
1999-01-20 07:59:39 +03:00
|
|
|
re_mbcinit(MBCTYPE_UTF8);
|
|
|
|
#else
|
|
|
|
re_mbcinit(MBCTYPE_ASCII);
|
|
|
|
#endif
|
|
|
|
#endif
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
rb_define_virtual_variable("$~", match_getter, match_setter);
|
|
|
|
rb_define_virtual_variable("$&", last_match_getter, 0);
|
|
|
|
rb_define_virtual_variable("$`", prematch_getter, 0);
|
|
|
|
rb_define_virtual_variable("$'", postmatch_getter, 0);
|
|
|
|
rb_define_virtual_variable("$+", last_paren_match_getter, 0);
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_virtual_variable("$=", ignorecase_getter, ignorecase_setter);
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_define_virtual_variable("$KCODE", kcode_getter, kcode_setter);
|
|
|
|
rb_define_virtual_variable("$-K", kcode_getter, kcode_setter);
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_cRegexp = rb_define_class("Regexp", rb_cObject);
|
|
|
|
rb_define_singleton_method(rb_cRegexp, "new", rb_reg_s_new, -1);
|
|
|
|
rb_define_singleton_method(rb_cRegexp, "compile", rb_reg_s_new, -1);
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_define_singleton_method(rb_cRegexp, "quote", rb_reg_s_quote, -1);
|
|
|
|
rb_define_singleton_method(rb_cRegexp, "escape", rb_reg_s_quote, -1);
|
2001-01-15 10:01:00 +03:00
|
|
|
rb_define_singleton_method(rb_cRegexp, "last_match", rb_reg_s_last_match, -1);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2000-05-22 11:09:55 +04:00
|
|
|
rb_define_method(rb_cRegexp, "initialize", rb_reg_initialize_m, -1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cRegexp, "clone", rb_reg_clone, 0);
|
|
|
|
rb_define_method(rb_cRegexp, "==", rb_reg_equal, 1);
|
|
|
|
rb_define_method(rb_cRegexp, "=~", rb_reg_match, 1);
|
|
|
|
rb_define_method(rb_cRegexp, "===", rb_reg_match, 1);
|
|
|
|
rb_define_method(rb_cRegexp, "~", rb_reg_match2, 0);
|
2000-01-05 07:41:21 +03:00
|
|
|
rb_define_method(rb_cRegexp, "match", rb_reg_match_m, 1);
|
2001-09-05 10:54:57 +04:00
|
|
|
rb_define_method(rb_cRegexp, "to_s", rb_reg_inspect, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cRegexp, "inspect", rb_reg_inspect, 0);
|
|
|
|
rb_define_method(rb_cRegexp, "source", rb_reg_source, 0);
|
|
|
|
rb_define_method(rb_cRegexp, "casefold?", rb_reg_casefold_p, 0);
|
2001-02-08 12:19:27 +03:00
|
|
|
rb_define_method(rb_cRegexp, "options", rb_reg_options_m, 0);
|
2000-01-05 07:41:21 +03:00
|
|
|
rb_define_method(rb_cRegexp, "kcode", rb_reg_kcode_m, 0);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_define_const(rb_cRegexp, "IGNORECASE", INT2FIX(RE_OPTION_IGNORECASE));
|
|
|
|
rb_define_const(rb_cRegexp, "EXTENDED", INT2FIX(RE_OPTION_EXTENDED));
|
2000-05-17 10:33:50 +04:00
|
|
|
rb_define_const(rb_cRegexp, "MULTILINE", INT2FIX(RE_OPTION_MULTILINE));
|
1999-08-13 09:45:20 +04:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_global_variable(®_cache);
|
|
|
|
|
2000-02-23 08:23:12 +03:00
|
|
|
rb_cMatch = rb_define_class("MatchData", rb_cObject);
|
|
|
|
rb_define_global_const("MatchingData", rb_cMatch);
|
1999-10-15 12:52:18 +04:00
|
|
|
rb_undef_method(CLASS_OF(rb_cMatch), "new");
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cMatch, "clone", match_clone, 0);
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_define_method(rb_cMatch, "size", match_size, 0);
|
|
|
|
rb_define_method(rb_cMatch, "length", match_size, 0);
|
|
|
|
rb_define_method(rb_cMatch, "offset", match_offset, 1);
|
|
|
|
rb_define_method(rb_cMatch, "begin", match_begin, 1);
|
|
|
|
rb_define_method(rb_cMatch, "end", match_end, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cMatch, "to_a", match_to_a, 0);
|
2001-05-02 08:22:21 +04:00
|
|
|
rb_define_method(rb_cMatch, "to_ary", match_to_a, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cMatch, "[]", match_aref, -1);
|
1999-08-13 09:45:20 +04:00
|
|
|
rb_define_method(rb_cMatch, "pre_match", rb_reg_match_pre, 0);
|
|
|
|
rb_define_method(rb_cMatch, "post_match", rb_reg_match_post, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cMatch, "to_s", match_to_s, 0);
|
|
|
|
rb_define_method(rb_cMatch, "inspect", rb_any_to_s, 0);
|
2001-09-05 10:54:57 +04:00
|
|
|
rb_define_method(rb_cMatch, "string", match_string, 0);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|