Skip to content

Commit

Permalink
Merge pull request #311 from tototoshi/issue-141
Browse files Browse the repository at this point in the history
Fixed a bug where the escape character used during CSV output is incorrect
  • Loading branch information
tototoshi authored Jun 23, 2024
2 parents 28fc5ab + 8da555c commit eb3ff8b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/com/github/tototoshi/csv/CSVWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class CSVWriter(protected val writer: Writer)(implicit val format: CSVFormat) ex
while (i < field.length) {
val char = field(i)
if (char == format.quoteChar || (format.quoting == QUOTE_NONE && char == format.delimiter)) {
printWriter.print(format.quoteChar)
printWriter.print(format.escapeChar)
}
printWriter.print(char)
i += 1
Expand Down
37 changes: 28 additions & 9 deletions src/test/scala/com/github/tototoshi/csv/CSVWriterSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class CSVWriterSpec extends AnyFunSpec with Matchers with BeforeAndAfter with Us
}
}

describe("#writeNext") {
describe("#writeRow") {
it("write single line to file") {
using(CSVWriter.open(new FileWriter("test.csv"))) { writer =>
writer.writeRow(List("a", "b", "c"))
Expand All @@ -141,18 +141,36 @@ class CSVWriterSpec extends AnyFunSpec with Matchers with BeforeAndAfter with Us

readFileAsString("test.csv") should be(expected)
}
describe("When a field contains quoteChar in it") {
it("should escape the quoteChar") {
using(CSVWriter.open(new FileWriter("test.csv"))) { writer =>
writer.writeRow(List("a", "b\"", "c"))
writer.writeRow(List("d", "e", "f"))
}

val expected = "a,\"b\"\"\",c\nd,e,f\n"
it("should escape the quoteChar with escapeChar when it is included in the field") {
using(CSVWriter.open(new FileWriter("test.csv"))) { writer =>
writer.writeRow(List("a", "b\"", "c"))
writer.writeRow(List("d", "e", "f"))
}

val expected = "a,\"b\"\"\",c\nd,e,f\n"

readFileAsString("test.csv") should be(expected)
readFileAsString("test.csv") should be(expected)
}

it("should escape the quoteChar with customized escapeChar when it is included in the field and escapeChar is changed from default value") {

object escapeCharChangedFormat extends DefaultCSVFormat {
override val escapeChar: Char = '\\'
}

using(CSVWriter.open(new FileWriter("test.csv"))(escapeCharChangedFormat)) { writer =>
writer.writeRow(List("a", "b\"", "c"))
writer.writeRow(List("d", "e", "f"))
}

val expected = """|a,"b\"",c
|d,e,f
|""".stripMargin

readFileAsString("test.csv") should be(expected)
}

describe("When a field contains delimiter in it") {
it("should escape the delimiter") {
using(CSVWriter.open(new FileWriter("test.csv"))) { writer =>
Expand Down Expand Up @@ -243,6 +261,7 @@ class CSVWriterSpec extends AnyFunSpec with Matchers with BeforeAndAfter with Us
}
}
}

}

describe("#flush") {
Expand Down

0 comments on commit eb3ff8b

Please sign in to comment.