From 354c8f135e52df3d6ff96ebebbd3fa3c1bd43f73 Mon Sep 17 00:00:00 2001 From: Him188 Date: Thu, 10 Sep 2020 13:36:52 +0800 Subject: [PATCH] Fix escape with unicode letters, close #11 --- .../net.mamoe.yamlkt/internal/Escape.kt | 2 +- .../mamoe/yamlkt/decoder/DecoderEscapeTest.kt | 10 ++++++++ .../mamoe/yamlkt/encoder/TestEncoderEscape.kt | 24 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/yamlkt/src/commonMain/kotlin/net.mamoe.yamlkt/internal/Escape.kt b/yamlkt/src/commonMain/kotlin/net.mamoe.yamlkt/internal/Escape.kt index 93fdba89..5103259f 100644 --- a/yamlkt/src/commonMain/kotlin/net.mamoe.yamlkt/internal/Escape.kt +++ b/yamlkt/src/commonMain/kotlin/net.mamoe.yamlkt/internal/Escape.kt @@ -407,7 +407,7 @@ internal fun String.getQuotationAvailability(): Int { doubleWithoutEscape = false canBeUnquoted = false } - doubleWithoutEscape && REPLACEMENT_CHARS[c.toInt()] != null -> { + doubleWithoutEscape && REPLACEMENT_CHARS.getOrNull(c.toInt()) != null -> { doubleWithoutEscape = false } c == '\'' -> canBeSingleQuoted = false diff --git a/yamlkt/src/jvmTest/kotlin/net/mamoe/yamlkt/decoder/DecoderEscapeTest.kt b/yamlkt/src/jvmTest/kotlin/net/mamoe/yamlkt/decoder/DecoderEscapeTest.kt index 59acf184..fcd86342 100644 --- a/yamlkt/src/jvmTest/kotlin/net/mamoe/yamlkt/decoder/DecoderEscapeTest.kt +++ b/yamlkt/src/jvmTest/kotlin/net/mamoe/yamlkt/decoder/DecoderEscapeTest.kt @@ -9,6 +9,16 @@ import kotlin.test.assertFailsWith internal class DecoderEscapeTest { + @Test + fun escapeColon() { + assertEquals( + ":", + default.decodeAnyFromString( + """":"""" + ) + ) + } + @Test fun escapeMapping() { val mappings: Map = mapOf( diff --git a/yamlkt/src/jvmTest/kotlin/net/mamoe/yamlkt/encoder/TestEncoderEscape.kt b/yamlkt/src/jvmTest/kotlin/net/mamoe/yamlkt/encoder/TestEncoderEscape.kt index 566ff0dc..31952e5e 100644 --- a/yamlkt/src/jvmTest/kotlin/net/mamoe/yamlkt/encoder/TestEncoderEscape.kt +++ b/yamlkt/src/jvmTest/kotlin/net/mamoe/yamlkt/encoder/TestEncoderEscape.kt @@ -37,6 +37,30 @@ internal class TestEncoderEscape { assertEquals("\'\n\'", best.encodeToString("\n")) // will adjust to single } + @Test + fun colonEscape() { + assertEquals("\":\"", double.encodeToString(":")) + assertEquals("\':\'", single.encodeToString(":")) + assertEquals("\':\'", none.encodeToString(":")) + assertEquals("\':\'", best.encodeToString(":")) + } + + @Test + fun colonEscapeWithChinese() { + assertEquals("\":好\"", double.encodeToString(":好")) + assertEquals("\':好\'", single.encodeToString(":好")) + assertEquals("\':好\'", none.encodeToString(":好")) + assertEquals("\':好\'", best.encodeToString(":好")) + } + + @Test + fun spaceEscape() { + assertEquals("\" \"", double.encodeToString(" ")) + assertEquals("\' \'", single.encodeToString(" ")) + assertEquals("\' \'", none.encodeToString(" ")) + assertEquals("\' \'", best.encodeToString(" ")) + } + @Test fun testUnicodeEscape() { assertEquals(