Skip to content

Issue 66 #94

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 9 additions & 12 deletions src/main/scala/com/github/tototoshi/csv/CSVParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ object CSVParser {
pos += 1
}
case '\r' => {
if (pos + 1 < buflen && buf(1) == '\n') {
if (pos + 1 < buflen && buf(pos + 1) == '\n') {
pos += 1
}
fields :+= field.toString
Expand Down Expand Up @@ -115,7 +115,7 @@ object CSVParser {
pos += 1
}
case '\r' => {
if (pos + 1 < buflen && buf(1) == '\n') {
if (pos + 1 < buflen && buf(pos + 1) == '\n') {
pos += 1
}
fields :+= field.toString
Expand Down Expand Up @@ -160,7 +160,7 @@ object CSVParser {
pos += 1
}
case '\r' => {
if (pos + 1 < buflen && buf(1) == '\n') {
if (pos + 1 < buflen && buf(pos + 1) == '\n') {
pos += 1
}
fields :+= field.toString
Expand Down Expand Up @@ -223,7 +223,7 @@ object CSVParser {
pos += 1
}
case '\r' => {
if (pos + 1 < buflen && buf(1) == '\n') {
if (pos + 1 < buflen && buf(pos + 1) == '\n') {
pos += 1
}
fields :+= field.toString
Expand Down Expand Up @@ -283,14 +283,11 @@ object CSVParser {
None
}
case _ => {
if (!field.isEmpty) {
// When no crlf at end of file
state match {
case Field | QuoteEnd => {
fields :+= field.toString
}
case _ => {
}
state match {
case Field | QuoteEnd => {
fields :+= field.toString
}
case _ => {
}
}
Some(fields.toList)
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/simple-crlf.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a,b,c
d,e,f

59 changes: 50 additions & 9 deletions src/test/scala/com/github/tototoshi/csv/CSVReaderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,49 @@ class CSVReaderSpec extends FunSpec with Matchers with Using {
}
}

it("should be able to read an empty line") {
using(CSVReader.open("src/test/resources/has-empty-line.csv", "utf-8")) { reader =>
val lines = reader.all()
lines(1) should be(List(""))
describe("#emptyLines") {
it("should be able to read an empty line") {
using(CSVReader.open("src/test/resources/has-empty-line.csv", "utf-8")) { reader =>
val lines = reader.all()
lines(1) should be(List(""))
}
}

it("should treat empty line as Nil") {
val format = new DefaultCSVFormat {
override val treatEmptyLineAsNil = true
}
using(CSVReader.open("src/test/resources/has-empty-line.csv", "utf-8")(format)) { reader =>
val lines = reader.all()
lines(1) should be(Nil)
}
}

val format = new DefaultCSVFormat {
override val treatEmptyLineAsNil = true
it("should be able to read empty field with line feed") {
val csvString = "\"\"\r\n"
var res: List[String] = Nil
using(CSVReader.open(new StringReader(csvString))) { reader =>
val lines = reader.all()
lines(0) should be(List(""))
}
}
using(CSVReader.open("src/test/resources/has-empty-line.csv", "utf-8")(format)) { reader =>
val lines = reader.all()
lines(1) should be(Nil)

it("should be able to read empty field without line feed") {
val csvString = "\"\""
var res: List[String] = Nil
using(CSVReader.open(new StringReader(csvString))) { reader =>
val lines = reader.all()
lines(0) should be(List(""))
}
}

it("should be able to read empty last field without line feed") {
val csvString = "a,b,c,\"\",d,\"\""
var res: List[String] = Nil
using(CSVReader.open(new StringReader(csvString))) { reader =>
val lines = reader.all()
lines(0) should be(List("a", "b", "c", "", "d", ""))
}
}
}

Expand All @@ -96,6 +127,16 @@ class CSVReaderSpec extends FunSpec with Matchers with Using {
res.mkString should be("abcdef")
}

it("read simple CSV from file with CRLF") {
var res: List[String] = Nil
using(CSVReader.open(new FileReader("src/test/resources/simple-crlf.csv"))) { reader =>
reader foreach { fields =>
res = res ++ fields
}
}
res.mkString should be("abcdef")
}

it("issue #22") {
var res: List[String] = Nil
using(CSVReader.open("src/test/resources/issue22.csv")) { reader =>
Expand Down