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

ruby-changes:17159

From: naruse <ko1@a...>
Date: Wed, 1 Sep 2010 16:55:05 +0900 (JST)
Subject: [ruby-changes:17159] Ruby:r29158 (trunk): * string.c (tr_setup_table): optimized. don't create hash objects

naruse	2010-09-01 16:52:16 +0900 (Wed, 01 Sep 2010)

  New Revision: 29158

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

  Log:
    * string.c (tr_setup_table): optimized. don't create hash objects
      when given pattern is ASCII only.
    
    * string.c (tr_find): ditto.

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

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 29157)
+++ ChangeLog	(revision 29158)
@@ -1,3 +1,10 @@
+Wed Sep  1 16:50:42 2010  NARUSE, Yui  <naruse@r...>
+
+	* string.c (tr_setup_table): optimized. don't create hash objects
+	  when given pattern is ASCII only.
+
+	* string.c (tr_find): ditto.
+
 Wed Sep  1 14:35:29 2010  NARUSE, Yui  <naruse@r...>
 
 	* array.c (rb_ary_rotate_m): fix typo of rdoc.
Index: string.c
===================================================================
--- string.c	(revision 29157)
+++ string.c	(revision 29158)
@@ -5073,8 +5073,9 @@
     return str;
 }
 
+#define TR_TABLE_SIZE 257
 static void
-tr_setup_table(VALUE str, char stable[256], int first,
+tr_setup_table(VALUE str, char stable[TR_TABLE_SIZE], int first,
 	       VALUE *tablep, VALUE *ctablep, rb_encoding *enc)
 {
     const unsigned int errc = -1;
@@ -5090,21 +5091,16 @@
     if (RSTRING_LEN(str) > 1 && rb_enc_ascget(tr.p, tr.pend, &l, enc) == '^') {
 	cflag = 1;
 	tr.p += l;
-
-	table = rb_hash_new();
-	ptable = *ctablep;
-	*ctablep = table;
     }
-    else {
-	table = rb_hash_new();
-	ptable = *tablep;
-	*tablep = table;
-    }
     if (first) {
 	for (i=0; i<256; i++) {
 	    stable[i] = 1;
 	}
+	stable[256] = cflag;
     }
+    else if (stable[256] && !cflag) {
+	stable[256] = 0;
+    }
     for (i=0; i<256; i++) {
 	buf[i] = cflag;
     }
@@ -5118,8 +5114,14 @@
 
 	    if (!table) {
 		table = rb_hash_new();
-		ptable = *tablep;
-		*tablep = table;
+		if (cflag) {
+		    ptable = *ctablep;
+		    *ctablep = table;
+		}
+		else {
+		    ptable = *tablep;
+		    *tablep = table;
+		}
 	    }
 	    if (!ptable || !NIL_P(rb_hash_aref(ptable, key))) {
 		rb_hash_aset(table, key, Qtrue);
@@ -5133,7 +5135,7 @@
 
 
 static int
-tr_find(unsigned int c, char table[256], VALUE del, VALUE nodel)
+tr_find(unsigned int c, char table[TR_TABLE_SIZE], VALUE del, VALUE nodel)
 {
     if (c < 256) {
 	return table[c] != 0;
@@ -5147,10 +5149,10 @@
 		return TRUE;
 	    }
 	}
-	else if (nodel && NIL_P(rb_hash_lookup(nodel, v))) {
-	    return TRUE;
+	else if (nodel && !NIL_P(rb_hash_lookup(nodel, v))) {
+	    return FALSE;
 	}
-	return FALSE;
+	return table[256] ? TRUE : FALSE;
     }
 }
 
@@ -5165,7 +5167,7 @@
 static VALUE
 rb_str_delete_bang(int argc, VALUE *argv, VALUE str)
 {
-    char squeez[256];
+    char squeez[TR_TABLE_SIZE];
     rb_encoding *enc = 0;
     char *s, *send, *t;
     VALUE del = 0, nodel = 0;
@@ -5260,7 +5262,7 @@
 static VALUE
 rb_str_squeeze_bang(int argc, VALUE *argv, VALUE str)
 {
-    char squeez[256];
+    char squeez[TR_TABLE_SIZE];
     rb_encoding *enc = 0;
     VALUE del = 0, nodel = 0;
     char *s, *send, *t;
@@ -5412,7 +5414,7 @@
 static VALUE
 rb_str_count(int argc, VALUE *argv, VALUE str)
 {
-    char table[256];
+    char table[TR_TABLE_SIZE];
     rb_encoding *enc = 0;
     VALUE del = 0, nodel = 0;
     char *s, *send;
Index: test/ruby/test_string.rb
===================================================================
--- test/ruby/test_string.rb	(revision 29157)
+++ test/ruby/test_string.rb	(revision 29158)
@@ -481,6 +481,7 @@
     assert_equal(0, S("y").count(S("a\\-z")))
     assert_equal(5, "abc\u{3042 3044 3046}".count("^a"))
     assert_equal(5, "abc\u{3042 3044 3046}".count("^\u3042"))
+    assert_equal(2, "abc\u{3042 3044 3046}".count("a-z", "^a"))
 
     assert_raise(ArgumentError) { "foo".count }
   end

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

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