misc_helpers.lua 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. -- Minetest: builtin/misc_helpers.lua
  2. --------------------------------------------------------------------------------
  3. -- Localize functions to avoid table lookups (better performance).
  4. local string_sub, string_find = string.sub, string.find
  5. --------------------------------------------------------------------------------
  6. function basic_dump(o)
  7. local tp = type(o)
  8. if tp == "number" then
  9. return tostring(o)
  10. elseif tp == "string" then
  11. return string.format("%q", o)
  12. elseif tp == "boolean" then
  13. return tostring(o)
  14. elseif tp == "nil" then
  15. return "nil"
  16. -- Uncomment for full function dumping support.
  17. -- Not currently enabled because bytecode isn't very human-readable and
  18. -- dump's output is intended for humans.
  19. --elseif tp == "function" then
  20. -- return string.format("loadstring(%q)", string.dump(o))
  21. else
  22. return string.format("<%s>", tp)
  23. end
  24. end
  25. local keywords = {
  26. ["and"] = true,
  27. ["break"] = true,
  28. ["do"] = true,
  29. ["else"] = true,
  30. ["elseif"] = true,
  31. ["end"] = true,
  32. ["false"] = true,
  33. ["for"] = true,
  34. ["function"] = true,
  35. ["goto"] = true, -- Lua 5.2
  36. ["if"] = true,
  37. ["in"] = true,
  38. ["local"] = true,
  39. ["nil"] = true,
  40. ["not"] = true,
  41. ["or"] = true,
  42. ["repeat"] = true,
  43. ["return"] = true,
  44. ["then"] = true,
  45. ["true"] = true,
  46. ["until"] = true,
  47. ["while"] = true,
  48. }
  49. local function is_valid_identifier(str)
  50. if not str:find("^[a-zA-Z_][a-zA-Z0-9_]*$") or keywords[str] then
  51. return false
  52. end
  53. return true
  54. end
  55. --------------------------------------------------------------------------------
  56. -- Dumps values in a line-per-value format.
  57. -- For example, {test = {"Testing..."}} becomes:
  58. -- _["test"] = {}
  59. -- _["test"][1] = "Testing..."
  60. -- This handles tables as keys and circular references properly.
  61. -- It also handles multiple references well, writing the table only once.
  62. -- The dumped argument is internal-only.
  63. function dump2(o, name, dumped)
  64. name = name or "_"
  65. -- "dumped" is used to keep track of serialized tables to handle
  66. -- multiple references and circular tables properly.
  67. -- It only contains tables as keys. The value is the name that
  68. -- the table has in the dump, eg:
  69. -- {x = {"y"}} -> dumped[{"y"}] = '_["x"]'
  70. dumped = dumped or {}
  71. if type(o) ~= "table" then
  72. return string.format("%s = %s\n", name, basic_dump(o))
  73. end
  74. if dumped[o] then
  75. return string.format("%s = %s\n", name, dumped[o])
  76. end
  77. dumped[o] = name
  78. -- This contains a list of strings to be concatenated later (because
  79. -- Lua is slow at individual concatenation).
  80. local t = {}
  81. for k, v in pairs(o) do
  82. local keyStr
  83. if type(k) == "table" then
  84. if dumped[k] then
  85. keyStr = dumped[k]
  86. else
  87. -- Key tables don't have a name, so use one of
  88. -- the form _G["table: 0xFFFFFFF"]
  89. keyStr = string.format("_G[%q]", tostring(k))
  90. -- Dump key table
  91. t[#t + 1] = dump2(k, keyStr, dumped)
  92. end
  93. else
  94. keyStr = basic_dump(k)
  95. end
  96. local vname = string.format("%s[%s]", name, keyStr)
  97. t[#t + 1] = dump2(v, vname, dumped)
  98. end
  99. return string.format("%s = {}\n%s", name, table.concat(t))
  100. end
  101. --------------------------------------------------------------------------------
  102. -- This dumps values in a one-statement format.
  103. -- For example, {test = {"Testing..."}} becomes:
  104. -- [[{
  105. -- test = {
  106. -- "Testing..."
  107. -- }
  108. -- }]]
  109. -- This supports tables as keys, but not circular references.
  110. -- It performs poorly with multiple references as it writes out the full
  111. -- table each time.
  112. -- The indent field specifies a indentation string, it defaults to a tab.
  113. -- Use the empty string to disable indentation.
  114. -- The dumped and level arguments are internal-only.
  115. function dump(o, indent, nested, level)
  116. if type(o) ~= "table" then
  117. return basic_dump(o)
  118. end
  119. -- Contains table -> true/nil of currently nested tables
  120. nested = nested or {}
  121. if nested[o] then
  122. return "<circular reference>"
  123. end
  124. nested[o] = true
  125. indent = indent or "\t"
  126. level = level or 1
  127. local t = {}
  128. local dumped_indexes = {}
  129. for i, v in ipairs(o) do
  130. t[#t + 1] = dump(v, indent, nested, level + 1)
  131. dumped_indexes[i] = true
  132. end
  133. for k, v in pairs(o) do
  134. if not dumped_indexes[k] then
  135. if type(k) ~= "string" or not is_valid_identifier(k) then
  136. k = "["..dump(k, indent, nested, level + 1).."]"
  137. end
  138. v = dump(v, indent, nested, level + 1)
  139. t[#t + 1] = k.." = "..v
  140. end
  141. end
  142. nested[o] = nil
  143. if indent ~= "" then
  144. local indent_str = "\n"..string.rep(indent, level)
  145. local end_indent_str = "\n"..string.rep(indent, level - 1)
  146. return string.format("{%s%s%s}",
  147. indent_str,
  148. table.concat(t, ","..indent_str),
  149. end_indent_str)
  150. end
  151. return "{"..table.concat(t, ", ").."}"
  152. end
  153. --------------------------------------------------------------------------------
  154. function string.split(str, delim, include_empty, max_splits, sep_is_pattern)
  155. delim = delim or ","
  156. max_splits = max_splits or -1
  157. local items = {}
  158. local pos, len, seplen = 1, #str, #delim
  159. local plain = not sep_is_pattern
  160. max_splits = max_splits + 1
  161. repeat
  162. local np, npe = string_find(str, delim, pos, plain)
  163. np, npe = (np or (len+1)), (npe or (len+1))
  164. if (not np) or (max_splits == 1) then
  165. np = len + 1
  166. npe = np
  167. end
  168. local s = string_sub(str, pos, np - 1)
  169. if include_empty or (s ~= "") then
  170. max_splits = max_splits - 1
  171. items[#items + 1] = s
  172. end
  173. pos = npe + 1
  174. until (max_splits == 0) or (pos > (len + 1))
  175. return items
  176. end
  177. --------------------------------------------------------------------------------
  178. function table.indexof(list, val)
  179. for i, v in ipairs(list) do
  180. if v == val then
  181. return i
  182. end
  183. end
  184. return -1
  185. end
  186. assert(table.indexof({"foo", "bar"}, "foo") == 1)
  187. assert(table.indexof({"foo", "bar"}, "baz") == -1)
  188. --------------------------------------------------------------------------------
  189. if INIT ~= "client" then
  190. function file_exists(filename)
  191. local f = io.open(filename, "r")
  192. if f == nil then
  193. return false
  194. else
  195. f:close()
  196. return true
  197. end
  198. end
  199. end
  200. --------------------------------------------------------------------------------
  201. function string:trim()
  202. return (self:gsub("^%s*(.-)%s*$", "%1"))
  203. end
  204. assert(string.trim("\n \t\tfoo bar\t ") == "foo bar")
  205. --------------------------------------------------------------------------------
  206. function math.hypot(x, y)
  207. local t
  208. x = math.abs(x)
  209. y = math.abs(y)
  210. t = math.min(x, y)
  211. x = math.max(x, y)
  212. if x == 0 then return 0 end
  213. t = t / x
  214. return x * math.sqrt(1 + t * t)
  215. end
  216. --------------------------------------------------------------------------------
  217. function math.sign(x, tolerance)
  218. tolerance = tolerance or 0
  219. if x > tolerance then
  220. return 1
  221. elseif x < -tolerance then
  222. return -1
  223. end
  224. return 0
  225. end
  226. --------------------------------------------------------------------------------
  227. function get_last_folder(text,count)
  228. local parts = text:split(DIR_DELIM)
  229. if count == nil then
  230. return parts[#parts]
  231. end
  232. local retval = ""
  233. for i=1,count,1 do
  234. retval = retval .. parts[#parts - (count-i)] .. DIR_DELIM
  235. end
  236. return retval
  237. end
  238. --------------------------------------------------------------------------------
  239. function cleanup_path(temppath)
  240. local parts = temppath:split("-")
  241. temppath = ""
  242. for i=1,#parts,1 do
  243. if temppath ~= "" then
  244. temppath = temppath .. "_"
  245. end
  246. temppath = temppath .. parts[i]
  247. end
  248. parts = temppath:split(".")
  249. temppath = ""
  250. for i=1,#parts,1 do
  251. if temppath ~= "" then
  252. temppath = temppath .. "_"
  253. end
  254. temppath = temppath .. parts[i]
  255. end
  256. parts = temppath:split("'")
  257. temppath = ""
  258. for i=1,#parts,1 do
  259. if temppath ~= "" then
  260. temppath = temppath .. ""
  261. end
  262. temppath = temppath .. parts[i]
  263. end
  264. parts = temppath:split(" ")
  265. temppath = ""
  266. for i=1,#parts,1 do
  267. if temppath ~= "" then
  268. temppath = temppath
  269. end
  270. temppath = temppath .. parts[i]
  271. end
  272. return temppath
  273. end
  274. function core.formspec_escape(text)
  275. if text ~= nil then
  276. text = string.gsub(text,"\\","\\\\")
  277. text = string.gsub(text,"%]","\\]")
  278. text = string.gsub(text,"%[","\\[")
  279. text = string.gsub(text,";","\\;")
  280. text = string.gsub(text,",","\\,")
  281. end
  282. return text
  283. end
  284. function core.wrap_text(text, max_length, as_table)
  285. local result = {}
  286. local line = {}
  287. if #text <= max_length then
  288. return as_table and {text} or text
  289. end
  290. for word in text:gmatch('%S+') do
  291. local cur_length = #table.concat(line, ' ')
  292. if cur_length > 0 and cur_length + #word + 1 >= max_length then
  293. -- word wouldn't fit on current line, move to next line
  294. table.insert(result, table.concat(line, ' '))
  295. line = {}
  296. end
  297. table.insert(line, word)
  298. end
  299. table.insert(result, table.concat(line, ' '))
  300. return as_table and result or table.concat(result, '\n')
  301. end
  302. --------------------------------------------------------------------------------
  303. if INIT == "game" then
  304. local dirs1 = {9, 18, 7, 12}
  305. local dirs2 = {20, 23, 22, 21}
  306. function core.rotate_and_place(itemstack, placer, pointed_thing,
  307. infinitestacks, orient_flags)
  308. orient_flags = orient_flags or {}
  309. local unode = core.get_node_or_nil(pointed_thing.under)
  310. if not unode then
  311. return
  312. end
  313. local undef = core.registered_nodes[unode.name]
  314. if undef and undef.on_rightclick then
  315. return undef.on_rightclick(pointed_thing.under, unode, placer,
  316. itemstack, pointed_thing)
  317. end
  318. local fdir = placer and core.dir_to_facedir(placer:get_look_dir()) or 0
  319. local above = pointed_thing.above
  320. local under = pointed_thing.under
  321. local iswall = (above.y == under.y)
  322. local isceiling = not iswall and (above.y < under.y)
  323. if undef and undef.buildable_to then
  324. iswall = false
  325. end
  326. if orient_flags.force_floor then
  327. iswall = false
  328. isceiling = false
  329. elseif orient_flags.force_ceiling then
  330. iswall = false
  331. isceiling = true
  332. elseif orient_flags.force_wall then
  333. iswall = true
  334. isceiling = false
  335. elseif orient_flags.invert_wall then
  336. iswall = not iswall
  337. end
  338. local param2 = fdir
  339. if iswall then
  340. param2 = dirs1[fdir + 1]
  341. elseif isceiling then
  342. if orient_flags.force_facedir then
  343. cparam2 = 20
  344. else
  345. param2 = dirs2[fdir + 1]
  346. end
  347. else -- place right side up
  348. if orient_flags.force_facedir then
  349. param2 = 0
  350. end
  351. end
  352. local old_itemstack = ItemStack(itemstack)
  353. local new_itemstack, removed = core.item_place_node(
  354. itemstack, placer, pointed_thing, param2
  355. )
  356. return infinitestacks and old_itemstack or new_itemstack
  357. end
  358. --------------------------------------------------------------------------------
  359. --Wrapper for rotate_and_place() to check for sneak and assume Creative mode
  360. --implies infinite stacks when performing a 6d rotation.
  361. --------------------------------------------------------------------------------
  362. local creative_mode_cache = core.settings:get_bool("creative_mode")
  363. local function is_creative(name)
  364. return creative_mode_cache or
  365. core.check_player_privs(name, {creative = true})
  366. end
  367. core.rotate_node = function(itemstack, placer, pointed_thing)
  368. local name = placer and placer:get_player_name() or ""
  369. local invert_wall = placer and placer:get_player_control().sneak or false
  370. core.rotate_and_place(itemstack, placer, pointed_thing,
  371. is_creative(name),
  372. {invert_wall = invert_wall})
  373. return itemstack
  374. end
  375. end
  376. --------------------------------------------------------------------------------
  377. function core.explode_table_event(evt)
  378. if evt ~= nil then
  379. local parts = evt:split(":")
  380. if #parts == 3 then
  381. local t = parts[1]:trim()
  382. local r = tonumber(parts[2]:trim())
  383. local c = tonumber(parts[3]:trim())
  384. if type(r) == "number" and type(c) == "number"
  385. and t ~= "INV" then
  386. return {type=t, row=r, column=c}
  387. end
  388. end
  389. end
  390. return {type="INV", row=0, column=0}
  391. end
  392. --------------------------------------------------------------------------------
  393. function core.explode_textlist_event(evt)
  394. if evt ~= nil then
  395. local parts = evt:split(":")
  396. if #parts == 2 then
  397. local t = parts[1]:trim()
  398. local r = tonumber(parts[2]:trim())
  399. if type(r) == "number" and t ~= "INV" then
  400. return {type=t, index=r}
  401. end
  402. end
  403. end
  404. return {type="INV", index=0}
  405. end
  406. --------------------------------------------------------------------------------
  407. function core.explode_scrollbar_event(evt)
  408. local retval = core.explode_textlist_event(evt)
  409. retval.value = retval.index
  410. retval.index = nil
  411. return retval
  412. end
  413. --------------------------------------------------------------------------------
  414. function core.rgba(r, g, b, a)
  415. return a and string.format("#%02X%02X%02X%02X", r, g, b, a) or
  416. string.format("#%02X%02X%02X", r, g, b)
  417. end
  418. --------------------------------------------------------------------------------
  419. function core.pos_to_string(pos, decimal_places)
  420. local x = pos.x
  421. local y = pos.y
  422. local z = pos.z
  423. if decimal_places ~= nil then
  424. x = string.format("%." .. decimal_places .. "f", x)
  425. y = string.format("%." .. decimal_places .. "f", y)
  426. z = string.format("%." .. decimal_places .. "f", z)
  427. end
  428. return "(" .. x .. "," .. y .. "," .. z .. ")"
  429. end
  430. --------------------------------------------------------------------------------
  431. function core.string_to_pos(value)
  432. if value == nil then
  433. return nil
  434. end
  435. local p = {}
  436. p.x, p.y, p.z = string.match(value, "^([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
  437. if p.x and p.y and p.z then
  438. p.x = tonumber(p.x)
  439. p.y = tonumber(p.y)
  440. p.z = tonumber(p.z)
  441. return p
  442. end
  443. local p = {}
  444. p.x, p.y, p.z = string.match(value, "^%( *([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+) *%)$")
  445. if p.x and p.y and p.z then
  446. p.x = tonumber(p.x)
  447. p.y = tonumber(p.y)
  448. p.z = tonumber(p.z)
  449. return p
  450. end
  451. return nil
  452. end
  453. assert(core.string_to_pos("10.0, 5, -2").x == 10)
  454. assert(core.string_to_pos("( 10.0, 5, -2)").z == -2)
  455. assert(core.string_to_pos("asd, 5, -2)") == nil)
  456. --------------------------------------------------------------------------------
  457. function core.string_to_area(value)
  458. local p1, p2 = unpack(value:split(") ("))
  459. if p1 == nil or p2 == nil then
  460. return nil
  461. end
  462. p1 = core.string_to_pos(p1 .. ")")
  463. p2 = core.string_to_pos("(" .. p2)
  464. if p1 == nil or p2 == nil then
  465. return nil
  466. end
  467. return p1, p2
  468. end
  469. local function test_string_to_area()
  470. local p1, p2 = core.string_to_area("(10.0, 5, -2) ( 30.2, 4, -12.53)")
  471. assert(p1.x == 10.0 and p1.y == 5 and p1.z == -2)
  472. assert(p2.x == 30.2 and p2.y == 4 and p2.z == -12.53)
  473. p1, p2 = core.string_to_area("(10.0, 5, -2 30.2, 4, -12.53")
  474. assert(p1 == nil and p2 == nil)
  475. p1, p2 = core.string_to_area("(10.0, 5,) -2 fgdf2, 4, -12.53")
  476. assert(p1 == nil and p2 == nil)
  477. end
  478. test_string_to_area()
  479. --------------------------------------------------------------------------------
  480. function table.copy(t, seen)
  481. local n = {}
  482. seen = seen or {}
  483. seen[t] = n
  484. for k, v in pairs(t) do
  485. n[(type(k) == "table" and (seen[k] or table.copy(k, seen))) or k] =
  486. (type(v) == "table" and (seen[v] or table.copy(v, seen))) or v
  487. end
  488. return n
  489. end
  490. --------------------------------------------------------------------------------
  491. -- mainmenu only functions
  492. --------------------------------------------------------------------------------
  493. if INIT == "mainmenu" then
  494. function core.get_game(index)
  495. local games = game.get_games()
  496. if index > 0 and index <= #games then
  497. return games[index]
  498. end
  499. return nil
  500. end
  501. end
  502. if INIT == "client" or INIT == "mainmenu" then
  503. function fgettext_ne(text, ...)
  504. text = core.gettext(text)
  505. local arg = {n=select('#', ...), ...}
  506. if arg.n >= 1 then
  507. -- Insert positional parameters ($1, $2, ...)
  508. local result = ''
  509. local pos = 1
  510. while pos <= text:len() do
  511. local newpos = text:find('[$]', pos)
  512. if newpos == nil then
  513. result = result .. text:sub(pos)
  514. pos = text:len() + 1
  515. else
  516. local paramindex =
  517. tonumber(text:sub(newpos+1, newpos+1))
  518. result = result .. text:sub(pos, newpos-1)
  519. .. tostring(arg[paramindex])
  520. pos = newpos + 2
  521. end
  522. end
  523. text = result
  524. end
  525. return text
  526. end
  527. function fgettext(text, ...)
  528. return core.formspec_escape(fgettext_ne(text, ...))
  529. end
  530. end
  531. local ESCAPE_CHAR = string.char(0x1b)
  532. function core.get_color_escape_sequence(color)
  533. return ESCAPE_CHAR .. "(c@" .. color .. ")"
  534. end
  535. function core.get_background_escape_sequence(color)
  536. return ESCAPE_CHAR .. "(b@" .. color .. ")"
  537. end
  538. function core.colorize(color, message)
  539. local lines = tostring(message):split("\n", true)
  540. local color_code = core.get_color_escape_sequence(color)
  541. for i, line in ipairs(lines) do
  542. lines[i] = color_code .. line
  543. end
  544. return table.concat(lines, "\n") .. core.get_color_escape_sequence("#ffffff")
  545. end
  546. function core.strip_foreground_colors(str)
  547. return (str:gsub(ESCAPE_CHAR .. "%(c@[^)]+%)", ""))
  548. end
  549. function core.strip_background_colors(str)
  550. return (str:gsub(ESCAPE_CHAR .. "%(b@[^)]+%)", ""))
  551. end
  552. function core.strip_colors(str)
  553. return (str:gsub(ESCAPE_CHAR .. "%([bc]@[^)]+%)", ""))
  554. end
  555. function core.translate(textdomain, str, ...)
  556. local start_seq
  557. if textdomain == "" then
  558. start_seq = ESCAPE_CHAR .. "T"
  559. else
  560. start_seq = ESCAPE_CHAR .. "(T@" .. textdomain .. ")"
  561. end
  562. local arg = {n=select('#', ...), ...}
  563. local end_seq = ESCAPE_CHAR .. "E"
  564. local arg_index = 1
  565. local translated = str:gsub("@(.)", function(matched)
  566. local c = string.byte(matched)
  567. if string.byte("1") <= c and c <= string.byte("9") then
  568. local a = c - string.byte("0")
  569. if a ~= arg_index then
  570. error("Escape sequences in string given to core.translate " ..
  571. "are not in the correct order: got @" .. matched ..
  572. "but expected @" .. tostring(arg_index))
  573. end
  574. if a > arg.n then
  575. error("Not enough arguments provided to core.translate")
  576. end
  577. arg_index = arg_index + 1
  578. return ESCAPE_CHAR .. "F" .. arg[a] .. ESCAPE_CHAR .. "E"
  579. elseif matched == "n" then
  580. return "\n"
  581. else
  582. return matched
  583. end
  584. end)
  585. if arg_index < arg.n + 1 then
  586. error("Too many arguments provided to core.translate")
  587. end
  588. return start_seq .. translated .. end_seq
  589. end
  590. function core.get_translator(textdomain)
  591. return function(str, ...) return core.translate(textdomain or "", str, ...) end
  592. end
  593. --------------------------------------------------------------------------------
  594. -- Returns the exact coordinate of a pointed surface
  595. --------------------------------------------------------------------------------
  596. function core.pointed_thing_to_face_pos(placer, pointed_thing)
  597. local eye_offset_first = placer:get_eye_offset()
  598. local node_pos = pointed_thing.under
  599. local camera_pos = placer:get_pos()
  600. local pos_off = vector.multiply(
  601. vector.subtract(pointed_thing.above, node_pos), 0.5)
  602. local look_dir = placer:get_look_dir()
  603. local offset, nc
  604. local oc = {}
  605. for c, v in pairs(pos_off) do
  606. if nc or v == 0 then
  607. oc[#oc + 1] = c
  608. else
  609. offset = v
  610. nc = c
  611. end
  612. end
  613. local fine_pos = {[nc] = node_pos[nc] + offset}
  614. camera_pos.y = camera_pos.y + 1.625 + eye_offset_first.y / 10
  615. local f = (node_pos[nc] + offset - camera_pos[nc]) / look_dir[nc]
  616. for i = 1, #oc do
  617. fine_pos[oc[i]] = camera_pos[oc[i]] + look_dir[oc[i]] * f
  618. end
  619. return fine_pos
  620. end
  621. function core.string_to_privs(str, delim)
  622. assert(type(str) == "string")
  623. delim = delim or ','
  624. local privs = {}
  625. for _, priv in pairs(string.split(str, delim)) do
  626. privs[priv:trim()] = true
  627. end
  628. return privs
  629. end
  630. function core.privs_to_string(privs, delim)
  631. assert(type(privs) == "table")
  632. delim = delim or ','
  633. local list = {}
  634. for priv, bool in pairs(privs) do
  635. if bool then
  636. list[#list + 1] = priv
  637. end
  638. end
  639. return table.concat(list, delim)
  640. end
  641. assert(core.string_to_privs("a,b").b == true)
  642. assert(core.privs_to_string({a=true,b=true}) == "a,b")