diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-10-26 07:05:42 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-10-26 07:05:42 +0000 |
commit | 2102e6443a357117e6a730ef12e0c2ea35749881 (patch) | |
tree | b12c38858055e56cb060d291be249ed9b5865f6b | |
parent | 990e3e6d9abe0f9d10be04dc5b724ece86a7a76a (diff) |
* string.c (rb_str_partition, rb_str_rpartition)
(rb_str_start_with, rb_str_end_with): preserve the last match
data. [ruby-core:39671] [Bug #5351]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@33527 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | string.c | 39 | ||||
-rw-r--r-- | test/ruby/test_string.rb | 22 |
3 files changed, 60 insertions, 7 deletions
@@ -1,3 +1,9 @@ +Wed Oct 26 16:05:39 2011 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * string.c (rb_str_partition, rb_str_rpartition) + (rb_str_start_with, rb_str_end_with): preserve the last match + data. [ruby-core:39671] [Bug #5351] + Sat Jul 30 00:30:07 2011 Nobuyoshi Nakada <nobu@ruby-lang.org> * configure.in (RSHIFT): quote to get rid of argument expansion @@ -2036,9 +2036,15 @@ regcomp_failed(str) } static VALUE -get_arg_pat(pat) +get_arg_pat(pat, reg) VALUE pat; + int *reg; { + if (TYPE(pat) == T_REGEXP) { + *reg = 1; + return pat; + } + *reg = 0; return rb_rescue2(get_pat_quoted, pat, regcomp_failed, pat, rb_eRegexpError, (VALUE)0); @@ -4903,6 +4909,8 @@ rb_str_partition(argc, argv, str) { VALUE sep; long pos; + int reg = 1; + VALUE match = Qundef; if (argc == 0) return rb_call_super(argc, argv); rb_scan_args(argc, argv, "1", &sep); @@ -4914,9 +4922,11 @@ rb_str_partition(argc, argv, str) rb_raise(rb_eTypeError, "type mismatch: %s given", rb_obj_classname(sep)); } - sep = get_arg_pat(tmp); + sep = get_arg_pat(tmp, ®); } + if (!reg) rb_match_busy(match = rb_backref_get()); pos = rb_reg_search(sep, str, 0, 0); + if (!reg) rb_backref_set(match); if (pos < 0) { failed: return rb_ary_new3(3, str, rb_str_new(0,0),rb_str_new(0,0)); @@ -4948,6 +4958,8 @@ rb_str_rpartition(str, sep) VALUE sep; { long pos = RSTRING(str)->len; + int reg = 1; + VALUE match = Qundef; if (TYPE(sep) != T_REGEXP) { VALUE tmp; @@ -4957,9 +4969,11 @@ rb_str_rpartition(str, sep) rb_raise(rb_eTypeError, "type mismatch: %s given", rb_obj_classname(sep)); } - sep = get_arg_pat(tmp); + sep = get_arg_pat(tmp, ®); } + if (!reg) rb_match_busy(match = rb_backref_get()); pos = rb_reg_search(sep, str, pos, 1); + if (!reg) rb_backref_set(match); if (pos < 0) { return rb_ary_new3(3, rb_str_new(0,0),rb_str_new(0,0), str); } @@ -4984,14 +4998,20 @@ rb_str_start_with(argc, argv, str) VALUE str; { int i; + int reg = 1; + long pos; VALUE pat; + VALUE match = Qundef; for (i=0; i<argc; i++) { VALUE prefix = rb_check_string_type(argv[i]); if (NIL_P(prefix)) continue; if (RSTRING(str)->len < RSTRING(prefix)->len) continue; - pat = get_arg_pat(prefix); - if (rb_reg_search(pat, str, 0, 1) >= 0) + pat = get_arg_pat(prefix, ®); + if (!reg) rb_match_busy(match = rb_backref_get()); + pos = rb_reg_search(pat, str, 0, 1); + if (!reg) rb_backref_set(match); + if (pos >= 0) return Qtrue; } return Qfalse; @@ -5011,16 +5031,21 @@ rb_str_end_with(argc, argv, str) VALUE str; { int i; + int reg = 1; long pos; VALUE pat; + VALUE match = Qundef; for (i=0; i<argc; i++) { VALUE suffix = rb_check_string_type(argv[i]); if (NIL_P(suffix)) continue; if (RSTRING(str)->len < RSTRING(suffix)->len) continue; - pat = get_arg_pat(suffix); + pat = get_arg_pat(suffix, ®); + if (!reg) rb_match_busy(match = rb_backref_get()); pos = rb_reg_adjust_startpos(pat, str, RSTRING(str)->len - RSTRING(suffix)->len, 0); - if (rb_reg_search(pat, str, pos, 0) >= 0) + pos = rb_reg_search(pat, str, pos, 0); + if (!reg) rb_backref_set(match); + if (pos >= 0) return Qtrue; } return Qfalse; diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index 7510bb9cb7..073aecd6f8 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -237,4 +237,26 @@ class TestString < Test::Unit::TestCase assert_equal("", result[5]) assert_equal("", result[6]) end + + def test_start_with + assert_equal(true, "abc".start_with?("a")) + assert_equal(false, "abc".start_with?("A")) + /\w/ =~ "xyz" + "abc".start_with?("a") + assert_equal("x", $&) + /\w/ =~ "" + "abc".start_with?("a") + assert_nil($&) + end + + def test_end_with + assert_equal(true, "abc".end_with?("c")) + assert_equal(false, "abc".end_with?("C")) + /\w/ =~ "xyz" + "abc".end_with?("c") + assert_equal("x", $&) + /\w/ =~ "" + "abc".end_with?("c") + assert_nil($&) + end end |