Skip to content

Commit

Permalink
Merge pull request #11 from JakeWharton/jw.string-builder.2023-01-23
Browse files Browse the repository at this point in the history
Add `StringBuilder.appendCodePoint`
  • Loading branch information
cketti authored Jan 23, 2023
2 parents 5e8ceaf + 07a21c3 commit de0ed20
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/commonImplementation/kotlin/CommonStringBuilderFunctions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package de.cketti.codepoints

object CommonStringBuilderFunctions {
fun appendCodePoint(builder: StringBuilder, codePoint: Int) {
if (CodePoints.isBmpCodePoint(codePoint)) {
builder.append(codePoint.toChar())
} else {
builder.append(CodePoints.highSurrogate(codePoint))
builder.append(CodePoints.lowSurrogate(codePoint))
}
}
}
7 changes: 7 additions & 0 deletions src/commonImplementation/kotlin/StringBuilderExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package de.cketti.codepoints

import kotlin.text.StringBuilder

actual fun StringBuilder.appendCodePoint(codePoint: Int): StringBuilder = apply {
CommonStringBuilderFunctions.appendCodePoint(this, codePoint)
}
17 changes: 17 additions & 0 deletions src/commonMain/kotlin/StringBuilderExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@file:Suppress(
"EXTENSION_SHADOWED_BY_MEMBER", // Kotlin/JVM aliases StringBuilder to j.l.StringBuilder.
"KotlinRedundantDiagnosticSuppress", // Above suppression only needed for JVM.
)

package de.cketti.codepoints

/**
* Appends the string representation of the [codePoint] argument to this sequence.
*
* The argument is appended to the contents of this sequence.
* The length of this sequence increases by [CodePoints.charCount].
*
* The overall effect is exactly as if the argument were converted to a char array by the function
* [CodePoints.toChars] and the characters in that array were then appended to this sequence.
*/
expect fun StringBuilder.appendCodePoint(codePoint: Int): StringBuilder
18 changes: 18 additions & 0 deletions src/commonTest/kotlin/StringBuilderExtensionsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package de.cketti.codepoints

import kotlin.test.assertEquals
import kotlin.test.Test

class StringBuilderExtensionsTest {
@Test
fun appendCodePoint() {
val actual = buildString {
appendCodePoint('a'.code)
append('b')
appendCodePoint("\uFFFF".codePointAt(0))
append('c')
appendCodePoint("\uD83E\uDD95".codePointAt(0))
}
assertEquals("ab\uFFFFc\uD83E\uDD95", actual)
}
}
12 changes: 12 additions & 0 deletions src/jvmMain/kotlin/StringBuilderExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@file:Suppress(
"NOTHING_TO_INLINE",
"EXTENSION_SHADOWED_BY_MEMBER",
)

package de.cketti.codepoints

import kotlin.text.StringBuilder

actual inline fun StringBuilder.appendCodePoint(codePoint: Int): StringBuilder {
return appendCodePoint(codePoint)!!
}

0 comments on commit de0ed20

Please sign in to comment.