summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-string-ext/src/string.c
diff options
context:
space:
mode:
authorTomoyuki Sahara <[email protected]>2013-08-26 23:20:22 +0900
committerTomoyuki Sahara <[email protected]>2013-08-26 23:20:22 +0900
commitf77dc2e6cb2c2ea21c6cca96f88d06acfbf30d17 (patch)
treebc0f3ab1cdb846fa004f6a19fc706492895a5a7d /mrbgems/mruby-string-ext/src/string.c
parent0e8efd4a218f3d235f88054f0d9d11f8bfdcfd33 (diff)
parent91b878e5dfa671b63c26019962736dda1a58f9fc (diff)
downloadmruby-f77dc2e6cb2c2ea21c6cca96f88d06acfbf30d17.tar.gz
mruby-f77dc2e6cb2c2ea21c6cca96f88d06acfbf30d17.zip
Merge branch 'mruby' into pr-typeerror-string-start-end-with
Conflicts: mrbgems/mruby-string-ext/src/string.c mrbgems/mruby-string-ext/test/string.rb
Diffstat (limited to 'mrbgems/mruby-string-ext/src/string.c')
-rw-r--r--mrbgems/mruby-string-ext/src/string.c27
1 files changed, 15 insertions, 12 deletions
diff --git a/mrbgems/mruby-string-ext/src/string.c b/mrbgems/mruby-string-ext/src/string.c
index 4bb9b9621..6718e734a 100644
--- a/mrbgems/mruby-string-ext/src/string.c
+++ b/mrbgems/mruby-string-ext/src/string.c
@@ -107,6 +107,7 @@ mrb_str_concat2(mrb_state *mrb, mrb_value self)
* # returns true if one of the prefixes matches.
* "hello".start_with?("heaven", "hell") #=> true
* "hello".start_with?("heaven", "paradise") #=> false
+ * "h".start_with?("heaven", "hell") #=> false
*/
static mrb_value
mrb_str_start_with(mrb_state *mrb, mrb_value self)
@@ -116,16 +117,17 @@ mrb_str_start_with(mrb_state *mrb, mrb_value self)
mrb_get_args(mrb, "*", &argv, &argc);
for (i = 0; i < argc; i++) {
- size_t len_l, len_r, len_cmp;
+ size_t len_l, len_r;
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(sub);
- len_cmp = (len_l > len_r) ? len_r : len_l;
- if (memcmp(RSTRING_PTR(self), RSTRING_PTR(sub), len_cmp) == 0) {
- return mrb_true_value();
- }
+ if (len_l >= len_r) {
+ if (memcmp(RSTRING_PTR(self), RSTRING_PTR(sub), len_r) == 0) {
+ return mrb_true_value();
+ }
+ }
}
return mrb_false_value();
}
@@ -144,18 +146,19 @@ mrb_str_end_with(mrb_state *mrb, mrb_value self)
mrb_get_args(mrb, "*", &argv, &argc);
for (i = 0; i < argc; i++) {
- size_t len_l, len_r, len_cmp;
+ size_t len_l, len_r;
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(sub);
- len_cmp = (len_l > len_r) ? len_r : len_l;
- if (memcmp(RSTRING_PTR(self) + (len_l - len_cmp),
- RSTRING_PTR(sub) + (len_r - len_cmp),
- len_cmp) == 0) {
- return mrb_true_value();
- }
+ if (len_l >= len_r) {
+ if (memcmp(RSTRING_PTR(self) + (len_l - len_r),
+ RSTRING_PTR(sub),
+ len_r) == 0) {
+ return mrb_true_value();
+ }
+ }
}
return mrb_false_value();
}