Skip to content

Commit

Permalink
Merge pull request #40 from cketti/build_string
Browse files Browse the repository at this point in the history
Add function to create a string from the given code points
  • Loading branch information
cketti authored Jun 24, 2024
2 parents b0de8a0 + dcf573a commit 91bed49
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
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)
}
}
}
}

0 comments on commit 91bed49

Please sign in to comment.