Skip to content

Commit a3cf01e

Browse files
authored
Treat bom-only CSV file as empty (#112)
* test: reproduce the problem we faced Signed-off-by: Kengo TODA <toda_k@henry.jp> * fix: lines contain only BOM were not handled as empty lines Signed-off-by: Kengo TODA <toda_k@henry.jp> * chore: follow suggestions from CodeFactor Signed-off-by: Kengo TODA <toda_k@henry.jp> * chore: follow suggestions from CodeFactor Signed-off-by: Kengo TODA <toda_k@henry.jp> Signed-off-by: Kengo TODA <toda_k@henry.jp>
1 parent 5cdaf60 commit a3cf01e

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

src/commonMain/kotlin/com/github/doyaaaaaken/kotlincsv/client/BufferedLineReader.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@ package com.github.doyaaaaaken.kotlincsv.client
66
internal class BufferedLineReader(
77
private val br: Reader
88
) {
9+
companion object {
10+
private const val BOM = '\uFEFF'
11+
}
12+
13+
private fun StringBuilder.isEmptyLine(): Boolean =
14+
this.isEmpty() || this.length == 1 && this[0] == BOM
915

1016
fun readLineWithTerminator(): String? {
1117
val sb = StringBuilder()
1218
do {
1319
val c = br.read()
1420

1521
if (c == -1) {
16-
if (sb.isEmpty()) {
22+
if (sb.isEmptyLine()) {
1723
return null
1824
} else {
1925
break

src/jvmTest/kotlin/com/github/doyaaaaaken/kotlincsv/client/CsvReaderTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ class CsvReaderTest : WordSpec({
138138
}.readAll(readTestDataFile("bom.csv"))
139139
result shouldBe listOf(listOf("a", "b", "c"))
140140
}
141+
"read empty csv with BOM" {
142+
val result = csvReader {
143+
escapeChar = '\\'
144+
}.readAll(readTestDataFile("empty-bom.csv"))
145+
result shouldBe listOf()
146+
}
141147
//refs https://github.com/tototoshi/scala-csv/issues/22
142148
"read csv with \u2028 field" {
143149
val result = csvReader().readAll(readTestDataFile("unicode2028.csv"))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+


0 commit comments

Comments
 (0)