From 6be6666fba64ccad17b789ed54eade1443b53d23 Mon Sep 17 00:00:00 2001 From: naruse Date: Thu, 25 Jul 2013 08:52:32 +0000 Subject: * re.c (rb_reg_to_s): convert closing parenthes to the target encoding if it is ASCII incompatible encoding. [ruby-core:56063] [Bug #8650] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42167 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- re.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 're.c') diff --git a/re.c b/re.c index e5cc79d445..dd40918f05 100644 --- a/re.c +++ b/re.c @@ -597,8 +597,30 @@ rb_reg_to_s(VALUE re) } rb_str_buf_cat2(str, ":"); - rb_reg_expr_str(str, (char*)ptr, len, enc, NULL); - rb_str_buf_cat2(str, ")"); + if (rb_enc_asciicompat(enc)) { + rb_reg_expr_str(str, (char*)ptr, len, enc, NULL); + rb_str_buf_cat2(str, ")"); + } + else { + const char *s, *e; + char *paren; + ptrdiff_t n; + rb_str_buf_cat2(str, ")"); + rb_enc_associate(str, rb_usascii_encoding()); + str = rb_str_encode(str, rb_enc_from_encoding(enc), 0, Qnil); + + /* backup encoded ")" to paren */ + s = RSTRING_PTR(str); + e = RSTRING_END(str); + s = rb_enc_left_char_head(s, e-1, e, enc); + n = e - s; + paren = ALLOCA_N(char, n); + memcpy(paren, s, n); + rb_str_resize(str, RSTRING_LEN(str) - n); + + rb_reg_expr_str(str, (char*)ptr, len, enc, NULL); + rb_str_buf_cat(str, paren, n); + } rb_enc_copy(str, re); OBJ_INFECT(str, re); -- cgit v1.2.3