cleanup AceSerializer-3.0 & LibBase64-1.0

@Bunny67 I need help on this. I am getting nowhere with the profile export -> text
This commit is contained in:
Crum
2018-06-13 12:58:06 -05:00
parent 800485a072
commit ee6136d532
2 changed files with 43 additions and 51 deletions
@@ -10,25 +10,23 @@
-- make into AceSerializer. -- make into AceSerializer.
-- @class file -- @class file
-- @name AceSerializer-3.0 -- @name AceSerializer-3.0
-- @release $Id: AceSerializer-3.0.lua 1135 2015-09-19 20:39:16Z nevcairiel $ -- @release $Id: AceSerializer-3.0.lua 910 2010-02-11 21:54:24Z mikk $
local MAJOR,MINOR = "AceSerializer-3.0", 5 local MAJOR,MINOR = "AceSerializer-3.0", 3
local AceSerializer, oldminor = LibStub:NewLibrary(MAJOR, MINOR) local AceSerializer, oldminor = LibStub:NewLibrary(MAJOR, MINOR)
if not AceSerializer then return end if not AceSerializer then return end
-- Lua APIs -- Lua APIs
local strbyte, strchar, gsub, gfind, format = string.byte, string.char, string.gsub, string.gfind, string.format local strbyte, strchar, gsub, gmatch, format = string.byte, string.char, string.gsub, string.gmatch, string.format
local assert, error, pcall = assert, error, pcall local assert, error, pcall = assert, error, pcall
local type, tostring, tonumber = type, tostring, tonumber local type, tostring, tonumber = type, tostring, tonumber
local pairs, select, frexp = pairs, select, math.frexp local pairs, select, frexp = pairs, select, math.frexp
local tconcat, tgetn = table.concat, table.getn local tconcat = table.concat
-- quick copies of string representations of wonky numbers -- quick copies of string representations of wonky numbers
local inf = 1/0 local serNaN = tostring(0/0)
local serInf = tostring(1/0)
local serNaN -- can't do this in 4.3, see ace3 ticket 268 local serNegInf = tostring(-1/0)
local serInf, serInfMac = "1.#INF", "inf"
local serNegInf, serNegInfMac = "-1.#INF", "-inf"
-- Serialization functions -- Serialization functions
@@ -62,15 +60,11 @@ local function SerializeValue(v, res, nres)
elseif t=="number" then -- ^N = number (just tostring()ed) or ^F (float components) elseif t=="number" then -- ^N = number (just tostring()ed) or ^F (float components)
local str = tostring(v) local str = tostring(v)
if tonumber(str)==v --[[not in 4.3 or str==serNaN]] then if tonumber(str)==v or str==serNaN or str==serInf or str==serNegInf then
-- translates just fine, transmit as-is -- translates just fine, transmit as-is
res[nres+1] = "^N" res[nres+1] = "^N"
res[nres+2] = str res[nres+2] = str
nres=nres+2 nres=nres+2
elseif v == inf or v == -inf then
res[nres+1] = "^N"
res[nres+2] = v == inf and serInf or serNegInf
nres=nres+2
else else
local m,e = frexp(v) local m,e = frexp(v)
res[nres+1] = "^F" res[nres+1] = "^F"
@@ -121,8 +115,9 @@ local serializeTbl = { "^1" } -- "^1" = Hi, I'm data serialized by AceSerializer
-- @return The data in its serialized form (string) -- @return The data in its serialized form (string)
function AceSerializer:Serialize(...) function AceSerializer:Serialize(...)
local nres = 1 local nres = 1
for i = 1,tgetn(arg) do
local v = arg[i] for i = 1, arg.n do
local v = select(i, unpack(arg))
nres = SerializeValue(v, serializeTbl, nres) nres = SerializeValue(v, serializeTbl, nres)
end end
@@ -148,12 +143,12 @@ local function DeserializeStringHelper(escape)
end end
local function DeserializeNumberHelper(number) local function DeserializeNumberHelper(number)
--[[ not in 4.3 if number == serNaN then if number == serNaN then
return 0/0 return 0/0
else]]if number == serNegInf or number == serNegInfMac then elseif number == serNegInf then
return -inf return -1/0
elseif number == serInf or number == serInfMac then elseif number == serInf then
return inf return 1/0
else else
return tonumber(number) return tonumber(number)
end end
@@ -245,7 +240,7 @@ end
function AceSerializer:Deserialize(str) function AceSerializer:Deserialize(str)
str = gsub(str, "[%c ]", "") -- ignore all control characters; nice for embedding in email and stuff str = gsub(str, "[%c ]", "") -- ignore all control characters; nice for embedding in email and stuff
local iter = gfind(str, "(^.)([^^]*)") -- Any ^x followed by string of non-^ local iter = gmatch(str, "(^.)([^^]*)") -- Any ^x followed by string of non-^
local ctl,data = iter() local ctl,data = iter()
if not ctl or ctl~="^1" then if not ctl or ctl~="^1" then
-- we purposefully ignore the data portion of the start code, it can be used as an extension mechanism -- we purposefully ignore the data portion of the start code, it can be used as an extension mechanism
+26 -29
View File
@@ -6,35 +6,32 @@ Description: A library to encode and decode Base64 strings
License: MIT License: MIT
]] ]]
local modf = math.modf
local sub = string.sub
local format = string.format
local getn = table.getn
local strlen = string.len
local byte = string.byte
local LibBase64 = LibStub:NewLibrary("LibBase64-1.0", 1) local LibBase64 = LibStub:NewLibrary("LibBase64-1.0", 1)
if not LibBase64 then if not LibBase64 then
return return
end end
local _chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" local error, tonumber, type = error, tonumber, type
local concat, insert = table.concat, table.insert
local byte, char, format, gsub, mod, sub = string.byte, string.char, string.format, string.gsub, string.fmod, string.sub
local _chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
local charTable = {} local charTable = {}
local byteToNum = {} local byteToNum = {}
local numToChar = {} local numToChar = {}
for i = 1, strlen(_chars) do local function convert(string)
charTable[i] = sub(_chars, i, i) local t = {}
gsub(string, "%-?%d+", function(n) t[getn(t)+1] = tonumber(n) end)
return t
end end
for i = 1, getn(charTable) do for i = 1, getn(convert(_chars)) do
numToChar[i - 1] = sub(_chars, i, i) numToChar[i - 1] = sub(_chars, i, i)
byteToNum[byte(_chars, i)] = i - 1 byteToNum[byte(_chars, i)] = i - 1
end end
_chars = nil _chars = nil
local A_byte = byte("A") local A_byte = byte("A")
local Z_byte = byte("Z") local Z_byte = byte("Z")
local a_byte = byte("a") local a_byte = byte("a")
@@ -68,7 +65,7 @@ function LibBase64:Encode(text, maxLineLength, lineEnding)
-- do nothing -- do nothing
elseif type(maxLineLength) ~= "number" then elseif type(maxLineLength) ~= "number" then
error(format("Bad argument #2 to `Encode'. Expected %q or %q, got %q", "number", "nil", type(maxLineLength)), 2) error(format("Bad argument #2 to `Encode'. Expected %q or %q, got %q", "number", "nil", type(maxLineLength)), 2)
elseif modf(maxLineLength, 4) ~= 0 then elseif mod(maxLineLength, 4) ~= 0 then
error(format("Bad argument #2 to `Encode'. Expected a multiple of 4, got %s", maxLineLength), 2) error(format("Bad argument #2 to `Encode'. Expected a multiple of 4, got %s", maxLineLength), 2)
elseif maxLineLength <= 0 then elseif maxLineLength <= 0 then
error(format("Bad argument #2 to `Encode'. Expected a number > 0, got %s", maxLineLength), 2) error(format("Bad argument #2 to `Encode'. Expected a number > 0, got %s", maxLineLength), 2)
@@ -95,16 +92,16 @@ function LibBase64:Encode(text, maxLineLength, lineEnding)
end end
local num = a * 2^16 + b * 2^8 + c local num = a * 2^16 + b * 2^8 + c
local d = modf(num, 2^6) local d = mod(num, 2^6)
num = (num - d) / 2^6 num = (num - d) / 2^6
local c = modf(num, 2^6) local c = mod(num, 2^6)
num = (num - c) / 2^6 num = (num - c) / 2^6
local b = modf(num, 2^6) local b = mod(num, 2^6)
num = (num - b) / 2^6 num = (num - b) / 2^6
local a = modf(num, 2^6) local a = mod(num, 2^6)
t[getn(t)+1] = numToChar[a] t[getn(t)+1] = numToChar[a]
@@ -115,12 +112,12 @@ function LibBase64:Encode(text, maxLineLength, lineEnding)
t[getn(t)+1] = (nilNum >= 1) and "=" or numToChar[d] t[getn(t)+1] = (nilNum >= 1) and "=" or numToChar[d]
currentLength = currentLength + 4 currentLength = currentLength + 4
if maxLineLength and modf(currentLength, maxLineLength) == 0 then if maxLineLength and mod(currentLength, maxLineLength) == 0 then
t[getn(t)+1] = lineEnding t[getn(t)+1] = lineEnding
end end
end end
local s = table.concat(t) local s = concat(t)
for i = 1, getn(t) do for i = 1, getn(t) do
t[i] = nil t[i] = nil
end end
@@ -172,20 +169,20 @@ function LibBase64:Decode(text)
local num = a * 2^18 + b * 2^12 + c * 2^6 + d local num = a * 2^18 + b * 2^12 + c * 2^6 + d
local c = modf(num, 2^8) local c = mod(num, 2^8)
num = (num - c) / 2^8 num = (num - c) / 2^8
local b = modf(num, 2^8) local b = mod(num, 2^8)
num = (num - b) / 2^8 num = (num - b) / 2^8
local a = modf(num, 2^8) local a = mod(num, 2^8)
t[getn(t)+1] = string.char(a) t[getn(t)+1] = char(a)
if nilNum < 2 then if nilNum < 2 then
t[getn(t)+1] = string.char(b) t[getn(t)+1] = char(b)
end end
if nilNum < 1 then if nilNum < 1 then
t[getn(t)+1] = string.char(c) t[getn(t)+1] = char(c)
end end
end end
@@ -193,7 +190,7 @@ function LibBase64:Decode(text)
t2[i] = nil t2[i] = nil
end end
local s = table.concat(t) local s = concat(t)
for i = 1, getn(t) do for i = 1, getn(t) do
t[i] = nil t[i] = nil
@@ -207,7 +204,7 @@ function LibBase64:IsBase64(text)
error(format("Bad argument #1 to `IsBase64'. Expected %q, got %q", "string", type(text)), 2) error(format("Bad argument #1 to `IsBase64'. Expected %q, got %q", "string", type(text)), 2)
end end
if modf(strlen(text), 4) ~= 0 then if mod(getn(text), 4) ~= 0 then
return false return false
end end
@@ -224,4 +221,4 @@ function LibBase64:IsBase64(text)
end end
return true return true
end end