Update `mrb_to_str` and related functions.

Contrary to the name, `mrb_to_str` just checks type, no conversion.
parent 98fc887c
...@@ -1186,6 +1186,7 @@ MRB_API void mrb_gc_unregister(mrb_state *mrb, mrb_value obj); ...@@ -1186,6 +1186,7 @@ MRB_API void mrb_gc_unregister(mrb_state *mrb, mrb_value obj);
MRB_API mrb_value mrb_to_int(mrb_state *mrb, mrb_value val); MRB_API mrb_value mrb_to_int(mrb_state *mrb, mrb_value val);
#define mrb_int(mrb, val) mrb_fixnum(mrb_to_int(mrb, val)) #define mrb_int(mrb, val) mrb_fixnum(mrb_to_int(mrb, val))
/* string type checking (contrary to the name, it doesn't convert) */
MRB_API mrb_value mrb_to_str(mrb_state *mrb, mrb_value val); MRB_API mrb_value mrb_to_str(mrb_state *mrb, mrb_value val);
MRB_API void mrb_check_type(mrb_state *mrb, mrb_value x, enum mrb_vtype t); MRB_API void mrb_check_type(mrb_state *mrb, mrb_value x, enum mrb_vtype t);
......
...@@ -499,30 +499,17 @@ mrb_notimplement_m(mrb_state *mrb, mrb_value self) ...@@ -499,30 +499,17 @@ mrb_notimplement_m(mrb_state *mrb, mrb_value self)
return mrb_nil_value(); return mrb_nil_value();
} }
#define CHECK_TYPE(mrb, val, t, c) do { \
if (mrb_type(val) != (t)) {\
mrb_raisef(mrb, E_TYPE_ERROR, "expected %l", c, sizeof(c "")-1);\
}\
} while (0)
static mrb_value
to_str(mrb_state *mrb, mrb_value val)
{
CHECK_TYPE(mrb, val, MRB_TT_STRING, "String");
return val;
}
static mrb_value static mrb_value
to_ary(mrb_state *mrb, mrb_value val) to_ary(mrb_state *mrb, mrb_value val)
{ {
CHECK_TYPE(mrb, val, MRB_TT_ARRAY, "Array"); mrb_check_type(mrb, val, MRB_TT_ARRAY);
return val; return val;
} }
static mrb_value static mrb_value
to_hash(mrb_state *mrb, mrb_value val) to_hash(mrb_state *mrb, mrb_value val)
{ {
CHECK_TYPE(mrb, val, MRB_TT_HASH, "Hash"); mrb_check_type(mrb, val, MRB_TT_HASH);
return val; return val;
} }
...@@ -686,7 +673,7 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) ...@@ -686,7 +673,7 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
} }
} }
if (i < argc) { if (i < argc) {
*p = to_str(mrb, ARGV[arg_i++]); *p = mrb_to_str(mrb, ARGV[arg_i++]);
i++; i++;
} }
} }
...@@ -747,7 +734,7 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) ...@@ -747,7 +734,7 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
} }
} }
if (i < argc) { if (i < argc) {
ss = to_str(mrb, ARGV[arg_i++]); ss = mrb_to_str(mrb, ARGV[arg_i++]);
*ps = RSTRING_PTR(ss); *ps = RSTRING_PTR(ss);
*pl = RSTRING_LEN(ss); *pl = RSTRING_LEN(ss);
i++; i++;
...@@ -769,7 +756,7 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) ...@@ -769,7 +756,7 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
} }
} }
if (i < argc) { if (i < argc) {
ss = to_str(mrb, ARGV[arg_i++]); ss = mrb_to_str(mrb, ARGV[arg_i++]);
*ps = RSTRING_CSTR(mrb, ss); *ps = RSTRING_CSTR(mrb, ss);
i++; i++;
} }
......
...@@ -1280,7 +1280,7 @@ mrb_str_aset(mrb_state *mrb, mrb_value str, mrb_value indx, mrb_value alen, mrb_ ...@@ -1280,7 +1280,7 @@ mrb_str_aset(mrb_state *mrb, mrb_value str, mrb_value indx, mrb_value alen, mrb_
{ {
mrb_int beg, len, charlen; mrb_int beg, len, charlen;
replace = mrb_to_str(mrb, replace); mrb_to_str(mrb, replace);
switch (str_convert_range(mrb, str, indx, alen, &beg, &len)) { switch (str_convert_range(mrb, str, indx, alen, &beg, &len)) {
case STR_OUT_OF_RANGE: case STR_OUT_OF_RANGE:
...@@ -2394,7 +2394,8 @@ mrb_str_to_inum(mrb_state *mrb, mrb_value str, mrb_int base, mrb_bool badcheck) ...@@ -2394,7 +2394,8 @@ mrb_str_to_inum(mrb_state *mrb, mrb_value str, mrb_int base, mrb_bool badcheck)
const char *s; const char *s;
mrb_int len; mrb_int len;
s = mrb_string_value_ptr(mrb, str); mrb_to_str(mrb, str);
s = RSTRING_PTR(str);
len = RSTRING_LEN(str); len = RSTRING_LEN(str);
return mrb_str_len_to_inum(mrb, s, len, base, badcheck); return mrb_str_len_to_inum(mrb, s, len, base, badcheck);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment