Skip to content

Commit

Permalink
Permitted to place the escape character before a non-special characte…
Browse files Browse the repository at this point in the history
…r so don't throw exception
  • Loading branch information
toshi committed Feb 15, 2016
1 parent 8fab90a commit 4de296d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 28 deletions.
36 changes: 10 additions & 26 deletions src/main/scala/com/github/tototoshi/csv/CSVParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ object CSVParser {
pos += 1
}
case `escapeChar` => {
if (pos + 1 < buflen
&& (buf(pos + 1) == escapeChar || buf(pos + 1) == delimiter)) {
if (pos + 1 < buflen) {
field += buf(pos + 1)
state = Field
pos += 2
Expand Down Expand Up @@ -130,14 +129,9 @@ object CSVParser {
c match {
case `escapeChar` => {
if (pos + 1 < buflen) {
if (buf(pos + 1) == escapeChar
|| buf(pos + 1) == delimiter) {
field += buf(pos + 1)
state = Field
pos += 2
} else {
throw new MalformedCSVException(buf.mkString)
}
field += buf(pos + 1)
state = Field
pos += 2
} else {
state = QuoteEnd
pos += 1
Expand Down Expand Up @@ -175,14 +169,9 @@ object CSVParser {
c match {
case `escapeChar` if escapeChar != quoteChar => {
if (pos + 1 < buflen) {
if (buf(pos + 1) == escapeChar
|| buf(pos + 1) == quoteChar) {
field += buf(pos + 1)
state = QuotedField
pos += 2
} else {
throw new MalformedCSVException(buf.mkString)
}
field += buf(pos + 1)
state = QuotedField
pos += 2
} else {
throw new MalformedCSVException(buf.mkString)
}
Expand Down Expand Up @@ -236,14 +225,9 @@ object CSVParser {
c match {
case `escapeChar` if escapeChar != quoteChar => {
if (pos + 1 < buflen) {
if (buf(pos + 1) == escapeChar
|| buf(pos + 1) == quoteChar) {
field += buf(pos + 1)
state = QuotedField
pos += 2
} else {
throw new MalformedCSVException(buf.mkString)
}
field += buf(pos + 1)
state = QuotedField
pos += 2
} else {
throw new MalformedCSVException(buf.mkString)
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/backslash-escape.csv
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"field1","field2","field3 says, \"escaped with backslash\""
"field1","field2","field3 says, \"escaped with backslash\"","\\\f\i\e\l\d\4",\\\f\i\e\l\d\5
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class CSVReaderSpec extends FunSpec with ShouldMatchers with Using {
res = res ++ fields
}
}
res should be(List("field1", "field2", "field3 says, \"escaped with backslash\""))
res should be(List("field1", "field2", "field3 says, \"escaped with backslash\"", "\\field4", "\\field5"))
}

it("read simple CSV file with empty quoted fields") {
Expand Down

0 comments on commit 4de296d

Please sign in to comment.