https://github.com/jprjr/netstring.lua
An implementation of DJB's netstring encoding format for Lua/LuaJIT.
https://github.com/jprjr/netstring.lua
Last synced: 3 months ago
JSON representation
An implementation of DJB's netstring encoding format for Lua/LuaJIT.
- Host: GitHub
- URL: https://github.com/jprjr/netstring.lua
- Owner: jprjr
- License: mit
- Created: 2016-08-11T16:01:01.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-06-22T16:02:15.000Z (about 8 years ago)
- Last Synced: 2025-03-25T05:41:39.939Z (3 months ago)
- Language: Lua
- Homepage:
- Size: 7.81 KB
- Stars: 4
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# netstring.lua
An implementation of DJB's [netstring](https://cr.yp.to/proto/netstrings.txt)
encoding format for Lua/LuaJIT.## Synopsis
```lua
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 numericlocal results, remains = netstring.decode("3:cat,4:dogs,5:uhohthisiswrong,")
for k,v in pairs(results) do
print(k,v)
end
-- 1 cat
-- 2 dogsprint(remains)
-- 5:uhohthisiswrong,
```## Usage
* `string, errs = netstring.encode(...)`
Takes any number of arguments and returns a string of concatenated
netstrings, and a table of strings describing encoding errors.* `table, string = netstring.decode(string)`
Attempts to decode each netstring passed to it, returns a table
of decoded netstrings, and a string containing any unprocessed data.## License
The MIT License (see LICENSE)