@@ -182,24 +182,19 @@ local function separated_values_iterator(file, parameters)
182
182
local function find (pattern , init )
183
183
local first , last , capture
184
184
while true do
185
- first , last , capture = buffer :find (pattern , anchor_pos + init - 1 )
185
+ first , last , capture = buffer :find (pattern , init )
186
186
-- if we found nothing, or the last character is at the end of the
187
187
-- buffer (and the match could potentially be longer) then read some
188
188
-- more.
189
189
if not first or last == # buffer then
190
190
local s = file :read (buffer_size )
191
- -- if we read nothing from the file...
192
- if not s then
193
- if first then
194
- -- ...and last == #buffer, then the capture we found above is good.
195
- return first - anchor_pos + 1 , last - anchor_pos + 1 , capture
196
- else
197
- return
198
- end
199
- end
191
+ -- if we read nothing from the file:
192
+ -- - and first is nil, then below we're returning nil
193
+ -- - and last == #buffer, then the capture we found above is good.
194
+ if not s then return first , last , capture end
200
195
buffer = buffer .. s
201
196
else
202
- return first - anchor_pos + 1 , last - anchor_pos + 1 , capture
197
+ return first , last , capture
203
198
end
204
199
end
205
200
end
@@ -231,6 +226,13 @@ local function separated_values_iterator(file, parameters)
231
226
end
232
227
233
228
229
+ local function field_find (pattern , init )
230
+ local f , l , c = find (pattern , init + anchor_pos - 1 )
231
+ if not f then return end
232
+ return f - anchor_pos + 1 , l - anchor_pos + 1 , c
233
+ end
234
+
235
+
234
236
-- If the user hasn't specified a separator, try to work out what it is.
235
237
local sep = parameters .separator
236
238
if not sep then
@@ -255,21 +257,21 @@ local function separated_values_iterator(file, parameters)
255
257
advance (1 )
256
258
local current_pos = 0
257
259
repeat
258
- local a , b , c = find (' "("?)' , current_pos + 1 )
260
+ local a , b , c = field_find (' "("?)' , current_pos + 1 )
259
261
current_pos = b
260
262
until c ~= ' "'
261
263
if not current_pos then
262
264
error ((" %s:%d:%d: unmatched quote" ):
263
265
format (filename , field_start_line , field_start_column ))
264
266
end
265
267
tidy = fix_quotes
266
- field_end , sep_end , this_sep = find (" *([^ ])" , current_pos + 1 )
268
+ field_end , sep_end , this_sep = field_find (" *([^ ])" , current_pos + 1 )
267
269
if this_sep and not this_sep :match (sep ) then
268
270
error ((" %s:%d:%d: unmatched quote" ):
269
271
format (filename , field_start_line , field_start_column ))
270
272
end
271
273
else
272
- field_end , sep_end , this_sep = find (sep , 1 )
274
+ field_end , sep_end , this_sep = field_find (sep , 1 )
273
275
tidy = trim_space
274
276
end
275
277
0 commit comments