Comparison

util/datamanager.lua @ 13182:c48ae06e24d6

util.datamanager: Fix indexing first item if not at the very start If the first item does not start at position 0 then the index function produces a phantom first entry covering position zero until where the real first item starts. When using the index, this would make it either appear as the first item was missing or cause an off-by-one issue with remaining items.
author Kim Alvefur <zash@zash.se>
date Mon, 10 Jul 2023 17:19:05 +0200
parent 13181:87487056bccb
child 13183:33b114fbb5de
comparison
equal deleted inserted replaced
13181:87487056bccb 13182:c48ae06e24d6
327 local fh, err, errno = io_open(filename); 327 local fh, err, errno = io_open(filename);
328 if not fh then 328 if not fh then
329 return fh, err, errno; 329 return fh, err, errno;
330 end 330 end
331 local prev_pos = 0; -- position before reading 331 local prev_pos = 0; -- position before reading
332 local last_item_start = 0; 332 local last_item_start = nil;
333 333
334 if items and items[1] then 334 if items and items[1] then
335 local last_item = items[#items]; 335 local last_item = items[#items];
336 last_item_start = fh:seek("set", last_item.start + last_item.length); 336 last_item_start = fh:seek("set", last_item.start + last_item.length);
337 else 337 else
338 items = {}; 338 items = {};
339 end 339 end
340 340
341 for line in fh:lines() do 341 for line in fh:lines() do
342 if line:sub(1, 4) == "item" then 342 if line:sub(1, 4) == "item" then
343 if prev_pos ~= 0 then 343 if prev_pos ~= 0 and last_item_start then
344 t_insert(items, { start = last_item_start; length = prev_pos - last_item_start }); 344 t_insert(items, { start = last_item_start; length = prev_pos - last_item_start });
345 end 345 end
346 last_item_start = prev_pos 346 last_item_start = prev_pos
347 end 347 end
348 -- seek position is at the start of the next line within each loop iteration 348 -- seek position is at the start of the next line within each loop iteration