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

Helpers for null-terminated Utf8 #3

Merged
merged 9 commits into from
Sep 9, 2019
Merged

Helpers for null-terminated Utf8 #3

merged 9 commits into from
Sep 9, 2019

Conversation

sjindel-google
Copy link
Contributor

No description provided.

@mit-mit mit-mit requested a review from lrhn September 6, 2019 08:10
@mit-mit
Copy link
Contributor

mit-mit commented Sep 6, 2019

LGTM overall, Lasse will do a style review

Copy link

@lrhn lrhn left a comment

Choose a reason for hiding this comment

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

Drive-by mostly-style comments.

}

static Pointer<Utf8> toUtf8(String s) {
final List<int> units = Utf8Encoder().convert(s);
Copy link

Choose a reason for hiding this comment

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

I believe Utf8Encoder.convert this is now typed to return Uint8List. You may want to use that as type.

You can use const Utf8Encoder() to avoid allocating a new object each time (if you care).
That's the same object returned by utf8.encoder as well, and you could also just write utf8.encode(s) for exactly the same result.
Easier to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see the new type:

abstract class Encoding extends Codec<String, List<int>> {

Copy link

@mkustermann mkustermann left a comment

Choose a reason for hiding this comment

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

Some dbc

@sjindel-google
Copy link
Contributor Author

Working on better tests.

@sjindel-google
Copy link
Contributor Author

PTAL

Copy link

@mkustermann mkustermann left a comment

Choose a reason for hiding this comment

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

LGTM with comments

Copy link
Contributor

@dcharkes dcharkes left a comment

Choose a reason for hiding this comment

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

LGTM to me as well.

int i = 0;
while (nativeString[i] != 0) {
if (++i == count) {
count *= 2;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we use a precise bound here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, because the length of the string can be only be detected by scanning until the first NULL byte.

Copy link
Contributor

Choose a reason for hiding this comment

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

But, why times 2 every time? Why not scan until null and use that as count?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is scanning until null.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I was confused by array.asExternalTypedData(count: count) where that count is potentially 2x the string length. We need an oversized TypedData wrapper in order to not create a new wrapper every character for scanning in the first place. (I hope we can switch to the normal Pointer<Int8>.load() soon, that would make it easier to read this code.)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants