office: fix CSV parser treating mid-field quotes as field quotes
- Commit
ac79a874b00780bb39ce3a6bfecb83d203305c44- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 20:10 UTC (28 days ago)
- Parents
d2dac01
Symptom: importing a CSV cell such as 5" nail (inches) or any unquoted field containing a quote flipped the parser into quoted mode, so following commas and newlines were swallowed into one giant field and the rest of the sheet collapsed. Root cause: stepCsvChar opened quoted mode on any quote character, even mid-field. Fix: a quote only opens a quoted field at the start of a field (empty field buffer); elsewhere it is literal text, matching RFC 4180 readers. Verification: new OdfParser2SheetTest.csv_midfield_quote_is_literal plus repeated_rows_and_columns_use_their_own_caps; targeted :office:testDevUnitTest for OdfParser2SheetTest passes.
Diff
office/src/main/java/com/vayunmathur/office/odf/OdfParserCsv.kt
diff --git a/office/src/main/java/com/vayunmathur/office/odf/OdfParserCsv.kt b/office/src/main/java/com/vayunmathur/office/odf/OdfParserCsv.ktindex 30a8f965a8..2eb0da0148 100644--- a/office/src/main/java/com/vayunmathur/office/odf/OdfParserCsv.kt+++ b/office/src/main/java/com/vayunmathur/office/odf/OdfParserCsv.kt@@ -49,7 +49,10 @@ private fun stepCsvChar(text: String, index: Int, acc: CsvAcc): Int { val c = text[i] when { acc.inQuotes -> i = stepCsvQuoted(text, i, c, acc)- c == '"' -> acc.inQuotes = true+ // A quote only opens a quoted field at the start of a field; a quote in+ // the middle of an unquoted field (e.g. 5" nail) is literal text.+ c == '"' && acc.sb.isEmpty() -> acc.inQuotes = true+ c == '"' -> acc.sb.append(c) c == acc.delimiter -> { acc.fields.add(acc.sb.toString()); acc.sb.clear() } c == '\r' -> { acc.endRecord(); if (i + 1 < text.length && text[i + 1] == '\n') i++ } c == '\n' -> acc.endRecord()
office/src/test/java/com/vayunmathur/office/odf/OdfParser2SheetTest.kt
diff --git a/office/src/test/java/com/vayunmathur/office/odf/OdfParser2SheetTest.kt b/office/src/test/java/com/vayunmathur/office/odf/OdfParser2SheetTest.ktindex 6ba6ab51cf..b3a771c7af 100644--- a/office/src/test/java/com/vayunmathur/office/odf/OdfParser2SheetTest.kt+++ b/office/src/test/java/com/vayunmathur/office/odf/OdfParser2SheetTest.kt@@ -274,6 +274,21 @@ class OdfParser2SheetTest { assertEquals(2, s.sheets.single().rows.single().cells.single().numberFormat!!.decimals) } + @Test+ fun repeated_rows_and_columns_use_their_own_caps() {+ // 300 repeated empty rows must be capped at the row cap (200), not the+ // column cap; 300 repeated columns must survive past the row cap (200).+ val doc = sheet(+ """<table:table table:name="S">""" ++ """<table:table-column table:number-columns-repeated="300"/>""" ++ """<table:table-row table:number-rows-repeated="300">""" ++ """<table:table-cell office:value-type="string"><text:p>x</text:p></table:table-cell>""" ++ """</table:table-row></table:table>""")+ val s = doc.sheets.single()+ assertEquals(300, s.columnWidths.size)+ assertEquals(200, s.rows.size)+ }+ // --- CSV --- @Test@@ -306,6 +321,14 @@ class OdfParser2SheetTest { assertEquals("abc", unterminated.sheets.single().rows.single().cells.single().text) } + @Test+ fun csv_midfield_quote_is_literal() {+ val doc = OdfParser.parseCsv("5\" nail,plain\nnext,row", "f.csv")+ val rows = doc.sheets.single().rows+ assertEquals(listOf("5\" nail", "plain"), rows[0].cells.map { it.text })+ assertEquals(listOf("next", "row"), rows[1].cells.map { it.text })+ }+ @Test fun csv_single_sheet_named_sheet_1() { val doc = OdfParser.parseCsv("only", "o.csv")