Skip to content

Commit

Permalink
Limit escaping to & char
Browse files Browse the repository at this point in the history
  • Loading branch information
e5l committed Oct 5, 2023
1 parent de82c41 commit e0e613c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/stream.kt
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ internal fun Appendable.escapeAppend(value: CharSequence) {
while (currentIndex < value.length) {
val code = value[currentIndex].code

if (code == '\\'.code) {
if (code == '\\'.code && currentIndex + 1 < value.length && value[currentIndex + 1] == '&') {
append(value.substring(lastIndex, currentIndex))
check(currentIndex + 1 < value.length) { "String must not end with '\\'." }
append(value[currentIndex + 1])
Expand Down
12 changes: 11 additions & 1 deletion src/commonTest/kotlin/EscapeAppendTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class EscapeAppendTest {
@Test
fun testAppendEscaped() {
assertEquals("&", escape("\\&"))
assertEquals("a", escape("\\a"))
assertEquals("\\a", escape("\\a"))
}

@Test
Expand All @@ -25,6 +25,16 @@ class EscapeAppendTest {
assertEquals("&&amp;", escape("\\&&"))
assertEquals("&amp;", escape("\\&amp;"))
}

@Test
fun testEscapeSlash() {
assertEquals("\\", escape("\\"))
}

@Test
fun testEscapeUnicode() {
assertEquals("\\u003d", escape("\\u003d"))
}
}

private fun escape(string: String): String = buildString {
Expand Down

0 comments on commit e0e613c

Please sign in to comment.