From 3055a3bc8c82a27c72f0fdf9fc2c52b385c37a16 Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Thu, 31 May 2012 16:34:57 +0900 Subject: reimplement String#* --- src/string.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/string.c b/src/string.c index 774277d20..a42565084 100644 --- a/src/string.c +++ b/src/string.c @@ -402,7 +402,33 @@ mrb_str_size(mrb_state *mrb, mrb_value self) static mrb_value mrb_str_times(mrb_state *mrb, mrb_value self) { - return mrb_nil_value(); + mrb_value str2; + mrb_int n,len,times; + char *ptr2; + + mrb_get_args(mrb, "i", ×); + if (times < 0) { + mrb_raise(mrb, E_ARGUMENT_ERROR, "negative argument"); + } + if (times && INT32_MAX/times < RSTRING_LEN(self)) { + mrb_raise(mrb, E_ARGUMENT_ERROR, "argument too big"); + } + + len = RSTRING_LEN(self)*times; + str2 = mrb_str_new_with_class(mrb, self, 0, len); + ptr2 = RSTRING_PTR(str2); + if (len > 0) { + n = RSTRING_LEN(self); + memcpy(ptr2, RSTRING_PTR(self), n); + while (n <= len/2) { + memcpy(ptr2 + n, ptr2, n); + n *= 2; + } + memcpy(ptr2 + n, ptr2, len-n); + } + ptr2[RSTRING_LEN(str2)] = '\0'; + + return str2; } /* -------------------------------------------------------------- */ -- cgit v1.2.3