Skip to content

Commit 4de296d

Browse files
author
toshi
committed
Permitted to place the escape character before a non-special character so don't throw exception
1 parent 8fab90a commit 4de296d

File tree

3 files changed

+12
-28
lines changed

3 files changed

+12
-28
lines changed

src/main/scala/com/github/tototoshi/csv/CSVParser.scala

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ object CSVParser {
8989
pos += 1
9090
}
9191
case `escapeChar` => {
92-
if (pos + 1 < buflen
93-
&& (buf(pos + 1) == escapeChar || buf(pos + 1) == delimiter)) {
92+
if (pos + 1 < buflen) {
9493
field += buf(pos + 1)
9594
state = Field
9695
pos += 2
@@ -130,14 +129,9 @@ object CSVParser {
130129
c match {
131130
case `escapeChar` => {
132131
if (pos + 1 < buflen) {
133-
if (buf(pos + 1) == escapeChar
134-
|| buf(pos + 1) == delimiter) {
135-
field += buf(pos + 1)
136-
state = Field
137-
pos += 2
138-
} else {
139-
throw new MalformedCSVException(buf.mkString)
140-
}
132+
field += buf(pos + 1)
133+
state = Field
134+
pos += 2
141135
} else {
142136
state = QuoteEnd
143137
pos += 1
@@ -175,14 +169,9 @@ object CSVParser {
175169
c match {
176170
case `escapeChar` if escapeChar != quoteChar => {
177171
if (pos + 1 < buflen) {
178-
if (buf(pos + 1) == escapeChar
179-
|| buf(pos + 1) == quoteChar) {
180-
field += buf(pos + 1)
181-
state = QuotedField
182-
pos += 2
183-
} else {
184-
throw new MalformedCSVException(buf.mkString)
185-
}
172+
field += buf(pos + 1)
173+
state = QuotedField
174+
pos += 2
186175
} else {
187176
throw new MalformedCSVException(buf.mkString)
188177
}
@@ -236,14 +225,9 @@ object CSVParser {
236225
c match {
237226
case `escapeChar` if escapeChar != quoteChar => {
238227
if (pos + 1 < buflen) {
239-
if (buf(pos + 1) == escapeChar
240-
|| buf(pos + 1) == quoteChar) {
241-
field += buf(pos + 1)
242-
state = QuotedField
243-
pos += 2
244-
} else {
245-
throw new MalformedCSVException(buf.mkString)
246-
}
228+
field += buf(pos + 1)
229+
state = QuotedField
230+
pos += 2
247231
} else {
248232
throw new MalformedCSVException(buf.mkString)
249233
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"field1","field2","field3 says, \"escaped with backslash\""
1+
"field1","field2","field3 says, \"escaped with backslash\"","\\\f\i\e\l\d\4",\\\f\i\e\l\d\5

src/test/scala/com/github/tototoshi/csv/CSVReaderSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class CSVReaderSpec extends FunSpec with ShouldMatchers with Using {
126126
res = res ++ fields
127127
}
128128
}
129-
res should be(List("field1", "field2", "field3 says, \"escaped with backslash\""))
129+
res should be(List("field1", "field2", "field3 says, \"escaped with backslash\"", "\\field4", "\\field5"))
130130
}
131131

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

0 commit comments

Comments
 (0)