diff options
| -rw-r--r-- | include/mrbconf.h | 7 | ||||
| -rw-r--r-- | mrblib/string.rb | 6 | ||||
| -rw-r--r-- | src/string.c | 27 | ||||
| -rw-r--r-- | test/t/string.rb | 2 |
4 files changed, 21 insertions, 21 deletions
diff --git a/include/mrbconf.h b/include/mrbconf.h index 62cc73a74..b5cb58ee6 100644 --- a/include/mrbconf.h +++ b/include/mrbconf.h @@ -44,13 +44,12 @@ //#define POOL_PAGE_SIZE 16000 /* -DDISABLE_XXXX to drop the feature */ -//#define DISABLE_REGEXP /* regular expression classes */ //#define DISABLE_SPRINTF /* Kernel.sprintf method */ //#define DISABLE_MATH /* Math functions */ //#define DISABLE_TIME /* Time class */ //#define DISABLE_STRUCT /* Struct class */ //#define DISABLE_STDIO /* use of stdio */ -//#define DISABLE_DEBUG /* hooks for debugger */ +//#define ENABLE_DEBUG /* hooks for debugger */ /* Now DISABLE_GEMS is added as a command line flag in Rakefile, */ /* we do not need to set it here. */ @@ -119,8 +118,8 @@ typedef short mrb_sym; #ifndef DISABLE_STDIO #define ENABLE_STDIO #endif -#ifndef DISABLE_DEBUG -#define ENABLE_DEBUG +#ifndef ENABLE_DEBUG +#define DISABLE_DEBUG #endif #ifndef FALSE diff --git a/mrblib/string.rb b/mrblib/string.rb index 8a4894dd4..131d30446 100644 --- a/mrblib/string.rb +++ b/mrblib/string.rb @@ -28,12 +28,10 @@ class String # # ISO 15.2.10.5.18 def gsub(*args, &block) - lc = '' if args.size == 2 - lc = args[1] if self[-1] == args[0] - split(args[0]).join(args[1]) + lc + split(args[0], -1).join(args[1]) elsif args.size == 1 && block - split(args[0]).join(block.call(args[0])) + split(args[0], -1).join(block.call(args[0])) else raise ArgumentError, "wrong number of arguments" end 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)) diff --git a/test/t/string.rb b/test/t/string.rb index 7d0b147d0..b2a70266b 100644 --- a/test/t/string.rb +++ b/test/t/string.rb @@ -205,7 +205,7 @@ assert('String#gsub!', '15.2.10.5.19') do b = 'abcabc' b.gsub!('b') { |w| w.capitalize } - a == 'aBcaBc' && b == 'aBcaBc' + a == 'aBcaBc' && b == 'aBcaBc' end assert('String#hash', '15.2.10.5.20') do |
