Skip to content
New issue

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

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

Already on GitHub? # to your account

Add function to create a string from the given code points #40

Merged
merged 1 commit into from
Jun 24, 2024

Conversation

cketti
Copy link
Owner

@cketti cketti commented Jun 23, 2024

CodePoints.toString(…) converts the given code points to a string.

Uses the String constructor supporting code points on the JVM; and a StringBuilder on all other platforms.

This function converts the given code points to a string.
@cketti cketti merged commit 91bed49 into main Jun 24, 2024
1 check passed
@cketti cketti deleted the build_string branch June 24, 2024 17:56
@vanniktech
Copy link

Would you be open to also provide specialized functions which only take 1, 2, or 3 code points? That way there's no overhead for the vararg. In my case I have quite a lot of usages where varargs would need to be allocated. It's similar to the standard lib which also provides these optimizations:

Screenshot 2024-06-25 at 12 43 08

Roman also wrote about this here.

@cketti
Copy link
Owner Author

cketti commented Jun 25, 2024

It wouldn't make a difference on the JVM because the String constructor requires an IntArray.

actual inline fun toString(vararg codePoints: Int): String {
return String(codePoints, offset = 0, length = codePoints.size)
}

Looking at the StringBuilder implementations for other platforms, I'm guessing optimizing the allocation for the arguments won't yield the biggest return.

But are you really creating strings in a performance-critical loop? If so, you might be better off by reusing a CharArray and calling CharArray.concatToString(startIndex, endIndex).

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants