Added String.fromByte with docs and unit test

This commit is contained in:
Walter Schell
2019-02-27 08:10:54 -05:00
parent 487f53ace1
commit a437e804ba
9 changed files with 44 additions and 0 deletions
+16
View File
@@ -920,6 +920,21 @@ DEF_PRIMITIVE(string_fromCodePoint)
RETURN_VAL(wrenStringFromCodePoint(vm, codePoint));
}
DEF_PRIMITIVE(string_fromByte)
{
if (!validateInt(vm, args[1], "Byte")) return false;
int byte = (int) AS_NUM(args[1]);
if (byte < 0)
{
RETURN_ERROR("Byte cannot be negative.");
}
else if (byte > 0xff)
{
RETURN_ERROR("Byte cannot be greater than 0xff.");
}
RETURN_VAL(wrenStringFromByte(vm, (uint8_t) byte));
}
DEF_PRIMITIVE(string_byteAt)
{
ObjString* string = AS_STRING(args[0]);
@@ -1305,6 +1320,7 @@ void wrenInitializeCore(WrenVM* vm)
vm->stringClass = AS_CLASS(wrenFindVariable(vm, coreModule, "String"));
PRIMITIVE(vm->stringClass->obj.classObj, "fromCodePoint(_)", string_fromCodePoint);
PRIMITIVE(vm->stringClass->obj.classObj, "fromByte(_)", string_fromByte);
PRIMITIVE(vm->stringClass, "+(_)", string_plus);
PRIMITIVE(vm->stringClass, "[_]", string_subscript);
PRIMITIVE(vm->stringClass, "byteAt_(_)", string_byteAt);
+9
View File
@@ -802,6 +802,15 @@ Value wrenStringFromCodePoint(WrenVM* vm, int value)
return OBJ_VAL(string);
}
Value wrenStringFromByte(WrenVM *vm, uint8_t value)
{
int length = 1;
ObjString* string = allocateString(vm, length);
string->value[0] = value;
hashString(string);
return OBJ_VAL(string);
}
Value wrenStringFormat(WrenVM* vm, const char* format, ...)
{
va_list argList;
+3
View File
@@ -738,6 +738,9 @@ Value wrenStringFormat(WrenVM* vm, const char* format, ...);
// Creates a new string containing the UTF-8 encoding of [value].
Value wrenStringFromCodePoint(WrenVM* vm, int value);
// Creates a new string from the integer representation of a byte
Value wrenStringFromByte(WrenVM* vm, uint8_t value);
// Creates a new string containing the code point in [string] starting at byte
// [index]. If [index] points into the middle of a UTF-8 sequence, returns an
// empty string.