summaryrefslogtreecommitdiffstats
path: root/builtin.c
diff options
context:
space:
mode:
authorNicolas Williams <nico@cryptonector.com>2013-11-29 15:49:11 -0600
committerNicolas Williams <nico@cryptonector.com>2013-12-04 18:21:39 -0600
commit5989dbdfcfb42aeb6abcb19289f797d9e260f145 (patch)
tree101a870351b2363d0458a6dd8829c07d04bc7215 /builtin.c
parentdb19a11399f085043ead11b6860089a807fd9ec1 (diff)
Add string multiplication by number
Diffstat (limited to 'builtin.c')
-rw-r--r--builtin.c13
1 files changed, 13 insertions, 0 deletions
diff --git a/builtin.c b/builtin.c
index 0d920abd..1e38482d 100644
--- a/builtin.c
+++ b/builtin.c
@@ -138,6 +138,19 @@ static jv f_multiply(jv input, jv a, jv b) {
jv_free(input);
if (jv_get_kind(a) == JV_KIND_NUMBER && jv_get_kind(b) == JV_KIND_NUMBER) {
return jv_number(jv_number_value(a) * jv_number_value(b));
+ } else if (jv_get_kind(a) == JV_KIND_STRING && jv_get_kind(b) == JV_KIND_NUMBER) {
+ int n;
+ size_t alen = jv_string_length_bytes(jv_copy(a));
+ jv res = a;
+
+ for (n = jv_number_value(b) - 1; n > 0; n--)
+ res = jv_string_append_buf(res, jv_string_value(a), alen);
+
+ if (n < 0) {
+ jv_free(a);
+ return jv_null();
+ }
+ return res;
} else {
return type_error2(a, b, "cannot be multiplied");
}