diff options
-rw-r--r-- | ChangeLog | 15 | ||||
-rw-r--r-- | compile.c | 26 | ||||
-rw-r--r-- | insns.def | 17 | ||||
-rw-r--r-- | re.c | 75 | ||||
-rw-r--r-- | test/ruby/test_m17n.rb | 18 |
5 files changed, 104 insertions, 47 deletions
@@ -1,3 +1,18 @@ +Tue Jan 29 16:59:01 2008 Tanaka Akira <akr@fsij.org> + + * insns.def (toregexp): generate a regexp from strings instead of one + string. + + * re.c (rb_reg_new_ary): defined for toregexp. it concatenates + strings after each string is preprocessed. + + * compile.c (compile_dstr_fragments): split from compile_dstr. + (compile_dstr): call compile_dstr_fragments. + (compile_dregx): defined for dynamic regexp. + (iseq_compile_each): use compile_dregx for dynamic regexp. + + [ruby-dev:33400] + Tue Jan 29 16:25:26 2008 NARUSE, Yui <naruse@ruby-lang.org> * common.mk, ext/extmkf.rb: always make encdb.h. @@ -1853,7 +1853,7 @@ iseq_set_sequence_stackcaching(rb_iseq_t *iseq, LINK_ANCHOR *anchor) static int -compile_dstr(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node) +compile_dstr_fragments(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int *cntp) { NODE *list = node->nd_next; VALUE lit = node->nd_lit; @@ -1867,12 +1867,30 @@ compile_dstr(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node) cnt++; list = list->nd_next; } + *cntp = cnt; + + return COMPILE_OK; +} +static int +compile_dstr(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node) +{ + int cnt; + compile_dstr_fragments(iseq, ret, node, &cnt); ADD_INSN1(ret, nd_line(node), concatstrings, INT2FIX(cnt)); return COMPILE_OK; } static int +compile_dregx(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node) +{ + int cnt; + compile_dstr_fragments(iseq, ret, node, &cnt); + ADD_INSN2(ret, nd_line(node), toregexp, INT2FIX(node->nd_cflag), INT2FIX(cnt)); + return COMPILE_OK; +} + +static int compile_branch_condition(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * cond, LABEL *then_label, LABEL *else_label) { @@ -4111,8 +4129,7 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped) break; } case NODE_DREGX:{ - compile_dstr(iseq, ret, node); - ADD_INSN1(ret, nd_line(node), toregexp, INT2FIX(node->nd_cflag)); + compile_dregx(iseq, ret, node); if (poped) { ADD_INSN(ret, nd_line(node), pop); @@ -4128,8 +4145,7 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped) ADD_INSN2(ret, nd_line(node), onceinlinecache, 0, lend); ADD_INSN(ret, nd_line(node), pop); - compile_dstr(iseq, ret, node); - ADD_INSN1(ret, nd_line(node), toregexp, INT2FIX(node->nd_cflag)); + compile_dregx(iseq, ret, node); ADD_INSN1(ret, nd_line(node), setinlinecache, lstart); ADD_LABEL(ret, lend); @@ -397,12 +397,19 @@ tostring */ DEFINE_INSN toregexp -(rb_num_t opt) -(VALUE str) -(VALUE val) +(rb_num_t opt, rb_num_t cnt) +(...) +(VALUE val) // inc += 1 - cnt; { - volatile VALUE tmp = str; /* for GC */ - val = rb_reg_new_str(str, opt); + VALUE rb_reg_new_ary(VALUE ary, int options); + int i; + VALUE ary = rb_ary_new2(cnt); + RBASIC(ary)->klass = 0; + for (i = 0; i < cnt; i++) { + rb_ary_store(ary, cnt-i-1, TOPN(i)); + } + POPN(cnt); + val = rb_reg_new_ary(ary, opt); } /** @@ -1949,36 +1949,53 @@ rb_reg_check_preprocess(VALUE str) return Qnil; } -#if 0 static VALUE -rb_reg_preprocess_obj(VALUE str, - rb_encoding **fixed_enc, onig_errmsg_buffer err) +rb_reg_preprocess_dregexp(VALUE ary) { - VALUE buf; - char *p, *end; - rb_encoding *enc; + rb_encoding *fixed_enc = 0; + onig_errmsg_buffer err = ""; + int i; + VALUE result = 0; + int argc = RARRAY_LEN(ary); + VALUE *argv = RARRAY_PTR(ary); - StringValue(str); - p = RSTRING_PTR(str); - end = p + RSTRING_LEN(str); - enc = rb_enc_get(str); + if (argc == 0) { + rb_raise(rb_eArgError, "no arguments given"); + } - buf = rb_reg_preprocess(p, end, enc, fixed_enc, err); - RB_GC_GUARD(str); - return buf; -} + for (i = 0; i < argc; i++) { + VALUE str = argv[i]; + VALUE buf; + char *p, *end; + rb_encoding *enc; -static VALUE -rb_reg_preprocess_m(VALUE klass, VALUE obj) -{ - rb_encoding *fixed_enc = 0; - onig_errmsg_buffer err = ""; - VALUE str = rb_reg_preprocess_obj(obj, &fixed_enc, err); - if (str == Qnil) - rb_raise(rb_eArgError, "%s", err); - return rb_assoc_new(str, fixed_enc ? Qtrue : Qfalse); + StringValue(str); + p = RSTRING_PTR(str); + end = p + RSTRING_LEN(str); + enc = rb_enc_get(str); + + buf = rb_reg_preprocess(p, end, enc, &fixed_enc, err); + RB_GC_GUARD(str); + + if (buf == Qnil) + rb_raise(rb_eArgError, "%s", err); + + if (i == 0) { + /* The encoding of the first fragment is the encoding + * given by the regexp option or script encoding. */ + if (fixed_enc == 0) { + rb_enc_copy(buf, str); + } + } + + if (!result) + result = buf; + else + rb_str_buf_append(result, buf); + } + + return result; } -#endif static int rb_reg_initialize(VALUE obj, const char *s, int len, rb_encoding *enc, @@ -2085,6 +2102,12 @@ rb_reg_new_str(VALUE s, int options) } VALUE +rb_reg_new_ary(VALUE ary, int opt) +{ + return rb_reg_new_str(rb_reg_preprocess_dregexp(ary), opt); +} + +VALUE rb_enc_reg_new(const char *s, long len, rb_encoding *enc, int options) { VALUE re = rb_reg_s_alloc(rb_cRegexp); @@ -3042,10 +3065,6 @@ Init_Regexp(void) rb_define_singleton_method(rb_cRegexp, "last_match", rb_reg_s_last_match, -1); rb_define_singleton_method(rb_cRegexp, "try_convert", rb_reg_s_try_convert, 1); -#if 0 - rb_define_singleton_method(rb_cRegexp, "preprocess", rb_reg_preprocess_m, 1); -#endif - rb_define_method(rb_cRegexp, "initialize", rb_reg_initialize_m, -1); rb_define_method(rb_cRegexp, "initialize_copy", rb_reg_init_copy, 1); rb_define_method(rb_cRegexp, "hash", rb_reg_hash, 0); diff --git a/test/ruby/test_m17n.rb b/test/ruby/test_m17n.rb index 056f4f3df5..922af05d5b 100644 --- a/test/ruby/test_m17n.rb +++ b/test/ruby/test_m17n.rb @@ -442,14 +442,14 @@ class TestM17N < Test::Unit::TestCase assert_raise(ArgumentError) { eval(s("/\#{r}\xc2\xa1/s")) } r = /\xc2\xa1/e - #assert_raise(ArgumentError) { eval(s("/\xc2\xa1\#{r}/s")) } - #assert_raise(ArgumentError) { eval(s("/\#{r}\xc2\xa1/s")) } + assert_raise(ArgumentError) { eval(s("/\xc2\xa1\#{r}/s")) } + assert_raise(ArgumentError) { eval(s("/\#{r}\xc2\xa1/s")) } r = eval(e("/\xc2\xa1/")) - #assert_raise(ArgumentError) { /\xc2\xa1#{r}/s } + assert_raise(ArgumentError) { /\xc2\xa1#{r}/s } r = /\xc2\xa1/e - #assert_raise(ArgumentError) { /\xc2\xa1#{r}/s } + assert_raise(ArgumentError) { /\xc2\xa1#{r}/s } end def test_begin_end_offset @@ -574,7 +574,7 @@ class TestM17N < Test::Unit::TestCase } assert_regexp_fixed_ascii8bit(/#{}\xc2\xa1/n) assert_regexp_fixed_ascii8bit(/\xc2\xa1#{}/n) - #assert_raise(SyntaxError) { s1, s2 = s('\xc2'), s('\xa1'); /#{s1}#{s2}/ } + assert_nothing_raised { s1, s2 = a('\xc2'), a('\xa1'); /#{s1}#{s2}/ } end def test_dynamic_eucjp_regexp @@ -584,7 +584,7 @@ class TestM17N < Test::Unit::TestCase assert_raise(SyntaxError) { eval('/\xc2#{}/e') } assert_raise(SyntaxError) { eval('/#{}\xc2/e') } assert_raise(SyntaxError) { eval('/\xc2#{}\xa1/e') } - #assert_raise(SyntaxError) { s1, s2 = e('\xc2'), e('\xa1'); /#{s1}#{s2}/ } + assert_raise(ArgumentError) { s1, s2 = e('\xc2'), e('\xa1'); /#{s1}#{s2}/ } end def test_dynamic_sjis_regexp @@ -594,7 +594,7 @@ class TestM17N < Test::Unit::TestCase assert_raise(SyntaxError) { eval('/\x81#{}/s') } assert_raise(SyntaxError) { eval('/#{}\x81/s') } assert_raise(SyntaxError) { eval('/\x81#{}\xa1/s') } - #assert_raise(SyntaxError) { s1, s2 = s('\x81'), s('\xa1'); /#{s1}#{s2}/ } + assert_raise(ArgumentError) { s1, s2 = s('\x81'), s('\xa1'); /#{s1}#{s2}/ } end def test_dynamic_utf8_regexp @@ -604,7 +604,7 @@ class TestM17N < Test::Unit::TestCase assert_raise(SyntaxError) { eval('/\xc2#{}/u') } assert_raise(SyntaxError) { eval('/#{}\xc2/u') } assert_raise(SyntaxError) { eval('/\xc2#{}\xa1/u') } - #assert_raise(SyntaxError) { s1, s2 = u('\xc2'), u('\xa1'); /#{s1}#{s2}/ } + assert_raise(ArgumentError) { s1, s2 = u('\xc2'), u('\xa1'); /#{s1}#{s2}/ } end def test_regexp_unicode @@ -1080,6 +1080,6 @@ class TestM17N < Test::Unit::TestCase assert_regexp_usascii_literal('/\u1234#{%q"\x80"}/', nil, SyntaxError) assert_regexp_usascii_literal('/\u1234#{"\x80"}/', nil, SyntaxError) assert_regexp_usascii_literal('/\u1234\x80/', nil, SyntaxError) - assert_regexp_usascii_literal('/\u1234#{}\x80/', nil, RegexpError) + assert_regexp_usascii_literal('/\u1234#{}\x80/', nil, ArgumentError) end end |