[前][次][番号順一覧][スレッド一覧]

ruby-changes:21478

From: nobu <ko1@a...>
Date: Wed, 26 Oct 2011 16:05:54 +0900 (JST)
Subject: [ruby-changes:21478] nobu:r33527 (ruby_1_8): * string.c (rb_str_partition, rb_str_rpartition)

nobu	2011-10-26 16:05:42 +0900 (Wed, 26 Oct 2011)

  New Revision: 33527

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=33527

  Log:
    * 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]

  Modified files:
    branches/ruby_1_8/ChangeLog
    branches/ruby_1_8/string.c
    branches/ruby_1_8/test/ruby/test_string.rb

Index: ruby_1_8/ChangeLog
===================================================================
--- ruby_1_8/ChangeLog	(revision 33526)
+++ ruby_1_8/ChangeLog	(revision 33527)
@@ -1,3 +1,9 @@
+Wed Oct 26 16:05:39 2011  Nobuyoshi Nakada  <nobu@r...>
+
+	* 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@r...>
 
 	* configure.in (RSHIFT): quote to get rid of argument expansion
Index: ruby_1_8/string.c
===================================================================
--- ruby_1_8/string.c	(revision 33526)
+++ ruby_1_8/string.c	(revision 33527)
@@ -2036,9 +2036,15 @@
 }
 
 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 @@
 {
     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_raise(rb_eTypeError, "type mismatch: %s given",
 		     rb_obj_classname(sep));
 	}
-        sep = get_arg_pat(tmp);
+        sep = get_arg_pat(tmp, &reg);
     }
+    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 @@
     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_raise(rb_eTypeError, "type mismatch: %s given",
 		     rb_obj_classname(sep));
 	}
-        sep = get_arg_pat(tmp);
+        sep = get_arg_pat(tmp, &reg);
     }
+    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 @@
     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, &reg);
+	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 @@
     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, &reg);
+	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;
Index: ruby_1_8/test/ruby/test_string.rb
===================================================================
--- ruby_1_8/test/ruby/test_string.rb	(revision 33526)
+++ ruby_1_8/test/ruby/test_string.rb	(revision 33527)
@@ -237,4 +237,26 @@
     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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]