• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sile-typesetter / sile / 9710003952

28 Jun 2024 08:24AM UTC coverage: 61.779% (-8.6%) from 70.4%
9710003952

push

github

alerque
chore(tooling): Update Git ignore list and sort for easier review

10631 of 17208 relevant lines covered (61.78%)

2071.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.0
/core/utilities/init.lua
1
--- SILE.utilities (aliased as SU)
2
-- @module SU
3
-- @alias utilities
4

5
local bitshim = require("bitshim")
126✔
6
local luautf8 = require("lua-utf8")
126✔
7
local semver = require("semver")
126✔
8

9
local utilities = {}
126✔
10

11
local epsilon = 1E-12
126✔
12

13
--- Generic
14
-- @section generic
15

16
--- Concatenate values from a table using a given separator.
17
-- Differs from `table.concat` in that all values are explicitly cast to strings, allowing debugging of tables that
18
-- include functions, other tables, data types, etc.
19
-- @tparam table array Input.
20
-- @tparam[opt=" "] string separator Separator.
21
function utilities.concat (array, separator)
126✔
22
   return table.concat(utilities.map(tostring, array), separator)
8✔
23
end
24

25
--- Execute a callback function on each value in a table.
26
-- @tparam function func Function to run on each value.
27
-- @tparam table array Input list-like table.
28
function utilities.map (func, array)
126✔
29
   local new_array = {}
28,717✔
30
   local last = #array
28,717✔
31
   for i = 1, last do
93,033✔
32
      new_array[i] = func(array[i])
121,803✔
33
   end
34
   return new_array
28,717✔
35
end
36

37
--- Require that an option table contains a specific value, otherwise raise an error.
38
-- @param options Input table of options.
39
-- @param name Name of the required option.
40
-- @param context User friendly name of the function or calling context.
41
-- @param required_type The name of a data type that the option must sucessfully cast to.
42
function utilities.required (options, name, context, required_type)
126✔
43
   if not options[name] then
5,857✔
44
      utilities.error(context .. " needs a " .. name .. " parameter")
×
45
   end
46
   if required_type then
5,857✔
47
      return utilities.cast(required_type, options[name])
1,084✔
48
   end
49
   return options[name]
4,773✔
50
end
51

