diff --git a/src/vm/wren_utils.c b/src/vm/wren_utils.c index b5c31888..1b366561 100644 --- a/src/vm/wren_utils.c +++ b/src/vm/wren_utils.c @@ -161,18 +161,16 @@ int wrenUtf8Decode(const uint8_t* bytes, uint32_t length) return value; } -int wrenUtf8DecodeNumBytes(const char* string, uint32_t index) +int wrenUtf8DecodeNumBytes(uint8_t byte) { - char first = string[index]; - // If the byte starts with 10xxxxx, it's the middle of a UTF-8 sequence, so // don't count it at all. - if ((first & 0xc0) == 0x80) return 0; + if ((byte & 0xc0) == 0x80) return 0; // The first byte's high bits tell us how many bytes are in the UTF-8 // sequence. - if ((first & 0xf8) == 0xf0) return 4; - if ((first & 0xf0) == 0xe0) return 3; - if ((first & 0xe0) == 0xc0) return 2; + if ((byte & 0xf8) == 0xf0) return 4; + if ((byte & 0xf0) == 0xe0) return 3; + if ((byte & 0xe0) == 0xc0) return 2; return 1; } diff --git a/src/vm/wren_utils.h b/src/vm/wren_utils.h index 469162c1..a1f2e2fd 100644 --- a/src/vm/wren_utils.h +++ b/src/vm/wren_utils.h @@ -114,11 +114,10 @@ int wrenUtf8Encode(int value, uint8_t* bytes); // Returns -1 if the bytes are not a valid UTF-8 sequence. int wrenUtf8Decode(const uint8_t* bytes, uint32_t length); -// Returns the number of bytes in the UTF-8 sequence starting at [index] in -// [string]. +// Returns the number of bytes in the UTF-8 sequence starting with [byte]. // // If the character at that index is not the beginning of a UTF-8 sequence, // returns 0. -int wrenUtf8DecodeNumBytes(const char* string, uint32_t index); +int wrenUtf8DecodeNumBytes(uint8_t byte); #endif diff --git a/src/vm/wren_value.c b/src/vm/wren_value.c index dec3c080..3eec04d9 100644 --- a/src/vm/wren_value.c +++ b/src/vm/wren_value.c @@ -638,7 +638,7 @@ Value wrenNewStringFromRange(WrenVM* vm, ObjString* source, int start, int length = 0; for (uint32_t i = 0; i < count; i++) { - length += wrenUtf8EncodeNumBytes(from[start + i * step]); + length += wrenUtf8DecodeNumBytes(from[start + i * step]); } ObjString* result = allocateString(vm, length); @@ -655,7 +655,7 @@ Value wrenNewStringFromRange(WrenVM* vm, ObjString* source, int start, to += wrenUtf8Encode(codePoint, to); } } - + hashString(result); return OBJ_VAL(result); } @@ -772,7 +772,7 @@ Value wrenStringCodePointAt(WrenVM* vm, ObjString* string, uint32_t index) { ASSERT(index < string->length, "Index out of bounds."); - int numBytes = wrenUtf8DecodeNumBytes(string->value, index); + int numBytes = wrenUtf8DecodeNumBytes(string->value[index]); return wrenNewString(vm, string->value + index, numBytes); }