todo: fix iCalendar import corrupting literal backslashes
- Commit
40088b72713d7a56d38eb4ec120ed3310dfac412- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-08 02:38 UTC (28 days ago)
- Parents
05b6fca
VTodo.unescape applied chained String replaces, handling '\\' last. An escaped literal backslash followed by n, N, comma or semicolon (e.g. a title like 'C:\new docs', escaped as 'C:\\new docs') was re-read as an escape sequence, so import turned it into a newline/comma/semicolon and dropped a backslash. Replace the chain with a single left-to-right pass that consumes each backslash escape exactly once; unknown escapes keep their backslash, matching the old lenient behaviour for well-formed input. User-visible effect: tasks imported from .ics files (ours or other apps') keep titles and notes containing backslashes intact. Verified with :todo:testDevUnitTest, including a new FormatsTest case round-tripping a title and notes with backslash-n and backslash-comma.
Diff
todo/src/main/java/com/vayunmathur/todo/domain/format/VTodo.kt
diff --git a/todo/src/main/java/com/vayunmathur/todo/domain/format/VTodo.kt b/todo/src/main/java/com/vayunmathur/todo/domain/format/VTodo.ktindex 02e7061f39..9e8d2e45df 100644--- a/todo/src/main/java/com/vayunmathur/todo/domain/format/VTodo.kt+++ b/todo/src/main/java/com/vayunmathur/todo/domain/format/VTodo.kt@@ -171,12 +171,31 @@ object VTodo { } private fun escape(s: String) = s.replace("\\", "\\\\").replace(";", "\\;").replace(",", "\\,").replace("\n", "\\n")- private fun unescape(s: String) = s- .replace("\\n", "\n")- .replace("\\N", "\n")- .replace("\\,", ",")- .replace("\\;", ";")- .replace("\\\\", "\\")+ /**+ * Single left-to-right pass. Chained replaces are wrong here: once `\\` unescapes to `\`,+ * a following `n` (or `,`, `;`) would read as a new escape, so a literal backslash-n+ * round-trips into a newline. Unknown escapes keep their backslash.+ */+ private fun unescape(s: String): String {+ if ('\\' !in s) return s+ val out = StringBuilder(s.length)+ var i = 0+ while (i < s.length) {+ val c = s[i]+ if (c != '\\' || i + 1 >= s.length) {+ out.append(c)+ i+++ continue+ }+ when (s[i + 1]) {+ 'n', 'N' -> out.append('\n')+ '\\', ',', ';' -> out.append(s[i + 1])+ else -> out.append(c).append(s[i + 1])+ }+ i += 2+ }+ return out.toString()+ } private fun utc(ms: Long): String { val t = Instant.fromEpochMilliseconds(ms).toLocalDateTime(TimeZone.UTC)
todo/src/test/java/com/vayunmathur/todo/domain/format/FormatsTest.kt
diff --git a/todo/src/test/java/com/vayunmathur/todo/domain/format/FormatsTest.kt b/todo/src/test/java/com/vayunmathur/todo/domain/format/FormatsTest.ktindex c79989cd9a..50800420f7 100644--- a/todo/src/test/java/com/vayunmathur/todo/domain/format/FormatsTest.kt+++ b/todo/src/test/java/com/vayunmathur/todo/domain/format/FormatsTest.kt@@ -102,8 +102,20 @@ class FormatsTest { } @Test- fun `icalendar from other apps maps priority and dates`() {- val ics = """+ fun `icalendar round trips literal backslashes before escapable chars`() {+ val tricky = Todo(+ syncId = "x1",+ title = "C:\\new docs",+ notes = "a\\nb and c\\,d",+ createdAt = day(3, 1),+ )+ val back = VTodo.parse(VTodo.export(ExportSource(listOf(tricky), emptyList())), zone).single()+ assertEquals(tricky.title, back.title)+ assertEquals(tricky.notes, back.notes)+ }++ @Test+ fun `icalendar from other apps maps priority and dates`() { val ics = """ BEGIN:VCALENDAR BEGIN:VTODO UID:abc