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

Issues/58 #59

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
7 changes: 4 additions & 3 deletions src/main/scala/com/github/tototoshi/csv/CSVParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,14 @@ object CSVParser {
c match {
case `escapeChar` if escapeChar != quoteChar => {
if (pos + 1 < buflen) {
if (buf(pos + 1) == escapeChar
|| buf(pos + 1) == quoteChar) {
if (buf(pos + 1) == quoteChar) {
field += buf(pos + 1)
state = QuotedField
pos += 2
} else {
throw new MalformedCSVException(buf.mkString)
field += c
state = QuotedField
pos += 1
}
} else {
throw new MalformedCSVException(buf.mkString)
Expand Down
1 change: 0 additions & 1 deletion src/main/scala/com/github/tototoshi/csv/CSVReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ class CSVReader protected (private val lineReader: LineReader)(implicit format:
case None => readNext().getOrElse(throw new NoSuchElementException("next on empty iterator"))
}
}

}

def iteratorWithHeaders: Iterator[Map[String, String]] = {
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/tabs-LF-inside-quotes.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"a" "a a" "a a a\b
b"
"a" "b" "c"
23 changes: 21 additions & 2 deletions src/test/scala/com/github/tototoshi/csv/CSVReaderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class CSVReaderSpec extends FunSpec with ShouldMatchers with Using {
override val quoteChar: Char = '$'
}

using(CSVReader.open("src/test/resources/hash-separated-dollar-quote.csv")(format)) { reader =>
{
using(CSVReader.open("src/test/resources/hash-separated-dollar-quote.csv")(format)) {
reader => {
val map = reader.allWithHeaders()
map(0)("Foo ") should be("a")
}
Expand Down Expand Up @@ -336,6 +336,25 @@ class CSVReaderSpec extends FunSpec with ShouldMatchers with Using {
}
}
}

describe("when the file has tabs and new lines") {
it("should be throw exception against malformed input") {
object format extends DefaultCSVFormat {
override val delimiter: Char = '\t'
override val quoteChar: Char = '"'
override val escapeChar: Char = '\\'
override val lineTerminator: String = "\n"
override val quoting: Quoting = QUOTE_ALL
override val treatEmptyLineAsNil: Boolean = false
}
using(CSVReader.open(new FileReader("src/test/resources/tabs-LF-inside-quotes.csv"))(format)) { reader =>
val res = reader.all()

res(0) should be(List("a", "a a", "a\ta a\\b\nb"))
res(1) should be(List("a", "b", "c"))
}
}
}
}
}
}