Skip to content

Commit

Permalink
fix: only run index and slice utf8 version if there are multibyte cha…
Browse files Browse the repository at this point in the history
…racters
  • Loading branch information
liz3 committed Oct 28, 2024
1 parent 571c73e commit a98d2d0
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/vm/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1908,14 +1908,14 @@ static DictuInterpretResult run(DictuVM *vm) {

if (index >= 0 && index < len) {
ObjString* newString;
if(string->character_len != -1) {
if(string->character_len != -1 && string->character_len != string->length) {
utf8_int32_t ch;
char* ptr = string->chars;
for(size_t i = 0; i <= (size_t)index; i++)
ptr = utf8codepoint(ptr, &ch);
size_t cpSize = utf8codepointsize(ch);
newString = copyString(vm, ptr - cpSize, cpSize);

} else {
newString = copyString(vm, &string->chars[index], 1);
}
Expand Down Expand Up @@ -2134,7 +2134,7 @@ static DictuInterpretResult run(DictuVM *vm) {
if (indexStart > indexEnd) {
returnVal = OBJ_VAL(copyString(vm, "", 0));
} else {
if(string->character_len != -1) {
if(string->character_len != -1 && string->character_len != string->length) {
utf8_int32_t ch;
char* ptr = string->chars;
// first we need to advance by the skip;
Expand Down

0 comments on commit a98d2d0

Please sign in to comment.