Skip to content

Commit 495bf30

Browse files
fix: return module (#3)
* fix: return module Previously loading this module would set `pack` and `unpack` into the global `string` library. Since these methods now exist (since Lua 5.3), other Lua modules will use these functions if they exist, assuming that they are the implemenations in Lua 5.3. More generally, modern best practice is to return the module when loading instead of setting anything globally. Obviously this is a breaking change. * bump version to 2.0.0 * Remove Makefile Use `luarocks build` or `luarocks make` instead. The "builtin" process is much better at providing the appropriate CFLAGS. * Fix test Still not a real test, since it doesn't show failure, but at least now it runs to completion. * Update README.md Since it no longer pollutes the `string` table, it's just a normal module. Also use a bit of formatting. Co-authored-by: Javier Guerra <javier@guerrag.com>
1 parent a6be401 commit 495bf30

File tree

5 files changed

+35
-59
lines changed

5 files changed

+35
-59
lines changed

Makefile

Lines changed: 0 additions & 28 deletions
This file was deleted.

README.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
This library is extended from `lpack` library on Luarocks to support Hexdecimal data too.
22

33
This is a simple Lua library for packing and unpacking binary data.
4-
The library adds two functions to the string library: pack and unpack.
4+
The library provides two functions: `pack` and `unpack`.
55

6-
pack is called as follows: pack(F,x1,x2,...), where F is a string describing
7-
how the values x1, x2, ... are to be interpreted and formatted. Each letter
8-
in the format string F consumes one of the given values. Only values of type
9-
number or string are accepted. pack returns a (binary) string containing the
10-
values packed as described in F. The letter codes understood by pack are listed
6+
`pack` is called as follows: `pack(F,x1,x2,...)`, where `F` is a string describing
7+
how the values `x1`, `x2`, ... are to be interpreted and formatted. Each letter
8+
in the format string `F` consumes one of the given values. Only values of type
9+
number or string are accepted. `pack` returns a (binary) string containing the
10+
values packed as described in `F`. The letter codes understood by pack are listed
1111
in lpack.c (they are inspired by Perl's codes but are not the same). Numbers
12-
following letter codes in F indicate repetitions.
12+
following letter codes in `F` indicate repetitions.
1313

14-
unpack is called as follows: unpack(s,F,[init]), where s is a (binary) string
15-
containing data packed as if by pack, F is a format string describing what is
16-
to be read from s, and the optional init marks where in s to begin reading the
17-
values. unpack returns one value per letter in F until F or s is exhausted
18-
(the letters codes are the same as for pack, except that numbers following `A'
14+
`unpack` is called as follows: `unpack(s,F,[init])`, where `s` is a (binary) string
15+
containing data packed as if by `pack`, `F` is a format string describing what is
16+
to be read from `s`, and the optional `init` marks where in `s` to begin reading the
17+
values. `unpack` returns one value per letter in `F` until `F` or `s` is exhausted
18+
(the letters codes are the same as for `pack`, except that numbers following ``A'`
1919
are interpreted as the number of characters to read into the string, not as
2020
repetitions).
2121

22-
The first value returned by unpack is the next unread position in s, which can
23-
be used as the init position in a subsequent call to unpack. This allows you to
24-
unpack values in a loop or in several steps. If the position returned by unpack
25-
is beyond the end of s, then s has been exhausted; any calls to unpack starting
26-
beyond the end of s will always return nil values.
22+
The first value returned by `unpack` is the next unread position in `s`, which can
23+
be used as the init position in a subsequent call to `unpack`. This allows you to
24+
unpack values in a loop or in several steps. If the position returned by `unpack`
25+
is beyond the end of `s`, then `s` has been exhausted; any calls to `unpack` starting
26+
beyond the end of `s` will always return `nil` values.
2727

2828
-------------------------------------------------------------------------------
2929

30+
```
3031
pack library:
3132
pack(f,...) unpack(s,f,[init])
33+
```
3234

3335
-------------------------------------------------------------------------------
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package="lua_pack"
2-
version="1.0.5-0"
2+
version="2.0.0-0"
33
source = {
44
url = "git://github.com/mashape/lua-pack",
5-
tag = "1.0.5"
5+
tag = "2.0.0"
66
}
77
description = {
88
summary = "This is a simple Lua library for packing and unpacking binary data",

lua_pack.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -384,11 +384,13 @@ static const luaL_Reg R[] =
384384

385385
int luaopen_lua_pack(lua_State *L)
386386
{
387-
#ifdef USE_GLOBALS
388-
lua_register(L,"bpack",l_pack);
389-
lua_register(L,"bunpack",l_unpack);
390-
#else
391-
luaL_openlib(L, LUA_STRLIBNAME, R, 0);
392-
#endif
393-
return 0;
387+
lua_newtable(L);
388+
389+
lua_pushcfunction(L, &l_pack);
390+
lua_setfield(L, -2, "pack");
391+
392+
lua_pushcfunction(L, &l_unpack);
393+
lua_setfield(L, -2, "unpack");
394+
395+
return 1;
394396
}

test.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
require"pack"
1+
local pack = require"lua_pack"
22

3-
bpack=string.pack
4-
bunpack=string.unpack
3+
local bpack = pack.pack
4+
local bunpack = pack.unpack
55

66
function hex(s)
77
s=string.gsub(s,"(.)",function (x) return string.format("%02X",string.byte(x)) end)
88
return s
99
end
1010

11-
a=bpack("Ab8","\027Lua",5*16+1,0,1,4,4,4,8,0)
11+
a=bpack("AC8","\027Lua",5*16+1,0,1,4,4,4,8,0)
1212
print(hex(a),string.len(a))
1313

1414
b=string.dump(hex)
1515
b=string.sub(b,1,string.len(a))
1616
print(a==b,string.len(b))
17-
print(bunpack(b,"bA3b8"))
17+
print(bunpack(b,"CA3C8"))
1818

1919
i=314159265 f="<I>I=I"
2020
a=bpack(f,i,i,i)

0 commit comments

Comments
 (0)