Rework string-building code
authorLanius Trolling <lanius@laniustrolling.dev>
Sat, 6 Apr 2024 16:42:03 +0000 (12:42 -0400)
committerLanius Trolling <lanius@laniustrolling.dev>
Sat, 6 Apr 2024 16:42:03 +0000 (12:42 -0400)
src/jvmMain/kotlin/info/mechyrdia/lore/parser_html.kt

index 862a56c324f323d4bdd68156b5a2e74f65e307ef..7383dfab28c09b47885a93be91804bdb5160ed0a 100644 (file)
@@ -16,11 +16,13 @@ typealias HtmlBuilderSubject = TagConsumer<*>.() -> Any?
 context(T)
 operator fun <T : Tag> (TagConsumer<*>.() -> Any?).unaryPlus() = with(HtmlLexerTagConsumer(consumer)) { this@unaryPlus() }
 
-fun (TagConsumer<*>.() -> Any?).toFragment() = buildString {
-       val builder = appendHTML()
-       with(HtmlLexerTagConsumer(builder)) { this@toFragment() }
-       builder.finalize()
-}
+fun (TagConsumer<*>.() -> Any?).toFragment() = StringBuilder()
+       .appendHTML()
+       .also { builder ->
+               with(HtmlLexerTagConsumer(builder)) { this@toFragment() }
+       }
+       .finalize()
+       .toString()
 
 class HtmlLexerTagConsumer private constructor(private val downstream: TagConsumer<*>) : TagConsumer<Unit> {
        override fun onTagStart(tag: Tag) {