From 2155d1b315e8153d1753e0f6b91c061ea2581dc7 Mon Sep 17 00:00:00 2001 From: Alexandre Erwin Ittner Date: Wed, 6 Jul 2005 03:17:28 +0000 Subject: [PATCH] *** empty log message *** git-svn-id: file:///var/svn/lua-iconv/trunk@11 9538949d-8f27-0410-946f-ce01ef448559 --- luaiconv.c | 31 ++++++++++--------------------- test_iconv.lua | 4 ++-- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/luaiconv.c b/luaiconv.c index a7b8a89..73c3126 100644 --- a/luaiconv.c +++ b/luaiconv.c @@ -99,12 +99,12 @@ static int Liconv(lua_State *L) char *inbuf = (char*) getstring(L, 2); char *outbuf; char *outbufs; - size_t bsize = 4 * ibleft; /* Try to avoid concatenations on stack */ - size_t obleft = bsize; + size_t obsize = ibleft; /* Try to avoid concatenations on stack */ + size_t obleft = obsize; size_t ret = -1; int hasone = 0; - outbuf = (char*) malloc(bsize * sizeof(char)); + outbuf = (char*) malloc(obsize * sizeof(char)); if(outbuf == NULL) { lua_pushstring(L, ""); @@ -118,40 +118,29 @@ static int Liconv(lua_State *L) ret = iconv(cd, &inbuf, &ibleft, &outbuf, &obleft); if(ret == (size_t)(-1)) { + lua_pushlstring(L, outbufs, obsize - obleft); + if(hasone == 1) + lua_concat(L, 2); + hasone = 1; if(errno == EILSEQ) { - lua_pushlstring(L, outbufs, bsize-obleft); - if(hasone == 1) - lua_concat(L, 2); - hasone = 1; lua_pushnumber(L, ERROR_INVALID); free(outbufs); return 2; /* Invalid character sequence */ } else if(errno == EINVAL) { - lua_pushlstring(L, outbufs, bsize-obleft); - if(hasone == 1) - lua_concat(L, 2); - hasone = 1; lua_pushnumber(L, ERROR_INCOMPLETE); free(outbufs); return 2; /* Incomplete character sequence */ } else if(errno == E2BIG) { - lua_pushlstring(L, outbufs, bsize-obleft); - if(hasone == 1) - lua_concat(L, 2); - hasone = 1; - obleft = bsize; + obleft = obsize; + outbuf = outbufs; } else { - lua_pushlstring(L, outbufs, bsize-obleft); - if(hasone == 1) - lua_concat(L, 2); - hasone = 1; lua_pushnumber(L, ERROR_UNKNOWN); free(outbufs); return 2; /* Unknown error */ @@ -160,7 +149,7 @@ static int Liconv(lua_State *L) } while(ret != (size_t) 0); - lua_pushlstring(L, outbufs, bsize-obleft); + lua_pushlstring(L, outbufs, obsize - obleft); if(hasone == 1) lua_concat(L, 2); free(outbufs); diff --git a/test_iconv.lua b/test_iconv.lua index 8557b4f..c4deae0 100644 --- a/test_iconv.lua +++ b/test_iconv.lua @@ -7,8 +7,8 @@ cd = iconv.new("utf-8", "iso-8859-1") assert(cd, "Invalid conversion") -ret, err = cd:iconv("Isso é um teste com acentuação") --- ret, err = cd:iconv("çãÃÉÍÓÚÇÑÿ ") +--ret, err = cd:iconv("Isso é um teste com acentuação") +ret, err = cd:iconv("çãÃÉÍÓÚÇÑÿa") if err == iconv.ERROR_INCOMPLETE then print("Error: Incomplete input.")