contacts: group and nickname values split on unescaped commas only
- Commit
e8d9f76a008a59186227b336acbe9e22c274e6b8- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 06:56 UTC (5 days ago)
- Parents
66059d9
applyCategories unescaped the whole CATEGORIES value first
and only then split on commas, so the exporter's correct
CATEGORIES:Books\, Club,Family came back as three groups —
one real group ('Books, Club') became two, and a stray 'Club'
group was created as if the contact belonged to it.
applyNickname had the same reversed order, splitting one
escaped nickname into two. Split-then-unescape is the order
applyName/applyGender already use; both functions now
splitUnescaped first and unescape each part, distinct from
the earlier group-prefix fix (item1. framing) — this is
the escape/split ordering underneath it.
Two regression tests pin an escaped-comma category and
nickname staying single values. Verified: VCardParseTest
passes; :contacts:lint BUILD SUCCESSFUL; the 3 failing
:contacts:testDevUnitTest classes are Compose UI failures in
the other agents' dialog-conversion WIP, none exercising
VcfParser.Diff
contacts/src/main/java/com/vayunmathur/contacts/data/VcfParser.kt
diff --git a/contacts/src/main/java/com/vayunmathur/contacts/data/VcfParser.kt b/contacts/src/main/java/com/vayunmathur/contacts/data/VcfParser.ktindex 4e26e56681..80134ed2e0 100644--- a/contacts/src/main/java/com/vayunmathur/contacts/data/VcfParser.kt+++ b/contacts/src/main/java/com/vayunmathur/contacts/data/VcfParser.kt@@ -314,10 +314,15 @@ private class VcfParser( } private fun applyNickname(builder: ContactBuilder, decodedValue: String) {- val value = unescapeV(decodedValue)- // NICKNAME may hold a comma-separated list; split on unescaped commas.- val parts = splitUnescaped(value, ',').map { it.trim() }.filter { it.isNotBlank() }- val names = parts.ifEmpty { listOf(value).filter { it.isNotBlank() } }+ // NICKNAME may hold a comma-separated list; split on unescaped commas+ // first, then unescape each part. Unescaping first would turn an+ // escaped "\," (a literal comma inside one nickname, as the exporter+ // writes it) into a separator and split one nickname into two.+ val parts = splitUnescaped(decodedValue, ',')+ .map { unescapeV(it).trim() }.filter { it.isNotBlank() }+ val names = parts.ifEmpty {+ listOf(unescapeV(decodedValue)).filter { it.isNotBlank() }+ } for (n in names) { if (builder.nicknames.size == 1 && builder.nicknames[0].nickname.isBlank()) { builder.nicknames.clear()@@ -354,8 +359,12 @@ private class VcfParser( // during import and its failures must never crash the import. @Suppress("TooGenericExceptionCaught") private fun applyCategories(builder: ContactBuilder, decodedValue: String) {- val value = unescapeV(decodedValue)- val names = splitUnescaped(value, ',').map { it.trim() }.filter { it.isNotBlank() }+ // Split on unescaped commas first, then unescape each name. Unescaping+ // first would turn an escaped "\," (a literal comma inside one group+ // name, as the exporter writes it) into a separator and split one+ // group into two, misattributing the membership.+ val names = splitUnescaped(decodedValue, ',')+ .map { unescapeV(it).trim() }.filter { it.isNotBlank() } if (names.isEmpty()) return try { builder.groups.addAll(resolveGroups(names))
contacts/src/test/java/com/vayunmathur/contacts/pure/VCardParseTest.kt
diff --git a/contacts/src/test/java/com/vayunmathur/contacts/pure/VCardParseTest.kt b/contacts/src/test/java/com/vayunmathur/contacts/pure/VCardParseTest.ktindex 5c0e27936a..3e5711e92c 100644--- a/contacts/src/test/java/com/vayunmathur/contacts/pure/VCardParseTest.kt+++ b/contacts/src/test/java/com/vayunmathur/contacts/pure/VCardParseTest.kt@@ -215,6 +215,45 @@ class VCardParseTest { assertFalse(no[0].isFavorite) } + @Test+ fun escapedCommaInCategoryStaysOneGroup() {+ // The exporter escapes a comma inside a group name ("Books\, Club");+ // unescaping before splitting turned it back into a separator and+ // resolved two groups ("Books" + "Club") for one membership.+ var resolvedNames: List<String> = emptyList()+ val contacts = VcfUtils.parseContacts(+ ByteArrayInputStream(+ vcard(+ "BEGIN:VCARD", "VERSION:3.0", "FN:A",+ "CATEGORIES:Books\\, Club,Family",+ "END:VCARD",+ ).toByteArray(Charsets.UTF_8),+ ),+ ) { names ->+ resolvedNames = names+ names.mapIndexed { index, _ ->+ com.vayunmathur.contacts.data.GroupMembership(0, index + 1L)+ }+ }+ assertEquals(listOf("Books, Club", "Family"), resolvedNames)+ assertEquals(listOf(1L, 2L), contacts[0].details.groups.map { it.groupId })+ }++ @Test+ fun escapedCommaInNicknameStaysOneNickname() {+ val contacts = parse(+ vcard(+ "BEGIN:VCARD", "VERSION:3.0", "FN:A",+ "NICKNAME:Doe\\, John,Johnny",+ "END:VCARD",+ ),+ )+ assertEquals(+ listOf("Doe, John", "Johnny"),+ contacts[0].details.nicknames.map { it.nickname },+ )+ }+ @Test fun categoriesReachTheGroupResolver() { val contacts = VcfUtils.parseContacts(