fix(esx_lib): correct isArray, toPascal and replace helpers

Three small logic bugs in the shared helpers, each verified with a standalone Lua
run.

isArray returned true for sparse tables like {[1]=1,[3]=3}: the in-loop
`count > maxIndex` check can never fire (with positive integer keys count is always
<= maxIndex), so it was dead code. Dropped it and return `count == maxIndex`, which
is false for gaps and still true for a dense sequence or an empty table.

toPascal lowercased the letters after an underscore: `[%w_]*` swallowed the
underscore and the next word into the part that gets lowercased, so "hello_world"
became "Helloworld". Excluding the underscore from that class leaves each word to be
capitalised, then the trailing gsub removes the underscores -> "HelloWorld".

replace escaped the pattern but not the replacement, so a "%" in the replacement
raised "invalid use of '%' in replacement string" and "%1" style sequences expanded
captures. Escape "%" in a string replacement; a function or table replacement is
left untouched.
This commit is contained in:
Selt
2026-07-27 20:22:11 +02:00
parent 5354e278de
commit 6cbc408102
2 changed files with 6 additions and 6 deletions
+5 -1
View File
@@ -47,7 +47,7 @@ end
function xLib.string.toPascal(s)
xLib.verify(s, 'string', true)
local res = s:gsub("(%a)([%w_]*)", function(first, rest)
local res = s:gsub("(%a)([%w]*)", function(first, rest)
return first:upper() .. rest:lower()
end):gsub("_", "")
@@ -114,6 +114,10 @@ end
function xLib.string.replace(s, old, new)
xLib.verify(s, 'string', true)
if type(new) == "string" then
new = new:gsub("%%", "%%%%")
end
local result = s:gsub(xLib.string.escapePattern(old), new)
return result
+1 -5
View File
@@ -14,13 +14,9 @@ function xLib.table.isArray(tbl)
end
count, maxIndex = count + 1, math.max(maxIndex, k)
if count > maxIndex then
return false
end
end
return true
return count == maxIndex
end
---@param tbl table