diff options
| author | Tomoyuki Sahara <[email protected]> | 2013-08-23 13:42:43 +0900 |
|---|---|---|
| committer | Tomoyuki Sahara <[email protected]> | 2013-08-23 15:06:26 +0900 |
| commit | 0e8efd4a218f3d235f88054f0d9d11f8bfdcfd33 (patch) | |
| tree | e93b441e955f5d835b55f8df67a591249d907297 | |
| parent | 53ff8172e549ebd90ca11d0a4f6e6a57bbd2edf9 (diff) | |
| download | mruby-0e8efd4a218f3d235f88054f0d9d11f8bfdcfd33.tar.gz mruby-0e8efd4a218f3d235f88054f0d9d11f8bfdcfd33.zip | |
Check type of arguments for #start_with and #end_with.
| -rw-r--r-- | mrbgems/mruby-string-ext/src/string.c | 18 | ||||
| -rw-r--r-- | mrbgems/mruby-string-ext/test/string.rb | 2 |
2 files changed, 14 insertions, 6 deletions
diff --git a/mrbgems/mruby-string-ext/src/string.c b/mrbgems/mruby-string-ext/src/string.c index edebcecbc..4bb9b9621 100644 --- a/mrbgems/mruby-string-ext/src/string.c +++ b/mrbgems/mruby-string-ext/src/string.c @@ -111,16 +111,19 @@ mrb_str_concat2(mrb_state *mrb, mrb_value self) static mrb_value mrb_str_start_with(mrb_state *mrb, mrb_value self) { - mrb_value *argv; + mrb_value *argv, sub; int argc, i; mrb_get_args(mrb, "*", &argv, &argc); for (i = 0; i < argc; i++) { size_t len_l, len_r, len_cmp; + int ai = mrb_gc_arena_save(mrb); + sub = mrb_string_type(mrb, argv[i]); + mrb_gc_arena_restore(mrb, ai); len_l = RSTRING_LEN(self); - len_r = RSTRING_LEN(argv[i]); + len_r = RSTRING_LEN(sub); len_cmp = (len_l > len_r) ? len_r : len_l; - if (memcmp(RSTRING_PTR(self), RSTRING_PTR(argv[i]), len_cmp) == 0) { + if (memcmp(RSTRING_PTR(self), RSTRING_PTR(sub), len_cmp) == 0) { return mrb_true_value(); } } @@ -136,17 +139,20 @@ mrb_str_start_with(mrb_state *mrb, mrb_value self) static mrb_value mrb_str_end_with(mrb_state *mrb, mrb_value self) { - mrb_value *argv; + mrb_value *argv, sub; int argc, i; mrb_get_args(mrb, "*", &argv, &argc); for (i = 0; i < argc; i++) { size_t len_l, len_r, len_cmp; + int ai = mrb_gc_arena_save(mrb); + sub = mrb_string_type(mrb, argv[i]); + mrb_gc_arena_restore(mrb, ai); len_l = RSTRING_LEN(self); - len_r = RSTRING_LEN(argv[i]); + len_r = RSTRING_LEN(sub); len_cmp = (len_l > len_r) ? len_r : len_l; if (memcmp(RSTRING_PTR(self) + (len_l - len_cmp), - RSTRING_PTR(argv[i]) + (len_r - len_cmp), + RSTRING_PTR(sub) + (len_r - len_cmp), len_cmp) == 0) { return mrb_true_value(); } diff --git a/mrbgems/mruby-string-ext/test/string.rb b/mrbgems/mruby-string-ext/test/string.rb index 9eb6f6aaa..496730a0e 100644 --- a/mrbgems/mruby-string-ext/test/string.rb +++ b/mrbgems/mruby-string-ext/test/string.rb @@ -104,9 +104,11 @@ end assert('String#start_with?') do assert_true "hello".start_with?("heaven", "hell") assert_true !"hello".start_with?("heaven", "paradise") + assert_raise TypeError do "hello".start_with?(true) end end assert('String#end_with?') do assert_true "string".end_with?("ing", "mng") assert_true !"string".end_with?("str", "tri") + assert_raise TypeError do "hello".end_with?(true) end end |
