summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-string-ext/src/string.c
diff options
context:
space:
mode:
Diffstat (limited to 'mrbgems/mruby-string-ext/src/string.c')
-rw-r--r--mrbgems/mruby-string-ext/src/string.c63
1 files changed, 43 insertions, 20 deletions
diff --git a/mrbgems/mruby-string-ext/src/string.c b/mrbgems/mruby-string-ext/src/string.c
index edebcecbc..3ec23c51f 100644
--- a/mrbgems/mruby-string-ext/src/string.c
+++ b/mrbgems/mruby-string-ext/src/string.c
@@ -1,7 +1,7 @@
-#include "mruby.h"
-#include "mruby/string.h"
#include <ctype.h>
#include <string.h>
+#include "mruby.h"
+#include "mruby/string.h"
static mrb_value
mrb_str_getbyte(mrb_state *mrb, mrb_value str)
@@ -33,8 +33,8 @@ mrb_str_swapcase_bang(mrb_state *mrb, mrb_value str)
struct RString *s = mrb_str_ptr(str);
mrb_str_modify(mrb, s);
- p = s->ptr;
- pend = s->ptr + s->len;
+ p = RSTRING_PTR(str);
+ pend = p + RSTRING_LEN(str);
while (p < pend) {
if (ISUPPER(*p)) {
*p = TOLOWER(*p);
@@ -107,22 +107,27 @@ 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)
{
- 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;
+ 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(argv[i]);
- len_cmp = (len_l > len_r) ? len_r : len_l;
- if (memcmp(RSTRING_PTR(self), RSTRING_PTR(argv[i]), len_cmp) == 0) {
- return mrb_true_value();
- }
+ len_r = RSTRING_LEN(sub);
+ if (len_l >= len_r) {
+ if (memcmp(RSTRING_PTR(self), RSTRING_PTR(sub), len_r) == 0) {
+ return mrb_true_value();
+ }
+ }
}
return mrb_false_value();
}
@@ -136,24 +141,40 @@ 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;
+ 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(argv[i]);
- 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),
- len_cmp) == 0) {
- return mrb_true_value();
- }
+ len_r = RSTRING_LEN(sub);
+ 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();
}
+static mrb_value
+mrb_str_hex(mrb_state *mrb, mrb_value self)
+{
+ return mrb_str_to_inum(mrb, self, 16, FALSE);
+}
+
+static mrb_value
+mrb_str_oct(mrb_state *mrb, mrb_value self)
+{
+ return mrb_str_to_inum(mrb, self, 8, FALSE);
+}
+
void
mrb_mruby_string_ext_gem_init(mrb_state* mrb)
{
@@ -167,6 +188,8 @@ mrb_mruby_string_ext_gem_init(mrb_state* mrb)
mrb_define_method(mrb, s, "<<", mrb_str_concat2, MRB_ARGS_REQ(1));
mrb_define_method(mrb, s, "start_with?", mrb_str_start_with, MRB_ARGS_REST());
mrb_define_method(mrb, s, "end_with?", mrb_str_end_with, MRB_ARGS_REST());
+ mrb_define_method(mrb, s, "hex", mrb_str_hex, MRB_ARGS_NONE());
+ mrb_define_method(mrb, s, "oct", mrb_str_oct, MRB_ARGS_NONE());
}
void