diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2017-08-12 10:36:54 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2017-08-18 22:17:47 +0900 |
| commit | 4c25738ac7de72b53e455288dc3104e24ca6869a (patch) | |
| tree | ae36726ebd143532eb05e96a4e79bc632b22612a /src/string.c | |
| parent | 2adb0c201ed4df3abc6ec0bd984aa1b4c3b305ab (diff) | |
| download | mruby-4c25738ac7de72b53e455288dc3104e24ca6869a.tar.gz mruby-4c25738ac7de72b53e455288dc3104e24ca6869a.zip | |
Merge `str_buf_cat` and `mrb_str_cat`.
Diffstat (limited to 'src/string.c')
| -rw-r--r-- | src/string.c | 87 |
1 files changed, 41 insertions, 46 deletions
diff --git a/src/string.c b/src/string.c index ae650865d..2415adb11 100644 --- a/src/string.c +++ b/src/string.c @@ -142,51 +142,6 @@ resize_capa(mrb_state *mrb, struct RString *s, size_t capacity) } } -static void -str_buf_cat(mrb_state *mrb, struct RString *s, const char *ptr, size_t len) -{ - size_t capa; - size_t total; - ptrdiff_t off = -1; - - if (len == 0) return; - mrb_str_modify(mrb, s); - if (ptr >= RSTR_PTR(s) && ptr <= RSTR_PTR(s) + (size_t)RSTR_LEN(s)) { - off = ptr - RSTR_PTR(s); - } - - capa = RSTR_CAPA(s); - if (capa <= RSTRING_EMBED_LEN_MAX) - capa = RSTRING_EMBED_LEN_MAX+1; - - total = RSTR_LEN(s)+len; - if (total >= MRB_INT_MAX) { - size_error: - mrb_raise(mrb, E_ARGUMENT_ERROR, "string size too big"); - } - if (capa <= total) { - while (total > capa) { - if (capa <= MRB_INT_MAX / 2) { - capa *= 2; - } - else { - capa = total; - } - } - if (capa < total || capa > MRB_INT_MAX) { - goto size_error; - } - resize_capa(mrb, s, capa); - } - if (off != -1) { - ptr = RSTR_PTR(s) + off; - } - memcpy(RSTR_PTR(s) + RSTR_LEN(s), ptr, len); - mrb_assert_int_fit(size_t, total, mrb_int, MRB_INT_MAX); - RSTR_SET_LEN(s, total); - RSTR_PTR(s)[total] = '\0'; /* sentinel */ -} - MRB_API mrb_value mrb_str_new(mrb_state *mrb, const char *p, size_t len) { @@ -2605,7 +2560,47 @@ mrb_str_dump(mrb_state *mrb, mrb_value str) MRB_API mrb_value mrb_str_cat(mrb_state *mrb, mrb_value str, const char *ptr, size_t len) { - str_buf_cat(mrb, mrb_str_ptr(str), ptr, len); + struct RString *s = mrb_str_ptr(str); + size_t capa; + size_t total; + ptrdiff_t off = -1; + + if (len == 0) return str; + mrb_str_modify(mrb, s); + if (ptr >= RSTR_PTR(s) && ptr <= RSTR_PTR(s) + (size_t)RSTR_LEN(s)) { + off = ptr - RSTR_PTR(s); + } + + capa = RSTR_CAPA(s); + if (capa <= RSTRING_EMBED_LEN_MAX) + capa = RSTRING_EMBED_LEN_MAX+1; + + total = RSTR_LEN(s)+len; + if (total >= MRB_INT_MAX) { + size_error: + mrb_raise(mrb, E_ARGUMENT_ERROR, "string size too big"); + } + if (capa <= total) { + while (total > capa) { + if (capa <= MRB_INT_MAX / 2) { + capa *= 2; + } + else { + capa = total; + } + } + if (capa < total || capa > MRB_INT_MAX) { + goto size_error; + } + resize_capa(mrb, s, capa); + } + if (off != -1) { + ptr = RSTR_PTR(s) + off; + } + memcpy(RSTR_PTR(s) + RSTR_LEN(s), ptr, len); + mrb_assert_int_fit(size_t, total, mrb_int, MRB_INT_MAX); + RSTR_SET_LEN(s, total); + RSTR_PTR(s)[total] = '\0'; /* sentinel */ return str; } |
