1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* string.c (rb_str_upto): keep first width. [ruby-dev:39361]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25050 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2009-09-23 04:14:23 +00:00
parent 5a993c5d22
commit 1b541acd69
4 changed files with 28 additions and 4 deletions

View file

@ -1,3 +1,7 @@
Wed Sep 23 13:14:21 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
* string.c (rb_str_upto): keep first width. [ruby-dev:39361]
Wed Sep 23 11:28:06 2009 Nobuyoshi Nakada <nobu@ruby-lang.org> Wed Sep 23 11:28:06 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
* tool/instruction.rb (make_header_prepare_stack): check stack * tool/instruction.rb (make_header_prepare_stack): check stack

View file

@ -26,6 +26,8 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
#define numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
#undef rb_str_new_cstr #undef rb_str_new_cstr
#undef rb_tainted_str_new_cstr #undef rb_tainted_str_new_cstr
#undef rb_usascii_str_new_cstr #undef rb_usascii_str_new_cstr
@ -3102,8 +3104,10 @@ rb_str_upto(int argc, VALUE *argv, VALUE beg)
if (ascii && ISDIGIT(RSTRING_PTR(beg)[0]) && ISDIGIT(RSTRING_PTR(end)[0])) { if (ascii && ISDIGIT(RSTRING_PTR(beg)[0]) && ISDIGIT(RSTRING_PTR(end)[0])) {
char *s, *send; char *s, *send;
VALUE b, e; VALUE b, e;
int width;
s = RSTRING_PTR(beg); send = RSTRING_END(beg); s = RSTRING_PTR(beg); send = RSTRING_END(beg);
width = rb_long2int(send - s);
while (s < send) { while (s < send) {
if (!ISDIGIT(*s)) goto no_digits; if (!ISDIGIT(*s)) goto no_digits;
s++; s++;
@ -3118,20 +3122,22 @@ rb_str_upto(int argc, VALUE *argv, VALUE beg)
if (FIXNUM_P(b) && FIXNUM_P(e)) { if (FIXNUM_P(b) && FIXNUM_P(e)) {
long bi = FIX2LONG(b); long bi = FIX2LONG(b);
long ei = FIX2LONG(e); long ei = FIX2LONG(e);
char buf[sizeof(long)*3+1]; rb_encoding *usascii = rb_usascii_encoding();
while (bi <= ei) { while (bi <= ei) {
if (excl && bi == ei) break; if (excl && bi == ei) break;
sprintf(buf, "%ld", bi); rb_yield(rb_enc_sprintf(usascii, "%.*ld", width, bi));
rb_yield(rb_usascii_str_new_cstr(buf));
bi++; bi++;
} }
} }
else { else {
ID op = excl ? '<' : rb_intern("<="); ID op = excl ? '<' : rb_intern("<=");
VALUE args[2], fmt = rb_obj_freeze(rb_usascii_str_new_cstr("%.*d"));
args[0] = INT2FIX(width);
while (rb_funcall(b, op, 1, e)) { while (rb_funcall(b, op, 1, e)) {
rb_yield(rb_obj_as_string(b)); args[1] = b;
rb_yield(rb_str_format(numberof(args), args, fmt));
b = rb_funcall(b, succ, 0, 0); b = rb_funcall(b, succ, 0, 0);
} }
} }

View file

@ -14,6 +14,7 @@ class TestRange < Test::Unit::TestCase
assert_equal(["6", "7", "8"], ("6".."8").to_a, "[ruby-talk:343187]") assert_equal(["6", "7", "8"], ("6".."8").to_a, "[ruby-talk:343187]")
assert_equal(["6", "7"], ("6"..."8").to_a) assert_equal(["6", "7"], ("6"..."8").to_a)
assert_equal(["9", "10"], ("9".."10").to_a) assert_equal(["9", "10"], ("9".."10").to_a)
assert_equal(["09", "10"], ("09".."10").to_a, "[ruby-dev:39361]")
assert_equal(["9", "10"], (SimpleDelegator.new("9").."10").to_a) assert_equal(["9", "10"], (SimpleDelegator.new("9").."10").to_a)
assert_equal(["9", "10"], ("9"..SimpleDelegator.new("10")).to_a) assert_equal(["9", "10"], ("9"..SimpleDelegator.new("10")).to_a)
end end

View file

@ -1577,6 +1577,19 @@ class TestString < Test::Unit::TestCase
assert_equal(676, count) assert_equal(676, count)
end end
def test_upto_numeric
a = S("00")
start = S("00")
count = 0
assert_equal(S("00"), a.upto(S("23")) {|s|
assert_equal(start, s, "[ruby-dev:39361]")
assert_equal(Encoding::US_ASCII, s.encoding)
start.succ!
count += 1
})
assert_equal(24, count, "[ruby-dev:39361]")
end
def test_mod_check def test_mod_check
assert_raise(RuntimeError) { assert_raise(RuntimeError) {
s = "" s = ""