Skip to content

Commit 0534d40

Browse files
committed
Added pairsSorted(), sortNatural() and compareNatural().
1 parent f0a3c08 commit 0534d40

File tree

1 file changed

+70
-8
lines changed

1 file changed

+70
-8
lines changed

preprocess.lua

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
- escapePattern
1919
- getFileContents, fileExists
2020
- pack
21+
- pairsSorted
2122
- printf
2223
- run
24+
- sortNatural, compareNatural
2325
- tokenize, newToken, concatTokens, removeUselessTokens, eachToken, isToken, getNextUsefulToken
2426
- toLua, serialize, evaluate
2527
Only during processing:
@@ -237,8 +239,10 @@ local isToken, isTokenAndNotNil
237239
local loadLuaString, loadLuaFile
238240
local outputLineNumber, maybeOutputLineNumber
239241
local pack, unpack
242+
local pairsSorted
240243
local printf, printTokens, printError, printfError, printErrorTraceback
241244
local serialize, toLua, evaluate
245+
local sortNatural, compareNatural
242246
local tableInsert, tableRemove, tableInsertFormat
243247
local utf8GetCharLength, utf8GetCodepointAndLength
244248

@@ -282,7 +286,7 @@ function printError(s)
282286
io.stderr:write(s, "\n")
283287
end
284288
function printfError(s, ...)
285-
printError(s:format(...))
289+
printError(F(s, ...))
286290
end
287291

288292
-- printErrorTraceback( message [, level=1 ] )
@@ -335,9 +339,9 @@ end
335339
-- errorf( [ level=1, ] string, ... )
336340
function errorf(sOrLevel, ...)
337341
if type(sOrLevel) == "number" then
338-
error(string.format(...), (sOrLevel == 0 and 0 or 1+sOrLevel))
342+
error(F(...), (sOrLevel == 0 and 0 or 1+sOrLevel))
339343
else
340-
error(sOrLevel:format(...), 2)
344+
error(F(sOrLevel, ...), 2)
341345
end
342346
end
343347

@@ -351,7 +355,7 @@ end
351355

352356
-- errorOnLine( path, lineNumber, agent=nil, s, ... )
353357
function errorOnLine(path, ln, agent, s, ...)
354-
s = s:format(...)
358+
s = F(s, ...)
355359
if agent then
356360
errorfLine("%s:%d: [%s] %s", path, ln, agent, s)
357361
else
@@ -376,7 +380,7 @@ do
376380
end
377381

378382
function errorInFile(contents, path, pos, agent, s, ...)
379-
s = s:format(...)
383+
s = F(s, ...)
380384

381385
pos = math.min(math.max(pos, 1), #contents+1)
382386
local ln = getLineNumber(contents, pos)
@@ -1291,7 +1295,7 @@ tableInsert = table.insert
12911295
tableRemove = table.remove
12921296

12931297
function tableInsertFormat(t, s, ...)
1294-
tableInsert(t, s:format(...))
1298+
tableInsert(t, F(s, ...))
12951299
end
12961300

12971301

@@ -1346,6 +1350,46 @@ end
13461350

13471351

13481352

1353+
-- for k, v in pairsSorted( table ) do
1354+
function pairsSorted(t)
1355+
local keys = {}
1356+
for k in pairs(t) do
1357+
tableInsert(keys, k)
1358+
end
1359+
sortNatural(keys)
1360+
1361+
local i = 0
1362+
1363+
return function()
1364+
i = i+1
1365+
local k = keys[i]
1366+
if k ~= nil then return k, t[k] end
1367+
end
1368+
end
1369+
1370+
1371+
1372+
-- sortNatural( array )
1373+
-- aIsLessThanB = compareNatural( a, b )
1374+
do
1375+
local function pad(numStr)
1376+
return F("%03d%s", #numStr, numStr)
1377+
end
1378+
function compareNatural(a, b)
1379+
if type(a) == "number" and type(b) == "number" then
1380+
return a < b
1381+
else
1382+
return (tostring(a):gsub("%d+", pad) < tostring(b):gsub("%d+", pad))
1383+
end
1384+
end
1385+
1386+
function sortNatural(t, k)
1387+
table.sort(t, compareNatural)
1388+
end
1389+
end
1390+
1391+
1392+
13491393
--==============================================================
13501394
--= Preprocessor Functions =====================================
13511395
--==============================================================
@@ -1410,16 +1454,34 @@ metaFuncs.isToken = isToken
14101454
metaFuncs.copyTable = copyTable
14111455

14121456
-- unpack()
1413-
-- value1, ... = unpack( table [, fromIndex=1, toIndex=#table ] )
1457+
-- value1, ... = unpack( array [, fromIndex=1, toIndex=#array ] )
14141458
-- Is _G.unpack() in Lua 5.1 and alias for table.unpack() in Lua 5.2+.
14151459
metaFuncs.unpack = unpack
14161460

14171461
-- pack()
14181462
-- values = pack( value1, ... )
1419-
-- Put values in a new table. values.n is the amount of values (which can be zero)
1463+
-- Put values in a new array. values.n is the amount of values (which can be zero)
14201464
-- including nil values. Alias for table.pack() in Lua 5.2+.
14211465
metaFuncs.pack = pack
14221466

1467+
-- pairsSorted()
1468+
-- for key, value in pairsSorted( table ) do
1469+
-- Same as pairs() but the keys are sorted (ascending).
1470+
metaFuncs.pairsSorted = pairsSorted
1471+
1472+
-- sortNatural()
1473+
-- sortNatural( array )
1474+
-- Sort an array using compareNatural().
1475+
metaFuncs.sortNatural = sortNatural
1476+
1477+
-- compareNatural()
1478+
-- aIsLessThanB = compareNatural( a, b )
1479+
-- Compare two strings. Numbers in the strings are compared as numbers (as opposed to as strings).
1480+
-- Examples:
1481+
-- print( "foo9" < "foo10" ) -- false
1482+
-- print(compareNatural("foo9", "foo10")) -- true
1483+
metaFuncs.compareNatural = compareNatural
1484+
14231485
-- run()
14241486
-- returnValue1, ... = run( path [, arg1, ... ] )
14251487
-- Execute a Lua file. Similar to dofile().

0 commit comments

Comments
 (0)