Modern Apps git

← Modern-Apps.git

office: respect CSV text qualifiers on import and export (quoted newlines/commas)

Commit
186c410afafc30efe1bdfbce23e2f3338139c396
Author
Vayun Mathur <[email protected]>
When
2026-08-06 22:17 UTC (4 months ago)
Parents
da41b69

Diff

office/src/main/java/com/vayunmathur/office/odf/OdfParser.kt
diff --git a/office/src/main/java/com/vayunmathur/office/odf/OdfParser.kt b/office/src/main/java/com/vayunmathur/office/odf/OdfParser.ktindex 1ab845ac7f..4c7dfff302 100644--- a/office/src/main/java/com/vayunmathur/office/odf/OdfParser.kt+++ b/office/src/main/java/com/vayunmathur/office/odf/OdfParser.kt@@ -2317,35 +2317,44 @@ object OdfParser {      fun parseCsv(text: String, fileName: String, delimiter: Char = ','): OdfDocument.Spreadsheet {         val rows = mutableListOf<OdfRow>()-        val lines = text.lines()-        for (line in lines) {-            if (line.isBlank()) continue-            val cells = parseCsvLine(line, delimiter).map { OdfCell(text = it) }-            rows.add(OdfRow(cells))-        }-        return OdfDocument.Spreadsheet(fileName, listOf(OdfSheet("Sheet 1", rows)))-    }--    private fun parseCsvLine(line: String, delimiter: Char = ','): List<String> {         val fields = mutableListOf<String>()         val sb = StringBuilder()         var inQuotes = false++        fun endRecord() {+            fields.add(sb.toString()); sb.clear()+            // Skip truly blank lines (mirrors the old per-line isBlank() skip) so trailing+            // newlines and blank separators don't create empty rows. A row like ",," has+            // multiple (empty) fields and is kept, matching the previous behavior.+            if (!(fields.size == 1 && fields[0].isBlank())) {+                rows.add(OdfRow(fields.map { OdfCell(text = it) }))+            }+            fields.clear()+        }++        // Single pass over the whole text so a quoted field can span commas AND newlines.+        // RFC-4180 style: "" inside a quoted field is a literal quote; \r\n / \r / \n end a record.         var i = 0-        while (i < line.length) {-            val c = line[i]+        while (i < text.length) {+            val c = text[i]             when {-                c == '"' && !inQuotes -> inQuotes = true-                c == '"' && inQuotes -> {-                    if (i + 1 < line.length && line[i + 1] == '"') { sb.append('"'); i++ }-                    else inQuotes = false+                inQuotes -> when {+                    c == '"' && i + 1 < text.length && text[i + 1] == '"' -> { sb.append('"'); i++ }+                    c == '"' -> inQuotes = false+                    else -> sb.append(c) // newlines/commas inside quotes are literal content                 }-                c == delimiter && !inQuotes -> { fields.add(sb.toString()); sb.clear() }+                c == '"' -> inQuotes = true+                c == delimiter -> { fields.add(sb.toString()); sb.clear() }+                c == '\r' -> { endRecord(); if (i + 1 < text.length && text[i + 1] == '\n') i++ }+                c == '\n' -> endRecord()                 else -> sb.append(c)             }             i++         }-        fields.add(sb.toString())-        return fields+        // Flush the final record when the file doesn't end with a newline.+        if (sb.isNotEmpty() || fields.isNotEmpty()) endRecord()++        return OdfDocument.Spreadsheet(fileName, listOf(OdfSheet("Sheet 1", rows)))     }      private const val LINK_COLOR = 0xFF0066CCL
office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.kt
diff --git a/office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.kt b/office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.ktindex 0e7edc48be..9b2b8c03fe 100644--- a/office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.kt+++ b/office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.kt@@ -1961,7 +1961,7 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application)         for (row in sheet.rows) {             sb.appendLine(row.cells.joinToString(delimiter.toString()) { cell ->                 val text = cell.text-                if (text.contains(delimiter) || text.contains("\"") || text.contains("\n")) {+                if (text.contains(delimiter) || text.contains('"') || text.contains('\n') || text.contains('\r')) {                     "\"${text.replace("\"", "\"\"")}\""                 } else text             })