Compare commits

...

21 Commits

Author SHA1 Message Date
Egor
3c729ec869
fix: lua export (#19)
* fix: lua export
2025-01-19 19:12:55 +01:00
paulobaggio
5452834c16
fix: lua-iconv test at Rockylinux 9 fail using lua 5.1 version (#15) 2024-11-13 21:31:59 +01:00
Thijs Schreijer
dbdd9d9ab8 release 7.1.0
Updates the dev rockspec to pin to a tag, such that new rockspecs
generated from it will not point to `master` but to the version
tag.
2023-10-30 15:56:22 +01:00
Thijs Schreijer
9eced53a8d fix(open): return an error on open/new
The method would not return an error message, now it does,
typically if a conversion is not supported.
2023-10-30 14:49:22 +01:00
Thijs Schreijer
1e6d024dc2 chore(dev): add a 'dev' rockspec to build from the source tree 2023-10-30 14:49:22 +01:00
Alexey Melnichuk
11545d0533
fix: Enable compiling with MSVC (#4) 2023-10-17 08:54:21 +03:00
Caleb Maclennan
9e865ff345
Bump rockrel high enough luarocks considers v7.0.0 newer than v7 2023-10-17 08:39:15 +03:00
Caleb Maclennan
8e19fde5f3
Use source URL format such that LuaRocks can infer source.dir from source.url 2023-10-07 22:23:34 +03:00
Matthieu Coudron
e4d200c102
Merge pull request #11 from lunarmodules/fix-rockspec 2023-10-07 20:57:28 +02:00
Matthieu Coudron
ba4f3a4518 build: added 6.1 rockspec 2023-10-07 14:52:13 +02:00
Matthieu Coudron
ac7f962b31 fix rockspec url 2023-10-07 00:41:15 +02:00
Caleb Maclennan
80e3266ced
Merge pull request #9 from lunarmodules/retool 2023-10-07 00:42:33 +03:00
Caleb Maclennan
51c1d732ed
chore: Fix lint issues, mostly with proper variable scopes 2023-10-05 20:48:51 +03:00
Caleb Maclennan
939fc2901b
docs: Format README as Markdown 2023-10-04 23:41:10 +03:00
Caleb Maclennan
c2ee26e1ef
ci: Add lunarmodules lint, test, and deploy workflows 2023-10-04 23:33:53 +03:00
Jonathan Ringer
b761918588
Update ittner homepage 2021-12-28 11:12:49 -08:00
Alexandre Erwin Ittner
2205a0fe27 Some code style changes and typo fixes 2013-10-23 00:12:24 -02:00
Alexandre Erwin Ittner
d41e0d345d Optimize memory handling in conversion function
Handling memory in this function is almost a Catch-22 between too many
mallocs and frees fragmenting memory in systems with bad allocators and
string concatenations abusing the Lua garbage collector. To avoid this,
now it uses a static buffer in the stack and a luaL_Buffer to handle
the concatenations. This remove all direct calls to memory allocators
from the library and lets Lua use its own.
2013-10-23 00:08:28 -02:00
Alexandre Erwin Ittner
49eb7b534f Add two new test cases 2012-05-22 00:58:48 -03:00
Alexandre Erwin Ittner
6f265ded5d Fix invalid error report
cd:iconv returned an invalid ERROR_UNKNOW code instead of 'nil' on
successful conversions. This usually passes unnoticed since the return
string is what the user actually expects.
2012-05-22 00:39:41 -03:00
Alexandre Erwin Ittner
1876a6de19 Add revision number to the rockspec
Thanks Hisham.
2012-05-20 23:24:48 -03:00
15 changed files with 579 additions and 188 deletions

34
.github/workflows/deploy.yml vendored Normal file
View File

@ -0,0 +1,34 @@
name: Deploy
on: [ push, workflow_dispatch ]
jobs:
affected:
uses: lunarmodules/.github/.github/workflows/list_affected_rockspecs.yml@main
build:
needs: affected
if: ${{ needs.affected.outputs.rockspecs }}
uses: lunarmodules/.github/.github/workflows/test_build_rock.yml@main
with:
rockspecs: ${{ needs.affected.outputs.rockspecs }}
upload:
needs: [ affected, build ]
# Only run upload if:
# 1. We are on the canonical repository (no uploads from forks)
# 2. The current commit is either tagged or on the default branch (the workflow will upload dev/scm rockspecs any
# time they are touched, tagged ones whenever the edited rockspec and tag match)
# 3. Some rockspecs were changed — this implies the commit changing the rockspec is the same one that gets tagged
if: >-
${{
github.repository == 'lunarmodules/lua-iconv' &&
( github.ref_name == 'master' || startsWith(github.ref, 'refs/tags/') ) &&
needs.affected.outputs.rockspecs
}}
uses: lunarmodules/.github/.github/workflows/upload_to_luarocks.yml@main
with:
rockspecs: ${{ needs.affected.outputs.rockspecs }}
secrets:
apikey: ${{ secrets.LUAROCKS_APIKEY }}

13
.github/workflows/luacheck.yml vendored Normal file
View File

@ -0,0 +1,13 @@
name: Luacheck
on: [push, pull_request]
jobs:
luacheck:
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Luacheck
uses: lunarmodules/luacheck@v1

13
CHANGELOG.md Normal file
View File

@ -0,0 +1,13 @@
# Changelog
### 7.1.0 released 30-Oct-2023
* fix: rockspecs for MacOS
* feat: `iconv.new` now returns a second value in case of errors
---
### 7.0.0 released 17-Oct-2023
* First release from its new Lunar Modules home

View File

@ -68,5 +68,5 @@ clean:
$(RM) iconv.so iconv.lo $(RM) iconv.so iconv.lo
test: iconv.so test_iconv.lua test: iconv.so test_iconv.lua
lua test_iconv.lua $(LUABIN) test_iconv.lua

129
README
View File

@ -1,129 +0,0 @@
Lua-iconv: performs character set conversions in Lua
(c) 2005-11 Alexandre Erwin Ittner <alexandre@ittner.com.br>
Project page: http://ittner.github.com/lua-iconv/
=== Introduction ===
Lua-iconv is POSIX 'iconv' binding for the Lua Programming Language. The
iconv library converts a sequence of characters from one codeset into a
sequence of corresponding characters in another codeset. The codesets
are those specified in the iconv.new() call that returned the conversion
descriptor, cd.
Lua-iconv 7 *requires* Lua 5.1 or Lua 5.2. For Lua 5.0, use the first
release (lua-iconv-r1).
Details on iconv may be obtained in the Open Group's interface definition
(http://www.opengroup.org/onlinepubs/007908799/xsh/iconv.h.html).
=== Download and installation ===
Lua-iconv can be obtained from its GitHub project page
(https://github.com/ittner/lua-iconv/downloads), from a LuaRocks server or
from some Linux distributions which already provide it (eg. Debian).
Unless you downloaded a compiled package, you must build the library for
your system. If you have LuaRocks installed, all the process is automatic;
just fire up your favourite shell and type, as root:
luarocks install lua-iconv
and the package will be downloaded from a rock server, installed and
configured. Otherwise, you must compile and install the package. In a system
with pkg-config (as many Linux distributions and Unix flavors) open a shell,
untar the distribution package and, within the program directory, type:
make install
as root. The library will be compiled and installed on the in the correct
path (which is defined in lua5.x.pc). Compiling on systems without pkg-config
requires manual changes in the Makefile (this includes Windows).
=== Loading and initialization ===
Lua-iconv is a shared library that must be loaded in the Lua interpreter
before use. You can simply do a
local iconv = require("iconv")
call to load up the library (that, of course, must be installed in a
directory from package.cpath).
=== API documentation ===
cd = iconv.new(to, from)
cd = iconv.open(to, from)
Opens a new conversion descriptor, from the 'from' charset to the
'to' charset. Concatenating "//TRANSLIT" to the first argument will
enable character transliteration and concatenating "//IGNORE" to
the first argument will cause iconv to ignore any invalid characters
found in the input string.
This function returns a new converter or nil on error.
nstr, err = cd:iconv(str)
Converts the 'str' string to the desired charset. This method always
returns two arguments: the converted string and an error code, which
may have any of the following values:
nil
No error. Conversion was successful.
iconv.ERROR_NO_MEMORY
Failed to allocate enough memory in the conversion process.
iconv.ERROR_INVALID
An invalid character was found in the input sequence.
iconv.ERROR_INCOMPLETE
An incomplete character was found in the input sequence.
iconv.ERROR_FINALIZED
Trying to use an already-finalized converter. This usually means
that the user was tweaking the garbage collector private methods.
iconv.ERROR_UNKNOWN
There was an unknown error.
=== License ===
Lua-iconv is copyrighted free software: it can be used for both academic
and commercial purposes at absolutely no cost. There are no royalties
or GNU-like "copyleft" restrictions. The legal details are below:
Lua-iconv is (c) 2005-11 Alexandre Erwin Ittner
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject
to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHOR OR COPYRIGHT HOLDER BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
If you use this package in a product, an acknowledgment in the product
documentation would be greatly appreciated (but it is not required).

145
README.md Normal file
View File

@ -0,0 +1,145 @@
# Lua-iconv
Perform character set conversions in Lua
(c) 2005-11 Alexandre Erwin Ittner <alexandre@ittner.com.br>
## Introduction
Lua-iconv provides POSIX 'iconv' bindings for the Lua Programming Language.
The iconv library converts a sequence of characters from one codeset into a sequence of corresponding characters in another codeset.
The codesets are those specified in the `iconv.new()` call that returned the conversion descriptor, `cd`.
Lua-iconv 7 *requires* Lua 5.1 or Lua 5.2. For Lua 5.0, use the first release (lua-iconv-r1).
Details on iconv may be obtained in the [Open Group's interface definition](http://www.opengroup.org/onlinepubs/007908799/xsh/iconv.h.html).
## Download and installation
Lua-iconv can be obtained from [its GitHub project page](https://github.com/lunarmodules/lua-iconv), from a LuaRocks server, or from some Linux distributions which already provide it (eg. Debian).
Unless you downloaded a compiled package, you must build the library for your system.
If you have LuaRocks installed, all the process is automatic; just fire up your favourite shell and type, as root:
```console
luarocks install lua-iconv
```
and the package will be downloaded from a rock server, installed and configured.
Otherwise, you must compile and install the package.
In a system with pkg-config (as many Linux distributions and Unix flavors) open a shell, untar the distribution package and, within the program directory, type:
```console
make install
```
as root.
The library will be compiled and installed on the in the correct path (which is defined in lua5.x.pc).
Compiling on systems without pkg-config requires manual changes in the Makefile (this includes Windows).
## Loading and initialization
Lua-iconv is a shared library that must be loaded in the Lua interpreter before use.
You can simply do a
```lua
local iconv = require("iconv")
```
call to load up the library (that, of course, must be installed in a directory from `package.cpath`).
## API documentation
```lua
cd, err = iconv.new(to, from)
cd, err = iconv.open(to, from)
```
Opens a new conversion descriptor, from the 'from' charset to the 'to' charset.
Concatenating "//TRANSLIT" to the first argument will enable character transliteration and concatenating "//IGNORE" to the first argument will cause iconv to ignore any invalid characters found in the input string.
The error code, may have any of the following values:
* `nil`
No error. Creation was successful.
* `iconv.ERROR_INVALID`
The conversion from `from` to `to` is not supported by the implementation.
* `iconv.ERROR_UNKNOWN`
There was an unknown error.
This function returns a new converter or nil+err on error.
```lua
nstr, err = cd:iconv(str)
```
Converts the 'str' string to the desired charset.
This method always returns two arguments: the converted string and an error code, which may have any of the following values:
* `nil`
No error. Conversion was successful.
* `iconv.ERROR_NO_MEMORY`
Failed to allocate enough memory in the conversion process.
* `iconv.ERROR_INVALID`
An invalid character was found in the input sequence.
* `iconv.ERROR_INCOMPLETE`
An incomplete character was found in the input sequence.
* `iconv.ERROR_FINALIZED`
Trying to use an already-finalized converter. This usually means
that the user was tweaking the garbage collector private methods.
* `iconv.ERROR_UNKNOWN`
There was an unknown error.
## License
Lua-iconv is copyrighted free software: it can be used for both academic
and commercial purposes at absolutely no cost. There are no royalties
or GNU-like "copyleft" restrictions. The legal details are below:
Lua-iconv is (c) 2005-11 Alexandre Erwin Ittner
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject
to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHOR OR COPYRIGHT HOLDER BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
If you use this package in a product, an acknowledgment in the product
documentation would be greatly appreciated (but it is not required).

62
lua-iconv-dev-1.rockspec Normal file
View File

@ -0,0 +1,62 @@
local package_name = "lua-iconv"
local package_version = "dev"
local rockspec_revision = "1"
local github_account_name = "lunarmodules"
local github_repo_name = package_name
package = package_name
version = package_version.."-"..rockspec_revision
source = {
url = "git+https://github.com/"..github_account_name.."/"..github_repo_name..".git",
branch = (package_version == "dev") and "master" or nil,
tag = (package_version ~= "dev") and ("v" .. package_version) or nil,
}
description = {
summary = "Lua binding to the iconv",
detailed = [[
Lua binding to the POSIX 'iconv' library, which converts a sequence of
characters from one codeset into a sequence of corresponding characters
in another codeset.
]],
license = "MIT/X11",
homepage = "https://github.com/"..github_account_name.."/"..github_repo_name,
}
dependencies = {
"lua >= 5.1",
}
external_dependencies = {
ICONV = {
header = "iconv.h"
}
}
build = {
type = "builtin",
modules = {
iconv = {
sources = {"luaiconv.c"},
incdirs = {"$(ICONV_INCDIR)"},
libdirs = {"$(ICONV_LIBDIR)"}
}
},
platforms = {
cygwin = {
modules = {
iconv = {
libraries = {"iconv"}
}
}
},
macosx = {
modules = {
iconv = {
libraries = {"iconv"}
}
}
},
}
}

View File

@ -39,8 +39,14 @@
#define LIB_VERSION LIB_NAME " 7" #define LIB_VERSION LIB_NAME " 7"
#define ICONV_TYPENAME "iconv_t" #define ICONV_TYPENAME "iconv_t"
#ifdef _WIN32
#define LUAEXPORT __declspec(dllexport)
#else
#define LUAEXPORT __attribute__((visibility("default")))
#endif
#if LUA_VERSION_NUM < 501 #if LUA_VERSION_NUM < 501
#error "Unsuported Lua version. You must use Lua >= 5.1" #error "Unsupported Lua version. You must use Lua >= 5.1"
#endif #endif
#if LUA_VERSION_NUM < 502 #if LUA_VERSION_NUM < 502
@ -67,14 +73,16 @@
static void push_iconv_t(lua_State *L, iconv_t cd) { static void push_iconv_t(lua_State *L, iconv_t cd)
{
BOXPTR(L, cd); BOXPTR(L, cd);
luaL_getmetatable(L, ICONV_TYPENAME); luaL_getmetatable(L, ICONV_TYPENAME);
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
} }
static iconv_t get_iconv_t(lua_State *L, int i) { static iconv_t get_iconv_t(lua_State *L, int i)
{
if (luaL_checkudata(L, i, ICONV_TYPENAME) != NULL) { if (luaL_checkudata(L, i, ICONV_TYPENAME) != NULL) {
iconv_t cd = UNBOXPTR(L, i); iconv_t cd = UNBOXPTR(L, i);
return cd; /* May be NULL. This must be checked by the caller. */ return cd; /* May be NULL. This must be checked by the caller. */
@ -85,29 +93,42 @@ static iconv_t get_iconv_t(lua_State *L, int i) {
} }
static int Liconv_open(lua_State *L) { static int Liconv_open(lua_State *L)
{
const char *tocode = luaL_checkstring(L, 1); const char *tocode = luaL_checkstring(L, 1);
const char *fromcode = luaL_checkstring(L, 2); const char *fromcode = luaL_checkstring(L, 2);
iconv_t cd = iconv_open(tocode, fromcode); iconv_t cd = iconv_open(tocode, fromcode);
if (cd != (iconv_t)(-1)) if (cd != (iconv_t)(-1)) {
push_iconv_t(L, cd); /* ok */ push_iconv_t(L, cd); /* ok */
else return 1;
} else {
lua_pushnil(L); /* error */ lua_pushnil(L); /* error */
return 1; if (errno == EINVAL)
lua_pushnumber(L, ERROR_INVALID);
else
lua_pushnumber(L, ERROR_UNKNOWN);
return 2;
};
} }
/* Use a fixed-size buffer in the stack to avoid a lot of small mallocs
* and prevent memory fragmentation. This should not be a problem in any
* contemporary general purpose system but, if you are running in a very
* limited stack system you may use a smaller buffer, but the luaL_Buffer
* will compensate this with more reallocs and memcpys.
*/
#define CONV_BUF_SIZE 256 #define CONV_BUF_SIZE 256
static int Liconv(lua_State *L) { static int Liconv(lua_State *L)
{
iconv_t cd = get_iconv_t(L, 1); iconv_t cd = get_iconv_t(L, 1);
size_t ibleft = lua_rawlen(L, 2); size_t ibleft = lua_rawlen(L, 2);
char *inbuf = (char*) luaL_checkstring(L, 2); char *inbuf = (char*) luaL_checkstring(L, 2);
char *outbuf; char outbufs[CONV_BUF_SIZE];
char *outbufs; char *outbuf = outbufs;
size_t obsize = (ibleft > CONV_BUF_SIZE) ? ibleft : CONV_BUF_SIZE; size_t obleft = CONV_BUF_SIZE;
size_t obleft = obsize;
size_t ret = -1; size_t ret = -1;
int hasone = 0; luaL_Buffer b;
if (cd == NULL) { if (cd == NULL) {
lua_pushstring(L, ""); lua_pushstring(L, "");
@ -115,52 +136,43 @@ static int Liconv(lua_State *L) {
return 2; return 2;
} }
outbuf = (char*) malloc(obsize * sizeof(char)); luaL_buffinit(L, &b);
if (outbuf == NULL) {
lua_pushstring(L, "");
lua_pushnumber(L, ERROR_NO_MEMORY);
return 2;
}
outbufs = outbuf;
do { do {
ret = iconv(cd, &inbuf, &ibleft, &outbuf, &obleft); ret = iconv(cd, &inbuf, &ibleft, &outbuf, &obleft);
if (ret == (size_t)(-1)) { if (ret == (size_t)(-1)) {
lua_pushlstring(L, outbufs, obsize - obleft); luaL_addlstring(&b, outbufs, CONV_BUF_SIZE - obleft);
if (hasone == 1)
lua_concat(L, 2);
hasone = 1;
if (errno == EILSEQ) { if (errno == EILSEQ) {
luaL_pushresult(&b);
lua_pushnumber(L, ERROR_INVALID); lua_pushnumber(L, ERROR_INVALID);
free(outbufs);
return 2; /* Invalid character sequence */ return 2; /* Invalid character sequence */
} else if (errno == EINVAL) { } else if (errno == EINVAL) {
luaL_pushresult(&b);
lua_pushnumber(L, ERROR_INCOMPLETE); lua_pushnumber(L, ERROR_INCOMPLETE);
free(outbufs);
return 2; /* Incomplete character sequence */ return 2; /* Incomplete character sequence */
} else if (errno == E2BIG) { } else if (errno == E2BIG) {
obleft = obsize; obleft = CONV_BUF_SIZE;
outbuf = outbufs; outbuf = outbufs;
} else { } else {
luaL_pushresult(&b);
lua_pushnumber(L, ERROR_UNKNOWN); lua_pushnumber(L, ERROR_UNKNOWN);
free(outbufs);
return 2; /* Unknown error */ return 2; /* Unknown error */
} }
} }
} while (ret != (size_t) 0); } while (ret == (size_t) -1);
lua_pushlstring(L, outbufs, obsize - obleft); luaL_addlstring(&b, outbufs, CONV_BUF_SIZE - obleft);
if (hasone == 1) luaL_pushresult(&b);
lua_concat(L, 2); lua_pushnil(L);
free(outbufs); return 2; /* Done */
return 1; /* Done */
} }
#ifdef HAS_ICONVLIST /* a GNU extension? */ #ifdef HAS_ICONVLIST /* a GNU extension? */
static int push_one(unsigned int cnt, char *names[], void *data) { static int push_one(unsigned int cnt, char *names[], void *data)
{
lua_State *L = (lua_State*) data; lua_State *L = (lua_State*) data;
int n = (int) lua_tonumber(L, -1); int n = (int) lua_tonumber(L, -1);
int i; int i;
@ -180,7 +192,8 @@ static int push_one(unsigned int cnt, char *names[], void *data) {
} }
static int Liconvlist(lua_State *L) { static int Liconvlist(lua_State *L)
{
lua_newtable(L); lua_newtable(L);
lua_pushnumber(L, 1); lua_pushnumber(L, 1);
@ -195,7 +208,8 @@ static int Liconvlist(lua_State *L) {
#endif #endif
static int Liconv_close(lua_State *L) { static int Liconv_close(lua_State *L)
{
iconv_t cd = get_iconv_t(L, 1); iconv_t cd = get_iconv_t(L, 1);
if (cd != NULL && iconv_close(cd) == 0) { if (cd != NULL && iconv_close(cd) == 0) {
/* Mark the pointer as freed, preventing interpreter crashes /* Mark the pointer as freed, preventing interpreter crashes
@ -205,7 +219,7 @@ static int Liconv_close(lua_State *L) {
lua_pushboolean(L, 1); /* ok */ lua_pushboolean(L, 1); /* ok */
} }
else else
lua_pushnil(L); /* error */ lua_pushnil(L); /* error, called from __gc, so no need for error messsage */
return 1; return 1;
} }
@ -221,7 +235,8 @@ static const luaL_Reg iconv_funcs[] = {
}; };
int luaopen_iconv(lua_State *L) { LUAEXPORT int luaopen_iconv(lua_State *L)
{
luaL_newlib(L, iconv_funcs); luaL_newlib(L, iconv_funcs);
TBL_SET_INT_CONST(L, ERROR_NO_MEMORY); TBL_SET_INT_CONST(L, ERROR_NO_MEMORY);

View File

@ -3,8 +3,6 @@ local fp = assert(io.open(arg[1], "rb"))
local str = assert(fp:read("*a")) local str = assert(fp:read("*a"))
fp:close() fp:close()
local i
local c = 0
local ostr = "local xxxxxxxxxxx = \"" local ostr = "local xxxxxxxxxxx = \""
for i = 1, string.len(str) do for i = 1, string.len(str) do
ostr = ostr .. "\\" .. string.byte(string.sub(str, i, i+1)) ostr = ostr .. "\\" .. string.byte(string.sub(str, i, i+1))

View File

@ -0,0 +1,52 @@
-- Packs lua-iconv into a LuaRock
-- rockspec based uppon the file provided by DarkGod <darkgod at net-core.org>
package = "lua-iconv"
version = "6-1"
source = {
url = "http://luaforge.net/frs/download.php/4181/lua-iconv-6.tar.gz",
md5 = "2cec334c5786d7c420a53003d6cb93d4"
}
description = {
summary = "Lua binding to the iconv",
detailed = [[
Lua binding to the POSIX 'iconv' library, which converts a sequence of
characters from one codeset into a sequence of corresponding characters
in another codeset.
]],
license = "MIT/X11",
homepage = "http://luaforge.net/projects/lua-iconv/"
}
dependencies = {
"lua >= 5.1",
}
external_dependencies = {
ICONV = {
header = "iconv.h"
}
}
build = {
type = "builtin",
modules = {
iconv = {
sources = {"luaiconv.c"},
incdirs = {"$(ICONV_INCDIR)"},
libdirs = {"$(ICONV_LIBDIR)"}
}
},
platforms = {
cygwin = {
modules = {
iconv = {
libraries = {"iconv"}
}
}
}
}
}

View File

@ -3,7 +3,7 @@
-- rockspec based uppon the file provided by DarkGod <darkgod at net-core.org> -- rockspec based uppon the file provided by DarkGod <darkgod at net-core.org>
package = "lua-iconv" package = "lua-iconv"
version = "7" version = "7-1"
source = { source = {
url = "https://github.com/downloads/ittner/lua-iconv/lua-iconv-7.tar.gz", url = "https://github.com/downloads/ittner/lua-iconv/lua-iconv-7.tar.gz",
@ -18,7 +18,7 @@ description = {
in another codeset. in another codeset.
]], ]],
license = "MIT/X11", license = "MIT/X11",
homepage = "http://ittner.github.com/lua-iconv/" homepage = "https://github.com/ittner/lua-iconv/"
} }
dependencies = { dependencies = {

View File

@ -0,0 +1,51 @@
-- Packs lua-iconv into a LuaRock
-- rockspec based uppon the file provided by DarkGod <darkgod at net-core.org>
package = "lua-iconv"
version = "7.0.0-2"
source = {
url = "https://github.com/lunarmodules/lua-iconv/archive/v7.0.0/lua-iconv-7.0.0.tar.gz",
}
description = {
summary = "Lua binding to the iconv",
detailed = [[
Lua binding to the POSIX 'iconv' library, which converts a sequence of
characters from one codeset into a sequence of corresponding characters
in another codeset.
]],
license = "MIT/X11",
homepage = "https://github.com/lunarmodules/lua-iconv/"
}
dependencies = {
"lua >= 5.1",
}
external_dependencies = {
ICONV = {
header = "iconv.h"
}
}
build = {
type = "builtin",
modules = {
iconv = {
sources = {"luaiconv.c"},
incdirs = {"$(ICONV_INCDIR)"},
libdirs = {"$(ICONV_LIBDIR)"}
}
},
platforms = {
cygwin = {
modules = {
iconv = {
libraries = {"iconv"}
}
}
}
}
}

View File

@ -0,0 +1,50 @@
-- Packs lua-iconv into a LuaRock
-- rockspec based uppon the file provided by DarkGod <darkgod at net-core.org>
package = "lua-iconv"
version = "7.0.0-4"
source = {
url = "https://github.com/lunarmodules/lua-iconv/archive/v7.0.0/lua-iconv-7.0.0.tar.gz",
}
description = {
summary = "Lua binding to the iconv",
detailed = [[
Lua binding to the POSIX 'iconv' library, which converts a sequence of
characters from one codeset into a sequence of corresponding characters
in another codeset.
]],
license = "MIT/X11",
homepage = "https://github.com/lunarmodules/lua-iconv/"
}
dependencies = {
"lua >= 5.1",
}
external_dependencies = {
ICONV = {
header = "iconv.h"
}
}
build = {
type = "builtin",
modules = {
iconv = {
sources = {"luaiconv.c"},
incdirs = {"$(ICONV_INCDIR)"},
libdirs = {"$(ICONV_LIBDIR)"}
}
},
platforms = {
cygwin = {
modules = {
iconv = {
libraries = {"iconv"}
}
}
}
}
}

View File

@ -0,0 +1,62 @@
local package_name = "lua-iconv"
local package_version = "7.1.0"
local rockspec_revision = "1"
local github_account_name = "lunarmodules"
local github_repo_name = package_name
package = package_name
version = package_version.."-"..rockspec_revision
source = {
url = "git+https://github.com/"..github_account_name.."/"..github_repo_name..".git",
branch = (package_version == "dev") and "master" or nil,
tag = (package_version ~= "dev") and ("v" .. package_version) or nil,
}
description = {
summary = "Lua binding to the iconv",
detailed = [[
Lua binding to the POSIX 'iconv' library, which converts a sequence of
characters from one codeset into a sequence of corresponding characters
in another codeset.
]],
license = "MIT/X11",
homepage = "https://github.com/"..github_account_name.."/"..github_repo_name,
}
dependencies = {
"lua >= 5.1",
}
external_dependencies = {
ICONV = {
header = "iconv.h"
}
}
build = {
type = "builtin",
modules = {
iconv = {
sources = {"luaiconv.c"},
incdirs = {"$(ICONV_INCDIR)"},
libdirs = {"$(ICONV_LIBDIR)"}
}
},
platforms = {
cygwin = {
modules = {
iconv = {
libraries = {"iconv"}
}
}
},
macosx = {
modules = {
iconv = {
libraries = {"iconv"}
}
}
},
}
}

View File

@ -1,3 +1,5 @@
-- -*- coding: utf-8 -*-
local iconv = require("iconv") local iconv = require("iconv")
-- Set your terminal encoding here -- Set your terminal encoding here
@ -74,20 +76,30 @@ local ebcdic = "\193\150\64\147\150\149\135\133\107\64\129\150\64\147\164\129"
.. "\37" .. "\37"
function check_one(to, from, text) local function ErrMsg(errno)
if errno == iconv.ERROR_INCOMPLETE then
return "Incomplete input."
elseif errno == iconv.ERROR_INVALID then
return "Invalid input."
elseif errno == iconv.ERROR_NO_MEMORY then
return "Failed to allocate memory."
elseif errno == iconv.ERROR_UNKNOWN then
return "There was an unknown error."
elseif errno == iconv.FINALIZED then
return "Handle was already finalized."
end
return "Unknown error: "..tostring(errno)
end
local function check_one(to, from, text)
print("\n-- Testing conversion from " .. from .. " to " .. to) print("\n-- Testing conversion from " .. from .. " to " .. to)
local cd = iconv.new(to .. "//TRANSLIT", from) local cd, errno = iconv.new(to .. "//TRANSLIT", from)
assert(cd, "Failed to create a converter object.") assert(cd, "Failed to create a converter object: " .. ErrMsg(errno))
local ostr, err = cd:iconv(text) local ostr, err = cd:iconv(text)
if err == iconv.ERROR_INCOMPLETE then if err then
print("ERROR: Incomplete input.") print("ERROR: " .. ErrMsg(err))
elseif err == iconv.ERROR_INVALID then
print("ERROR: Invalid input.")
elseif err == iconv.ERROR_NO_MEMORY then
print("ERROR: Failed to allocate memory.")
elseif err == iconv.ERROR_UNKNOWN then
print("ERROR: There was an unknown error.")
end end
print(ostr) print(ostr)
end end
@ -100,11 +112,24 @@ check_one(termcs, "EBCDIC-CP-ES", ebcdic)
-- The library must never crash the interpreter, even if the user tweaks -- The library must never crash the interpreter, even if the user tweaks
-- with the garbage collector methods. -- with the garbage collector methods.
local cd = iconv.new("iso-8859-1", "utf-8") local _, e, cd, s, gc
local _, e = cd:iconv("atenção") cd = iconv.new("iso-8859-1", "utf-8")
_, e = cd:iconv("atenção")
assert(e == nil, "Unexpected conversion error") assert(e == nil, "Unexpected conversion error")
local gc = getmetatable(cd).__gc gc = getmetatable(cd).__gc
gc(cd) gc(cd)
local _, e = cd:iconv("atenção") _, e = cd:iconv("atenção")
assert(e == iconv.ERROR_FINALIZED, "Failed to detect double-freed objects") assert(e == iconv.ERROR_FINALIZED, "Failed to detect double-freed objects")
gc(cd) gc(cd)
-- Test expected return values
cd = iconv.new("ascii", "utf-8")
_, e = cd:iconv("atenção")
assert(e == iconv.ERROR_INVALID, "Unexpected return value for invalid conversion")
cd = iconv.new("iso-8859-1", "utf-8")
s, e = cd:iconv("atenção")
assert(s == "aten\231\227o", "Unexpected result for valid conversion")
assert(e == nil, "Unexpected return value for valid conversion")