2016-02-04 18:24:39 +00:00
|
|
|
--[[--
|
|
|
|
Miscellaneous helper functions for KOReader frontend.
|
|
|
|
]]
|
|
|
|
|
2015-02-01 09:40:34 +00:00
|
|
|
local util = {}
|
|
|
|
|
|
|
|
function util.stripePunctuations(word)
|
|
|
|
if not word then return end
|
|
|
|
-- strip ASCII punctuation characters around word
|
|
|
|
-- and strip any generic punctuation (U+2000 - U+206F) in the word
|
2016-02-12 14:55:02 +00:00
|
|
|
return word:gsub("\226[\128-\131][\128-\191]", ''):gsub("^%p+", ''):gsub("%p+$", '')
|
2015-02-01 09:40:34 +00:00
|
|
|
end
|
|
|
|
|
2015-04-22 06:17:06 +00:00
|
|
|
--[[
|
|
|
|
Lua doesn't have a string.split() function and most of the time
|
|
|
|
you don't really need it because string.gmatch() is enough.
|
|
|
|
However string.gmatch() has one significant disadvantage for me:
|
|
|
|
You can't split a string while matching both the delimited
|
|
|
|
strings and the delimiters themselves without tracking positions
|
|
|
|
and substrings. The gsplit function below takes care of
|
|
|
|
this problem.
|
|
|
|
Author: Peter Odding
|
|
|
|
License: MIT/X11
|
|
|
|
Source: http://snippets.luacode.org/snippets/String_splitting_130
|
|
|
|
--]]
|
|
|
|
function util.gsplit(str, pattern, capture)
|
|
|
|
pattern = pattern and tostring(pattern) or '%s+'
|
|
|
|
if (''):find(pattern) then
|
|
|
|
error('pattern matches empty string!', 2)
|
|
|
|
end
|
|
|
|
return coroutine.wrap(function()
|
|
|
|
local index = 1
|
|
|
|
repeat
|
|
|
|
local first, last = str:find(pattern, index)
|
|
|
|
if first and last then
|
|
|
|
if index < first then
|
|
|
|
coroutine.yield(str:sub(index, first - 1))
|
|
|
|
end
|
|
|
|
if capture then
|
|
|
|
coroutine.yield(str:sub(first, last))
|
|
|
|
end
|
|
|
|
index = last + 1
|
|
|
|
else
|
|
|
|
if index <= #str then
|
|
|
|
coroutine.yield(str:sub(index))
|
|
|
|
end
|
|
|
|
break
|
|
|
|
end
|
|
|
|
until index > #str
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2016-01-29 06:37:46 +00:00
|
|
|
-- https://gist.github.com/jesseadams/791673
|
2016-01-03 09:08:26 +00:00
|
|
|
function util.secondsToClock(seconds, withoutSeconds)
|
|
|
|
seconds = tonumber(seconds)
|
2015-11-27 15:13:01 +00:00
|
|
|
if seconds == 0 or seconds ~= seconds then
|
|
|
|
if withoutSeconds then
|
|
|
|
return "00:00";
|
|
|
|
else
|
|
|
|
return "00:00:00";
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local hours = string.format("%02.f", math.floor(seconds / 3600));
|
|
|
|
local mins = string.format("%02.f", math.floor(seconds / 60 - (hours * 60)));
|
|
|
|
if withoutSeconds then
|
|
|
|
return hours .. ":" .. mins
|
|
|
|
end
|
|
|
|
local secs = string.format("%02.f", math.floor(seconds - hours * 3600 - mins * 60));
|
|
|
|
return hours .. ":" .. mins .. ":" .. secs
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-04 18:24:39 +00:00
|
|
|
--- Returns number of keys in a table.
|
|
|
|
---- @param T Lua table
|
|
|
|
---- @return number of keys in table T
|
2016-01-29 06:37:46 +00:00
|
|
|
function util.tableSize(T)
|
2015-11-27 15:13:01 +00:00
|
|
|
local count = 0
|
|
|
|
for _ in pairs(T) do count = count + 1 end
|
|
|
|
return count
|
|
|
|
end
|
|
|
|
|
2016-01-31 22:23:44 +00:00
|
|
|
-- append all elements from t2 into t1
|
|
|
|
function util.arrayAppend(t1, t2)
|
2016-02-12 14:55:02 +00:00
|
|
|
for _, v in ipairs(t2) do
|
2016-01-31 22:23:44 +00:00
|
|
|
table.insert(t1, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-12 14:55:02 +00:00
|
|
|
-- Returns the index within this string of the last occurrence of the specified character
|
|
|
|
-- or -1 if the character does not occur.
|
|
|
|
-- To find . you need to escape it.
|
|
|
|
function util.lastIndexOf(string, ch)
|
|
|
|
local i = string:match(".*" .. ch .. "()")
|
|
|
|
if i == nil then return -1 else return i - 1 end
|
|
|
|
end
|
|
|
|
|
2015-02-01 09:40:34 +00:00
|
|
|
return util
|