feat: replace public byteAt with private byteAt_ and add O(1) bytes.count

Remove the public `byteAt(_)` method from String and its ByteSequence wrapper, renaming it to the private `byteAt_(_)` to eliminate redundancy with `.bytes[_]`. Add a native `byteCount_` primitive on String and expose it as `count` on StringByteSequence, enabling O(1) byte length queries. Update all test files to use the new `.bytes` subscript API and add a dedicated `count.wren` test for the new functionality.
This commit is contained in:
Bob Nystrom
2015-09-11 06:52:18 +00:00
parent 0397da2451
commit 731e1f4b1d
9 changed files with 40 additions and 57 deletions
+11 -3
View File
@@ -151,9 +151,11 @@ static const char* coreLibSource =
" _string = string\n"
" }\n"
"\n"
" [index] { _string.byteAt(index) }\n"
" [index] { _string.byteAt_(index) }\n"
" iterate(iterator) { _string.iterateByte_(iterator) }\n"
" iteratorValue(iterator) { _string.byteAt(iterator) }\n"
" iteratorValue(iterator) { _string.byteAt_(iterator) }\n"
"\n"
" count { _string.byteCount_ }\n"
"}\n"
"\n"
"class List is Sequence {\n"
@@ -1079,6 +1081,11 @@ DEF_PRIMITIVE(string_byteAt)
RETURN_NUM((uint8_t)string->value[index]);
}
DEF_PRIMITIVE(string_byteCount)
{
RETURN_NUM(AS_STRING(args[0])->length);
}
DEF_PRIMITIVE(string_codePointAt)
{
ObjString* string = AS_STRING(args[0]);
@@ -1409,7 +1416,8 @@ void wrenInitializeCore(WrenVM* vm)
PRIMITIVE(vm->stringClass->obj.classObj, "fromCodePoint(_)", string_fromCodePoint);
PRIMITIVE(vm->stringClass, "+(_)", string_plus);
PRIMITIVE(vm->stringClass, "[_]", string_subscript);
PRIMITIVE(vm->stringClass, "byteAt(_)", string_byteAt);
PRIMITIVE(vm->stringClass, "byteAt_(_)", string_byteAt);
PRIMITIVE(vm->stringClass, "byteCount_", string_byteCount);
PRIMITIVE(vm->stringClass, "codePointAt(_)", string_codePointAt);
PRIMITIVE(vm->stringClass, "contains(_)", string_contains);
PRIMITIVE(vm->stringClass, "count", string_count);