Merge pull request #660 from walterschell/feature/string_fromByte

Added String.fromByte with docs and unit test
This commit is contained in:
Bob Nystrom
2019-02-28 19:31:26 -08:00
committed by GitHub
9 changed files with 44 additions and 0 deletions

View File

@ -57,6 +57,15 @@ Creates a new string containing the UTF-8 encoding of `codePoint`.
It is a runtime error if `codePoint` is not an integer between `0` and
`0x10ffff`, inclusive.
### String.**fromByte**(byte)
Creates a new string containing the single byte `byte`.
:::wren
String.fromByte(255) //> <20>
It is a runtime error if `byte` is not an integer between `0` and `0xff`, inclusive.
## Methods
### **bytes**

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

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;

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.

View File

@ -0,0 +1,3 @@
System.print(String.fromByte(65)) // expect: A
System.print(String.fromByte(0).bytes[0]) // expect: 0
System.print(String.fromByte(255).bytes[0]) // expect: 255

View File

@ -0,0 +1 @@
System.print(String.fromByte(12.34)) // expect runtime error: Byte must be an integer.

View File

@ -0,0 +1 @@
System.print(String.fromByte("not num")) // expect runtime error: Byte must be a number.

View File

@ -0,0 +1 @@
System.print(String.fromByte(0xff + 1)) // expect runtime error: Byte cannot be greater than 0xff.

View File

@ -0,0 +1 @@
System.print(String.fromByte(-1)) // expect runtime error: Byte cannot be negative.