mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* string.c (rb_str_times): reduce loop overhead.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15514 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
71c5e48598
commit
35cb0f807b
3 changed files with 23 additions and 5 deletions
|
@ -1,3 +1,7 @@
|
|||
Sun Feb 17 09:17:08 2008 Tanaka Akira <akr@fsij.org>
|
||||
|
||||
* string.c (rb_str_times): reduce loop overhead.
|
||||
|
||||
Sun Feb 17 03:37:01 2008 Tanaka Akira <akr@fsij.org>
|
||||
|
||||
* include/ruby/re.h (struct rmatch_offset): new struct for character
|
||||
|
|
13
string.c
13
string.c
|
@ -762,7 +762,7 @@ VALUE
|
|||
rb_str_times(VALUE str, VALUE times)
|
||||
{
|
||||
VALUE str2;
|
||||
long i, len;
|
||||
long n, len;
|
||||
|
||||
len = NUM2LONG(times);
|
||||
if (len < 0) {
|
||||
|
@ -773,9 +773,14 @@ rb_str_times(VALUE str, VALUE times)
|
|||
}
|
||||
|
||||
str2 = rb_str_new5(str, 0, len *= RSTRING_LEN(str));
|
||||
for (i = 0; i < len; i += RSTRING_LEN(str)) {
|
||||
memcpy(RSTRING_PTR(str2) + i,
|
||||
RSTRING_PTR(str), RSTRING_LEN(str));
|
||||
if (len) {
|
||||
n = RSTRING_LEN(str);
|
||||
memcpy(RSTRING_PTR(str2), RSTRING_PTR(str), n);
|
||||
while (n <= len/2) {
|
||||
memcpy(RSTRING_PTR(str2) + n, RSTRING_PTR(str2), n);
|
||||
n *= 2;
|
||||
}
|
||||
memcpy(RSTRING_PTR(str2) + n, RSTRING_PTR(str2), len-n);
|
||||
}
|
||||
RSTRING_PTR(str2)[RSTRING_LEN(str2)] = '\0';
|
||||
OBJ_INFECT(str2, str);
|
||||
|
|
|
@ -174,7 +174,7 @@ class TestString < Test::Unit::TestCase
|
|||
s = "a"
|
||||
10.times {|i|
|
||||
s << s
|
||||
assert_equal("a" * (2<<i), s)
|
||||
assert_equal("a" * (2 << i), s)
|
||||
}
|
||||
end
|
||||
|
||||
|
@ -1370,4 +1370,13 @@ class TestString < Test::Unit::TestCase
|
|||
def test_end_with?
|
||||
assert("abc".end_with?("c"))
|
||||
end
|
||||
|
||||
def test_times
|
||||
s1 = ''
|
||||
100.times {|n|
|
||||
s2 = "a" * n
|
||||
assert_equal(s1, s2)
|
||||
s1 << 'a'
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue