diff options
Diffstat (limited to 'src/string.c')
| -rw-r--r-- | src/string.c | 29 |
1 files changed, 23 insertions, 6 deletions
diff --git a/src/string.c b/src/string.c index 85a3cc2a7..dff8de90f 100644 --- a/src/string.c +++ b/src/string.c @@ -41,7 +41,7 @@ const char mrb_digitmap[] = "0123456789abcdefghijklmnopqrstuvwxyz"; typedef struct mrb_shared_string { - mrb_bool nofree; + mrb_bool nofree : 1; int refcnt; char *ptr; mrb_int len; @@ -54,6 +54,21 @@ typedef struct mrb_shared_string { static mrb_value str_replace(mrb_state *mrb, struct RString *s1, struct RString *s2); static mrb_value mrb_str_subseq(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len); +mrb_int +mrb_str_strlen(mrb_state *mrb, struct RString *s) +{ + mrb_int i, max = STR_LEN(s); + char *p = STR_PTR(s); + + if (!p) return 0; + for (i=0; i<max; i++) { + if (p[i] == '\0') { + mrb_raise(mrb, E_ARGUMENT_ERROR, "string contains null byte"); + } + } + return max; +} + #define RESIZE_CAPA(s,capacity) do {\ if (STR_EMBED_P(s)) {\ if (RSTRING_EMBED_LEN_MAX < (capacity)) {\ @@ -2066,12 +2081,14 @@ char * mrb_string_value_cstr(mrb_state *mrb, mrb_value *ptr) { struct RString *ps = mrb_str_ptr(*ptr); - char *s = STR_PTR(ps); + mrb_int len = mrb_str_strlen(mrb, ps); + char *p = STR_PTR(ps); - if (!s || STR_LEN(ps) != strlen(s)) { - mrb_raise(mrb, E_ARGUMENT_ERROR, "string contains null byte"); + if (!p || p[len] != '\0') { + mrb_str_modify(mrb, ps); + return STR_PTR(ps); } - return s; + return p; } mrb_value @@ -2518,7 +2535,7 @@ mrb_str_inspect(mrb_state *mrb, mrb_value str) continue; } } - mrb_str_buf_cat(mrb, result, "\"", 1); + mrb_str_cat_lit(mrb, result, "\""); return result; } |
