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

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

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Added
- `CodePoint.toUnicodeNotation()` returns the standard Unicode notation of a code point, e.g. `U+1F4E7`.
- `CharSequence.codePointCount()` variant without parameters.
- `CodePoints.toString(…)` creates a string from the given code points.

## [0.8.0] - 2024-06-09
### Changed
Expand Down
9 changes: 9 additions & 0 deletions kotlin-codepoints/src/commonMain/kotlin/CodePoints.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,13 @@ expect object CodePoints {
* `destination[offset]` (high-surrogate) and `destination[offset+1]` (low-surrogate), and 2 is returned.
*/
fun toChars(codePoint: Int, destination: CharArray, offset: Int): Int

/**
* Converts the given code points to a string.
*
* @param codePoints Array of code points.
*
* @throws IllegalArgumentException If any invalid code point is found in [codePoints].
*/
fun toString(vararg codePoints: Int): String
}
13 changes: 13 additions & 0 deletions kotlin-codepoints/src/commonTest/kotlin/CodePointsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,17 @@ class CodePointsTest {
}
assertContentEquals(charArrayOf('z', 'z'), chars)
}

@Test
fun toString_test() {
assertEquals("", CodePoints.toString(*intArrayOf()))
assertEquals("a", CodePoints.toString('a'.code))
assertEquals("\uD83E\uDD95", CodePoints.toString(0x1F995))
assertEquals("\uD83E\uDD95", CodePoints.toString(0xD83E, 0xDD95))
assertEquals("a\uD83E\uDD95z", CodePoints.toString('a'.code, 0x1F995, 'z'.code))

assertFailsWith<IllegalArgumentException> {
CodePoints.toString('a'.code, 0x110000)
}
}
}
4 changes: 4 additions & 0 deletions kotlin-codepoints/src/jvmMain/kotlin/CodePoints.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ actual object CodePoints {
actual inline fun toChars(codePoint: Int, destination: CharArray, offset: Int): Int {
return Character.toChars(codePoint, destination, offset)
}

actual inline fun toString(vararg codePoints: Int): String {
return String(codePoints, offset = 0, length = codePoints.size)
}
}
11 changes: 11 additions & 0 deletions kotlin-codepoints/src/nonJvmMain/kotlin/CodePoints.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,15 @@ actual object CodePoints {

this[index] = value
}

actual fun toString(vararg codePoints: Int): String {
require(codePoints.all { isValidCodePoint(it) }) { "Array contains at least one invalid code point" }

val charCount = codePoints.sumOf { charCount(it) }
return buildString(capacity = charCount) {
for (codePoint in codePoints) {
appendCodePoint(codePoint)
}
}
}
}
Loading