Skip to content

Commit 88e30b6

Browse files
committed
Test at all buffer sizes from 1 to 16. Fix the resulting bug when an embedded quote straddles two buffer blocks
1 parent 13e3b69 commit 88e30b6

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

lua/csv.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,15 @@ local function separated_values_iterator(file, parameters)
184184
local first, last, capture
185185
while true do
186186
first, last, capture = buffer:find(pattern, anchor_pos + offset - 1)
187-
if not first then
187+
-- if we found nothing, or the last character is at the end of the
188+
-- buffer (and the match could potentially be longer) then read some
189+
-- more.
190+
if not first or last == #buffer then
188191
local s = file:read(buffer_size)
189-
if not s then return end
192+
-- if we read nothing from the file:
193+
-- - and first is nil, then we found nothing. This will return nil.
194+
-- - and last == #buffer, then the capture we found above is good.
195+
if not s then return first, last, capture end
190196
buffer = buffer..s
191197
else
192198
return first - anchor_pos + 1, last - anchor_pos + 1, capture

lua/test.lua

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,20 @@ local function testhandle(handle, correct_result)
2929
end
3030

3131
local function test(filename, correct_result, parameters)
32-
local f = csv.open(filename, parameters)
33-
local fileok = testhandle(f, correct_result)
32+
parameters = parameters or {}
33+
for i = 1, 16 do
34+
parameters.buffer_size = i
35+
local f = csv.open(filename, parameters)
36+
local fileok = testhandle(f, correct_result)
3437

35-
if fileok then
36-
f = io.open(filename, "r")
37-
local data = f:read("*a")
38-
f:close()
38+
if fileok then
39+
f = io.open(filename, "r")
40+
local data = f:read("*a")
41+
f:close()
3942

40-
f = csv.openstring(data, parameters)
41-
testhandle(f, correct_result)
43+
f = csv.openstring(data, parameters)
44+
testhandle(f, correct_result)
45+
end
4246
end
4347
end
4448

@@ -50,26 +54,25 @@ newline!
5054
embedded
5155
newline,embedded
5256
newline,embedded
53-
newline!]], { buffer_size=4})
57+
newline!]])
5458

5559
test("../test-data/embedded-quotes.csv", [[
5660
embedded "quotes",embedded "quotes",embedded "quotes"!
5761
embedded "quotes",embedded "quotes",embedded "quotes"!]])
5862

5963
test("../test-data/header.csv", [[
6064
alpha:ONE,bravo:two,charlie:3!
61-
alpha:four,bravo:five,charlie:6!]], {header=true, buffer_size=5})
65+
alpha:four,bravo:five,charlie:6!]], {header=true})
6266

6367
test("../test-data/header.csv", [[
6468
apple:one,charlie:30!
6569
apple:four,charlie:60!]],
66-
{ buffer_size = 7,
67-
columns = {
70+
{ columns = {
6871
apple = { name = "ALPHA", transform = string.lower },
6972
charlie = { transform = function(x) return tonumber(x) * 10 end }}})
7073

7174
test("../test-data/blank-line.csv", [[
72-
this,file,ends,with,a,blank,line!]], { buffer_size=11 })
75+
this,file,ends,with,a,blank,line!]])
7376

7477

7578
if errors == 0 then

0 commit comments

Comments
 (0)