|
18 | 18 | - escapePattern |
19 | 19 | - getFileContents, fileExists |
20 | 20 | - pack |
| 21 | + - pairsSorted |
21 | 22 | - printf |
22 | 23 | - run |
| 24 | + - sortNatural, compareNatural |
23 | 25 | - tokenize, newToken, concatTokens, removeUselessTokens, eachToken, isToken, getNextUsefulToken |
24 | 26 | - toLua, serialize, evaluate |
25 | 27 | Only during processing: |
@@ -237,8 +239,10 @@ local isToken, isTokenAndNotNil |
237 | 239 | local loadLuaString, loadLuaFile |
238 | 240 | local outputLineNumber, maybeOutputLineNumber |
239 | 241 | local pack, unpack |
| 242 | +local pairsSorted |
240 | 243 | local printf, printTokens, printError, printfError, printErrorTraceback |
241 | 244 | local serialize, toLua, evaluate |
| 245 | +local sortNatural, compareNatural |
242 | 246 | local tableInsert, tableRemove, tableInsertFormat |
243 | 247 | local utf8GetCharLength, utf8GetCodepointAndLength |
244 | 248 |
|
@@ -282,7 +286,7 @@ function printError(s) |
282 | 286 | io.stderr:write(s, "\n") |
283 | 287 | end |
284 | 288 | function printfError(s, ...) |
285 | | - printError(s:format(...)) |
| 289 | + printError(F(s, ...)) |
286 | 290 | end |
287 | 291 |
|
288 | 292 | -- printErrorTraceback( message [, level=1 ] ) |
|
335 | 339 | -- errorf( [ level=1, ] string, ... ) |
336 | 340 | function errorf(sOrLevel, ...) |
337 | 341 | 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)) |
339 | 343 | else |
340 | | - error(sOrLevel:format(...), 2) |
| 344 | + error(F(sOrLevel, ...), 2) |
341 | 345 | end |
342 | 346 | end |
343 | 347 |
|
|
351 | 355 |
|
352 | 356 | -- errorOnLine( path, lineNumber, agent=nil, s, ... ) |
353 | 357 | function errorOnLine(path, ln, agent, s, ...) |
354 | | - s = s:format(...) |
| 358 | + s = F(s, ...) |
355 | 359 | if agent then |
356 | 360 | errorfLine("%s:%d: [%s] %s", path, ln, agent, s) |
357 | 361 | else |
|
376 | 380 | end |
377 | 381 |
|
378 | 382 | function errorInFile(contents, path, pos, agent, s, ...) |
379 | | - s = s:format(...) |
| 383 | + s = F(s, ...) |
380 | 384 |
|
381 | 385 | pos = math.min(math.max(pos, 1), #contents+1) |
382 | 386 | local ln = getLineNumber(contents, pos) |
@@ -1291,7 +1295,7 @@ tableInsert = table.insert |
1291 | 1295 | tableRemove = table.remove |
1292 | 1296 |
|
1293 | 1297 | function tableInsertFormat(t, s, ...) |
1294 | | - tableInsert(t, s:format(...)) |
| 1298 | + tableInsert(t, F(s, ...)) |
1295 | 1299 | end |
1296 | 1300 |
|
1297 | 1301 |
|
@@ -1346,6 +1350,46 @@ end |
1346 | 1350 |
|
1347 | 1351 |
|
1348 | 1352 |
|
| 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 | + |
1349 | 1393 | --============================================================== |
1350 | 1394 | --= Preprocessor Functions ===================================== |
1351 | 1395 | --============================================================== |
@@ -1410,16 +1454,34 @@ metaFuncs.isToken = isToken |
1410 | 1454 | metaFuncs.copyTable = copyTable |
1411 | 1455 |
|
1412 | 1456 | -- unpack() |
1413 | | --- value1, ... = unpack( table [, fromIndex=1, toIndex=#table ] ) |
| 1457 | +-- value1, ... = unpack( array [, fromIndex=1, toIndex=#array ] ) |
1414 | 1458 | -- Is _G.unpack() in Lua 5.1 and alias for table.unpack() in Lua 5.2+. |
1415 | 1459 | metaFuncs.unpack = unpack |
1416 | 1460 |
|
1417 | 1461 | -- pack() |
1418 | 1462 | -- 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) |
1420 | 1464 | -- including nil values. Alias for table.pack() in Lua 5.2+. |
1421 | 1465 | metaFuncs.pack = pack |
1422 | 1466 |
|
| 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 | + |
1423 | 1485 | -- run() |
1424 | 1486 | -- returnValue1, ... = run( path [, arg1, ... ] ) |
1425 | 1487 | -- Execute a Lua file. Similar to dofile(). |
|
0 commit comments