summaryrefslogtreecommitdiff
path: root/re.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-07-05 08:12:18 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-07-05 08:12:18 +0000
commit46603a78afde1386f92b38f9b16ca86daa93ad88 (patch)
treef3594fecc31b6f210f95143c7a20c2ea55ae68ce /re.c
parenta0d50fa3c494ea5afb98c05be049a0cde6fde28d (diff)
* include/ruby/{intern,ruby}.h, compile.[ch], error.c, eval.c,
eval_load.c, gc.c, iseq.c, main.c, parse.y, re.c, ruby.c, yarvcore.[ch] (ruby_eval_tree, ruby_sourcefile, ruby_sourceline, ruby_nerrs): purge global variables. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12700 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 're.c')
-rw-r--r--re.c70
1 files changed, 43 insertions, 27 deletions
diff --git a/re.c b/re.c
index ec82593395..f8af10a7f3 100644
--- a/re.c
+++ b/re.c
@@ -21,6 +21,8 @@
VALUE rb_eRegexpError;
+typedef char onig_errmsg_buffer[ONIG_MAX_ERROR_MESSAGE_LEN];
+
#define BEG(no) regs->beg[no]
#define END(no) regs->end[no]
@@ -595,14 +597,11 @@ rb_reg_to_s(VALUE re)
}
static void
-rb_reg_raise(const char *s, long len, const char *err, VALUE re, int ce)
+rb_reg_raise(const char *s, long len, const char *err, VALUE re)
{
VALUE desc = rb_reg_desc(s, len, re);
- if (ce)
- rb_compile_error("%s: %s", err, RSTRING_PTR(desc));
- else
- rb_raise(rb_eRegexpError, "%s: %s", err, RSTRING_PTR(desc));
+ rb_raise(rb_eRegexpError, "%s: %s", err, RSTRING_PTR(desc));
}
@@ -685,10 +684,9 @@ rb_reg_kcode_m(VALUE re)
}
static Regexp*
-make_regexp(const char *s, long len, int flags, int ce)
+make_regexp(const char *s, long len, int flags, onig_errmsg_buffer err)
{
Regexp *rp;
- char err[ONIG_MAX_ERROR_MESSAGE_LEN];
int r;
OnigErrorInfo einfo;
@@ -705,7 +703,7 @@ make_regexp(const char *s, long len, int flags, int ce)
OnigDefaultSyntax);
if (r) {
onig_error_code_to_str((UChar*)err, r);
- rb_reg_raise(s, len, err, 0, ce);
+ return 0;
}
r = onig_compile(rp, (UChar*)s, (UChar*)(s + len), &einfo);
@@ -713,7 +711,6 @@ make_regexp(const char *s, long len, int flags, int ce)
if (r != 0) {
onig_free(rp);
(void )onig_error_code_to_str((UChar*)err, r, &einfo);
- rb_reg_raise(s, len, err, 0, ce);
return 0;
}
return rp;
@@ -907,7 +904,7 @@ rb_reg_prepare_re(VALUE re)
}
if (need_recompile) {
- char err[ONIG_MAX_ERROR_MESSAGE_LEN];
+ onig_errmsg_buffer err;
int r;
OnigErrorInfo einfo;
regex_t *reg, *reg2;
@@ -924,8 +921,8 @@ rb_reg_prepare_re(VALUE re)
reg->options, onigenc_get_default_encoding(),
OnigDefaultSyntax, &einfo);
if (r) {
- onig_error_code_to_str((UChar*)err, r, &einfo);
- rb_reg_raise((char* )pattern, RREGEXP(re)->len, err, re, Qfalse);
+ onig_error_code_to_str((UChar*)err, r, &einfo);
+ rb_reg_raise((char* )pattern, RREGEXP(re)->len, err, re);
}
RREGEXP(re)->ptr = reg2;
@@ -1016,9 +1013,9 @@ rb_reg_search(VALUE re, VALUE str, long pos, long reverse)
return result;
}
else {
- char err[ONIG_MAX_ERROR_MESSAGE_LEN];
+ onig_errmsg_buffer err;
onig_error_code_to_str((UChar*)err, result);
- rb_reg_raise(RREGEXP(re)->str, RREGEXP(re)->len, err, 0, Qfalse);
+ rb_reg_raise(RREGEXP(re)->str, RREGEXP(re)->len, err, 0);
}
}
@@ -1460,10 +1457,9 @@ match_inspect(VALUE match)
VALUE rb_cRegexp;
-static void
+static int
rb_reg_initialize(VALUE obj, const char *s, long len,
- int options,
- int ce) /* call rb_compile_error() */
+ int options, onig_errmsg_buffer err)
{
struct RRegexp *re = RREGEXP(obj);
@@ -1486,7 +1482,8 @@ rb_reg_initialize(VALUE obj, const char *s, long len,
options |= ONIG_OPTION_IGNORECASE;
FL_SET(re, REG_CASESTATE);
}
- re->ptr = make_regexp(s, len, options & ARG_REG_OPTION_MASK, ce);
+ re->ptr = make_regexp(s, len, options & ARG_REG_OPTION_MASK, err);
+ if (!re->ptr) return -1;
re->str = ALLOC_N(char, len+1);
memcpy(re->str, s, len);
re->str[len] = '\0';
@@ -1494,7 +1491,7 @@ rb_reg_initialize(VALUE obj, const char *s, long len,
if (options & ARG_KCODE_MASK) {
kcode_reset_option();
}
- if (ce) FL_SET(obj, REG_LITERAL);
+ return 0;
}
static VALUE
@@ -1514,18 +1511,27 @@ VALUE
rb_reg_new(const char *s, long len, int options)
{
VALUE re = rb_reg_s_alloc(rb_cRegexp);
+ char err[ONIG_MAX_ERROR_MESSAGE_LEN];
- rb_reg_initialize(re, s, len, options, Qfalse);
- return (VALUE)re;
+ if (rb_reg_initialize(re, s, len, options, err) != 0) {
+ rb_reg_raise(s, len, err, re);
+ }
+
+ return re;
}
VALUE
-rb_reg_compile(const char *s, long len, int options)
+rb_reg_compile(const char *s, long len, int options, const char *file, int line)
{
VALUE re = rb_reg_s_alloc(rb_cRegexp);
+ char err[ONIG_MAX_ERROR_MESSAGE_LEN];
- rb_reg_initialize(re, s, len, options, Qtrue);
- return (VALUE)re;
+ if (rb_reg_initialize(re, s, len, options, err) != 0) {
+ VALUE desc = rb_reg_desc(s, len, re);
+ rb_compile_error(file, line, "%s: %s", err, RSTRING_PTR(desc));
+ }
+ FL_SET(re, REG_LITERAL);
+ return re;
}
static int case_cache;
@@ -1805,6 +1811,7 @@ rb_reg_match_m(int argc, VALUE *argv, VALUE re)
static VALUE
rb_reg_initialize_m(int argc, VALUE *argv, VALUE self)
{
+ onig_errmsg_buffer err;
const char *s;
long len;
int flags = 0;
@@ -1838,7 +1845,9 @@ rb_reg_initialize_m(int argc, VALUE *argv, VALUE self)
s = StringValuePtr(argv[0]);
len = RSTRING_LEN(argv[0]);
}
- rb_reg_initialize(self, s, len, flags, Qfalse);
+ if (rb_reg_initialize(self, s, len, flags, err) != 0) {
+ rb_reg_raise(s, len, err, self);
+ }
return self;
}
@@ -2081,6 +2090,10 @@ rb_reg_s_union(int argc, VALUE *argv)
static VALUE
rb_reg_init_copy(VALUE copy, VALUE re)
{
+ onig_errmsg_buffer err;
+ const char *s;
+ long len;
+
if (copy == re) return copy;
rb_check_frozen(copy);
/* need better argument type check */
@@ -2088,8 +2101,11 @@ rb_reg_init_copy(VALUE copy, VALUE re)
rb_raise(rb_eTypeError, "wrong argument type");
}
rb_reg_check(re);
- rb_reg_initialize(copy, RREGEXP(re)->str, RREGEXP(re)->len,
- rb_reg_options(re), Qfalse);
+ s = RREGEXP(re)->str;
+ len = RREGEXP(re)->len;
+ if (rb_reg_initialize(copy, s, len, rb_reg_options(re), err) != 0) {
+ rb_reg_raise(s, len, err, copy);
+ }
return copy;
}