diff options
author | Nicolas Williams <nico@cryptonector.com> | 2013-11-29 15:49:11 -0600 |
---|---|---|
committer | Nicolas Williams <nico@cryptonector.com> | 2013-12-04 18:21:39 -0600 |
commit | 5989dbdfcfb42aeb6abcb19289f797d9e260f145 (patch) | |
tree | 101a870351b2363d0458a6dd8829c07d04bc7215 /builtin.c | |
parent | db19a11399f085043ead11b6860089a807fd9ec1 (diff) |
Add string multiplication by number
Diffstat (limited to 'builtin.c')
-rw-r--r-- | builtin.c | 13 |
1 files changed, 13 insertions, 0 deletions
@@ -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"); } |