From ec4756f633d62223cb05b5f24aee0842a471fb9d Mon Sep 17 00:00:00 2001 From: akr Date: Thu, 14 Feb 2008 03:34:12 +0000 Subject: [PATCH] * re.c (rb_reg_preprocess_dregexp): use non-preprocessed regexp source for result. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15465 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ re.c | 25 ++++++++++++++----------- test/ruby/test_m17n.rb | 6 ++++++ 3 files changed, 25 insertions(+), 11 deletions(-) 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)