diff options
| author | Yukihiro Matz Matsumoto <[email protected]> | 2013-02-18 18:47:25 +0900 |
|---|---|---|
| committer | Yukihiro Matz Matsumoto <[email protected]> | 2013-02-18 18:47:25 +0900 |
| commit | 7ce9cc3b83f941843599a092e119688a40ddf6ef (patch) | |
| tree | a9dd380f06baeb707efe6f7f6cccb303015f4e86 /src | |
| parent | c9176abee3cba5e72683669f39e97f863c38e475 (diff) | |
| download | mruby-7ce9cc3b83f941843599a092e119688a40ddf6ef.tar.gz mruby-7ce9cc3b83f941843599a092e119688a40ddf6ef.zip | |
String#split should honor limit arg
Diffstat (limited to 'src')
| -rw-r--r-- | src/string.c | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/src/string.c b/src/string.c index 332d79376..6afb4f6ca 100644 --- a/src/string.c +++ b/src/string.c @@ -1868,11 +1868,12 @@ mrb_str_split_m(mrb_state *mrb, mrb_value str) int argc; mrb_value spat = mrb_nil_value(); enum {awk, string, regexp} split_type = string; - long beg, end, i = 0; - mrb_int lim = -1; + long beg, end, i = 0, lim_p; + mrb_int lim = 0; mrb_value result, tmp; argc = mrb_get_args(mrb, "|oi", &spat, &lim); + lim_p = (lim > 0 && argc == 2); if (argc == 2) { if (lim == 1) { if (RSTRING_LEN(str) == 0) @@ -1917,7 +1918,7 @@ mrb_str_split_m(mrb_state *mrb, mrb_value str) else { end = ptr - bptr; skip = 0; - if (lim >= 0 && lim <= i) break; + if (lim_p && lim <= i) break; } } else if (ascii_isspace(c)) { @@ -1925,7 +1926,7 @@ mrb_str_split_m(mrb_state *mrb, mrb_value str) mrb_gc_arena_restore(mrb, ai); skip = 1; beg = ptr - bptr; - if (lim >= 0) ++i; + if (lim_p) ++i; } else { end = ptr - bptr; @@ -1944,7 +1945,7 @@ mrb_str_split_m(mrb_state *mrb, mrb_value str) mrb_ary_push(mrb, result, mrb_str_subseq(mrb, str, ptr-temp, 1)); mrb_gc_arena_restore(mrb, ai); ptr++; - if (lim >= 0 && lim <= ++i) break; + if (lim_p && lim <= ++i) break; } } else { @@ -1956,7 +1957,7 @@ mrb_str_split_m(mrb_state *mrb, mrb_value str) mrb_ary_push(mrb, result, mrb_str_subseq(mrb, str, ptr - temp, end)); mrb_gc_arena_restore(mrb, ai); ptr += end + slen; - if (lim >= 0 && lim <= ++i) break; + if (lim_p && lim <= ++i) break; } } beg = ptr - temp; @@ -1964,14 +1965,16 @@ mrb_str_split_m(mrb_state *mrb, mrb_value str) else { mrb_raise(mrb, E_NOTIMP_ERROR, "Regexp Class not implemented"); } - if (RSTRING_LEN(str) > 0 && (lim >= 0 || RSTRING_LEN(str) > beg || lim < 0)) { - if (RSTRING_LEN(str) == beg) - tmp = mrb_str_new_empty(mrb, str); - else - tmp = mrb_str_subseq(mrb, str, beg, RSTRING_LEN(str)-beg); + if (RSTRING_LEN(str) > 0 && (lim_p || RSTRING_LEN(str) > beg || lim < 0)) { + if (RSTRING_LEN(str) == beg) { + tmp = mrb_str_new_empty(mrb, str); + } + else { + tmp = mrb_str_subseq(mrb, str, beg, RSTRING_LEN(str)-beg); + } mrb_ary_push(mrb, result, tmp); } - if (lim < 0) { + if (!lim_p && lim == 0) { long len; while ((len = RARRAY_LEN(result)) > 0 && (tmp = RARRAY_PTR(result)[len-1], RSTRING_LEN(tmp) == 0)) |
