From 08aa30a7c0803c12283c91d9caa4d334f33fc437 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Tue, 13 Sep 2022 19:32:03 +0900 Subject: [PATCH] Calculate hash while multiplying strings --- src/obj_str.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/obj_str.c b/src/obj_str.c index 7478006..040a66b 100644 --- a/src/obj_str.c +++ b/src/obj_str.c @@ -385,14 +385,18 @@ KRK_Method(str,__mul__) { char * out = malloc(totalLength + 1); char * c = out; + uint32_t hash = 0; + for (krk_integer_type i = 0; i < howMany; ++i) { for (size_t j = 0; j < self->length; ++j) { - *(c++) = self->chars[j]; + *c = self->chars[j]; + hash = (int)*c + (hash << 6) + (hash << 16) - hash; + c++; } } *c = '\0'; - return OBJECT_VAL(krk_takeString(out, totalLength)); + return OBJECT_VAL(krk_takeStringVetted(out, totalLength, self->codesLength * howMany, (self->obj.flags & KRK_OBJ_FLAGS_STRING_MASK), hash)); } KRK_Method(str,__rmul__) {