Skip to content

Commit de0ed20

Browse files
authored
Merge pull request #11 from JakeWharton/jw.string-builder.2023-01-23
Add `StringBuilder.appendCodePoint`
2 parents 5e8ceaf + 07a21c3 commit de0ed20

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package de.cketti.codepoints
2+
3+
object CommonStringBuilderFunctions {
4+
fun appendCodePoint(builder: StringBuilder, codePoint: Int) {
5+
if (CodePoints.isBmpCodePoint(codePoint)) {
6+
builder.append(codePoint.toChar())
7+
} else {
8+
builder.append(CodePoints.highSurrogate(codePoint))
9+
builder.append(CodePoints.lowSurrogate(codePoint))
10+
}
11+
}
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package de.cketti.codepoints
2+
3+
import kotlin.text.StringBuilder
4+
5+
actual fun StringBuilder.appendCodePoint(codePoint: Int): StringBuilder = apply {
6+
CommonStringBuilderFunctions.appendCodePoint(this, codePoint)
7+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@file:Suppress(
2+
"EXTENSION_SHADOWED_BY_MEMBER", // Kotlin/JVM aliases StringBuilder to j.l.StringBuilder.
3+
"KotlinRedundantDiagnosticSuppress", // Above suppression only needed for JVM.
4+
)
5+
6+
package de.cketti.codepoints
7+
8+
/**
9+
* Appends the string representation of the [codePoint] argument to this sequence.
10+
*
11+
* The argument is appended to the contents of this sequence.
12+
* The length of this sequence increases by [CodePoints.charCount].
13+
*
14+
* The overall effect is exactly as if the argument were converted to a char array by the function
15+
* [CodePoints.toChars] and the characters in that array were then appended to this sequence.
16+
*/
17+
expect fun StringBuilder.appendCodePoint(codePoint: Int): StringBuilder
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package de.cketti.codepoints
2+
3+
import kotlin.test.assertEquals
4+
import kotlin.test.Test
5+
6+
class StringBuilderExtensionsTest {
7+
@Test
8+
fun appendCodePoint() {
9+
val actual = buildString {
10+
appendCodePoint('a'.code)
11+
append('b')
12+
appendCodePoint("\uFFFF".codePointAt(0))
13+
append('c')
14+
appendCodePoint("\uD83E\uDD95".codePointAt(0))
15+
}
16+
assertEquals("ab\uFFFFc\uD83E\uDD95", actual)
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@file:Suppress(
2+
"NOTHING_TO_INLINE",
3+
"EXTENSION_SHADOWED_BY_MEMBER",
4+
)
5+
6+
package de.cketti.codepoints
7+
8+
import kotlin.text.StringBuilder
9+
10+
actual inline fun StringBuilder.appendCodePoint(codePoint: Int): StringBuilder {
11+
return appendCodePoint(codePoint)!!
12+
}

0 commit comments

Comments
 (0)