mirror of
https://github.com/geoffleyland/lua-csv.git
synced 2024-11-23 01:34:19 +00:00
csv.lua: move find's offsetting into field_find
This commit is contained in:
parent
6892667042
commit
0daa2ae5d7
30
lua/csv.lua
30
lua/csv.lua
@ -182,24 +182,19 @@ local function separated_values_iterator(file, parameters)
|
|||||||
local function find(pattern, init)
|
local function find(pattern, init)
|
||||||
local first, last, capture
|
local first, last, capture
|
||||||
while true do
|
while true do
|
||||||
first, last, capture = buffer:find(pattern, anchor_pos + init - 1)
|
first, last, capture = buffer:find(pattern, init)
|
||||||
-- if we found nothing, or the last character is at the end of the
|
-- if we found nothing, or the last character is at the end of the
|
||||||
-- buffer (and the match could potentially be longer) then read some
|
-- buffer (and the match could potentially be longer) then read some
|
||||||
-- more.
|
-- more.
|
||||||
if not first or last == #buffer then
|
if not first or last == #buffer then
|
||||||
local s = file:read(buffer_size)
|
local s = file:read(buffer_size)
|
||||||
-- if we read nothing from the file...
|
-- if we read nothing from the file:
|
||||||
if not s then
|
-- - and first is nil, then below we're returning nil
|
||||||
if first then
|
-- - and last == #buffer, then the capture we found above is good.
|
||||||
-- ...and last == #buffer, then the capture we found above is good.
|
if not s then return first, last, capture end
|
||||||
return first - anchor_pos + 1, last - anchor_pos + 1, capture
|
|
||||||
else
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end
|
|
||||||
buffer = buffer..s
|
buffer = buffer..s
|
||||||
else
|
else
|
||||||
return first - anchor_pos + 1, last - anchor_pos + 1, capture
|
return first, last, capture
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -231,6 +226,13 @@ local function separated_values_iterator(file, parameters)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local function field_find(pattern, init)
|
||||||
|
local f, l, c = find(pattern, init + anchor_pos - 1)
|
||||||
|
if not f then return end
|
||||||
|
return f - anchor_pos + 1, l - anchor_pos + 1, c
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
-- If the user hasn't specified a separator, try to work out what it is.
|
-- If the user hasn't specified a separator, try to work out what it is.
|
||||||
local sep = parameters.separator
|
local sep = parameters.separator
|
||||||
if not sep then
|
if not sep then
|
||||||
@ -255,7 +257,7 @@ local function separated_values_iterator(file, parameters)
|
|||||||
advance(1)
|
advance(1)
|
||||||
local current_pos = 0
|
local current_pos = 0
|
||||||
repeat
|
repeat
|
||||||
local a, b, c = find('"("?)', current_pos + 1)
|
local a, b, c = field_find('"("?)', current_pos + 1)
|
||||||
current_pos = b
|
current_pos = b
|
||||||
until c ~= '"'
|
until c ~= '"'
|
||||||
if not current_pos then
|
if not current_pos then
|
||||||
@ -263,13 +265,13 @@ local function separated_values_iterator(file, parameters)
|
|||||||
format(filename, field_start_line, field_start_column))
|
format(filename, field_start_line, field_start_column))
|
||||||
end
|
end
|
||||||
tidy = fix_quotes
|
tidy = fix_quotes
|
||||||
field_end, sep_end, this_sep = find(" *([^ ])", current_pos+1)
|
field_end, sep_end, this_sep = field_find(" *([^ ])", current_pos+1)
|
||||||
if this_sep and not this_sep:match(sep) then
|
if this_sep and not this_sep:match(sep) then
|
||||||
error(("%s:%d:%d: unmatched quote"):
|
error(("%s:%d:%d: unmatched quote"):
|
||||||
format(filename, field_start_line, field_start_column))
|
format(filename, field_start_line, field_start_column))
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
field_end, sep_end, this_sep = find(sep, 1)
|
field_end, sep_end, this_sep = field_find(sep, 1)
|
||||||
tidy = trim_space
|
tidy = trim_space
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user