Fix logic in project's file insertion

The function "file_search" in core.init was sometimes giving a wrong index
value, off by one.

The problem happened for example when the entry to search was "less than"
the first entry, the function returned a value of two instead of one as
expected.

The bug was easily observed creating a new directory with a name that comes
as the first in alphabetical order within the project.
This commit is contained in:
Francesco Abbate 2021-11-26 13:45:13 +01:00
parent e512c57637
commit 405bd1c2bd
1 changed files with 2 additions and 3 deletions

View File

@ -191,7 +191,6 @@ end
local function file_search(files, info) local function file_search(files, info)
local filename, type = info.filename, info.type local filename, type = info.filename, info.type
local inf, sup = 1, #files local inf, sup = 1, #files
if sup <= 0 then return 1, false end
while sup - inf > 8 do while sup - inf > 8 do
local curr = math.floor((inf + sup) / 2) local curr = math.floor((inf + sup) / 2)
if system.path_compare(filename, type, files[curr].filename, files[curr].type) then if system.path_compare(filename, type, files[curr].filename, files[curr].type) then
@ -200,12 +199,12 @@ local function file_search(files, info)
inf = curr inf = curr
end end
end end
repeat while inf <= sup and not system.path_compare(filename, type, files[inf].filename, files[inf].type) do
if files[inf].filename == filename then if files[inf].filename == filename then
return inf, true return inf, true
end end
inf = inf + 1 inf = inf + 1
until inf > sup or system.path_compare(filename, type, files[inf].filename, files[inf].type) end
return inf, false return inf, false
end end