Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat bom-only CSV file as empty #112

Merged
merged 4 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ package com.github.doyaaaaaken.kotlincsv.client
internal class BufferedLineReader(
private val br: Reader
) {
companion object {
private const val BOM = '\uFEFF'
}

private fun StringBuilder.isEmptyLine(): Boolean =
this.isEmpty() || this.length == 1 && this[0] == BOM
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code below seems to better grasp the intent.
this.isEmpty() || (this.length == 1 && this[0] == BOM)

Copy link
Contributor Author

@KengoTODA KengoTODA Dec 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer it too, but CodeFactor did not like it so I fixed it as 5231efb 😇

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh... 😇 OK, Let's leave it as is!


fun readLineWithTerminator(): String? {
val sb = StringBuilder()
do {
val c = br.read()

if (c == -1) {
if (sb.isEmpty()) {
if (sb.isEmptyLine()) {
return null
} else {
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ class CsvReaderTest : WordSpec({
}.readAll(readTestDataFile("bom.csv"))
result shouldBe listOf(listOf("a", "b", "c"))
}
"read empty csv with BOM" {
val result = csvReader {
escapeChar = '\\'
}.readAll(readTestDataFile("empty-bom.csv"))
result shouldBe listOf()
}
//refs https://github.com/tototoshi/scala-csv/issues/22
"read csv with \u2028 field" {
val result = csvReader().readAll(readTestDataFile("unicode2028.csv"))
Expand Down
1 change: 1 addition & 0 deletions src/jvmTest/resources/testdata/csv/empty-bom.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@