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

sile-typesetter / sile / 9474085178

11 Jun 2024 10:59PM UTC coverage: 69.177% (+13.9%) from 55.259%
9474085178

push

github

alerque
style(classes): Restyle with stylua

1 of 1 new or added line in 1 file covered. (100.0%)

104 existing lines in 9 files now uncovered.

11978 of 17315 relevant lines covered (69.18%)

4307.68 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")
183✔
6
local luautf8 = require("lua-utf8")
183✔
7
local semver = require("semver")
183✔
8

9
local utilities = {}
183✔
10

11
local epsilon = 1E-12
183✔
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)
183✔
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)
183✔
29
   local new_array = {}
39,195✔
30
   local last = #array
39,195✔
31
   for i = 1, last do
126,019✔
32
      new_array[i] = func(array[i])
164,619✔
33
   end
34
   return new_array
39,195✔
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)
183✔
43
   if not options[name] then
8,578✔
44
      utilities.error(context .. " needs a " .. name .. " parameter")
×
45
   end
46
   if required_type then
8,578✔
47
      return utilities.cast(required_type, options[name])
1,623✔
48
   end
49
   return options[name]
6,955✔
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)
183✔
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
24✔
64
         return a < b
16✔
65
      elseif type(a) == "number" then
8✔
66
         return true
×
67
      else
68
         return false
8✔
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)
183✔
85
   local ptr = start
365✔
86
   local room = stop - start + 1
365✔
87
   local last = replacement and #replacement or 0
365✔
88
   for i = 1, last do
2,586✔
89
      if room > 0 then
2,221✔
90
         room = room - 1
337✔
91
         array[ptr] = replacement[i]
337✔
92
      else
93
         table.insert(array, ptr, replacement[i])
1,884✔
94
      end
95
      ptr = ptr + 1
2,221✔
96
   end
97

98
   for _ = 1, room do
393✔
99
      table.remove(array, ptr)
28✔
100
   end
101
   return array
365✔
102
end
103

104
-- TODO: Unused, now deprecated?
105
function utilities.inherit (orig, spec)
183✔
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)
183✔
133
   if value == false then
7,287✔
134
      return false
114✔
135
   end
136
   if value == true then
7,173✔
137
      return true
2,956✔
138
   end
139
   if value == "false" then
4,217✔
140
      return false
15✔
141
   end
142
   if value == "true" then
4,202✔
143
      return true
19✔
144
   end
145
   if value == "no" then
4,183✔
146
      preferbool()
×
147
      return false
×
148
   end
149
   if value == "yes" then
4,183✔
150
      preferbool()
×
151
      return true
×
152
   end
153
   if value == nil then
4,183✔
154
      return default == true
4,183✔
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)
183✔
170
   local actualType = SU.type(value)
382,380✔
171
   wantedType = string.lower(wantedType)
764,760✔
172
   if wantedType:match(actualType) then
382,380✔
173
      return value
132,030✔
174
   elseif actualType == "nil" and wantedType:match("nil") then
250,350✔
175
      return nil
×
176
   elseif wantedType:match("length") then
250,350✔
177
      return SILE.types.length(value)
40,394✔
178
   elseif wantedType:match("measurement") then
210,138✔
179
      return SILE.types.measurement(value)
4,901✔
180
   elseif wantedType:match("vglue") then
205,237✔
181
      return SILE.types.node.vglue(value)
10✔
182
   elseif wantedType:match("glue") then
205,227✔
183
      return SILE.types.node.glue(value)
74✔
184
   elseif wantedType:match("kern") then
205,153✔
185
      return SILE.types.node.kern(value)
×
186
   elseif actualType == "nil" then
205,153✔
187
      SU.error("Cannot cast nil to " .. wantedType)
×
188
   elseif wantedType:match("boolean") then
205,153✔
189
      return SU.boolean(value)
19✔
190
   elseif wantedType:match("string") then
205,134✔
191
      return tostring(value)
×
192
   elseif wantedType:match("number") then
205,134✔
193
      if type(value) == "table" and type(value.tonumber) == "function" then
205,109✔
194
         return value:tonumber()
201,049✔
195
      end
196
      local num = tonumber(value)
4,060✔
197
      if not num then
4,060✔
198
         SU.error("Cannot cast '" .. value .. "'' to " .. wantedType)
×
199
      end
200
      return num
4,060✔
201
   elseif wantedType:match("integer") then
25✔
202
      local num
203
      if type(value) == "table" and type(value.tonumber) == "function" then
25✔
204
         num = value:tonumber()
×
205
      else
206
         num = tonumber(value)
25✔
207
      end
208
      if not num then
25✔
209
         SU.error("Cannot cast '" .. value .. "'' to " .. wantedType)
