diff --git a/ChangeLog b/ChangeLog index c8e35b6f83..1aef78224c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Thu Feb 14 12:30:02 2008 Tanaka Akira + + * re.c (rb_reg_preprocess_dregexp): use non-preprocessed regexp source + for result. + Thu Feb 14 01:43:16 2008 Nobuyoshi Nakada * lib/timeout.rb (Timeout::timeout): made sensitive to location on the diff --git a/re.c b/re.c index 61e3a57dfa..cd93024302 100644 --- a/re.c +++ b/re.c @@ -1953,6 +1953,7 @@ static VALUE rb_reg_preprocess_dregexp(VALUE ary) { rb_encoding *fixed_enc = 0; + rb_encoding *regexp_enc = 0; onig_errmsg_buffer err = ""; int i; VALUE result = 0; @@ -1967,31 +1968,33 @@ rb_reg_preprocess_dregexp(VALUE ary) VALUE str = argv[i]; VALUE buf; char *p, *end; - rb_encoding *enc; + rb_encoding *src_enc; StringValue(str); p = RSTRING_PTR(str); end = p + RSTRING_LEN(str); - enc = rb_enc_get(str); + src_enc = rb_enc_get(str); - buf = rb_reg_preprocess(p, end, enc, &fixed_enc, err); - RB_GC_GUARD(str); + buf = rb_reg_preprocess(p, end, src_enc, &fixed_enc, err); 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 (fixed_enc != 0) { + if (regexp_enc != 0 && regexp_enc != fixed_enc) { + rb_raise(rb_eArgError, "encoding mismatch in dynamic regexp : %s and %s", + rb_enc_name(regexp_enc), rb_enc_name(fixed_enc)); } + regexp_enc = fixed_enc; } if (!result) - result = buf; + result = rb_str_new3(str); else - rb_str_buf_append(result, buf); + rb_str_buf_append(result, str); + } + if (regexp_enc) { + rb_enc_associate(result, regexp_enc); } return result; diff --git a/test/ruby/test_m17n.rb b/test/ruby/test_m17n.rb index deb20d52f5..2d48580ba1 100644 --- a/test/ruby/test_m17n.rb +++ b/test/ruby/test_m17n.rb @@ -452,6 +452,12 @@ class TestM17N < Test::Unit::TestCase assert_raise(ArgumentError) { /\xc2\xa1#{r}/s } end + def test_regexp_embed_preprocess + r1 = /\xa4\xa2/e + r2 = /#{r1}/ + assert(r2.source.include?(r1.source)) + end + def test_begin_end_offset str = e("\244\242\244\244\244\246\244\250\244\252a") assert(/(a)/ =~ str)