diff --git a/ftcsv.lua b/ftcsv.lua index e94a567..83abe7a 100644 --- a/ftcsv.lua +++ b/ftcsv.lua @@ -91,9 +91,6 @@ else if j == nil then return nil end - if i == nil then - return inputLength-1, doubleQuoteEscape - end difference = j - i if difference >= 1 then doubleQuoteEscape = true end if difference == 1 then @@ -285,9 +282,14 @@ local function parseString(inputString, i, options) -- in buffered mode and it can't find the closing quote -- it usually means in the middle of a buffer and need to backtrack - if i == nil and buffered then - outResults[lineNum] = nil - return outResults, lineStart + if i == nil then + if buffered then + outResults[lineNum] = nil + return outResults, lineStart + else + error("ftcsv: can't find closing quote in row " .. options.rowOffset + lineNum .. + ". Try running with the option ignoreQuotes=true if the source incorrectly uses quotes.") + end end -- Increment Counter diff --git a/spec/error_spec.lua b/spec/error_spec.lua index c7a287f..38cb81d 100644 --- a/spec/error_spec.lua +++ b/spec/error_spec.lua @@ -55,3 +55,10 @@ describe("parseLine features small, nonworking buffer size", function() assert.has_error(test, "ftcsv: parseLine currently doesn't support loading from string") end) end) + +it("should error when dealing with quotes", function() + local test = function() + local actual = ftcsv.parse('a,b,c\n"apple,banana,carrot', ",", {loadFromString=true}) + end + assert.has_error(test, "ftcsv: can't find closing quote in row 1. Try running with the option ignoreQuotes=true if the source incorrectly uses quotes.") +end) \ No newline at end of file diff --git a/spec/feature_spec.lua b/spec/feature_spec.lua index dd6451d..c58bbc9 100644 --- a/spec/feature_spec.lua +++ b/spec/feature_spec.lua @@ -318,4 +318,14 @@ describe("csv features", function() assert.are.same(expected, actual) end) + it("should handle ignoring the single quote", function() + local expected = {} + expected[1] = {} + expected[1].a = '"apple' + expected[1].b = "banana" + expected[1].c = "carrot" + local actual = ftcsv.parse('a,b,c\n"apple,banana,carrot', ",", {loadFromString=true, ignoreQuotes=true}) + assert.are.same(expected, actual) + end) + end) \ No newline at end of file