Move some util functions around
This commit is contained in:
parent
4ff9f5bb07
commit
2680dff0f3
|
@ -48,14 +48,15 @@ local url_characters_legacy =
|
|||
url_characters ..
|
||||
[[$-+!*'(),]]
|
||||
|
||||
local url_characters_rev = {}
|
||||
for i = 1,string.len(url_characters) do
|
||||
url_characters_rev[string.sub(url_characters,i,i)] = i
|
||||
end
|
||||
local url_characters_rev_legacy = {}
|
||||
for i = 1,string.len(url_characters_legacy) do
|
||||
url_characters_rev_legacy[string.sub(url_characters_legacy,i,i)] = i
|
||||
local function str2set(str)
|
||||
local tbl = {}
|
||||
for i = 1, #str do
|
||||
tbl[string.sub(str,i,i)] = i
|
||||
end
|
||||
return tbl
|
||||
end
|
||||
local url_characters_rev = str2set(url_characters)
|
||||
local url_characters_rev_legacy = str2set(url_characters_legacy)
|
||||
|
||||
--[[ md
|
||||
@name lua/util/encode_id
|
||||
|
@ -64,10 +65,9 @@ according to the {{doc/url_spec}
|
|||
]]
|
||||
function util.encode_id(number)
|
||||
local result = {}
|
||||
local charlen = string.len(url_characters)
|
||||
repeat
|
||||
local pos = (number % charlen) + 1
|
||||
number = math.floor(number / charlen)
|
||||
local pos = (number % #url_characters) + 1
|
||||
number = math.floor(number / #url_characters)
|
||||
table.insert(result,string.sub(url_characters,pos,pos))
|
||||
until number == 0
|
||||
return table.concat(result)
|
||||
|
@ -81,10 +81,9 @@ function util.encode_id(number)
|
|||
return new_encode(number)
|
||||
else
|
||||
local result = {}
|
||||
local charlen = string.len(url_characters_legacy)
|
||||
repeat
|
||||
local pos = (number % charlen) + 1
|
||||
number = math.floor(number / charlen)
|
||||
local pos = (number % #url_characters_legacy) + 1
|
||||
number = math.floor(number / #url_characters_legacy)
|
||||
table.insert(result,string.sub(url_characters_legacy,pos,pos))
|
||||
until number == 0
|
||||
return table.concat(result)
|
||||
|
@ -97,11 +96,10 @@ Given a short HTML-safe url path, convert it to a storyid
|
|||
function util.decode_id(s)
|
||||
local res, id = pcall(function()
|
||||
local n = 0
|
||||
local charlen = string.len(url_characters)
|
||||
for i = 1,string.len(s) do
|
||||
local char = string.sub(s,i,i)
|
||||
local pos = url_characters_rev[char] - 1
|
||||
n = n + (pos*math.pow(charlen,i-1))
|
||||
n = n + (pos*math.pow(#url_characters,i-1))
|
||||
end
|
||||
return n
|
||||
end)
|
||||
|
@ -119,11 +117,10 @@ local new_decode = util.decode_id
|
|||
function util.decode_id(s)
|
||||
local res, id = pcall(function()
|
||||
local n = 0
|
||||
local charlen = string.len(url_characters_legacy)
|
||||
for i = 1,string.len(s) do
|
||||
local char = string.sub(s,i,i)
|
||||
local pos = url_characters_rev_legacy[char] - 1
|
||||
n = n + (pos * math.pow(charlen,i-1))
|
||||
n = n + (pos * math.pow(#url_characters_legacy,i-1))
|
||||
end
|
||||
return n
|
||||
end)
|
||||
|
@ -172,10 +169,8 @@ function util.parse_tags(str)
|
|||
local tags = {}
|
||||
for tag in string.gmatch(str,"([^;]+)") do
|
||||
assert(tag, "Found a nil or false tag in:" .. str)
|
||||
local tag_trimmed = string.match(tag,"%s*(.*)%s*")
|
||||
local tag_lower = string.lower(tag_trimmed)
|
||||
local tag_capitalized = string.gsub(tag_lower,"^.",string.upper)
|
||||
assert(tag_capitalized, "After processing tag:" .. tag .. " it was falsey.")
|
||||
local tag_fmt = tag:match("%s*(.*)%s*"):lower():gsub("^.",string.upper)
|
||||
assert(tag_fmt, "After processing tag:" .. tag .. " it was falsey.")
|
||||
if string.len(tag_capitalized) > 0 then
|
||||
table.insert(tags, tag_capitalized)
|
||||
end
|
||||
|
@ -194,9 +189,7 @@ comment :: table {
|
|||
}
|
||||
]]
|
||||
function util.get_comments(sid)
|
||||
stmnt_comments:bind_names{
|
||||
id = sid
|
||||
}
|
||||
stmnt_comments:bind_names{id = sid}
|
||||
local comments = {}
|
||||
for com_author, com_isanon, com_text in db.sql_rows(stmnt_comments) do
|
||||
table.insert(comments,{
|
||||
|
|
Loading…
Reference in New Issue