使用UTF8字符的Lua string.format

Mar*_*rio 7 lua utf-8

如何使用 string.format 和包含 UTF-8 字符的字符串获得“正确”格式?

\n\n

例子:

\n\n
local str = "\\xE2\\x88\\x9E"\nprint(utf8.len(str), string.len(str))\nprint(str)\nprint(string.format("###%-5s###", str))\nprint(string.format("###%-5s###", \'x\'))\n
Run Code Online (Sandbox Code Playgroud)\n\n

输出:

\n\n
1   3\n\xe2\x88\x9e\n###\xe2\x88\x9e  ###\n###x    ###\n
Run Code Online (Sandbox Code Playgroud)\n\n

它看起来像string.format使用无穷大符号的字节长度而不是“字符长度”。\n是否有等效的 UTF-8 string.format?

\n

Ego*_*off 4

function utf8.format(fmt, ...)\n   local args, strings, pos = {...}, {}, 0\n   for spec in fmt:gmatch\'%%.-([%a%%])\' do\n      pos = pos + 1\n      local s = args[pos]\n      if spec == \'s\' and type(s) == \'string\' and s ~= \'\' then\n         table.insert(strings, s)\n         args[pos] = \'\\1\'..(\'\\2\'):rep(utf8.len(s)-1)\n      end\n   end\n   return (\n      fmt:format(table.unpack(args))\n         :gsub(\'\\1\\2*\', function() return table.remove(strings, 1) end)\n   )\nend\n\nlocal str = "\\xE2\\x88\\x9E"\nprint(string.format("###%-5s###", str))  --> ###\xe2\x88\x9e  ###\nprint(string.format("###%-5s###", \'x\'))  --> ###x    ###\nprint(utf8.format  ("###%-5s###", str))  --> ###\xe2\x88\x9e    ###\nprint(utf8.format  ("###%-5s###", \'x\'))  --> ###x    ###\n
Run Code Online (Sandbox Code Playgroud)\n