summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-string-ext/src
diff options
context:
space:
mode:
Diffstat (limited to 'mrbgems/mruby-string-ext/src')
-rw-r--r--mrbgems/mruby-string-ext/src/string.c85
1 files changed, 71 insertions, 14 deletions
diff --git a/mrbgems/mruby-string-ext/src/string.c b/mrbgems/mruby-string-ext/src/string.c
index 09cbf7995..e3dabcc18 100644
--- a/mrbgems/mruby-string-ext/src/string.c
+++ b/mrbgems/mruby-string-ext/src/string.c
@@ -35,7 +35,7 @@ str_casecmp_p(const char *s1, mrb_int len1, const char *s2, mrb_int len2)
static mrb_value
int_chr_binary(mrb_state *mrb, mrb_value num)
{
- mrb_int cp = mrb_int(mrb, num);
+ mrb_int cp = mrb_as_int(mrb, num);
char c;
mrb_value str;
@@ -195,14 +195,14 @@ static mrb_value
mrb_str_start_with(mrb_state *mrb, mrb_value self)
{
const mrb_value *argv;
- mrb_value sub;
mrb_int argc, i;
mrb_get_args(mrb, "*", &argv, &argc);
for (i = 0; i < argc; i++) {
size_t len_l, len_r;
int ai = mrb_gc_arena_save(mrb);
- sub = mrb_ensure_string_type(mrb, argv[i]);
+ mrb_value sub = argv[i];
+ mrb_ensure_string_type(mrb, sub);
mrb_gc_arena_restore(mrb, ai);
len_l = RSTRING_LEN(self);
len_r = RSTRING_LEN(sub);
@@ -225,14 +225,14 @@ static mrb_value
mrb_str_end_with(mrb_state *mrb, mrb_value self)
{
const mrb_value *argv;
- mrb_value sub;
mrb_int argc, i;
mrb_get_args(mrb, "*", &argv, &argc);
for (i = 0; i < argc; i++) {
size_t len_l, len_r;
int ai = mrb_gc_arena_save(mrb);
- sub = mrb_ensure_string_type(mrb, argv[i]);
+ mrb_value sub = argv[i];
+ mrb_ensure_string_type(mrb, sub);
mrb_gc_arena_restore(mrb, ai);
len_l = RSTRING_LEN(self);
len_r = RSTRING_LEN(sub);
@@ -262,7 +262,7 @@ enum tr_pattern_type {
<range> ::= <ch> '-' <ch>
*/
struct tr_pattern {
- uint8_t type; // 1:in-order, 2:range
+ uint8_t type; // 1:in-order, 2:range
mrb_bool flag_reverse : 1;
mrb_bool flag_on_heap : 1;
uint16_t n;
@@ -329,9 +329,9 @@ tr_parse_pattern(mrb_state *mrb, struct tr_pattern *ret, const mrb_value v_patte
mrb_int len;
while (i < pattern_length) {
- if ((i+2) < pattern_length && pattern[i] != '\\' && pattern[i+1] == '-')
+ if ((i+2) < pattern_length && pattern[i] != '\\' && pattern[i+1] == '-')
break;
- i++;
+ i++;
}
len = i - start_pos;
@@ -375,7 +375,7 @@ tr_find_character(const struct tr_pattern *pat, const char *pat_str, int ch)
if (pat->type == TR_IN_ORDER) {
int i;
for (i = 0; i < pat->n; i++) {
- if (pat_str[pat->val.start_pos + i] == ch) ret = n_sum + i;
+ if (pat_str[pat->val.start_pos + i] == ch) ret = n_sum + i;
}
}
else if (pat->type == TR_RANGE) {
@@ -509,7 +509,7 @@ str_tr(mrb_state *mrb, mrb_value str, mrb_value p1, mrb_value p2, mrb_bool squee
if (n >= 0) {
flag_changed = TRUE;
if (rep == NULL) {
- j--;
+ j--;
}
else {
mrb_int c = tr_get_character(rep, RSTRING_PTR(p2), n);
@@ -521,8 +521,8 @@ str_tr(mrb_state *mrb, mrb_value str, mrb_value p1, mrb_value p2, mrb_bool squee
if (c > 0x80) {
mrb_raisef(mrb, E_ARGUMENT_ERROR, "character (%i) out of range", c);
}
- lastch = c;
- s[i] = (char)c;
+ lastch = c;
+ s[i] = (char)c;
}
}
}
@@ -835,13 +835,13 @@ mrb_str_count(mrb_state *mrb, mrb_value str)
static mrb_value
mrb_str_hex(mrb_state *mrb, mrb_value self)
{
- return mrb_str_to_inum(mrb, self, 16, FALSE);
+ return mrb_str_to_integer(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);
+ return mrb_str_to_integer(mrb, self, 8, FALSE);
}
/*
@@ -1174,6 +1174,60 @@ mrb_str_del_suffix(mrb_state *mrb, mrb_value self)
return mrb_str_substr(mrb, self, 0, slen-plen);
}
+#define lesser(a,b) (((a)>(b))?(b):(a))
+
+/*
+ * call-seq:
+ * str.casecmp(other_str) -> -1, 0, +1 or nil
+ *
+ * Case-insensitive version of <code>String#<=></code>.
+ *
+ * "abcdef".casecmp("abcde") #=> 1
+ * "aBcDeF".casecmp("abcdef") #=> 0
+ * "abcdef".casecmp("abcdefg") #=> -1
+ * "abcdef".casecmp("ABCDEF") #=> 0
+ */
+static mrb_value
+mrb_str_casecmp(mrb_state *mrb, mrb_value self)
+{
+ mrb_value str;
+
+ mrb_get_args(mrb, "o", &str);
+ if (!mrb_string_p(str)) return mrb_nil_value();
+
+ struct RString *s1 = mrb_str_ptr(self);
+ struct RString *s2 = mrb_str_ptr(str);
+ mrb_int len = lesser(RSTR_LEN(s1), RSTR_LEN(s2));
+ char *p1 = RSTR_PTR(s1);
+ char *p2 = RSTR_PTR(s2);
+
+ for (mrb_int i=0; i<len; i++) {
+ int c1 = p1[i], c2 = p2[i];
+ if (ISASCII(c1) && ISUPPER(c1)) c1 = TOLOWER(c1);
+ if (ISASCII(c2) && ISUPPER(c2)) c2 = TOLOWER(c2);
+ if (c1 > c2) return mrb_fixnum_value(1);
+ if (c1 < c2) return mrb_fixnum_value(-1);
+ }
+ if (RSTR_LEN(s1) == RSTR_LEN(s2)) return mrb_fixnum_value(0);
+ if (RSTR_LEN(s1) > RSTR_LEN(s2)) return mrb_fixnum_value(1);
+ return mrb_fixnum_value(-1);
+}
+
+/*
+ * call-seq:
+ * str.casecmp?(other) -> true, false, or nil
+ *
+ * Returns true if str and other_str are equal after case folding,
+ * false if they are not equal, and nil if other is not a string.
+ */
+static mrb_value
+mrb_str_casecmp_p(mrb_state *mrb, mrb_value self)
+{
+ mrb_value c = mrb_str_casecmp(mrb, self);
+ if (mrb_nil_p(c)) return c;
+ return mrb_bool_value(mrb_fixnum(c) == 0);
+}
+
static mrb_value
mrb_str_lines(mrb_state *mrb, mrb_value self)
{
@@ -1184,6 +1238,7 @@ mrb_str_lines(mrb_state *mrb, mrb_value self)
char *p = b, *t;
char *e = b + RSTRING_LEN(self);
+ mrb->c->ci->mid = 0;
result = mrb_ary_new(mrb);
ai = mrb_gc_arena_save(mrb);
while (p < e) {
@@ -1230,6 +1285,8 @@ mrb_mruby_string_ext_gem_init(mrb_state* mrb)
mrb_define_method(mrb, s, "delete_prefix", mrb_str_del_prefix, MRB_ARGS_REQ(1));
mrb_define_method(mrb, s, "delete_suffix!", mrb_str_del_suffix_bang, MRB_ARGS_REQ(1));
mrb_define_method(mrb, s, "delete_suffix", mrb_str_del_suffix, MRB_ARGS_REQ(1));
+ mrb_define_method(mrb, s, "casecmp", mrb_str_casecmp, MRB_ARGS_REQ(1));
+ mrb_define_method(mrb, s, "casecmp?", mrb_str_casecmp_p, MRB_ARGS_REQ(1));
mrb_define_method(mrb, s, "__lines", mrb_str_lines, MRB_ARGS_NONE());