mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* string.c (tr_setup_table): optimized. don't create hash objects
when given pattern is ASCII only. * string.c (tr_find): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29158 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
d53e0fe330
commit
09fb7189ec
3 changed files with 29 additions and 19 deletions
|
@ -1,3 +1,10 @@
|
|||
Wed Sep 1 16:50:42 2010 NARUSE, Yui <naruse@ruby-lang.org>
|
||||
|
||||
* 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@ruby-lang.org>
|
||||
|
||||
* array.c (rb_ary_rotate_m): fix typo of rdoc.
|
||||
|
|
40
string.c
40
string.c
|
@ -5073,8 +5073,9 @@ rb_str_tr(VALUE str, VALUE src, VALUE repl)
|
|||
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,20 +5091,15 @@ tr_setup_table(VALUE str, char stable[256], int first,
|
|||
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 @@ tr_setup_table(VALUE str, char stable[256], int first,
|
|||
|
||||
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 @@ tr_setup_table(VALUE str, char stable[256], int first,
|
|||
|
||||
|
||||
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 @@ tr_find(unsigned int c, char table[256], VALUE del, VALUE nodel)
|
|||
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 @@ tr_find(unsigned int c, char table[256], VALUE del, VALUE nodel)
|
|||
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 @@ rb_str_delete(int argc, VALUE *argv, VALUE str)
|
|||
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 @@ rb_str_tr_s(VALUE str, VALUE src, VALUE repl)
|
|||
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;
|
||||
|
|
|
@ -481,6 +481,7 @@ class TestString < Test::Unit::TestCase
|
|||
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
|
||||
|
|
Loading…
Add table
Reference in a new issue