Commits

Anonymous committed 8759fba

Implement string multiplication.

Comments (0)

Files changed (1)

     return (AR)self;
 }
 
-/* invoked by AR_LIST_CONCAT */
+/* invoked by AR_STRING_CONCAT */
 AR
 _arana_api_string_concat(AR_SIG, AR self, AR other)
 {
 }
 
 static AR
+arana_string_mul(AR_SIG, AR self, AR args)
+{
+    AR other;
+    AR_PARSE(args, "I::mul", &other);
+    unsigned long times = AR_AS_LONG(other);
+    if (times < 0)
+        times = 0;
+    Ar_size_t size = AR_STRING_SIZE(self);
+    Ar_size_t totalsize = size * times;
+    char *result = AR_ALLOC(totalsize + 1);
+    char *cur = result;
+    if (!result)
+        AR_RAISE_OOM();
+    for (int i = 0; i < times; ++i) {
+        memcpy(cur, AR_AS_CHARP(self), size);
+        cur += size;
+    }
+    return AR_STRING2_NOCOPY(result, totalsize);
+}
+
+static AR
 arana_string_to_string(AR_SIG, AR self, AR args)
 {
     AR_ASSERT_EMPTY_ARGS(args, "to_string");
 {
     AR type = AR_CREATE_TYPE(ArStringType);
     AR_BIND_METHOD(type, ":concat", arana_string_concat);
+    AR_BIND_METHOD(type, ":mul", arana_string_mul);
     AR_BIND_METHOD(type, "to_string", arana_string_to_string);
     AR_BIND_METHOD(type, "to_repr", arana_string_to_repr);
     AR_BIND_METHOD(type, "get_iterator", arana_string_get_iterator);