1
0
forked from Mirror/wren

Fix bug in passing a range as the string subscript.

This commit is contained in:
Bob Nystrom
2015-09-08 20:59:47 -07:00
parent 783a5b750a
commit 1df7565bb9
3 changed files with 10 additions and 13 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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);
}