×
210
      end
211
      if not wantedType:match("number") and num % 1 ~= 0 then
25✔
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
25✔
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)
183✔
228
   if type(value) == "number" then
1,943,506✔
229
      return math.floor(value) == value and "integer" or "number"
470,408✔
230
   elseif type(value) == "table" and value.prototype then
1,473,098✔
231
      return value:prototype()
×
232
   elseif type(value) == "table" and value.is_a then
1,473,098✔
233
      return value.type
721,460✔
234
   else
235
      return type(value)
751,638✔
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, ...)
183✔
254
   if SILE.quiet then
131,821✔
255
      return
×
256
   end
257
   if utilities.debugging(category) then
263,642✔
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)
183✔
279
   return SILE.debugFlags.all and category ~= "profile" or SILE.debugFlags[category]
136,829✔
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)
183✔
292
   warnat, errorat = semver(warnat or 0), semver(errorat or 0)
21✔
293
   local current = SILE.version and semver(SILE.version:match("v([0-9]*.[0-9]*.[0-9]*)")) or warnat
14✔
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 "()"
14✔
300
   local _new = new and "Please use " .. (new .. brackets) .. " instead." or "Plase don't use it."
7✔
301
   local msg = (old .. brackets)
7✔
302
      .. " was deprecated in SILE v"
×
303
      .. tostring(warnat)
7✔
304
      .. ". "
×
305
      .. _new
7✔
306
      .. (extra and ("\n\n" .. extra .. "\n") or "")
7✔
307
   if errorat and current >= errorat then
7✔
308
      SU.error(msg)
×
309
   elseif warnat and current >= warnat then
7✔
310
      SU.warn(msg)
7✔
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 (...)
183✔
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
183✔
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)
183✔
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)
183✔
342
   if SILE.quiet then
30✔
343
      return
×
344
   end
345
   io.stderr:write("\n! " .. message .. "\n")
30✔
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)
183✔
353
   utilities.msg(message)
30✔
354
   if SILE.traceback or isbug then
30✔
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())
58✔
363
   end
364
   io.stderr:write("\n")
30✔
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)
183✔
378
   lhs = SU.cast("number", lhs)
90✔
379
   rhs = SU.cast("number", rhs)
90✔
380
   local abs = math.abs
45✔
381
   return abs(lhs - rhs) <= epsilon * (abs(lhs) + abs(rhs))
45✔
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)
183✔
388
   local total = 0
11,476✔
389
   local last = #array
11,476✔
390
   for i = 1, last do
22,027✔
391
      total = total + array[i]
10,551✔
392
   end
393
   return total
11,476✔
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 (...)
183✔
400
   local input = pl.utils.pack(...)
25,237✔
401
   local max = table.remove(input, 1)
25,237✔
402
   for _, val in ipairs(input) do
83,586✔
403
      if val > max then
58,349✔
404
         max = val
20,886✔
405
      end
406
   end
407
   return max
25,237✔
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 (...)
183✔
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)
183✔
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)
183✔
451
   local rv = {}
866✔
452
   local max = math.max(pl.utils.unpack(pl.tablex.keys(items)))
2,598✔
453
   for i = 1, max do
19,192✔
454
      if items[i] then
18,326✔
455
         rv[#rv + 1] = items[i]
18,326✔
456
      end
457
   end
458
   return rv
866✔
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)
183✔
464
   local tmp, j
465
   for i = 1, math.floor(#tbl / 2) do
830✔
466
      tmp = tbl[i]
536✔
467
      j = #tbl - i + 1
536✔
468
      tbl[i] = tbl[j]
536✔
469
      tbl[j] = tmp
536✔
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)
183✔
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)
183✔
494
   if spring == 0 then
20,471✔
495
      return inf_bad
636✔
496
   end
497
   local bad = math.floor(100 * math.abs(shortfall / spring) ^ 3)
19,835✔
498
   return math.min(inf_bad, bad)
19,835✔
499
end
500

501
function utilities.rationWidth (target, width, ratio)
183✔
502
   if ratio < 0 and width.shrink:tonumber() > 0 then
18,489✔
503
      target:___add(width.shrink:tonumber() * ratio)
3,696✔
504
   elseif ratio > 0 and width.stretch:tonumber() > 0 then
25,420✔
505
      target:___add(width.stretch:tonumber() * ratio)
6,582✔
506
   end
507
   return target
15,047✔
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)
183✔
519
   string = string and tostring(string) or ""
1,944✔
520
   pattern = pattern and tostring(pattern) or "%s+"
1,944✔
521
   local length = #string
