1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
if not modules then modules = { } end modules ['sort-ini'] = {
version = 1.001,
comment = "companion to sort-ini.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
-- todo:
--
-- out of range
-- uppercase
-- texutil compatible
-- always expand to utf
local utf = unicode.utf8
local gsub, rep, sort, concat = string.gsub, string.rep, table.sort, table.concat
local utfcharacters, utfvalues, strcharacters = string.utfcharacters, string.utfvalues, string.characters
local trace_sorters = false -- true
sorters = { }
sorters.comparers = { }
sorters.splitters = { }
sorters.entries = { }
sorters.mappings = { }
sorters.replacements = { }
sorters.language = 'en'
local mappings = sorters.mappings
local entries = sorters.entries
function sorters.comparers.basic(sort_a,sort_b)
-- sm assignment is slow, will become sorters.initialize
local sm = mappings[sorters.language or sorters.defaultlanguage] or mappings.en
if #sort_a > #sort_b then
if #sort_b == 0 then
return 1
else
for i=1,#sort_b do
local ai, bi = sort_a[i], sort_b[i]
local am, bm = sm[ai], sm[bi]
if am and bm then
if am > bm then
return 1
elseif am < bm then
return -1
end
else
if ai > bi then
return 1
elseif ai < bi then
return -1
end
end
end
return 1
end
elseif #sort_a < #sort_b then
if #sort_a == 0 then
return -1
else
for i=1,#sort_a do
local ai, bi = sort_a[i], sort_b[i]
local am, bm = sm[ai], sm[bi]
if am and bm then
if am > bm then
return 1
elseif am < bm then
return -1
end
else
if ai > bi then
return 1
elseif ai < bi then
return -1
end
end
end
return -1
end
elseif #sort_a == 0 then
return 0
else
for i=1,#sort_a do
local ai, bi = sort_a[i], sort_b[i]
local am, bm = sm[ai], sm[bi]
if am and bm then
if am > bm then
return 1
elseif am < bm then
return -1
end
else
if ai > bi then
return 1
elseif ai < bi then
return -1
end
end
end
return 0
end
end
local function padd(s) return rep(" ",10-#s) .. s end -- or format with padd
function sorters.strip(str) -- todo: only letters and such utf.gsub("([^%w%d])","")
if str then
str = gsub(str,"\\%S*","")
str = gsub(str,"[%s%[%](){}%$\"\']*","")
str = gsub(str,"(%d+)",padd) -- sort numbers properly
return str
else
return ""
end
end
function sorters.firstofsplit(split)
-- numbers are left padded by spaces
local se = entries[sorters.language or sorters.defaultlanguage] or entries.en-- slow, will become sorters.initialize
local vs = split[1]
local entry = (vs and vs[1]) or ""
return entry, (se and se[entry]) or "\000"
end
sorters.defaultlanguage = 'en'
-- beware, numbers get spaces in front
function sorters.splitters.utf(str) -- brrr, todo: language
local r = sorters.replacements[sorters.language] or sorters.replacements[sorters.defaultlanguage] or { }
-- local m = mappings [sorters.language] or mappings [sorters.defaultlanguage] or { }
local u = characters.uncompose
local b = utf.byte
local t = { }
for _,v in next, r do
str = gsub(str,v[1],v[2])
end
for c in utfcharacters(str) do -- maybe an lpeg
if #c == 1 then
t[#t+1] = c
else
for cc in strcharacters(c) do
t[#t+1] = cc
end
end
end
return t
end
function sorters.sort(entries,cmp)
if trace_sorters then
sort(entries, function(a,b)
local r = cmp(a,b)
local as, bs = a.split, b.split
if as and bs then
logs.report("sorter","%s %s %s",
concat(as[1]), (not r and "?") or (r<0 and "<") or (r>0 and ">") or "=", concat(bs[1]))
end
return r == -1
end)
else
sort(entries, function(a,b)
return cmp(a,b) == -1
end)
end
end
|