summaryrefslogtreecommitdiff
path: root/biblio/citation-style-language/citeproc-util.lua
diff options
context:
space:
mode:
Diffstat (limited to 'biblio/citation-style-language/citeproc-util.lua')
-rw-r--r--biblio/citation-style-language/citeproc-util.lua53
1 files changed, 34 insertions, 19 deletions
diff --git a/biblio/citation-style-language/citeproc-util.lua b/biblio/citation-style-language/citeproc-util.lua
index 71221e6bec..1602511d8e 100644
--- a/biblio/citation-style-language/citeproc-util.lua
+++ b/biblio/citation-style-language/citeproc-util.lua
@@ -11,6 +11,21 @@ local inspect -- only load it when debugging
local util = {}
+-- Deep copy
+function util.deep_copy(obj)
+ local res
+ if type(obj) == "table" then
+ res = {}
+ for key, value in pairs(obj) do
+ res[key] = util.deep_copy(value)
+ end
+ else
+ res = obj
+ end
+ return res
+end
+
+-- Shallow copy
function util.clone(obj)
if type(obj) == "table" then
local res = {}
@@ -56,7 +71,7 @@ function util.to_list(str)
end
function util.to_ordinal (n)
- assert(type(n) == "number")
+ -- assert(type(n) == "number")
local last_digit = n % 10
if last_digit == 1 and n ~= 11
then return tostring(n) .. "st"
@@ -237,9 +252,9 @@ end
-- Python list.extend()
function util.extend(first, second)
- if not second then
- print(debug.traceback())
- end
+ -- if not second then
+ -- print(debug.traceback())
+ -- end
local l = #first
for i, element in ipairs(second) do
first[l + i] = element
@@ -262,17 +277,17 @@ end
function util.lstrip (str)
if not str then
- return nil
+ error("Invalid input")
end
- local res = string.gsub(str, "^%s+", "")
+ local res = string.gsub(str, "^%s*", "")
return res
end
function util.rstrip (str)
if not str then
- return nil
+ error("Invalid input")
end
- local res = string.gsub(str, "%s+$", "")
+ local res = string.gsub(str, "%s*$", "")
return res
end
@@ -280,16 +295,16 @@ function util.strip (str)
return util.lstrip(util.rstrip(str))
end
-function util.startswith (str, prefix)
- if not str then
- return false
+function util.startswith(str, prefix)
+ if not str or type(str) ~= "string" then
+ print(debug.traceback())
end
return string.sub(str, 1, #prefix) == prefix
end
-function util.endswith (str, suffix)
- if not str then
- return false
+function util.endswith(str, suffix)
+ if not str or type(str) ~= "string" then
+ print(debug.traceback())
end
return string.sub(str, -#suffix) == suffix
end
@@ -307,7 +322,7 @@ function util.is_numeric (str)
if not string.match(w, "^%a*%d+%a*$") and
not string.match(w, "^[MDCLXVI]+$") and
not string.match(w, "^[mdclxvi]+$") then
- -- Roman number withou validation
+ -- Roman number without validation
return false
end
end
@@ -828,7 +843,7 @@ function util.has_cjk_char(s)
end
function util.convert_roman (number)
- assert(type(number) == "number")
+ -- assert(type(number) == "number")
local output = {}
for _, tuple in ipairs(util.roman_numerals) do
local letter, value = table.unpack(tuple)
@@ -979,9 +994,9 @@ util.superscripts = {
-- File IO
function util.read_file(path)
- if not path then
- print(debug.traceback())
- end
+ -- if not path then
+ -- print(debug.traceback())
+ -- end
local file = io.open(path, "r")
if not file then
-- util.error(string.format('Cannot read file "%s".', path))