bugfix. String#[]= methods sometime malfunction when source string was shrinked.
@@ -557,8 +557,8 @@ static void c_string_slice(struct VM *vm, mrbc_value v[], int argc)
*/
static void c_string_insert(struct VM *vm, mrbc_value v[], int argc)
{
- int nth;
- int len;
+ mrbc_int_t nth;
+ mrbc_int_t len;
const mrbc_value *val;
/*
@@ -597,13 +597,21 @@ static void c_string_insert(struct VM *vm, mrbc_value v[], int argc)
return;
}
- uint8_t *str = mrbc_realloc(vm, mrbc_string_cstr(v), len1 + len2 - len + 1);
+ int len3 = len1 + len2 - len; // filal length.
+ uint8_t *str = v->string->data;
+ if( len1 < len3 ) {
+ str = mrbc_realloc(vm, str, len3+1); // expand
if( !str ) return;
+ }
memmove( str + nth + len2, str + nth + len, len1 - nth - len + 1 );
memcpy( str + nth, mrbc_string_cstr(val), len2 );
- v->string->size = len1 + len2 - len;
+ if( len1 > len3 ) {
+ str = mrbc_realloc(vm, str, len3+1); // shrink
+ }
+
+ v->string->size = len1 + len2 - len;
v->string->data = str;
}
