Update all code styles

This commit is contained in:
Cooldude2606
2024-09-28 01:56:54 +01:00
parent 5e2a62ab27
commit 292c1a1b68
194 changed files with 9817 additions and 9703 deletions

View File

@@ -1,4 +1,4 @@
--luacheck:ignore global table
-- luacheck:ignore global table
local random = math.random
local floor = math.floor
@@ -42,17 +42,18 @@ function table.array_insert(tbl, start_index, values)
if start_index then
local starting_length = #tbl
local adding_length = #values
local move_to = start_index+adding_length+1
for offset = starting_length-start_index, 0, -1 do
tbl[move_to+offset] = tbl[starting_length+offset]
local move_to = start_index + adding_length + 1
for offset = starting_length - start_index, 0, -1 do
tbl[move_to + offset] = tbl[starting_length + offset]
end
start_index = start_index-1
start_index = start_index - 1
else
start_index = #tbl
end
for offset, item in ipairs(values) do
tbl[start_index+offset] = item
tbl[start_index + offset] = item
end
return tbl
@@ -125,6 +126,7 @@ function table.get_key(tbl, element)
return k
end
end
return nil
end
@@ -138,6 +140,7 @@ function table.get_index(tbl, element)
return i
end
end
return nil
end
@@ -166,9 +169,10 @@ local format_number, distance = table.deconstruct(require('util'), 'format_numbe
]]
function table.deconstruct(tbl, ...)
local values = {}
for _, key in pairs({...}) do
for _, key in pairs{ ... } do
table.insert(values, tbl[key])
end
return table.unpack(values)
end
@@ -239,7 +243,7 @@ function table.shuffle(t, rng)
local rand = rng or math.random
local iterations = #t
if iterations == 0 then
error('Not a sequential table')
error("Not a sequential table")
return
end
local j
@@ -255,19 +259,19 @@ end
-- @param x one comparator operand
-- @param y the other comparator operand
-- @return true if x logically comes before y in a list, false otherwise
local function sortFunc(x, y) --sorts tables with mixed index types.
local function sortFunc(x, y) -- sorts tables with mixed index types.
local tx = type(x)
local ty = type(y)
if tx == ty then
if type(x) == 'string' then
if type(x) == "string" then
return string.lower(x) < string.lower(y)
else
return x < y
end
elseif tx == 'number' then
return true --only x is a number and goes first
elseif tx == "number" then
return true -- only x is a number and goes first
else
return false --only y is a number and goes first
return false -- only y is a number and goes first
end
end
@@ -280,7 +284,7 @@ function table.get_values(tbl, sorted, as_string)
if not tbl then return {} end
local valueset = {}
local n = 0
if as_string then --checking as_string /before/ looping is faster
if as_string then -- checking as_string /before/ looping is faster
for _, v in pairs(tbl) do
n = n + 1
valueset[n] = tostring(v)
@@ -306,7 +310,7 @@ function table.get_keys(tbl, sorted, as_string)
if not tbl then return {} end
local keyset = {}
local n = 0
if as_string then --checking as_string /before/ looping is faster
if as_string then -- checking as_string /before/ looping is faster
for k, _ in pairs(tbl) do
n = n + 1
keyset[n] = tostring(k)
@@ -328,13 +332,17 @@ end
-- @treturn table the sorted table
function table.alphanum_sort(tbl)
local o = table.get_keys(tbl)
local function padnum(d) local dec, n = string.match(d, "(%.?)0*(.+)")
return #dec > 0 and ("%.12f"):format(d) or ("%s%03d%s"):format(dec, #n, n) end
local function padnum(d)
local dec, n = string.match(d, "(%.?)0*(.+)")
return #dec > 0 and ("%.12f"):format(d) or ("%s%03d%s"):format(dec, #n, n)
end
table.sort(o, function(a, b)
return tostring(a):gsub("%.?%d+", padnum)..("%3d"):format(#b)
< tostring(b):gsub("%.?%d+", padnum)..("%3d"):format(#a) end)
return tostring(a):gsub("%.?%d+", padnum) .. ("%3d"):format(#b)
< tostring(b):gsub("%.?%d+", padnum) .. ("%3d"):format(#a)
end)
local _tbl = {}
for _, k in pairs(o) do _tbl[k] = tbl[k] end
return _tbl
end
@@ -345,6 +353,7 @@ function table.key_sort(tbl)
local o = table.get_keys(tbl, true)
local _tbl = {}
for _, k in pairs(o) do _tbl[k] = tbl[k] end
return _tbl
end
@@ -365,7 +374,7 @@ end
end
]]
function table.binary_search(t, target)
--For some reason bit32.bnot doesn't return negative numbers so I'm using ~x = -1 - x instead.
-- For some reason bit32.bnot doesn't return negative numbers so I'm using ~x = -1 - x instead.
local lower = 1
local upper = #t
@@ -390,7 +399,7 @@ function table.binary_search(t, target)
end
-- add table-related functions that exist in base factorio/util to the 'table' table
require 'util'
require "util"
--- Similar to serpent.block, returns a string with a pretty representation of a table.
-- Notice: This method is not appropriate for saving/restoring tables. It is meant to be used by the programmer mainly while debugging a program.
@@ -400,7 +409,7 @@ require 'util'
-- process is a function which allow altering the passed object before transforming it into a string.
-- A typical way to use it would be to remove certain values so that they don't appear at all.
-- return <string> the prettied table
table.inspect = require 'modules.exp_util.include.inspect' --- @dep modules.exp_util.includes.inspect
table.inspect = require "modules.exp_util.include.inspect" --- @dep modules.exp_util.includes.inspect
--- Takes a table and returns the number of entries in the table. (Slower than #table, faster than iterating via pairs)
table.size = table_size