Skip to content
This repository was archived by the owner on Jan 17, 2024. It is now read-only.

Minor Utf8.strlen() improvement #65

Merged
merged 1 commit into from
Nov 18, 2020

Conversation

jpnurmi
Copy link
Contributor

@jpnurmi jpnurmi commented Nov 18, 2020

As suggested in #61, using indexOf(0) to search the zero byte has a bit
less overhead than indexWhere(closure).

As suggested in dart-archive#61, using indexOf(0) to search the zero byte has a bit
less overhead than indexWhere(closure).
@dcharkes dcharkes merged commit b34a792 into dart-archive:master Nov 18, 2020
@jpnurmi jpnurmi deleted the strlen-index-of branch November 18, 2020 10:01
@@ -26,7 +26,7 @@ class Utf8 extends Struct {
static int strlen(Pointer<Utf8> string) {
final Pointer<Uint8> array = string.cast<Uint8>();
final Uint8List nativeString = array.asTypedList(_maxSize);
return nativeString.indexWhere((char) => char == 0);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also check if UTF-16 had the same issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Utf16 is missing strlen(). Would it have to create a String to scan the code units? Something like String.fromCharCodes(cstr.asTypedList()).length?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, there is no strlen there because UTF-16 only goes from string to bytes, not the other way.
(I thought I saw two strlen implementations at some point. Maybe I was just looking at before and after a CL 🤭).

dcharkes pushed a commit to dart-lang/native that referenced this pull request Jan 16, 2024
As suggested in dart-archive/ffi#61, using indexOf(0) to search the zero byte has a bit
less overhead than indexWhere(closure).
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging this pull request may close these issues.

3 participants