Skip to content

Commit eb929ad

Browse files
committed
Break out tostr variations
1 parent a800692 commit eb929ad

File tree

1 file changed

+46
-15
lines changed

1 file changed

+46
-15
lines changed

strings.p8

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,59 @@ end
7171

7272

7373
------------------------------------------------------------------------
74-
-- replacement tostr() that serializes tables
74+
-- replacement tostr() with additional features
7575
-- all versions ", ..." can be removed to save tokens if you never use tostr(number,true)
7676
local _tostr = tostr
77-
-- this version respects __tostring and prints array values in order without keys
78-
local function tostr(n, ...)
77+
78+
-- this version respects __tostring
79+
local function tostr_tostring(n, ...)
80+
if type(n) == "table" then
81+
local m = getmetatable(n)
82+
if m and m.__tostring then
83+
return m.__tostring(n, ...)
84+
end
85+
end
86+
return _tostr(n, ...)
87+
end
88+
89+
-- this version prints tables recursively with all keys in unpredictable order
90+
local function tostr_tables(t, ...)
91+
if type(n) == "table" then
92+
local s = "{"
93+
for k, v in pairs(t) do
94+
s = s .. (s=="{" and '' or ",") .. tostr(k, ...) .. "=" .. tostr(v, ...) -- mishandles reserved words that require ["key"]
95+
end
96+
return s
97+
end
98+
return _tostr(t, ...)
99+
end
100+
101+
-- this version prints tables recursively with array values in order without keys then remaining table keys in unpredictable order
102+
local function tostr_arrays_tables(n, ...)
103+
if type(n) == "table" then
104+
local f, s = {}, "{" -- "[table:{" would avoid ambiguity with literal strings that look like tables
105+
for i = 1, #n do
106+
s = s .. (i == 1 and '' or ",") .. tostr(n[i])
107+
f[i] = true
108+
end
109+
for k, v in pairs(n) do
110+
if not f[k] then
111+
s = s .. (s == "{" and '' or ",") .. tostr(k) .. "=" .. tostr(v) -- mishandles reserved words that require ["key"]
112+
end
113+
end
114+
return s .. "}" -- .. "]" to match less ambiguous alternative above
115+
end
116+
return _tostr(n, ...)
117+
end
118+
119+
-- this version respects __tostring and prints tables recursively with array values in order without keys then remaining table keys in unpredictable order
120+
local function tostr_tostring_arrays_tables(n, ...)
79121
if type(n) == "table" then
80122
local m = getmetatable(n)
81123
if m and m.__tostring then
82124
return m.__tostring(n, ...)
83125
else
84-
local f, s = {}, "{" -- "[table:{" avoids ambiguity with literal strings that look like tables
126+
local f, s = {}, "{" -- "[table:{" would avoid ambiguity with literal strings that look like tables
85127
for i = 1, #n do
86128
s = s .. (i == 1 and '' or ",") .. tostr(n[i])
87129
f[i] = true
@@ -96,17 +138,6 @@ local function tostr(n, ...)
96138
end
97139
return _tostr(n, ...)
98140
end
99-
-- this version prints all keys, in unpredictable order
100-
local function tostr(t, ...)
101-
if type(n) == "table" then
102-
local s = "{"
103-
for k, v in pairs(t) do
104-
s = s .. (s=="{" and '' or ",") .. tostr(k, ...) .. "=" .. tostr(v, ...) -- mishandles reserved words that require ["key"]
105-
end
106-
return s
107-
end
108-
return _tostr(t, ...)
109-
end
110141

111142

112143
------------------------------------------------------------------------

0 commit comments

Comments
 (0)