From 6423011986aca8e71508a8ffe6df823e44c196bd Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sun, 9 Mar 2014 02:35:49 +0900 Subject: remove invocation of strlen() on buffer of strings; with refactoring --- src/string.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'src/string.c') diff --git a/src/string.c b/src/string.c index 123a1dcb5..5f1f5b50b 100644 --- a/src/string.c +++ b/src/string.c @@ -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= 0); - mrb_assert((size_t)len <= SIZE_MAX); - if (!s || (size_t)len != 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 -- cgit v1.2.3