Commit 2d5943b6 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

Merge pull request #2524 from mattn/fix-succ

Fix String#succ. "-".succ should be "."
parents 4c0f40f6 4ece1e3e
......@@ -252,7 +252,8 @@ static mrb_value
mrb_str_succ_bang(mrb_state *mrb, mrb_value self)
{
mrb_value result;
char *p, *e, *b, *t, *prepend;
unsigned char *p, *e, *b, *t;
char *prepend;
struct RString *s = mrb_str_ptr(self);
size_t l;
......@@ -261,20 +262,41 @@ mrb_str_succ_bang(mrb_state *mrb, mrb_value self)
mrb_str_modify(mrb, s);
l = RSTRING_LEN(self);
b = p = RSTRING_PTR(self);
b = p = (unsigned char*) RSTRING_PTR(self);
t = e = p + l;
*(e--) = 0;
while (b < e) {
if (ISALNUM(*b))
// find trailing ascii/number
while (e >= b) {
if (ISALNUM(*e))
break;
b++;
e--;
}
if (e < b) {
e = p + l - 1;
result = mrb_str_new_lit(mrb, "");
} else {
// find leading letter of the ascii/number
b = e;
while (b > p) {
if (!ISALNUM(*b) || (ISALNUM(*b) && *b != '9' && *b != 'z' && *b != 'Z'))
break;
b--;
}
if (!ISALNUM(*b))
b++;
result = mrb_str_new(mrb, (char*) p, b - p);
}
result = mrb_str_new(mrb, p, b - p);
while (e >= b) {
if (!ISALNUM(*e))
if (!ISALNUM(*e)) {
if (*e == 0xff) {
mrb_str_cat_cstr(mrb, result, "\x01");
(*e) = 0;
} else
(*e)++;
break;
}
prepend = NULL;
if (*e == '9') {
if (e == b) prepend = "1";
......@@ -292,7 +314,7 @@ mrb_str_succ_bang(mrb_state *mrb, mrb_value self)
if (prepend) mrb_str_cat_cstr(mrb, result, prepend);
e--;
}
result = mrb_str_cat(mrb, result, b, t - b);
result = mrb_str_cat(mrb, result, (char*) b, t - b);
l = RSTRING_LEN(result);
mrb_str_resize(mrb, self, l);
memcpy(RSTRING_PTR(self), RSTRING_PTR(result), l);
......
......@@ -281,9 +281,13 @@ assert('String#succ') do
assert_equal "bA", "aZ".succ
assert_equal "1A", "0Z".succ
assert_equal "-", "-".succ
assert_equal ".", "-".succ
assert_equal "\x01\x00", "\xff".succ
assert_equal "-b", "-a".succ
assert_equal "-aa", "-z".succ
assert_equal "-a-b-", "-a-a-".succ
assert_equal "-b-", "-a-".succ
assert_equal "-aa-", "-z-".succ
assert_equal "あb", "あa".succ
assert_equal "あba", "あaz".succ
......@@ -341,13 +345,19 @@ assert('String#succ') do
assert_equal "1A", a
a = "-"; a.succ!
assert_equal "-", a
assert_equal ".", a
a = "\xff"; a.succ!
assert_equal "\x01\x00", a
a = "-a"; a.succ!
assert_equal "-b", a
a = "-z"; a.succ!
assert_equal "-aa", a
a = "あ"; a.succ!
assert_equal "あ", a
a = "-a-a-"; a.succ!
assert_equal "-a-b-", a
a = "-a-"; a.succ!
assert_equal "-b-", a
a = "-z-"; a.succ!
assert_equal "-aa-", a
a = "あa"; a.succ!
assert_equal "あb", a
a = "あaz"; a.succ!
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment