From 6bd797c75e305dcdf41ee6ad4bf80a5e567eebd8 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Sun, 25 Jun 2017 13:16:34 +0200 Subject: [PATCH] Improved sorting. --- README.md | 15 +++++++++------ pretty.lua | 7 +++++-- test/test_pretty.lua | 32 ++++++++++++++------------------ test/test_sorting.lua | 6 ++++++ 4 files changed, 34 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 88099f1..1a0bdb2 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,9 @@ I'm looking into implementing following features: non-breaking strings. Attempt to break near whitespace. - Attempt to fit output within a predefined width limit. Default to 80. - Find a better name than `pretty`. -- Add options for colored output, and allow custom formatting. +- Add option for colored output. Primarily syntax highlighting, but also + [BlueJ-style](www.bluej.org/about.html) scope highlighting, with some faint + background colors. - Look more into `string.dump` in the core library. - Better support upvalues for in functions. Complete support is impossible without traversing the original code or inspecting the intermediate @@ -98,11 +100,12 @@ I'm looking into implementing following features: ## Other pretty printers -`pretty` is a large and slow library, and is not designed for serialization -purposes, nor is `pretty` concerned with offering the same level of -customization as other libraries do. +`pretty` is large, slow, and requires the debug library to work. It's not +designed for serialization purposes, nor is it concerned with offering the same +level of customization as other libraries do. -Luckily Lua has a large library of pretty printers and serialization libraries: +If you want a sleek, fast, customizable or embeddable library, there are other +options. Lua has a large library of pretty printers and serialization libraries: - [inspect.lua](github.com/kikito/inspect.lua): One of the classic debugging pretty printers. @@ -114,4 +117,4 @@ Luckily Lua has a large library of pretty printers and serialization libraries: - [binser](github.com/bakpakin/binser): Library for special purpose serialization. -Others can be found at [the lua-users wiki](lua-users.org/wiki/TableSerialization). +Find others at [the lua-users wiki](lua-users.org/wiki/TableSerialization). diff --git a/pretty.lua b/pretty.lua index d4a7a61..0761ce8 100644 --- a/pretty.lua +++ b/pretty.lua @@ -82,8 +82,11 @@ local function padnum(d) end local function alphanum_compare_strings (a, b) - return tostring(a):gsub("%.?%d+", padnum)..("%3d"):format(#b) - < tostring(b):gsub("%.?%d+", padnum)..("%3d"):format(#a) + local a_padded = tostring(a):gsub("%.?%d+", padnum)..("\0%3d"):format(#b) + local b_padded = tostring(b):gsub("%.?%d+", padnum)..("\0%3d"):format(#a) + local A_padded, B_padded = a_padded:upper(), b_padded:upper() + if A_padded == B_padded then return a_padded < b_padded end + return A_padded < B_padded end local function compare_key_value_pairs (a, b) diff --git a/test/test_pretty.lua b/test/test_pretty.lua index 4428a20..1bf7235 100644 --- a/test/test_pretty.lua +++ b/test/test_pretty.lua @@ -224,13 +224,11 @@ format_test { expect = '{ a = { 1, 2, 3 }, b = { 4, 5, 6 } }', } -if HAS_UNICODE_IDEN then - format_test { - name = 'Unicode characters can be used as string keys in tables', - input = loadstring 'return { a = 1, ψ = 2 }' (), - expect = '{ a = 1, ψ = 2 }', - } -end +format_test { + name = 'Unicode characters can be used as string keys in tables', + input = { ['a'] = 1, ['ψ'] = 2 }, + expect = '{ a = 1, ψ = 2 }', +} format_test { input = { [100] = 'Hi', [300] = 'Hello' }, @@ -308,17 +306,15 @@ format_test { expect = '{\n { a = \'hello\', b = \'hi\' },\n { a = \'hi\', b = \'hello\' }\n}', } -if HAS_UNICODE_IDEN then - format_test { - name = 'Proper alignment when using unicode characters as keys', - input = loadstring [[return { - djævle = 'dyr?', - europa = 'måne', - øå = 'en å på en ø?', - }]] (), - expect = '{\n djævle = \'dyr?\',\n europa = \'måne\',\n øå = \'en å på en ø?\'\n}', - } -end +format_test { + name = 'Proper alignment when using unicode characters as keys', + input = { + ['djævle'] = 'dyr?', + ['europa'] = 'måne', + ['øå'] = 'en å på en ø?', + }, + expect = '{\n djævle = \'dyr?\',\n europa = \'måne\',\n øå = \'en å på en ø?\'\n}', +} -------------------------------------------------------------------------------- -- Table recursion diff --git a/test/test_sorting.lua b/test/test_sorting.lua index 583fd71..4859733 100644 --- a/test/test_sorting.lua +++ b/test/test_sorting.lua @@ -47,6 +47,12 @@ format_test { expect = '{ b = true, a = {} }', } +format_test { + name = 'Keys should be sorted such that uppercase and lowercase lies near each other', + input = { Dave = 1, dave = 2, mary = 3, Mary = 4, Adam = 5, adam = 6 }, + expect = '{\n Adam = 5,\n adam = 6,\n Dave = 1,\n dave = 2,\n Mary = 4,\n mary = 3\n}', +} + format_test { input = { a = {}, b = true, b1 = false }, expect = '{ b = true, b1 = false, a = {} }',