diff options
author | Norbert Preining <norbert@preining.info> | 2021-08-15 03:00:39 +0000 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2021-08-15 03:00:39 +0000 |
commit | 8e38006fd2e220c3a27208477dab7abbf7256370 (patch) | |
tree | d63fa0168a10601bb6a10a1e3684ed25e52a68c9 /indexing/xindex/lualatex/xindex-baselib.lua | |
parent | 769e7ba80785adf6ccb03041b687fcd0253e9371 (diff) |
CTAN sync 202108150300
Diffstat (limited to 'indexing/xindex/lualatex/xindex-baselib.lua')
-rw-r--r-- | indexing/xindex/lualatex/xindex-baselib.lua | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/indexing/xindex/lualatex/xindex-baselib.lua b/indexing/xindex/lualatex/xindex-baselib.lua new file mode 100644 index 0000000000..ba1e0d27cc --- /dev/null +++ b/indexing/xindex/lualatex/xindex-baselib.lua @@ -0,0 +1,159 @@ +----------------------------------------------------------------------- +-- FILE: xindex-baselib.lua +-- DESCRIPTION: baselibrary for xindex.lua +-- REQUIREMENTS: +-- AUTHOR: Herbert Voß +-- LICENSE: LPPL1.3 +-- +-- $Id: xindex-baselib.lua 11 2021-08-14 09:46:46Z hvoss $ +----------------------------------------------------------------------- + +if not modules then modules = { } end modules ['xindex-baselib'] = { + version = 0.31, + comment = "base library to xindex.lua", + author = "Herbert Voss", + copyright = "Herbert Voss", + license = "LPPL 1.3" +} + +function dofile(filename) + local file = kpse.find_file(filename) + local f = assert(loadfile(file)) + return f() +end + +-- see if the file exists +function file_exists(file) +local f = io.open(file, "rb") +if f then f:close() end +return f ~= nil +end + +function array_concat(...) + local t = {} + for n = 1,select("#",...) do + local arg = select(n,...) + if type(arg)=="table" then + for _,v in ipairs(arg) do + t[#t+1] = v + end + else + t[#t+1] = arg + end + end + return t +end + +function IntegerOrString(str) + local x = tonumber(str) + if x == nil -- true, if str contains _not_ only digits -> integer + then return str + else return x + end +end + +function getPathFileExt(str) + local filepath = str:match("(.*[/\\])") + local filename = str:match("^.+/(.+)$") + local fileext = str:match("^.+(%..+)$") + return filepath,filename,fileext +end + +local romanMap = { + I = 1, + V = 5, + X = 10, + L = 50, + C = 100, + D = 500, + M = 1000, +} + +local numbers = { 1, 5, 10, 50, 100, 500, 1000 } +local romanchars = { "I", "V", "X", "L", "C", "D", "M" } +local RomanNumerals = { } + +function numberToRoman(s) + --s = tostring(s) + s = tonumber(s) + if not s or s ~= s then error"Unable to convert to number" end + if s == math.huge then error"Unable to convert infinity" end + s = math.floor(s) + if s <= 0 then return s end + local ret = "" + for i = #numbers, 1, -1 do + local num = numbers[i] + while s - num >= 0 and s > 0 do + ret = ret .. romanchars[i] + s = s - num + end + for j = 1, i - 1 do + local n2 = numbers[j] + if s - (num - n2) >= 0 and s < num and s > 0 and num - n2 ~= n2 then + ret = ret .. romanchars[j] .. romanchars[i] + s = s - (num - n2) + break + end + end + end + return ret +end + +function romanToNumber(s) +-- if args_v then print("romanToNumber: "..tostring(s)) end +-- if not s then return end + s = s:upper() + local ret = 0 + local i = 1 + while i <= s:len() do + local c = s:sub(i, i) + if c ~= " " then -- allow spaces + local m = romanMap[c] + if not m then return nil end --error("Unknown Roman Numeral '" .. c .. "'") + local next = s:sub(i + 1, i + 1) + local nextm = romanMap[next] + if next and nextm then + if nextm > m then + ret = ret + (nextm - m) + i = i + 1 + else + ret = ret + m + end + else + ret = ret + m + end + end + i = i + 1 + end + return ret +end + +function string:split() + local sep, fields = " ", {} + local pattern = string.format("([^%s]+)", sep) + self:gsub(pattern, function(c) fields[#fields+1] = c end) + return fields +end + +function shellsort(a) + local inc = math.ceil( #a / 2 ) + while inc > 0 do + for i = inc, #a do + local tmp = a[i] + local j = i + while j > inc and not UTFCompare(a[j-inc],tmp) do + a[j] = a[j-inc] + j = j - inc + end + a[j] = tmp + end + inc = math.floor( 0.5 + inc / 2.2 ) + end + return a +end + +function stripLeadingSpaces(str) + return str:gsub("^%s*(.-)%s*$", "%1") +end + + |