re.c: do not escape terminator in Regexp.union

* re.c (rb_reg_str_with_term): change terminator.

* re.c (rb_reg_s_union): terminator in source string does not need
  to be escaped.  terminators are outside of regexp source itself.
  [ruby-core:86149] [Bug #14608]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62779 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2018-03-16 13:37:44 +00:00
parent 0eddedbf5c
commit 8a8f542c43
2 changed files with 24 additions and 12 deletions

31
re.c
View File

@ -351,7 +351,7 @@ rb_reg_check(VALUE re)
static void static void
rb_reg_expr_str(VALUE str, const char *s, long len, rb_reg_expr_str(VALUE str, const char *s, long len,
rb_encoding *enc, rb_encoding *resenc) rb_encoding *enc, rb_encoding *resenc, int term)
{ {
const char *p, *pend; const char *p, *pend;
int cr = ENC_CODERANGE_UNKNOWN; int cr = ENC_CODERANGE_UNKNOWN;
@ -372,7 +372,7 @@ rb_reg_expr_str(VALUE str, const char *s, long len,
break; break;
} }
} }
else if (c != '/' && rb_enc_isprint(c, enc)) { else if (c != term && rb_enc_isprint(c, enc)) {
p += clen; p += clen;
} }
else { else {
@ -399,11 +399,6 @@ rb_reg_expr_str(VALUE str, const char *s, long len,
p += n; p += n;
continue; continue;
} }
else if (c == '/') {
char c = '\\';
rb_str_buf_cat(str, &c, 1);
rb_str_buf_cat(str, p, clen);
}
else if (c == -1) { else if (c == -1) {
clen = rb_enc_precise_mbclen(p, pend, enc); clen = rb_enc_precise_mbclen(p, pend, enc);
if (!MBCLEN_CHARFOUND_P(clen)) { if (!MBCLEN_CHARFOUND_P(clen)) {
@ -420,6 +415,11 @@ rb_reg_expr_str(VALUE str, const char *s, long len,
rb_str_buf_cat(str, p, clen); rb_str_buf_cat(str, p, clen);
} }
} }
else if (c == term) {
char c = '\\';
rb_str_buf_cat(str, &c, 1);
rb_str_buf_cat(str, p, clen);
}
else if (rb_enc_isprint(c, enc)) { else if (rb_enc_isprint(c, enc)) {
rb_str_buf_cat(str, p, clen); rb_str_buf_cat(str, p, clen);
} }
@ -452,7 +452,7 @@ rb_reg_desc(const char *s, long len, VALUE re)
else { else {
rb_enc_associate(str, rb_usascii_encoding()); rb_enc_associate(str, rb_usascii_encoding());
} }
rb_reg_expr_str(str, s, len, enc, resenc); rb_reg_expr_str(str, s, len, enc, resenc, '/');
rb_str_buf_cat2(str, "/"); rb_str_buf_cat2(str, "/");
if (re) { if (re) {
char opts[4]; char opts[4];
@ -513,6 +513,7 @@ rb_reg_inspect(VALUE re)
return rb_reg_desc(RREGEXP_SRC_PTR(re), RREGEXP_SRC_LEN(re), re); return rb_reg_desc(RREGEXP_SRC_PTR(re), RREGEXP_SRC_LEN(re), re);
} }
static VALUE rb_reg_str_with_term(VALUE re, int term);
/* /*
* call-seq: * call-seq:
@ -536,6 +537,12 @@ rb_reg_inspect(VALUE re)
static VALUE static VALUE
rb_reg_to_s(VALUE re) rb_reg_to_s(VALUE re)
{
return rb_reg_str_with_term(re, '/');
}
static VALUE
rb_reg_str_with_term(VALUE re, int term)
{ {
int options, opt; int options, opt;
const int embeddable = ONIG_OPTION_MULTILINE|ONIG_OPTION_IGNORECASE|ONIG_OPTION_EXTEND; const int embeddable = ONIG_OPTION_MULTILINE|ONIG_OPTION_IGNORECASE|ONIG_OPTION_EXTEND;
@ -615,7 +622,7 @@ rb_reg_to_s(VALUE re)
rb_str_buf_cat2(str, ":"); rb_str_buf_cat2(str, ":");
if (rb_enc_asciicompat(enc)) { if (rb_enc_asciicompat(enc)) {
rb_reg_expr_str(str, (char*)ptr, len, enc, NULL); rb_reg_expr_str(str, (char*)ptr, len, enc, NULL, term);
rb_str_buf_cat2(str, ")"); rb_str_buf_cat2(str, ")");
} }
else { else {
@ -635,7 +642,7 @@ rb_reg_to_s(VALUE re)
memcpy(paren, s, n); memcpy(paren, s, n);
rb_str_resize(str, RSTRING_LEN(str) - n); rb_str_resize(str, RSTRING_LEN(str) - n);
rb_reg_expr_str(str, (char*)ptr, len, enc, NULL); rb_reg_expr_str(str, (char*)ptr, len, enc, NULL, term);
rb_str_buf_cat(str, paren, n); rb_str_buf_cat(str, paren, n);
} }
rb_enc_copy(str, re); rb_enc_copy(str, re);
@ -664,7 +671,7 @@ rb_enc_reg_error_desc(const char *s, long len, rb_encoding *enc, int options, co
rb_enc_associate(desc, enc); rb_enc_associate(desc, enc);
rb_str_buf_cat2(desc, ": /"); rb_str_buf_cat2(desc, ": /");
rb_reg_expr_str(desc, s, len, enc, resenc); rb_reg_expr_str(desc, s, len, enc, resenc, '/');
opts[0] = '/'; opts[0] = '/';
option_to_str(opts + 1, options); option_to_str(opts + 1, options);
rb_str_buf_cat2(desc, opts); rb_str_buf_cat2(desc, opts);
@ -3651,7 +3658,7 @@ rb_reg_s_union(VALUE self, VALUE args0)
else { else {
has_asciionly = 1; has_asciionly = 1;
} }
v = rb_reg_to_s(v); v = rb_reg_str_with_term(v, -1);
} }
else { else {
rb_encoding *enc; rb_encoding *enc;

View File

@ -90,6 +90,11 @@ class TestRegexp < Test::Unit::TestCase
rescue ArgumentError rescue ArgumentError
:ok :ok
end end
re = Regexp.union(/\//, "")
re2 = eval(re.inspect)
assert_equal(re.to_s, re2.to_s)
assert_equal(re.source, re2.source)
assert_equal(re, re2)
end end
def test_word_boundary def test_word_boundary