Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use convertToZeroTerminatedString on both wasm and js targets #972

Merged
merged 3 commits into from
Aug 7, 2024

Conversation

Schahen
Copy link
Collaborator

@Schahen Schahen commented Aug 6, 2024

This is supposed to be fix for #967

The idea is pass strings as (utf8) byte arrays directly through the border
just like we do it on native

@Schahen Schahen requested a review from eymar August 6, 2024 15:03
@Schahen Schahen requested a review from igordmn August 6, 2024 15:18

val utf8 = string.encodeToByteArray() // encodeToByteArray encodes to utf8
// TODO Remove array copy, use `skString(data, length)` instead of `skString(data)`
return utf8.copyOf(utf8.size + 1)
Copy link
Member

Choose a reason for hiding this comment

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

Is it guaranteed that the 1 extra byte would be \0? Or maybe we better set its value explicitly?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it's guaranteed by spec for ByteArray.copyOf from Kotlin stdlib

@@ -23,6 +23,12 @@ class ManagedStringTest {
assertEquals("你好", s3)
}

@Test
fun emptyStringTest() {
Copy link
Member

Choose a reason for hiding this comment

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

What do you think about adding a few more simple tests with intiially empty strings:

s.insert(0, ""), s.append("").

Also I'm wondering what is going to happen if we call insert with an index out of bounds. Also remove with an index out of bounds.

@igordmn igordmn removed their request for review August 6, 2024 16:23
/**
* Converts String to zero-terminated utf-8 byte array.
*/
internal fun convertToZeroTerminatedString(string: String): ByteArray {
Copy link
Member

Choose a reason for hiding this comment

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

The name is confusing - I thought that toInterop is recursive. It converts to ByteArray, not string

Also better to include "utf8" in some form to the name

@Schahen Schahen merged commit 2a47e23 into master Aug 7, 2024
6 checks passed
@Schahen Schahen deleted the shagen/fix-empty-string-js-wasm branch August 7, 2024 23:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants