From dcf573ad2488f25328942aea6aad379c4669d825 Mon Sep 17 00:00:00 2001 From: cketti Date: Mon, 24 Jun 2024 00:59:25 +0200 Subject: [PATCH] =?UTF-8?q?Add=20`CodePoints.toString(=E2=80=A6)`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This function converts the given code points to a string. --- CHANGELOG.md | 1 + .../src/commonMain/kotlin/CodePoints.kt | 9 +++++++++ .../src/commonTest/kotlin/CodePointsTest.kt | 13 +++++++++++++ kotlin-codepoints/src/jvmMain/kotlin/CodePoints.kt | 4 ++++ .../src/nonJvmMain/kotlin/CodePoints.kt | 11 +++++++++++ 5 files changed, 38 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 928f51b..58d814f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/kotlin-codepoints/src/commonMain/kotlin/CodePoints.kt b/kotlin-codepoints/src/commonMain/kotlin/CodePoints.kt index 95ce8c6..8e1857c 100644 --- a/kotlin-codepoints/src/commonMain/kotlin/CodePoints.kt +++ b/kotlin-codepoints/src/commonMain/kotlin/CodePoints.kt @@ -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 } diff --git a/kotlin-codepoints/src/commonTest/kotlin/CodePointsTest.kt b/kotlin-codepoints/src/commonTest/kotlin/CodePointsTest.kt index e056430..6ecffff 100644 --- a/kotlin-codepoints/src/commonTest/kotlin/CodePointsTest.kt +++ b/kotlin-codepoints/src/commonTest/kotlin/CodePointsTest.kt @@ -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 { + CodePoints.toString('a'.code, 0x110000) + } + } } diff --git a/kotlin-codepoints/src/jvmMain/kotlin/CodePoints.kt b/kotlin-codepoints/src/jvmMain/kotlin/CodePoints.kt index f969592..63918d2 100644 --- a/kotlin-codepoints/src/jvmMain/kotlin/CodePoints.kt +++ b/kotlin-codepoints/src/jvmMain/kotlin/CodePoints.kt @@ -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) + } } diff --git a/kotlin-codepoints/src/nonJvmMain/kotlin/CodePoints.kt b/kotlin-codepoints/src/nonJvmMain/kotlin/CodePoints.kt index 30e4777..7b25148 100644 --- a/kotlin-codepoints/src/nonJvmMain/kotlin/CodePoints.kt +++ b/kotlin-codepoints/src/nonJvmMain/kotlin/CodePoints.kt @@ -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) + } + } + } }