netstring
DJB 的 netstring 在 lua 中的实现
$ opm get jprjr/netstring
netstring.lua
DJB 的 netstring 编码格式在 Lua/LuaJIT 中的实现。
概要
local netstring = require'netstring'
print(netstring.encode(1,"hey","here's","some","netstrings",""))
-- 1:1,3:hey,6:here's,4:some,10:netstrings,0:, nil
-- you can also encode a table, provided it's like an array
-- keys are sorted numerically before encoding into netstrings
print(netstring.encode({ [50]="is", "this", [100]="supported" }))
-- 4:this,2:is,9:supported, nil
-- if the table has non-numeric keys, you'll get a table of errors
-- as the second return value. numeric keys are still encoded
local ns, errs = netstring.encode({ "this is fine", problem="this is not fine", "this is also fine"})
print(ns)
-- 12:this is fine,17:this is also fine,
for i in pairs(errs) do
print(errs[i])
end
-- arg 1 key "problem" is not numeric
local results, remains = netstring.decode("3:cat,4:dogs,5:uhohthisiswrong,")
for k,v in pairs(results) do
print(k,v)
end
-- 1 cat
-- 2 dogs
print(remains)
-- 5:uhohthisiswrong,
用法
string, errs = netstring.encode(...)
接收任意数量的参数,并返回一个连接后的 netstring 字符串,以及一个描述编码错误的字符串表格。
table, string = netstring.decode(string)
尝试解码传递给它的每个 netstring,返回一个解码后的 netstring 表格,以及一个包含任何未处理数据的字符串。
许可证
MIT 许可证(参见 LICENSE)
作者
John Regan
许可证
mit
依赖
luajit
版本
-
jprjr/netstring 1.0.6DJB 的 netstring 在 lua 中的实现 2017-06-22 16:01:24
-
jprjr/netstring 1.0.5DJB 的 netstring 在 lua 中的实现 2017-06-22 15:22:29
-
jprjr/netstring 1.0.4DJB 的 netstring 在 lua 中的实现 2017-06-22 14:56:52
-
jprjr/netstring 1.0.3DJB 的 netstring 在 lua 中的实现 2017-02-02 19:38:06