1,944✔
522
   return coroutine.wrap(function ()
1,944✔
523
      local index = 1
1,944✔
524
      repeat
525
         local first, last = string:find(pattern, index)
2,356✔
526
         if last then
2,356✔
527
            if index < first then
651✔
528
               coroutine.yield({ string = string:sub(index, first - 1) })
870✔
529
            end
530
            coroutine.yield({ separator = string:sub(first, last) })
1,302✔
531
            index = last + 1
651✔
532
         else
533
            if index <= length then
1,705✔
534
               coroutine.yield({ string = string:sub(index) })
3,410✔
535
            end
536
            break
1,705✔
537
         end
538
      until index > length
651✔
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)
183✔
546
   local seq = 0
164,108✔
547
   local val = -1
164,108✔
548
   for i = 1, #uchar do
330,836✔
549
      local c = string.byte(uchar, i)
168,631✔
550
      if seq == 0 then
168,631✔
551
         if val > -1 then
164,833✔
552
            return val
1,903✔
553
         end
554
         seq = c < 0x80 and 1
162,930✔
555
            or c < 0xE0 and 2
162,930✔
556
            or c < 0xF0 and 3
3,415✔
557
            or c < 0xF8 and 4 --c < 0xFC and 5 or c < 0xFE and 6 or
383✔
558
            or error("invalid UTF-8 character sequence")
×
559
         val = bitshim.band(c, 2 ^ (8 - seq) - 1)
162,930✔
560
      else
561
         val = bitshim.bor(bitshim.lshift(val, 6), bitshim.band(c, 0x3F))
3,798✔
562
      end
563
      seq = seq - 1
166,728✔
564
   end
565
   return val
162,205✔
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)
183✔
572
   local val = codepoint
359✔
573
   local cp = val
359✔
574
   local hex = (cp:match("[Uu]%+(%x+)") or cp:match("0[xX](%x+)"))
359✔
575
   if hex then
359✔
576
      cp = tonumber("0x" .. hex)
5✔
577
   elseif tonumber(cp) then
354✔
578
      cp = tonumber(cp)
×
579
   end
580

581
   if type(cp) == "number" then
359✔
582
      val = luautf8.char(cp)
5✔
583
   end
584
   return val
359✔
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)
183✔
593
   local pos = 1
33,739✔
594
   return function ()
595
      if pos > #ustr then
1,019,923✔
596
         return nil
33,739✔
597
      else
598
         local c1, c2, c3, c4, wchar, lowchar
599
         c1 = string.byte(ustr, pos, pos + 1)
986,184✔
600
         pos = pos + 1
986,184✔
601
         c2 = string.byte(ustr, pos, pos + 1)
986,184✔
602
         pos = pos + 1
986,184✔
603
         if endian == "be" then
986,184✔
604
            wchar = c1 * 256 + c2
986,184✔
605
         else
606
            wchar = c2 * 256 + c1
×
607
         end
608
         if not (wchar >= 0xD800 and wchar <= 0xDBFF) then
986,184✔
609
            return wchar
986,184✔
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)
183✔
631
   local rv = {}
115,725✔
632
   for _, cp in luautf8.next, str do
755,851✔
633
      table.insert(rv, luautf8.char(cp))
640,126✔
634
   end
635
   return rv
115,725✔
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)
183✔
UNCOV
644
   local chars = utilities.splitUtf8(str)
×
UNCOV
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)
183✔
UNCOV
654
   local chars = utilities.splitUtf8(str)
×
UNCOV
655
   return chars[1]
×
656
end
657

658
local byte, floor, reverse = string.byte, math.floor, string.reverse
183✔
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)
183✔
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")
33,742✔
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)
183✔
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)
183✔
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)
183✔
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)
183✔
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)
183✔
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)
183✔
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)
33,739✔
750

751
   if str:find(bom) == 1 then
33,739✔
752
      str = string.sub(str, 3, #str)
×
753
   end
754
   local ustr = ""
33,739✔
755
   for uchr in utilities.utf16codes(str, endianness) do
2,073,585✔
756
      ustr = ustr .. luautf8.char(uchr)
986,184✔
757
   end
758
   return ustr
33,739✔
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)
183✔
765
   return utf16_to_utf8(str, "be")
33,739✔
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)
183✔
772
   return utf16_to_utf8(str, "le")
×
773
end
774

775
function utilities.breadcrumbs ()
183✔
776
   local breadcrumbs = {}
158✔
777

778
   setmetatable(breadcrumbs, {
316✔
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 ()
158✔
792
      SU.dump(self)
×
793
   end
794

795
   function breadcrumbs:parent (count)
158✔
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)
158✔
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
158✔
812
end
813

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

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

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

821
function utilities.subContent (content)
183✔
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)
183✔
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)
183✔
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)
183✔
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)
183✔
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
183✔
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