52
--- Iterate over key/value pairs in sequence of the sorted keys.
53
-- Table iteration order with `pairs` is non-deterministic. This function returns an iterator that can be used in plais
54
-- of `pairs` that will iterate through the values in the order of their *sorted* keys.
55
-- @tparam table input Input table.
56
-- @usage for val in SU.sortedpairs({ b: "runs second", a: "runs first" ) do print(val) end
57
function utilities.sortedpairs (input)
126✔
58
   local keys = {}
2✔
59
   for k, _ in pairs(input) do
14✔
60
      keys[#keys + 1] = k
12✔
61
   end
62
   table.sort(keys, function (a, b)
4✔
63
      if type(a) == type(b) then
28✔
64
         return a < b
22✔
65
      elseif type(a) == "number" then
6✔
66
         return true
×
67
      else
68
         return false
6✔
69
      end
70
   end)
71
   return coroutine.wrap(function ()
2✔
72
      for i = 1, #keys do
14✔
73
         coroutine.yield(keys[i], input[keys[i]])
12✔
74
      end
75
   end)
76
end
77

78
--- Substitute a range of value(s) in one table with values from another.
79
-- @tparam table array Table to modify.
80
-- @tparam integer start First key to replace.
81
-- @tparam integer stop Last key to replace.
82
-- @tparam table replacement Table from which to pull key/values plairs to inject in array.
83
-- @treturn table array First input array modified with values from replacement.
84
function utilities.splice (array, start, stop, replacement)
126✔
85
   local ptr = start
167✔
86
   local room = stop - start + 1
167✔
87
   local last = replacement and #replacement or 0
167✔
88
   for i = 1, last do
1,375✔
89
      if room > 0 then
1,208✔
90
         room = room - 1
158✔
91
         array[ptr] = replacement[i]
158✔
92
      else
93
         table.insert(array, ptr, replacement[i])
1,050✔
94
      end
95
      ptr = ptr + 1
1,208✔
96
   end
97

98
   for _ = 1, room do
176✔
99
      table.remove(array, ptr)
9✔
100
   end
101
   return array
167✔
102
end
103

104
-- TODO: Unused, now deprecated?
105
function utilities.inherit (orig, spec)
126✔
106
   local new = pl.tablex.deepcopy(orig)
×
107
   if spec then
×
108
      for k, v in pairs(spec) do
×
109
         new[k] = v
×
110
      end
111
   end
112
   if new.init then
×
113
      new:init()
×
114
   end
115
   return new
×
116
end
117

118
--- Type handling
119
-- @section types
120

121
local function preferbool ()
122
   utilities.warn("Please use boolean values or strings such as 'true' and 'false' instead of 'yes' and 'no'.")
×
123
end
124

125
--- Cast user intput into a boolean type.
126
-- User input content such as options typed into documents will return string values such as "true" or "false rather
127
-- than true or false types. This evaluates those strings or other inputs ane returns a consistent boolean type in
128
-- return.
129
-- @tparam nil|bool|string value Input value such as a string to evaluate for thruthyness.
130
-- @tparam[opt=false] boolean default Whether to assume inputs that don't specifically evaluate to something should be true or false.
131
-- @treturn boolean
132
function utilities.boolean (value, default)
126✔
133
   if value == false then
4,975✔
134
      return false
21✔
135
   end
136
   if value == true then
4,954✔
137
      return true
2,041✔
138
   end
139
   if value == "false" then
2,913✔
140
      return false
2✔
141
   end
142
   if value == "true" then
2,911✔
143
      return true
5✔
144
   end
145
   if value == "no" then
2,906✔
146
      preferbool()
×
147
      return false
×
148
   end
149
   if value == "yes" then
2,906✔
150
      preferbool()
×
151
      return true
×
152
   end
153
   if value == nil then
2,906✔
154
      return default == true
2,906✔
155
   end
156
   if value == "" then
×
157
      return default == true
×
158
   end
159
   SU.error("Expecting a boolean value but got '" .. value .. "'")
×
160
   return default == true
×
161
end
162

163
--- Cast user intput to an expected type.
164
-- If possible, converts input from one type to another. Not all types can be cast. For example "four" can't be cast to
165
-- a number, but "4" or 4 can. Likewise "6pt" or 6 can be cast to a SILE.types.measurement, SILE.types.length, or even
166
-- a SILE.types.node.glue, but not a SILE.types.color.
167
-- @tparam string wantedType Expected type.
168
-- @return A value of the type wantedType.
169
function utilities.cast (wantedType, value)
126✔
170
   local actualType = SU.type(value)
276,717✔
171
   wantedType = string.lower(wantedType)
553,434✔
172
   if wantedType:match(actualType) then
276,717✔
173
      return value
94,086✔
174
   elseif actualType == "nil" and wantedType:match("nil") then
182,631✔
175
      return nil
×
176
   elseif wantedType:match("length") then
182,631✔
177
      return SILE.types.length(value)
30,409✔
178
   elseif wantedType:match("measurement") then
152,348✔
179
      return SILE.types.measurement(value)
3,642✔
180
   elseif wantedType:match("vglue") then
148,706✔
181
      return SILE.types.node.vglue(value)
5✔
182
   elseif wantedType:match("glue") then
148,701✔
183
      return SILE.types.node.glue(value)
47✔
184
   elseif wantedType:match("kern") then
148,654✔
185
      return SILE.types.node.kern(value)
×
186
   elseif actualType == "nil" then
148,654✔
187
      SU.error("Cannot cast nil to " .. wantedType)
×
188
   elseif wantedType:match("boolean") then
148,654✔
189
      return SU.boolean(value)
1✔
190
   elseif wantedType:match("string") then
148,653✔
191
      return tostring(value)
×
192
   elseif wantedType:match("number") then
148,653✔
193
      if type(value) == "table" and type(value.tonumber) == "function" then
148,636✔
194
         return value:tonumber()
145,881✔
195
      end
196
      local num = tonumber(value)
2,755✔
197
      if not num then
2,755✔
198
         SU.error("Cannot cast '" .. value .. "'' to " .. wantedType)
×
199
      end
200
      return num
2,755✔
201
   elseif wantedType:match("integer") then
17✔
202
      local num
203
      if type(value) == "table" and type(value.tonumber) == "function" then
17✔
204
         num = value:tonumber()
×
205
      else
206
         num = tonumber(value)
17✔
207
      end
208
      if not num then
17✔
209
         SU.error("Cannot cast '" .. value .. "'' to " .. wantedType)
×
210
      end
211
      if not wantedType:match("number") and num % 1 ~= 0 then
17✔
212
         -- Could be an error but since it wasn't checked before, let's just warn:
213
         -- Some packages might have wrongly typed settings, for instance.
214
         SU.warn("Casting an integer but got a float number " .. num)
×
215
      end
216
      return num
17✔
217
   else
218
      SU.error("Cannot cast to unrecognized type " .. wantedType)
×
219
   end
220
end
221

222
--- Return the type of an object
223
-- Like Lua's `type`, but also handles various SILE user data types.
224
-- @tparam any value Any input value. If a table is one of SILE's classes or types, report on it's internal type.
225
-- Otherwise use the output of `type`.
226
-- @treturn string
227
function utilities.type (value)
126✔
228
   if type(value) == "number" then
1,407,767✔
229
      return math.floor(value) == value and "integer" or "number"
342,572✔
230
   elseif type(value) == "table" and value.prototype then
1,065,195✔
231
      return value:prototype()
×
232
   elseif type(value) == "table" and value.is_a then
1,065,195✔
233
      return value.type
503,840✔
234
   else
235
      return type(value)
561,355✔
236
   end
237
end
238

239
--- Errors and debugging
240
-- @section errors
241

242
--- Output a debug message only if debugging for a specific category is enabled.
243
-- Importantly passing siries of strings, functions, or tables is more effecient than trying to formulate a full message
244
-- using concatentation and tostring() methods in the original code because it doesn't have to even run if the relevant
245
-- debug flag is not enabled.
246
-- @tparam text category Category flag for which this message should be output.
247
-- @tparam string|function|table ... Each argument will be returned separated by spaces, strings directly, functions by
248
-- evaluating them and assuming the return value is a string, and tables by using their internal :__tostring() methods.
249
-- @usage
250
--    > glue = SILE.types.node.glue("6em")
251
--    > SU.debug("foo", "A glue node", glue)
252
--    [foo] A glue node G<6em>
253
function utilities.debug (category, ...)
126✔
254
   if SILE.quiet then
83,397✔
255
      return
×
256
   end
257
   if utilities.debugging(category) then
166,794✔
258
      local inputs = pl.utils.pack(...)
×
259
      for i, input in ipairs(inputs) do
×
260
         if type(input) == "function" then
×
261
            local status, output = pcall(input)
×
262
            inputs[i] = status and output
×
263
               or SU.warn(("Output of %s debug function was an error: %s"):format(category, output))
×
264
         elseif type(input) ~= "string" then
×
265
            inputs[i] = tostring(input)
×
266
         end
267
      end
268
      local message = utilities.concat(inputs, " ")
×
269
      if message then
×
270
         io.stderr:write(("\n[%s] %s"):format(category, message))
×
271
      end
272
   end
273
end
274

275
--- Determine if a specific debug flag is set.
276
-- @tparam string category Name of the flag status to check, e.g. "frames".
277
-- @treturn boolean
278
function utilities.debugging (category)
126✔
279
   return SILE.debugFlags.all and category ~= "profile" or SILE.debugFlags[category]
86,555✔
280
end
281

282
--- Warn about use of a deprecated feature.
283
-- Checks the current version and decides whether to warn or error, then oatputs a message with as much useful
284
-- information as possible to make it easy for end users to update their usage.
285
-- @tparam string old The name of the deprecated interface.
286
-- @tparam string new A name of a suggested replacement interface.
287
-- @tparam string warnat The first release where the interface is considered deprecated, at which point their might be
288
-- a shim.
289
-- @tparam string errorat The first release where the interface is no longer functional even with a shim.
290
-- @tparam string extra Longer-form help to include in output separate from the expected one-liner of warning messages.
291
function utilities.deprecated (old, new, warnat, errorat, extra)
126✔
292
   warnat, errorat = semver(warnat or 0), semver(errorat or 0)
15✔
293
   local current = SILE.version and semver(SILE.version:match("v([0-9]*.[0-9]*.[0-9]*)")) or warnat
10✔
294
   -- SILE.version is defined *after* most of SILE loads. It’s available at
295
   -- runtime but not useful if we encounter deprecated code in core code. Users
296
   -- will never encounter this failure, but as a developer it’s hard to test a
297
   -- deprecation when core code refactoring is an all-or-nothing proposition.
298
   -- Hence we fake it ‘till we make it, all deprecations internally are warnings.
299
   local brackets = old:sub(1, 1) == "\\" and "" or "()"
10✔
300
   local _new = new and "Please use " .. (new .. brackets) .. " instead." or "Plase don't use it."
5✔
301
   local msg = (old .. brackets)
5✔
302
      .. " was deprecated in SILE v"
×
303
      .. tostring(warnat)
5✔
304
      .. ". "
×
305
      .. _new
5✔
306
      .. (extra and ("\n\n" .. extra .. "\n") or "")
5✔
307
   if errorat and current >= errorat then
5✔
308
      SU.error(msg)
×
309
   elseif warnat and current >= warnat then
5✔
310
      SU.warn(msg)
5✔
311
   end
312
end
313

314
--- Dump the contents of a any Lua type.
315
-- For quick debugging, can be used on any number of any type of Lua value. Pretty-prints tables.
316
-- @tparam any ... Any number of values
317
function utilities.dump (...)
126✔
318
   local arg = { ... } -- Avoid things that Lua stuffs in arg like args to self()
1✔
319
   pl.pretty.dump(#arg == 1 and arg[1] or arg, "/dev/stderr")
2✔
320
end
321

322
local _skip_traceback_levels = 2
126✔
323

324
--- Raise an error and exit.
325
-- Outputs a warning message via `warn`, then finishes up anything it can without processing more content, then exits.
326
-- @tparam string message The error message to give.
327
-- @tparam boolean isbug Whether or not hitting this error is expected to be a code bug (as opposed to misakes in user input).
328
function utilities.error (message, isbug)
126✔
329
   _skip_traceback_levels = 3
×
330
   utilities.warn(message, isbug)
×
331
   _skip_traceback_levels = 2
×
332
   io.stderr:flush()
×
333
   SILE.outputter:finish() -- Only really useful from the REPL but no harm in trying
×
334
   SILE.scratch.caughterror = true
×
335
   error("", 2)
×
336
end
337

338
--- Output an information message.
339
-- Basically like `warn`, except to source tracing information is added.
340
-- @tparam string message
341
function utilities.msg (message)
126✔
342
   if SILE.quiet then
33✔
343
      return
×
344
   end
345
   io.stderr:write("\n! " .. message .. "\n")
33✔
346
end
347

348
--- Output a warning.
349
-- Outputs a warning message including identifying where in the processing SILE is at when the warning is given.
350
-- @tparam string message The error message to give.
351
-- @tparam boolean isbug Whether or not hitting this warning is expected to be a code bug (as opposed to misakes in user input).
352
function utilities.warn (message, isbug)
126✔
353
   utilities.msg(message)
33✔
354
   if SILE.traceback or isbug then
33✔
355
      io.stderr:write(" at:\n" .. SILE.traceStack:locationTrace())
2✔
356
      if _skip_traceback_levels == 2 then
1✔
357
         io.stderr:write(
2✔
358
            debug.traceback("", _skip_traceback_levels) or "\t! debug.traceback() did not identify code location"
1✔
359
         )
360
      end
361
   else
362
      io.stderr:write(" at " .. SILE.traceStack:locationHead())
64✔
363
   end
364
   io.stderr:write("\n")
33✔
365
end
366

367
--- Math
368
-- @section math
369

370
--- Check equality of floating point values.
371
-- Comparing floating point numbers using math functions in Lua may give different and unexpected answers depending on
372
-- the Lua VM and other environmental factors. This normalizes them using our standard internal epsilon value and
373
-- compares the absolute intereger value to avoid floating point number wierdness.
374
-- @tparam float lhs
375
-- @tparam float rhs
376
-- @treturn boolean
377
function utilities.feq (lhs, rhs)
126✔
378
   lhs = SU.cast("number", lhs)
82✔
379
   rhs = SU.cast("number", rhs)
82✔
380
   local abs = math.abs
41✔
381
   return abs(lhs - rhs) <= epsilon * (abs(lhs) + abs(rhs))
41✔
382
end
383

384
--- Add up all the values in a table.
385
-- @tparam table array Input list-like table.
386
-- @treturn number Sum of all values.
387
function utilities.sum (array)
126✔
388
   local total = 0
8,399✔
389
   local last = #array
8,399✔
390
   for i = 1, last do
16,308✔
391
      total = total + array[i]
7,909✔
392
   end
393
   return total
8,399✔
394
end
395

396
--- Return maximum value of inputs.
397
-- `math.max`, but works on SILE types such as SILE.types.measurement.
398
-- Lua <= 5.2 can't handle math operators on objects.
399
function utilities.max (...)
126✔
400
   local input = pl.utils.pack(...)
18,294✔
401
   local max = table.remove(input, 1)
18,294✔
402
   for _, val in ipairs(input) do
61,059✔
403
      if val > max then
42,765✔
404
         max = val
15,354✔
405
      end
406
   end
407
   return max
18,294✔
408
end
409

410
--- Return minimum value of inputs.
411
-- `math.min`, but works on SILE types such as SILE.types.measurement.
412
-- Lua <= 5.2 can't handle math operators on objects.
413
function utilities.min (...)
126✔
414
   local input = pl.utils.pack(...)
7✔
415
   local min = input[1]
7✔
416
   for _, val in ipairs(input) do
21✔
417
      if val < min then
14✔
418
         min = val
×
419
      end
420
   end
421
   return min
7✔
422
end
423

424
--- Round and normalize a number for debugging.
425
-- LuaJIT 2.1 betas (and inheritors such as OpenResty and Moonjit) are biased
426
-- towards rounding 0.5 up to 1, all other Lua interpreters are biased
427
-- towards rounding such floating point numbers down.  This hack shaves off
428
-- just enough to fix the bias so our test suite works across interpreters.
429
-- Note that even a true rounding function here will fail because the bias is
430
-- inherent to the floating point type. Also note we are erroring in favor of
431
-- the *less* common option because the LuaJIT VMS are hopelessly broken
432
-- whereas normal LUA VMs can be cooerced.
433
-- @tparam number input Input value.
434
-- @treturn string Four-digit precision foating point.
435
function utilities.debug_round (input)
126✔
436
   if input > 0 then
×
437
      input = input + 0.00000000000001
×
438
   end
439
   if input < 0 then
×
440
      input = input - 0.00000000000001
×
441
   end
442
   return string.format("%.4f", input)
×
443
end
444

445
--- Remove empty spaces from list-like tables
446
-- Iterating list-like tables is hard if some values have been removed. This converts { 1 = "a", 3 = "b" } into
447
-- { 1 = "a", 2 = "b" } which can be iterated using `ipairs` without stopping after 1.
448
-- @tparam table items List-like table potentially with holes.
449
-- @treturn table List like table without holes.
450
function utilities.compress (items)
126✔
451
   local rv = {}
594✔
452
   local max = math.max(pl.utils.unpack(pl.tablex.keys(items)))
1,782✔
453
   for i = 1, max do
13,855✔
454
      if items[i] then
13,261✔
455
         rv[#rv + 1] = items[i]
13,261✔
456
      end
457
   end
458
   return rv
594✔
459
end
460

461
--- Reverse the order of a list-like table.
462
-- @tparam table tbl Input list-like table.
463
function utilities.flip_in_place (tbl)
126✔
464
   local tmp, j
465
   for i = 1, math.floor(#tbl / 2) do
808✔
466
      tmp = tbl[i]
520✔
467
      j = #tbl - i + 1
520✔
468
      tbl[i] = tbl[j]
520✔
469
      tbl[j] = tmp
520✔
470
   end
471
end
472

473
-- TODO: Before documenting, consider whether this should be private to the one existing usage.
474
function utilities.allCombinations (options)
126✔
475
   local count = 1
×
476
   for i = 1, #options do
×
477
      count = count * options[i]
×
478
   end
479
   return coroutine.wrap(function ()
×
480
      for i = 0, count - 1 do
×
481
         local this = i
×
482
         local rv = {}
×
483
         for j = 1, #options do
×
484
            local base = options[j]
×
485
            rv[#rv + 1] = this % base + 1
×
486
            this = (this - this % base) / base
×
487
         end
488
         coroutine.yield(rv)
×
489
      end
490
   end)
491
end
492

493
function utilities.rateBadness (inf_bad, shortfall, spring)
126✔
494
   if spring == 0 then
13,198✔
495
      return inf_bad
316✔
496
   end
497
   local bad = math.floor(100 * math.abs(shortfall / spring) ^ 3)
12,882✔
498
   return math.min(inf_bad, bad)
12,882✔
499
end
500

501
function utilities.rationWidth (target, width, ratio)
126✔
502
   if ratio < 0 and width.shrink:tonumber() > 0 then
12,774✔
503
      target:___add(width.shrink:tonumber() * ratio)
2,931✔
504
   elseif ratio > 0 and width.stretch:tonumber() > 0 then
16,699✔
505
      target:___add(width.stretch:tonumber() * ratio)
4,576✔
506
   end
507
   return target
10,150✔
508
end
509

510
--- Text handling
511
-- @section text
512

513
--- Iterate over a string split into tokens via a pattern.
514
-- @tparam string string Input string.
515
-- @tparam string pattern Pattern on which to split the input.
516
-- @treturn function An iterator function
517
-- @usage for str in SU.gtoke("foo-bar-baz", "-") do print(str) end
518
function utilities.gtoke (string, pattern)
126✔
519
   string = string and tostring(string) or ""
1,046✔
520
   pattern = pattern and tostring(pattern) or "%s+"
1,046✔
521
   local length = #string
1,046✔
522
   return coroutine.wrap(function ()
1,046✔
523
      local index = 1
1,046✔
524
      repeat
525
         local first, last = string:find(pattern, index)
1,331✔
526
         if last then
1,331✔
527
            if index < first then
438✔
528
               coroutine.yield({ string = string:sub(index, first - 1) })
570✔
529
            end
530
            coroutine.yield({ separator = string:sub(first, last) })
876✔
531
            index = last + 1
438✔
532
         else
533
            if index <= length then
893✔
534
               coroutine.yield({ string = string:sub(index) })
1,786✔
535
            end
536
            break
893✔
537
         end
538
      until index > length
438✔
539
   end)
540
end
541

542
--- Convert a Unicode character to its corresponding codepoint.
543
-- @tparam string uchar A single inicode character.
544
-- @return number The Unicode code point where uchar is encoded.
545
function utilities.codepoint (uchar)
126✔
546
   local seq = 0
123,430✔
547
   local val = -1
123,430✔
548
   for i = 1, #uchar do
248,871✔
549
      local c = string.byte(uchar, i)
126,803✔
550
      if seq == 0 then
126,803✔
551
         if val > -1 then
123,702✔
552
            return val
1,362✔
553
         end
554
         seq = c < 0x80 and 1
122,340✔
555
            or c < 0xE0 and 2
122,340✔
556
            or c < 0xF0 and 3
2,889✔
557
            or c < 0xF8 and 4 --c < 0xFC and 5 or c < 0xFE and 6 or
212✔
558
            or error("invalid UTF-8 character sequence")
×
559
         val = bitshim.band(c, 2 ^ (8 - seq) - 1)
122,340✔
560
      else
561
         val = bitshim.bor(bitshim.lshift(val, 6), bitshim.band(c, 0x3F))
3,101✔
562
      end
563
      seq = seq - 1
125,441✔
564
   end
565
   return val
122,068✔
566
end
567

568
--- Covert a code point to a Unicode character.
569
-- @tparam number|string codepoint Input code point value, either as a number or a string representing the decimal value "U+NNNN" or hex value "0xFFFF".
570
-- @treturn string The character replestened by a codepoint descriptions.
571
function utilities.utf8charfromcodepoint (codepoint)
126✔
572
   local val = codepoint
355✔
573
   local cp = val
355✔
574
   local hex = (cp:match("[Uu]%+(%x+)") or cp:match("0[xX](%x+)"))
355✔
575
   if hex then
355✔
576
      cp = tonumber("0x" .. hex)
1✔
577
   elseif tonumber(cp) then
354✔
578
      cp = tonumber(cp)
×
579
   end
580

581
   if type(cp) == "number" then
355✔
582
      val = luautf8.char(cp)
1✔
583
   end
584
   return val
355✔
585
end
586

587
--- Convert a UTF-16 encoded string to a series of code points.
588
-- Like `luautf8.codes`, but for UTF-16 strings.
589
-- @tparam string ustr Input string.
590
-- @tparam string endian Either "le" or "be" depending on the enconding endedness.
591
-- @treturn string Serious of hex encoded code points.
592
function utilities.utf16codes (ustr, endian)
126✔
593
   local pos = 1
25,013✔
594
   return function ()
595
      if pos > #ustr then
758,984✔
596
         return nil
25,013✔
597
      else
598
         local c1, c2, c3, c4, wchar, lowchar
599
         c1 = string.byte(ustr, pos, pos + 1)
733,971✔
600
         pos = pos + 1
733,971✔
601
         c2 = string.byte(ustr, pos, pos + 1)
733,971✔
602
         pos = pos + 1
733,971✔
603
         if endian == "be" then
733,971✔
604
            wchar = c1 * 256 + c2
733,971✔
605
         else
606
            wchar = c2 * 256 + c1
×
607
         end
608
         if not (wchar >= 0xD800 and wchar <= 0xDBFF) then
733,971✔
609
            return wchar
733,971✔
610
         end
611
         c3 = string.byte(ustr, pos, pos + 1)
×
612
         pos = pos + 1
×
613
         c4 = string.byte(ustr, pos, pos + 1)
×
614
         pos = pos + 1
×
615
         if endian == "be" then
×
616
            lowchar = c3 * 256 + c4
×
617
         else
618
            lowchar = c4 * 256 + c3
×
619
         end
620
         return 0x10000 + bitshim.lshift(bitshim.band(wchar, 0x03FF), 10) + bitshim.band(lowchar, 0x03FF)
×
621
      end
622
   end
623
end
624

625
--- Split a UTF-8 string into characters.
626
-- Lua's `string.split` will only explode a string by bytes. For text processing purposes it is usually more desirable
627
-- to split it into 1, 2, 3, or 4 byte grups matching the UTF-8 encoding.
628
-- @tparam string str Input UTF-8 encoded string.
629
-- @treturn table A list-like table of UTF8 strings each representing a Unicode char from the input string.
630
function utilities.splitUtf8 (str)
126✔
631
   local rv = {}
70,564✔
632
   for _, cp in luautf8.next, str do
468,163✔
633
      table.insert(rv, luautf8.char(cp))
397,599✔
634
   end
635
   return rv
70,564✔
636
end
637

638
--- The last Unicode character in a UTF-8 encoded string.
639
-- Uses `SU.splitUtf8` to break an string into segments represtenting encoded characters, returns the last one. May be
640
-- more than one byte.
641
-- @tparam string str Input string.
642
-- @treturn string A single Unicode character.
643
function utilities.lastChar (str)
126✔
644
   local chars = utilities.splitUtf8(str)
×
645
   return chars[#chars]
×
646
end
647

648
--- The first Unicode character in a UTF-8 encoded string.
649
-- Uses `SU.splitUtf8` to break an string into segments represtenting encoded characters, returns the first one. May be
650
-- more than one byte.
651
-- @tparam string str Input string.
652
-- @treturn string A single Unicode character.
653
function utilities.firstChar (str)
126✔
654
   local chars = utilities.splitUtf8(str)
×
655
   return chars[1]
×
656
end
657

658
local byte, floor, reverse = string.byte, math.floor, string.reverse
126✔
659

660
--- The Unicode character in a UTF-8 encoded string at a specifi position
661
-- Uses `SU.splitUtf8` to break an string into segments represtenting encoded characters, returns the Nth one. May be
662
-- more than one byte.
663
-- @tparam string str Input string.
664
-- @tparam number index Index of character to return.
665
-- @treturn string A single Unicode character.
666
function utilities.utf8charat (str, index)
126✔
667
   return str:sub(index):match("([%z\1-\127\194-\244][\128-\191]*)")
×
668
end
669

670
local utf16bom = function (endianness)
671
   return endianness == "be" and "\254\255" or endianness == "le" and "\255\254" or SU.error("Unrecognized endianness")
25,016✔
672
end
673

674
--- Encode a string to a hexidecimal replesentation.
675
-- @tparam string str Input UTF-8 string
676
-- @treturn string Hexidecimal replesentation of str.
677
function utilities.hexencoded (str)
126✔
678
   local ustr = ""
3✔
679
   for i = 1, #str do
71✔
680
      ustr = ustr .. string.format("%02x", byte(str, i, i + 1))
68✔
681
   end
682
   return ustr
3✔
683
end
684

685
--- Decode a hexidecimal replesentation into a string.
686
-- @tparam string str Input hexidecimal encoded string.
687
-- @treturn string UTF-8 string.
688
function utilities.hexdecoded (str)
126✔
689
   if #str % 2 == 1 then
×
690
      SU.error("Cannot decode hex string with odd len")
×
691
   end
692
   local ustr = ""
×
693
   for i = 1, #str, 2 do
×
694
      ustr = ustr .. string.char(tonumber(string.sub(str, i, i + 1), 16))
×
695
   end
696
   return ustr
×
697
end
698

699
local uchr_to_surrogate_pair = function (uchr, endianness)
700
   local hi, lo = floor((uchr - 0x10000) / 0x400) + 0xd800, (uchr - 0x10000) % 0x400 + 0xdc00
×
701
   local s_hi, s_lo =
702
      string.char(floor(hi / 256)) .. string.char(hi % 256), string.char(floor(lo / 256)) .. string.char(lo % 256)
×
703
   return endianness == "le" and (reverse(s_hi) .. reverse(s_lo)) or s_hi .. s_lo
×
704
end
705

706
local uchr_to_utf16_double_byte = function (uchr, endianness)
707
   local ustr = string.char(floor(uchr / 256)) .. string.char(uchr % 256)
93✔
708
   return endianness == "le" and reverse(ustr) or ustr
31✔
709
end
710

711
local utf8_to_utf16 = function (str, endianness)
712
   local ustr = utf16bom(endianness)
3✔
713
   for _, uchr in luautf8.codes(str) do
34✔
714
      ustr = ustr
×
715
         .. (uchr < 0x10000 and uchr_to_utf16_double_byte(uchr, endianness) or uchr_to_surrogate_pair(uchr, endianness))
62✔
716
   end
717
   return ustr
3✔
718
end
719

720
--- Convert a UTF-8 string to big-endian UTF-16.
721
-- @tparam string str UTF-8 encoded string.
722
-- @treturn string Big-endian UTF-16 encoded string.
723
function utilities.utf8_to_utf16be (str)
126✔
724
   return utf8_to_utf16(str, "be")
3✔
725
end
726

727
--- Convert a UTF-8 string to little-endian UTF-16.
728
-- @tparam string str UTF-8 encoded string.
729
-- @treturn string Little-endian UTF-16 encoded string.
730
function utilities.utf8_to_utf16le (str)
126✔
731
   return utf8_to_utf16(str, "le")
×
732
end
733

734
--- Convert a UTF-8 string to big-endian UTF-16, then encode in hex.
735
-- @tparam string str UTF-8 encoded string.
736
-- @treturn string Hexidecimal representation of a big-endian UTF-16 encoded string.
737
function utilities.utf8_to_utf16be_hexencoded (str)
126✔
738
   return utilities.hexencoded(utilities.utf8_to_utf16be(str))
6✔
739
end
740

741
--- Convert a UTF-8 string to little-endian UTF-16, then encode in hex.
742
-- @tparam string str UTF-8 encoded string.
743
-- @treturn string Hexidecimal representation of a little-endian UTF-16 encoded string.
744
function utilities.utf8_to_utf16le_hexencoded (str)
126✔
745
   return utilities.hexencoded(utilities.utf8_to_utf16le(str))
×
746
end
747

748
local utf16_to_utf8 = function (str, endianness)
749
   local bom = utf16bom(endianness)
25,013✔
750

751
   if str:find(bom) == 1 then
25,013✔
752
      str = string.sub(str, 3, #str)
×
753
   end
754
   local ustr = ""
25,013✔
755
   for uchr in utilities.utf16codes(str, endianness) do
1,542,981✔
756
      ustr = ustr .. luautf8.char(uchr)
733,971✔
757
   end
758
   return ustr
25,013✔
759
end
760

761
--- Convert a big-endian UTF-16 string to UTF-8.
762
-- @tparam string str Big-endian UTF-16 encoded string.
763
-- @treturn string UTF-8 encoded string.
764
function utilities.utf16be_to_utf8 (str)
126✔
765
   return utf16_to_utf8(str, "be")
25,013✔
766
end
767

768
--- Convert a little-endian UTF-16 string to UTF-8.
769
-- @tparam string str Little-endian UTF-16 encoded string.
770
-- @treturn string UTF-8 encoded string.
771
function utilities.utf16le_to_utf8 (str)
126✔
772
   return utf16_to_utf8(str, "le")
×
773
end
774

775
function utilities.breadcrumbs ()
126✔
776
   local breadcrumbs = {}
122✔
777

778
   setmetatable(breadcrumbs, {
244✔
779
      __index = function (_, key)
780
         local frame = SILE.traceStack[key]
×
781
         return frame and frame.command or nil
×
782
      end,
783
      __len = function (_)
784
         return #SILE.traceStack
×
785
      end,
786
      __tostring = function (self)
787
         return "B»" .. table.concat(self, "»")
×
788
      end,
789
   })
790

791
   function breadcrumbs:dump ()
122✔
792
      SU.dump(self)
×
793
   end
794

795
   function breadcrumbs:parent (count)
122✔
796
      -- Note LuaJIT does not support __len, so this has to work even when that metamethod doesn't fire...
797
      return self[#SILE.traceStack - (count or 1)]
×
798
   end
799

800
   function breadcrumbs:contains (needle, startdepth)
122✔
801
      startdepth = startdepth or 0
×
802
      for i = startdepth, #SILE.traceStack - 1 do
×
803
         local frame = SILE.traceStack[#SILE.traceStack - i]
×
804
         if frame.command == needle then
×
805
            return true, #self - i
×
806
         end
807
      end
808
      return false, -1
×
809
   end
810

811
   return breadcrumbs
122✔
812
end
813

814
utilities.formatNumber = require("core.utilities.numbers")
126✔
815

816
utilities.collatedSort = require("core.utilities.sorting")
126✔
817

818
utilities.ast = require("core.utilities.ast")
126✔
819
utilities.debugAST = utilities.ast.debug
126✔
820

821
function utilities.subContent (content)
126✔
822
   SU.deprecated(
×
823
      "SU.subContent",
824
      "SU.ast.subContent",
825
      "0.15.0",
826
      "0.17.0",
827
      [[
×
828
    Note that the new implementation no longer introduces an id="stuff" key.]]
×
829
   )
830
   return utilities.ast.subContent(content)
×
831
end
832

833
function utilities.hasContent (content)
126✔
834
   SU.deprecated("SU.hasContent", "SU.ast.hasContent", "0.15.0", "0.17.0")
×
835
   return SU.ast.hasContent(content)
×
836
end
837

838
function utilities.contentToString (content)
126✔
839
   SU.deprecated("SU.contentToString", "SU.ast.contentToString", "0.15.0", "0.17.0")
×
840
   return SU.ast.contentToString(content)
×
841
end
842

843
function utilities.walkContent (content, action)
126✔
844
   SU.deprecated("SU.walkContent", "SU.ast.walkContent", "0.15.0", "0.17.0")
×
845
   SU.ast.walkContent(content, action)
×
846
end
847

848
function utilities.stripContentPos (content)
126✔
849
   SU.deprecated("SU.stripContentPos", "SU.ast.stripContentPos", "0.15.0", "0.17.0")
×
850
   return SU.ast.stripContentPos(content)
×
851
end
852

853
return utilities
126✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc