Skip to content

Commit

Permalink
Use unescape when parsing, and escape when serializing.
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Aug 22, 2023
1 parent b334f07 commit 1e229d2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class SnapshotValueReader(val lineReader: LineReader) {
if (buffer.isEmpty()) {
return SnapshotValue.EMPTY
}
return SnapshotValue.of(bodyEsc.escape(buffer.toString().trim()))
return SnapshotValue.of(bodyEsc.unescape(buffer.toString().trim()))
}

/** Same as nextValue, but faster. */
Expand All @@ -134,7 +134,7 @@ class SnapshotValueReader(val lineReader: LineReader) {
} else if (key.endsWith(" ")) {
throw ParseException(lineReader, "Trailing spaces are disallowed: '$key'")
} else {
nameEsc.escape(key)
nameEsc.unescape(key)
}
}
private fun nextLine(): String? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,28 @@ class SnapshotValueReaderTest {
╔═ test with \∕slash\∕ in name ═╗
╔═ test with \(square brackets\) in name ═╗
╔═ test with \\backslash\\ in name ═╗
╔═ test with \nnewline\n in name ═╗
╔═ test with\nnewline\nin name ═╗
╔═ test with \ttab\t in name ═╗
╔═ test with \┌\─ ascii art \┐\─ in name ═╗
╔═ test with \┌\─ ascii art \─\┐ in name ═╗
"""
.trimIndent())
reader.peekKey() shouldBe "test with \\\\slash\\\\ in name"
reader.peekKey() shouldBe "test with /slash/ in name"
reader.nextValue().valueString() shouldBe ""
reader.peekKey() shouldBe "test with \\\\(square brackets\\\\) in name"
reader.peekKey() shouldBe "test with [square brackets] in name"
reader.nextValue().valueString() shouldBe ""
reader.peekKey() shouldBe "test with \\\\\\\\backslash\\\\\\\\ in name"
reader.peekKey() shouldBe """test with \backslash\ in name"""
reader.nextValue().valueString() shouldBe ""
reader.peekKey() shouldBe "test with \\\\nnewline\\\\n in name"
reader.peekKey() shouldBe
"""
test with
newline
in name
"""
.trimIndent()
reader.nextValue().valueString() shouldBe ""
reader.peekKey() shouldBe "test with \\\\ttab\\\\t in name"
reader.peekKey() shouldBe "test with \ttab\t in name"
reader.nextValue().valueString() shouldBe ""
reader.peekKey() shouldBe "test with \\\\\\\\ ascii art \\\\\\\\ in name"
reader.peekKey() shouldBe "test with ╔═ ascii art ═╗ in name"
reader.nextValue().valueString() shouldBe ""
}

Expand All @@ -117,9 +123,8 @@ class SnapshotValueReaderTest {
reader.peekKey() shouldBe "ascii art okay"
reader.nextValue().valueString() shouldBe "╔══╗"
reader.peekKey() shouldBe "escaped iff on first line"
reader.nextValue().valueString() shouldBe "\uD801\uDF43\uD801\uDF41══╗"
reader.nextValue().valueString() shouldBe """𐝁══╗"""
reader.peekKey() shouldBe "body escape characters"
reader.nextValue().valueString() shouldBe
"\uD801\uDF43\uD801\uDF43\uD801\uDF43\uD801\uDF41\uD801\uDF43\uD801\uDF43\uD801\uDF43\uD801\uDF43 linear a is dead"
reader.nextValue().valueString() shouldBe """𐝁𐝃 linear a is dead"""
}
}

0 comments on commit 1e229d2

Please sign in to comment.