Skip to content

Commit 91bed49

Browse files
authored
Merge pull request #40 from cketti/build_string
Add function to create a string from the given code points
2 parents b0de8a0 + dcf573a commit 91bed49

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### Added
88
- `CodePoint.toUnicodeNotation()` returns the standard Unicode notation of a code point, e.g. `U+1F4E7`.
99
- `CharSequence.codePointCount()` variant without parameters.
10+
- `CodePoints.toString(…)` creates a string from the given code points.
1011

1112
## [0.8.0] - 2024-06-09
1213
### Changed

kotlin-codepoints/src/commonMain/kotlin/CodePoints.kt

+9
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,13 @@ expect object CodePoints {
8888
* `destination[offset]` (high-surrogate) and `destination[offset+1]` (low-surrogate), and 2 is returned.
8989
*/
9090
fun toChars(codePoint: Int, destination: CharArray, offset: Int): Int
91+
92+
/**
93+
* Converts the given code points to a string.
94+
*
95+
* @param codePoints Array of code points.
96+
*
97+
* @throws IllegalArgumentException If any invalid code point is found in [codePoints].
98+
*/
99+
fun toString(vararg codePoints: Int): String
91100
}

kotlin-codepoints/src/commonTest/kotlin/CodePointsTest.kt

+13
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,17 @@ class CodePointsTest {
153153
}
154154
assertContentEquals(charArrayOf('z', 'z'), chars)
155155
}
156+
157+
@Test
158+
fun toString_test() {
159+
assertEquals("", CodePoints.toString(*intArrayOf()))
160+
assertEquals("a", CodePoints.toString('a'.code))
161+
assertEquals("\uD83E\uDD95", CodePoints.toString(0x1F995))
162+
assertEquals("\uD83E\uDD95", CodePoints.toString(0xD83E, 0xDD95))
163+
assertEquals("a\uD83E\uDD95z", CodePoints.toString('a'.code, 0x1F995, 'z'.code))
164+
165+
assertFailsWith<IllegalArgumentException> {
166+
CodePoints.toString('a'.code, 0x110000)
167+
}
168+
}
156169
}

kotlin-codepoints/src/jvmMain/kotlin/CodePoints.kt

+4
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,8 @@ actual object CodePoints {
4242
actual inline fun toChars(codePoint: Int, destination: CharArray, offset: Int): Int {
4343
return Character.toChars(codePoint, destination, offset)
4444
}
45+
46+
actual inline fun toString(vararg codePoints: Int): String {
47+
return String(codePoints, offset = 0, length = codePoints.size)
48+
}
4549
}

kotlin-codepoints/src/nonJvmMain/kotlin/CodePoints.kt

+11
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,15 @@ actual object CodePoints {
7474

7575
this[index] = value
7676
}
77+
78+
actual fun toString(vararg codePoints: Int): String {
79+
require(codePoints.all { isValidCodePoint(it) }) { "Array contains at least one invalid code point" }
80+
81+
val charCount = codePoints.sumOf { charCount(it) }
82+
return buildString(capacity = charCount) {
83+
for (codePoint in codePoints) {
84+
appendCodePoint(codePoint)
85+
}
86+
}
87+
}
7788
}

0 commit comments

Comments
 (0)