diff options
Diffstat (limited to 'src/string.c')
| -rw-r--r-- | src/string.c | 80 |
1 files changed, 37 insertions, 43 deletions
diff --git a/src/string.c b/src/string.c index ab53c2e69..247ee78c2 100644 --- a/src/string.c +++ b/src/string.c @@ -24,7 +24,7 @@ typedef struct mrb_shared_string { int refcnt; - mrb_int len; + mrb_int capa; char *ptr; } mrb_shared_string; @@ -77,9 +77,9 @@ str_init_shared(mrb_state *mrb, const struct RString *orig, struct RString *s, m } else { shared = (mrb_shared_string *)mrb_malloc(mrb, sizeof(mrb_shared_string)); - shared->refcnt = 2; + shared->refcnt = 1; shared->ptr = orig->as.heap.ptr; - shared->len = orig->as.heap.len; + shared->capa = orig->as.heap.aux.capa; } s->as.heap.ptr = orig->as.heap.ptr; s->as.heap.len = orig->as.heap.len; @@ -516,7 +516,7 @@ mrb_memsearch(const void *x0, mrb_int m, const void *y0, mrb_int n) static void str_make_shared(mrb_state *mrb, struct RString *orig, struct RString *s) { - mrb_int len = RSTR_LEN(orig); + size_t len = (size_t)orig->as.heap.len; mrb_assert(!RSTR_EMBED_P(orig)); if (RSTR_NOFREE_P(orig)) { @@ -534,10 +534,10 @@ str_make_shared(mrb_state *mrb, struct RString *orig, struct RString *s) else { if (orig->as.heap.aux.capa > orig->as.heap.len) { orig->as.heap.ptr = (char *)mrb_realloc(mrb, orig->as.heap.ptr, len+1); + orig->as.heap.aux.capa = len; } str_init_shared(mrb, orig, s, NULL); - orig->as.heap.aux.shared = s->as.heap.aux.shared; - RSTR_SET_TYPE_FLAG(orig, SHARED); + str_init_shared(mrb, orig, orig, s->as.heap.aux.shared); } } @@ -780,7 +780,7 @@ mrb_str_modify_keep_ascii(mrb_state *mrb, struct RString *s) mrb_shared_string *shared = s->as.heap.aux.shared; if (shared->refcnt == 1 && s->as.heap.ptr == shared->ptr) { - s->as.heap.aux.capa = shared->len; + s->as.heap.aux.capa = shared->capa; s->as.heap.ptr[s->as.heap.len] = '\0'; mrb_free(mrb, shared); } @@ -1759,28 +1759,25 @@ mrb_str_include(mrb_state *mrb, mrb_value self) static mrb_value mrb_str_index_m(mrb_state *mrb, mrb_value str) { - mrb_value *argv; - mrb_int argc; mrb_value sub; - mrb_int pos, clen; + mrb_int pos; - mrb_get_args(mrb, "*!", &argv, &argc); - if (argc == 2) { - mrb_get_args(mrb, "oi", &sub, &pos); - } - else { - pos = 0; - if (argc > 0) - sub = argv[0]; - else + switch (mrb_get_args(mrb, "|oi", &sub, &pos)) { + case 0: sub = mrb_nil_value(); - } - if (pos < 0) { - clen = RSTRING_CHAR_LEN(str); - pos += clen; - if (pos < 0) { - return mrb_nil_value(); - } + /* fall through */ + case 1: + pos = 0; + break; + case 2: + if (pos < 0) { + mrb_int clen = RSTRING_CHAR_LEN(str); + pos += clen; + if (pos < 0) { + return mrb_nil_value(); + } + } + break; } switch (mrb_type(sub)) { @@ -2009,28 +2006,25 @@ mrb_str_reverse(mrb_state *mrb, mrb_value str) static mrb_value mrb_str_rindex(mrb_state *mrb, mrb_value str) { - mrb_value *argv; - mrb_int argc; mrb_value sub; mrb_int pos, len = RSTRING_CHAR_LEN(str); - mrb_get_args(mrb, "*!", &argv, &argc); - if (argc == 2) { - mrb_get_args(mrb, "oi", &sub, &pos); - if (pos < 0) { - pos += len; + switch (mrb_get_args(mrb, "|oi", &sub, &pos)) { + case 0: + sub = mrb_nil_value(); + /* fall through */ + case 1: + pos = len; + break; + case 2: if (pos < 0) { - return mrb_nil_value(); + pos += len; + if (pos < 0) { + return mrb_nil_value(); + } } - } - if (pos > len) pos = len; - } - else { - pos = len; - if (argc > 0) - sub = argv[0]; - else - sub = mrb_nil_value(); + if (pos > len) pos = len; + break; } pos = chars2bytes(str, 0, pos); |
