misc fixes in Libs

This commit is contained in:
Pinya
2018-07-22 21:20:20 +03:00
parent 5a9c2e9ade
commit 7621e0f4af
4 changed files with 41 additions and 51 deletions
+26 -35
View File
@@ -14,14 +14,13 @@ end
local error = error
local type = type
local mod = math.mod
local fmod = math.fmod
local byte, char, format, len, sub = string.byte, string.char, string.format, string.len, string.sub
local concat = table.concat
local concat, getn, tinsert, wipe = table.concat, table.getn, table.insert, table.wipe
local _chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
local byteToNum = {}
local numToChar = {}
for i = 1, len(_chars) do
numToChar[i - 1] = sub(_chars, i, i)
byteToNum[byte(_chars, i)] = i - 1
@@ -60,7 +59,7 @@ function LibBase64:Encode(text, maxLineLength, lineEnding)
-- do nothing
elseif type(maxLineLength) ~= "number" then
error(format("Bad argument #2 to `Encode'. Expected %q or %q, got %q", "number", "nil", type(maxLineLength)), 2)
elseif mod(maxLineLength, 4) ~= 0 then
elseif fmod(maxLineLength, 4) ~= 0 then
error(format("Bad argument #2 to `Encode'. Expected a multiple of 4, got %s", maxLineLength), 2)
elseif maxLineLength <= 0 then
error(format("Bad argument #2 to `Encode'. Expected a number > 0, got %s", maxLineLength), 2)
@@ -89,35 +88,33 @@ function LibBase64:Encode(text, maxLineLength, lineEnding)
end
local num = a * 2^16 + b * 2^8 + c
local d = mod(num, 2^6)
local d = fmod(num, 2^6)
num = (num - d) / 2^6
local c = mod(num, 2^6)
local c = fmod(num, 2^6)
num = (num - c) / 2^6
local b = mod(num, 2^6)
local b = fmod(num, 2^6)
num = (num - b) / 2^6
local a = mod(num, 2^6)
local a = fmod(num, 2^6)
t[getn(t)+1] = numToChar[a]
tinsert(t, numToChar[a])
t[getn(t)+1] = numToChar[b]
tinsert(t, numToChar[b])
t[getn(t)+1] = (nilNum >= 2) and "=" or numToChar[c]
tinsert(t, ((nilNum >= 2) and "=" or numToChar[c]))
t[getn(t)+1] = (nilNum >= 1) and "=" or numToChar[d]
tinsert(t, ((nilNum >= 1) and "=" or numToChar[d]))
currentLength = currentLength + 4
if maxLineLength and mod(currentLength, maxLineLength) == 0 then
t[getn(t)+1] = lineEnding
if maxLineLength and fmod(currentLength, maxLineLength) == 0 then
tinsert(t, lineEnding)
end
end
local s = concat(t)
for i = 1, getn(t) do
t[i] = nil
end
wipe(t)
return s
end
@@ -141,13 +138,11 @@ function LibBase64:Decode(text)
else
local num = byteToNum[byte]
if not num then
for i = 1, getn(t2) do
t2[k] = nil
end
wipe(t2)
error(format("Bad argument #1 to `Decode'. Received an invalid char: %q", sub(text, i, i)), 2)
end
t2[getn(t2)+1] = num
tinsert(t2, num)
end
end
@@ -166,32 +161,28 @@ function LibBase64:Decode(text)
local num = a * 2^18 + b * 2^12 + c * 2^6 + d
local c = mod(num, 2^8)
local c = fmod(num, 2^8)
num = (num - c) / 2^8
local b = mod(num, 2^8)
local b = fmod(num, 2^8)
num = (num - b) / 2^8
local a = mod(num, 2^8)
local a = fmod(num, 2^8)
t[getn(t)+1] = char(a)
tinsert(t, char(a))
if nilNum < 2 then
t[getn(t)+1] = char(b)
tinsert(t, char(b))
end
if nilNum < 1 then
t[getn(t)+1] = char(c)
tinsert(t, char(c))
end
end
for i = 1, getn(t2) do
t2[i] = nil
end
wipe(t2)
local s = concat(t)
for i = 1, getn(t) do
t[i] = nil
end
wipe(t)
return s
end
@@ -201,7 +192,7 @@ function LibBase64:IsBase64(text)
error(format("Bad argument #1 to `IsBase64'. Expected %q, got %q", "string", type(text)), 2)
end
if mod(len(text), 4) ~= 0 then
if fmod(len(text), 4) ~= 0 then
return false
end
+13 -14
View File
@@ -34,6 +34,7 @@ local assert = assert
local table_insert = table.insert
local table_remove = table.remove
local table_concat = table.concat
local table_wipe = table.wipe
local string_char = string.char
local string_byte = string.byte
local string_len = string.len
@@ -41,7 +42,7 @@ local string_gsub = string.gsub
local string_sub = string.sub
local unpack = unpack
local pairs = pairs
local math_mod = math.mod
local math_fmod = math.fmod
local bit_band = bit.band
local bit_bor = bit.bor
local bit_bxor = bit.bxor
@@ -99,15 +100,13 @@ end
-- the bytes returned by this do not contain "\000"
local bytes = {}
local function encode(x)
for k = 1, getn(bytes) do
bytes[k] = nil
end
table_wipe(bytes)
bytes[getn(bytes) + 1] = math_mod(x, 255)
table_insert(bytes, math_fmod(x, 255))
x=math.floor(x/255)
while x > 0 do
bytes[getn(bytes) + 1] = math_mod(x, 255)
table_insert(bytes, math_fmod(x, 255))
x=math.floor(x/255)
end
if getn(bytes) == 1 and bytes[1] > 0 and bytes[1] < 250 then
@@ -168,7 +167,7 @@ function LibCompress:CompressLZW(uncompressed)
dict_size = dict_size + 1
local r = encode(dict[w])
ressize = ressize + string_len(r)
result[getn(result) + 1] = r
table_insert(result, r)
w = c
end
end
@@ -176,7 +175,7 @@ function LibCompress:CompressLZW(uncompressed)
if w then
local r = encode(dict[w])
ressize = ressize + string_len(r)
result[getn(result) + 1] = r
table_insert(result, r)
end
if (string_len(uncompressed) + 1) > ressize then
@@ -214,15 +213,15 @@ function LibCompress:DecompressLZW(compressed)
local delta, k
k, delta = decode(compressed, t)
t = t + delta
result[getn(result) + 1] = dict[k]
table_insert(result, dict[k])
local w = dict[k]
local entry
while t <= getn(compressed) do
while t <= string_len(compressed) do
k, delta = decode(compressed, t)
t = t + delta
entry = dict[k] or (w..string_sub(w, 1, 1))
result[getn(result) + 1] = entry
table_insert(result, entry)
dict[dict_size] = w..string_sub(entry, 1, 1)
dict_size = dict_size + 1
w = entry
@@ -881,7 +880,7 @@ function LibCompress:GetEncodeTable(reservedChars, escapeChars, mapChars)
return nil, "No escape characters supplied"
end
if getn(reservedChars) < getn(mapChars) then
if string_len(reservedChars) < string_len(mapChars) then
return nil, "Number of reserved characters must be at least as many as the number of mapped chars"
end
@@ -914,8 +913,8 @@ function LibCompress:GetEncodeTable(reservedChars, escapeChars, mapChars)
local escapeCharIndex, escapeChar = 0
-- map single byte to single byte
if getn(mapChars) > 0 then
for i = 1, getn(mapChars) do
if string_len(mapChars) > 0 then
for i = 1, string_len(mapChars) do
from = string_sub(reservedChars, i, i)
to = string_sub(mapChars, i, i)
encode_translate[from] = to
@@ -152,7 +152,7 @@ function lib:VersionCheck()
local E = ElvUI[1]
if event == "CHAT_MSG_ADDON" then
if not (arg1 == lib.prefix and arg4 and arg1 and not strmatch(arg1, "^%s-$")) then return end
if not (arg1 == lib.prefix and arg4 and arg2 and not strmatch(arg2, "^%s-$")) then return end
if arg4 == E.myname then return end
if not E["pluginRecievedOutOfDateMessage"] then
@@ -173,7 +173,7 @@ function lib:Register(mediatype, key, data, langmask)
mediatype = lower(mediatype)
if mediatype == lib.MediaType.FONT and ((langmask and band(langmask, LOCALE_MASK) == 0) or not (langmask or locale_is_western)) then return false end
if mediatype == lib.MediaType.SOUND and type(data) == "string" then
local path = data
local path = lower(data)
-- Only wav, ogg and mp3 are valid sounds.
if not find(path, ".ogg", nil, true) and not find(path, ".mp3", nil, true) and not find(path, ".wav", nil, true) then
return false