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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/src/utf8.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 🤭).

return nativeString.indexOf(0);
}

/// Creates a [String] containing the characters UTF-8 encoded in [string].
Expand Down