forked from Mirror/wren
Added String.fromByte with docs and unit test
This commit is contained in:
@ -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
|
It is a runtime error if `codePoint` is not an integer between `0` and
|
||||||
`0x10ffff`, inclusive.
|
`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
|
## Methods
|
||||||
|
|
||||||
### **bytes**
|
### **bytes**
|
||||||
|
|||||||
@ -920,6 +920,21 @@ DEF_PRIMITIVE(string_fromCodePoint)
|
|||||||
RETURN_VAL(wrenStringFromCodePoint(vm, codePoint));
|
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)
|
DEF_PRIMITIVE(string_byteAt)
|
||||||
{
|
{
|
||||||
ObjString* string = AS_STRING(args[0]);
|
ObjString* string = AS_STRING(args[0]);
|
||||||
@ -1305,6 +1320,7 @@ void wrenInitializeCore(WrenVM* vm)
|
|||||||
|
|
||||||
vm->stringClass = AS_CLASS(wrenFindVariable(vm, coreModule, "String"));
|
vm->stringClass = AS_CLASS(wrenFindVariable(vm, coreModule, "String"));
|
||||||
PRIMITIVE(vm->stringClass->obj.classObj, "fromCodePoint(_)", string_fromCodePoint);
|
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_plus);
|
||||||
PRIMITIVE(vm->stringClass, "[_]", string_subscript);
|
PRIMITIVE(vm->stringClass, "[_]", string_subscript);
|
||||||
PRIMITIVE(vm->stringClass, "byteAt_(_)", string_byteAt);
|
PRIMITIVE(vm->stringClass, "byteAt_(_)", string_byteAt);
|
||||||
|
|||||||
@ -802,6 +802,15 @@ Value wrenStringFromCodePoint(WrenVM* vm, int value)
|
|||||||
return OBJ_VAL(string);
|
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, ...)
|
Value wrenStringFormat(WrenVM* vm, const char* format, ...)
|
||||||
{
|
{
|
||||||
va_list argList;
|
va_list argList;
|
||||||
|
|||||||
@ -738,6 +738,9 @@ Value wrenStringFormat(WrenVM* vm, const char* format, ...);
|
|||||||
// Creates a new string containing the UTF-8 encoding of [value].
|
// Creates a new string containing the UTF-8 encoding of [value].
|
||||||
Value wrenStringFromCodePoint(WrenVM* vm, int 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
|
// 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
|
// [index]. If [index] points into the middle of a UTF-8 sequence, returns an
|
||||||
// empty string.
|
// empty string.
|
||||||
|
|||||||
3
test/core/string/from_byte.wren
Normal file
3
test/core/string/from_byte.wren
Normal 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
|
||||||
1
test/core/string/from_byte_not_int.wren
Normal file
1
test/core/string/from_byte_not_int.wren
Normal file
@ -0,0 +1 @@
|
|||||||
|
System.print(String.fromByte(12.34)) // expect runtime error: Byte must be an integer.
|
||||||
1
test/core/string/from_byte_not_num.wren
Normal file
1
test/core/string/from_byte_not_num.wren
Normal file
@ -0,0 +1 @@
|
|||||||
|
System.print(String.fromByte("not num")) // expect runtime error: Byte must be a number.
|
||||||
1
test/core/string/from_byte_too_large.wren
Normal file
1
test/core/string/from_byte_too_large.wren
Normal file
@ -0,0 +1 @@
|
|||||||
|
System.print(String.fromByte(0xff + 1)) // expect runtime error: Byte cannot be greater than 0xff.
|
||||||
1
test/core/string/from_byte_too_small.wren
Normal file
1
test/core/string/from_byte_too_small.wren
Normal file
@ -0,0 +1 @@
|
|||||||
|
System.print(String.fromByte(-1)) // expect runtime error: Byte cannot be negative.
|
||||||
Reference in New Issue
Block a user