Merge pull request #836 from iij/pr-mrb_str_to_cstr

string contains null byte check utility function (mrb_str_to_cstr)
This commit is contained in:
Yukihiro "Matz" Matsumoto
2013-02-11 08:38:33 -08:00
2 changed files with 13 additions and 0 deletions
+1
View File
@@ -80,6 +80,7 @@ mrb_value mrb_str_cat(mrb_state *mrb, mrb_value str, const char *ptr, long len);
mrb_value mrb_str_append(mrb_state *mrb, mrb_value str, mrb_value str2);
int mrb_str_cmp(mrb_state *mrb, mrb_value str1, mrb_value str2);
char *mrb_str_to_cstr(mrb_state *mrb, mrb_value str);
#if defined(__cplusplus)
} /* extern "C" { */
+12
View File
@@ -257,6 +257,18 @@ mrb_str_new_cstr(mrb_state *mrb, const char *p)
return mrb_obj_value(s);
}
char *
mrb_str_to_cstr(mrb_state *mrb, mrb_value str0)
{
mrb_value str;
str = mrb_str_new(mrb, RSTRING_PTR(str0), RSTRING_LEN(str0));
if (strlen(RSTRING_PTR(str)) != RSTRING_LEN(str)) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "string contains null byte");
}
return RSTRING_PTR(str);
}
static void
str_make_shared(mrb_state *mrb, struct RString *s)
{