Unify `mrb_str_to_str` to `mrb_obj_as_string`.
Redirect `mrb_str_to_str` to `mrb_obj_as_string` via C macro.
Inspired by
MODIFY
include/mruby/string.h
include/mruby/string.h
@@ -377,8 +377,9 @@ MRB_API double mrb_cstr_to_dbl(mrb_state *mrb, const char *s, mrb_bool badcheck)
/**
* Returns a converted string type.
* For type checking, non converting `mrb_to_str` is recommended.
+ * obsolete: use `mrb_obj_as_string()` instead.
*/
-MRB_API mrb_value mrb_str_to_str(mrb_state *mrb, mrb_value str);
+#define mrb_str_to_str(mrb, str) mrb_obj_as_string(mrb, str)
/**
* Returns true if the strings match and false if the strings don't match.

MODIFY
mrbgems/mruby-print/src/print.c
mrbgems/mruby-print/src/print.c
@@ -57,7 +57,7 @@ mrb_print(mrb_state *mrb, mrb_value self)
mrb_get_args(mrb, "*", &argv, &argc);
for (i=0; i<argc; i++) {
- mrb_value s = mrb_str_to_str(mrb, argv[i]);
+ mrb_value s = mrb_obj_as_string(mrb, argv[i]);
printstr(mrb, RSTRING_PTR(s), RSTRING_LEN(s));
}
return mrb_nil_value();
@@ -73,7 +73,7 @@ mrb_puts(mrb_state *mrb, mrb_value self)
mrb_get_args(mrb, "*", &argv, &argc);
for (i=0; i<argc; i++) {
- mrb_value s = mrb_str_to_str(mrb, argv[i]);
+ mrb_value s = mrb_obj_as_string(mrb, argv[i]);
mrb_int len = RSTRING_LEN(s);
printstr(mrb, RSTRING_PTR(s), len);
if (len == 0 || RSTRING_PTR(s)[len-1] != '\n') {

MODIFY
src/string.c
src/string.c
@@ -872,7 +872,7 @@ mrb_str_to_cstr(mrb_state *mrb, mrb_value str0)
MRB_API void
mrb_str_concat(mrb_state *mrb, mrb_value self, mrb_value other)
{
- other = mrb_str_to_str(mrb, other);
+ other = mrb_obj_as_string(mrb, other);
mrb_str_cat_str(mrb, self, other);
}
@@ -1085,30 +1085,11 @@ mrb_str_equal_m(mrb_state *mrb, mrb_value str1)
}
/* ---------------------------------- */
-MRB_API mrb_value
-mrb_str_to_str(mrb_state *mrb, mrb_value str)
-{
- switch (mrb_type(str)) {
- case MRB_TT_STRING:
- return str;
- case MRB_TT_SYMBOL:
- return mrb_sym_str(mrb, mrb_symbol(str));
- case MRB_TT_INTEGER:
- return mrb_fixnum_to_str(mrb, str, 10);
- case MRB_TT_SCLASS:
- case MRB_TT_CLASS:
- case MRB_TT_MODULE:
- return mrb_mod_to_s(mrb, str);
- default:
- return mrb_type_convert(mrb, str, MRB_TT_STRING, MRB_SYM(to_s));
- }
-}
-
/* obslete: use RSTRING_PTR() */
MRB_API const char*
mrb_string_value_ptr(mrb_state *mrb, mrb_value str)
{
- str = mrb_str_to_str(mrb, str);
+ str = mrb_obj_as_string(mrb, str);
return RSTRING_PTR(str);
}
@@ -1937,7 +1918,20 @@ mrb_str_intern(mrb_state *mrb, mrb_value self)
MRB_API mrb_value
mrb_obj_as_string(mrb_state *mrb, mrb_value obj)
{
- return mrb_str_to_str(mrb, obj);
+ switch (mrb_type(obj)) {
+ case MRB_TT_STRING:
+ return obj;
+ case MRB_TT_SYMBOL:
+ return mrb_sym_str(mrb, mrb_symbol(obj));
+ case MRB_TT_INTEGER:
+ return mrb_fixnum_to_str(mrb, obj, 10);
+ case MRB_TT_SCLASS:
+ case MRB_TT_CLASS:
+ case MRB_TT_MODULE:
+ return mrb_mod_to_s(mrb, obj);
+ default:
+ return mrb_type_convert(mrb, obj, MRB_TT_STRING, MRB_SYM(to_s));
+ }
}
MRB_API mrb_value

Unify `mrb_str_to_str` to `mrb_obj_as_string`.

Redirect `mrb_str_to_str` to `mrb_obj_as_string` via C macro.
Inspired by #5082