1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* parse.y (parser_tokadd_string): escape simple regexp meta

character termninators.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34929 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2012-03-06 07:24:07 +00:00
parent 8a4de7abf8
commit bc6dde9420
3 changed files with 53 additions and 2 deletions

View file

@ -1,3 +1,8 @@
Tue Mar 6 16:24:01 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
* parse.y (parser_tokadd_string): escape simple regexp meta
character termninators.
Tue Mar 6 10:11:43 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
* ext/io/console/console.c (set_rawmode): clear ECHOE and ECHOK

14
parse.y
View file

@ -6440,6 +6440,18 @@ parser_tokadd_mbchar(struct parser_params *parser, int c)
#define tokadd_mbchar(c) parser_tokadd_mbchar(parser, (c))
static inline int
simple_re_meta(int c)
{
switch (c) {
case '$': case '*': case '+': case '.':
case '?': case '^': case '|':
return TRUE;
default:
return FALSE;
}
}
static int
parser_tokadd_string(struct parser_params *parser,
int func, int term, int paren, long *nest,
@ -6520,7 +6532,7 @@ parser_tokadd_string(struct parser_params *parser,
goto non_ascii;
}
if (func & STR_FUNC_REGEXP) {
if (c == term) {
if (c == term && !simple_re_meta(c)) {
tokadd(c);
continue;
}

View file

@ -1,5 +1,5 @@
require 'test/unit'
require_relative 'envutil'
require 'envutil'
class TestRegexp < Test::Unit::TestCase
def setup
@ -161,6 +161,40 @@ class TestRegexp < Test::Unit::TestCase
assert_equal(':', %r:\::.source, bug5484)
end
def test_source_escaped
expected, result = "$*+.?^|".each_char.map {|c|
[
["\\#{c}", "\\#{c}", 1],
begin
re = eval("%r#{c}\\#{c}#{c}", nil, __FILE__, __LINE__)
t = eval("/\\#{c}/", nil, __FILE__, __LINE__).source
rescue SyntaxError => e
[e, t, nil]
else
[re.source, t, re =~ "a#{c}a"]
end
]
}.transpose
assert_equal(expected, result)
end
def test_source_unescaped
expected, result = "!\"#%&',-/:;=@_`~".each_char.map {|c|
[
["#{c}", "\\#{c}", 1],
begin
re = eval("%r#{c}\\#{c}#{c}", nil, __FILE__, __LINE__)
t = eval("%r{\\#{c}}", nil, __FILE__, __LINE__).source
rescue SyntaxError => e
[e, t, nil]
else
[re.source, t, re =~ "a#{c}a"]
end
]
}.transpose
assert_equal(expected, result)
end
def test_inspect
assert_equal('//', //.inspect)
assert_equal('//i', //i.inspect)