-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from JakeWharton/jw.string-builder.2023-01-23
Add `StringBuilder.appendCodePoint`
- Loading branch information
Showing
5 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
src/commonImplementation/kotlin/CommonStringBuilderFunctions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)!! | ||
} |