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

ruby-changes:30216

From: glass <ko1@a...>
Date: Wed, 31 Jul 2013 16:53:20 +0900 (JST)
Subject: [ruby-changes:30216] glass:r42268 (trunk): * string.c (rb_str_rindex): refactoring and avoid to call str_nth() if

glass	2013-07-31 16:53:08 +0900 (Wed, 31 Jul 2013)

  New Revision: 42268

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

  Log:
    * string.c (rb_str_rindex): refactoring and avoid to call str_nth() if
      pos == 0.

  Modified files:
    trunk/ChangeLog
    trunk/string.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 42267)
+++ ChangeLog	(revision 42268)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Jul 31 16:43:30 2013  Masaki Matsushita  <glass.saga@g...>
+
+	* string.c (rb_str_rindex): refactoring and avoid to call str_nth() if
+	  pos == 0.
+
 Wed Jul 31 14:41:36 2013  Akinori MUSHA  <knu@i...>
 
 	* lib/set.rb: [DOC] Add a couple of notes on Hash as storage.
Index: string.c
===================================================================
--- string.c	(revision 42267)
+++ string.c	(revision 42268)
@@ -2673,27 +2673,33 @@ rb_str_rindex(VALUE str, VALUE sub, long https://github.com/ruby/ruby/blob/trunk/string.c#L2673
     long len, slen;
     char *s, *sbeg, *e, *t;
     rb_encoding *enc;
-    int singlebyte = single_byte_optimizable(str);
+    int singlebyte;
 
     enc = rb_enc_check(str, sub);
-    if (is_broken_string(sub)) {
-	return -1;
-    }
-    len = str_strlen(str, enc);
+    if (is_broken_string(sub)) return -1;
+    singlebyte = single_byte_optimizable(str);
+    len = singlebyte ? RSTRING_LEN(str) : str_strlen(str, enc);
     slen = str_strlen(sub, enc);
+
     /* substring longer than string */
     if (len < slen) return -1;
-    if (len - pos < slen) {
-	pos = len - slen;
-    }
-    if (len == 0) {
-	return pos;
-    }
+    if (len - pos < slen) pos = len - slen;
+    if (len == 0) return pos;
+
     sbeg = RSTRING_PTR(str);
     e = RSTRING_END(str);
     t = RSTRING_PTR(sub);
     slen = RSTRING_LEN(sub);
+
+    if (pos == 0) {
+	if (memcmp(sbeg, t, slen) == 0)
+	    return 0;
+	else
+	    return -1;
+    }
+
     s = str_nth(sbeg, e, pos, enc, singlebyte);
+
     while (s) {
 	if (memcmp(s, t, slen) == 0) {
 	    return pos;

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

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