mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Check type of arguments for #start_with and #end_with.
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user