Skip to content
Snippets Groups Projects
Commit ff26e03b authored by Luigi Scarso's avatar Luigi Scarso
Browse files

clean up -- !!! WARNING: WORK IN PROGRESS !!!

parent 81ec0229
No related branches found
No related tags found
No related merge requests found
......@@ -23,10 +23,6 @@
/* http://lua.2524044.n2.nabble.com/converting-modules-in-written-in-C-for-Lua-5-1-to-Lua-5-2-td7642941.html */
#define luaL_setmetatable(L, tname) luaL_getmetatable(L, tname);lua_setmetatable(L, -2);
/* see http://comments.gmane.org/gmane.comp.programming.swig/18673 */
/*
# define lua_rawlen lua_objlen
*/
LUALIB_API void *(luaL_testudata) (lua_State *L, int ud, const char *tname);
......@@ -39,23 +35,7 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op);
#define lua_unlock(L) ((void) 0)
#endif
#define luaL_newlibtable(L,l) \
lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1)
#define luaL_newlibtable(L,l) lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1)
#define luaL_newlib(L,l) (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
/* Is this ok as replacement for lua_copy of lua 5.2.4 ? */
/*
#if defined(LUAJIT)
LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) {
TValue *fr, *to;
lua_lock(L);
fr = index2adr(L, fromidx);
to = index2adr(L, toidx);
api_checkvalidindex(L, to);
copyTV(L,to,fr);
lua_unlock(L);
}
#endif
*/
#endif
......@@ -25,100 +25,71 @@
#define LUA_CORE
#include "lua.h"
#ifdef LuajitTeX
#include "lauxlib.h"
#include "lualib.h"
#else
#include "luaconf.h"
#include "lapi.h"
#include "lundump.h"
#endif
static int str_split (lua_State *L) {
size_t l;
size_t i;
int n;
char *q, *p, *orig;
int mult = 0;
const char *s = luaL_checklstring(L, 1, &l);
const char *joiner = luaL_optstring(L, 2, " +");
lua_newtable(L);
if (l == 0) {
lua_pushvalue(L,1);
lua_rawseti(L,-2,1);
return 1;
}
orig = p = malloc(l+1);
if (p==NULL) {
fprintf(stderr, "fatal: memory exhausted (malloc of %u bytes).\n",(int)(l+1));
exit(EXIT_FAILURE);
}
strcpy(p,s);
n = 1;
q = p;
if (*joiner == 0) {
for (i=0;i<l;i++) {
lua_pushlstring(L,q,1); q++;
lua_rawseti(L,-2,n); n++;
}
free(orig);
return 1;
}
if (*(joiner+1) == '+') {
mult = 1;
while(*p==*joiner) {
p++;
l--;
static int bytepairs_aux (lua_State *L) {
size_t ls;
unsigned char i;
const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
int ind = lua_tointeger(L, lua_upvalueindex(2));
if (ind<(int)ls) {
if (ind+1<(int)ls) {
lua_pushinteger(L, (ind+2)); /* iterator */
} else {
lua_pushinteger(L, (ind+1)); /* iterator */
}
q = p;
}
for (i=0;i<l;i++) {
if (*(p+i)==*joiner) {
*(p+i) = 0;
lua_pushlstring(L,q,((p+i)-q));
lua_rawseti(L,-2,n); n++;
if (mult) {
while(*(p+i+1)==*joiner) {
i++;
}
}
q = p+i+1;
lua_replace(L, lua_upvalueindex(2));
i = (unsigned char)*(s+ind);
lua_pushinteger(L, i); /* byte one */
if (ind+1<(int)ls) {
i = (unsigned char)*(s+ind+1);
lua_pushinteger(L, i); /* byte two */
} else {
lua_pushnil(L); /* odd string length */
}
return 2;
}
if (mult && q==(p+l)) {
free(orig);
return 1;
}
if(q<=(p+l)) {
lua_pushlstring(L,q,strlen(q));
lua_rawseti(L,-2,n);
}
free(orig);
return 0; /* string ended */
}
static int str_bytepairs (lua_State *L) {
luaL_checkstring(L, 1);
lua_settop(L, 1);
lua_pushinteger(L, 0);
lua_pushcclosure(L, bytepairs_aux, 2);
return 1;
}
static int characters_aux (lua_State *L) {
static int bytes_aux (lua_State *L) {
size_t ls;
char b[2];
unsigned char i;
const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
int ind = lua_tointeger(L, lua_upvalueindex(2));
int ind = lua_tointeger(L, lua_upvalueindex(2));
if (ind<(int)ls) {
lua_pushinteger(L, (ind+1)); /* iterator */
lua_replace(L, lua_upvalueindex(2));
b[0] = *(s+ind); b[1] = 0;
lua_pushlstring(L, b, 1);
i = (unsigned char)*(s+ind);
lua_pushinteger(L, i); /* byte */
return 1;
}
return 0; /* string ended */
}
static int str_characters (lua_State *L) {
static int str_bytes (lua_State *L) {
luaL_checkstring(L, 1);
lua_settop(L, 1);
lua_pushinteger(L, 0);
lua_pushcclosure(L, characters_aux, 2);
lua_pushcclosure(L, bytes_aux, 2);
return 1;
}
static int utf_failed(lua_State *L, int new_ind) {
static char fffd [3] = {0xEF,0xBF,0xBD};
lua_pushinteger(L, new_ind); /* iterator */
......@@ -154,7 +125,6 @@ static int utfcharacters_aux (lua_State *L) {
return utf_failed(L,ind+1); /* we found a follow byte! */
}
static int str_utfcharacters (lua_State *L) {
luaL_checkstring(L, 1);
lua_settop(L, 1);
......@@ -163,7 +133,6 @@ static int str_utfcharacters (lua_State *L) {
return 1;
}
static int utfvalues_aux (lua_State *L) {
size_t ls;
unsigned char i = 0;
......@@ -211,7 +180,6 @@ static int utfvalues_aux (lua_State *L) {
return 0; /* string ended */
}
static int str_utfvalues (lua_State *L) {
luaL_checkstring(L, 1);
lua_settop(L, 1);
......@@ -220,8 +188,6 @@ static int str_utfvalues (lua_State *L) {
return 1;
}
static int characterpairs_aux (lua_State *L) {
size_t ls;
char b[2];
......@@ -247,7 +213,6 @@ static int characterpairs_aux (lua_State *L) {
return 0; /* string ended */
}
static int str_characterpairs (lua_State *L) {
luaL_checkstring(L, 1);
lua_settop(L, 1);
......@@ -256,65 +221,96 @@ static int str_characterpairs (lua_State *L) {
return 1;
}
static int bytes_aux (lua_State *L) {
static int characters_aux (lua_State *L) {
size_t ls;
unsigned char i;
char b[2];
const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
int ind = lua_tointeger(L, lua_upvalueindex(2));
int ind = lua_tointeger(L, lua_upvalueindex(2));
if (ind<(int)ls) {
lua_pushinteger(L, (ind+1)); /* iterator */
lua_replace(L, lua_upvalueindex(2));
i = (unsigned char)*(s+ind);
lua_pushinteger(L, i); /* byte */
b[0] = *(s+ind); b[1] = 0;
lua_pushlstring(L, b, 1);
return 1;
}
return 0; /* string ended */
}
static int str_bytes (lua_State *L) {
static int str_characters (lua_State *L) {
luaL_checkstring(L, 1);
lua_settop(L, 1);
lua_pushinteger(L, 0);
lua_pushcclosure(L, bytes_aux, 2);
lua_pushcclosure(L, characters_aux, 2);
return 1;
}
static int bytepairs_aux (lua_State *L) {
size_t ls;
unsigned char i;
const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
int ind = lua_tointeger(L, lua_upvalueindex(2));
if (ind<(int)ls) {
if (ind+1<(int)ls) {
lua_pushinteger(L, (ind+2)); /* iterator */
} else {
lua_pushinteger(L, (ind+1)); /* iterator */
static int str_split (lua_State *L) {
size_t l;
size_t i;
int n;
char *q, *p, *orig;
int mult = 0;
const char *s = luaL_checklstring(L, 1, &l);
const char *joiner = luaL_optstring(L, 2, " +");
lua_newtable(L);
if (l == 0) {
lua_pushvalue(L,1);
lua_rawseti(L,-2,1);
return 1;
}
orig = p = malloc(l+1);
if (p==NULL) {
fprintf(stderr, "fatal: memory exhausted (malloc of %u bytes).\n",(int)(l+1));
exit(EXIT_FAILURE);
}
strcpy(p,s);
n = 1;
q = p;
if (*joiner == 0) {
for (i=0;i<l;i++) {
lua_pushlstring(L,q,1); q++;
lua_rawseti(L,-2,n); n++;
}
lua_replace(L, lua_upvalueindex(2));
i = (unsigned char)*(s+ind);
lua_pushinteger(L, i); /* byte one */
if (ind+1<(int)ls) {
i = (unsigned char)*(s+ind+1);
lua_pushinteger(L, i); /* byte two */
} else {
lua_pushnil(L); /* odd string length */
free(orig);
return 1;
}
if (*(joiner+1) == '+') {
mult = 1;
while(*p==*joiner) {
p++;
l--;
}
return 2;
q = p;
}
return 0; /* string ended */
}
static int str_bytepairs (lua_State *L) {
luaL_checkstring(L, 1);
lua_settop(L, 1);
lua_pushinteger(L, 0);
lua_pushcclosure(L, bytepairs_aux, 2);
for (i=0;i<l;i++) {
if (*(p+i)==*joiner) {
*(p+i) = 0;
lua_pushlstring(L,q,((p+i)-q));
lua_rawseti(L,-2,n); n++;
if (mult) {
while(*(p+i+1)==*joiner) {
i++;
}
}
q = p+i+1;
}
}
if (mult && q==(p+l)) {
free(orig);
return 1;
}
if(q<=(p+l)) {
lua_pushlstring(L,q,strlen(q));
lua_rawseti(L,-2,n);
}
free(orig);
return 1;
}
#ifdef LuajitTeX
/* dump is built in */
#else
static int writer (lua_State *L, const void* b, size_t size, void* B) {
(void)L;
luaL_addlstring((luaL_Buffer*) B, (const char *)b, size);
......@@ -349,6 +345,7 @@ static int str_dump (lua_State *L) {
luaL_pushresult(&b);
return 1;
}
#endif /*ifdef LuajitTeX*/
static int str_bytetable (lua_State *L) {
size_t l;
......@@ -371,7 +368,11 @@ static const luaL_Reg strlibext[] = {
{"bytepairs", str_bytepairs},
{"bytetable", str_bytetable},
{"explode", str_split},
#ifdef LuajitTeX
/* luajit has dump built in */
#else
{"dump", str_dump},
#endif
{NULL, NULL}
};
......
......@@ -2,7 +2,7 @@
Copyright 2013 Luigi Scarso
Code from lstrlibext.c for LuaTeX
Code from lstrlibext.c for LuaTeX
Original version copyright 2012 Taco Hoekwater <taco@luatex.org>
This file is part of LuajitTeX.
......@@ -18,7 +18,7 @@
License for more details.
You should have received a copy of the GNU General Public License along
with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
with LuaTeX; if not, see <http://www.gnu.org/licenses/>.
*/
#include "ptexlib.h"
......@@ -30,9 +30,6 @@
#include "lauxlib.h"
#include "lualib.h"
/*#include "lua51/lj_obj.h"*/
/*#include "lua51/lj_gc.h"*/
/*#include "lua51/lj_err.h"*/
......@@ -45,8 +42,6 @@
/*#include "lua51/lj_char.h"*/
/*#include "lua51/lj_lib.h"*/
static int bytepairs_aux (lua_State *L) {
size_t ls;
unsigned char i;
......@@ -72,7 +67,6 @@ static int bytepairs_aux (lua_State *L) {
return 0; /* string ended */
}
static int str_bytepairs (lua_State *L) {
luaL_checkstring(L, 1);
lua_settop(L, 1);
......@@ -81,7 +75,6 @@ static int str_bytepairs (lua_State *L) {
return 1;
}
static int bytes_aux (lua_State *L) {
size_t ls;
unsigned char i;
......@@ -97,7 +90,6 @@ static int bytes_aux (lua_State *L) {
return 0; /* string ended */
}
static int str_bytes (lua_State *L) {
luaL_checkstring(L, 1);
lua_settop(L, 1);
......@@ -106,7 +98,6 @@ static int str_bytes (lua_State *L) {
return 1;
}
static int utf_failed(lua_State *L, int new_ind) {
static char fffd [3] = {0xEF,0xBF,0xBD};
lua_pushinteger(L, new_ind); /* iterator */
......@@ -115,7 +106,6 @@ static int utf_failed(lua_State *L, int new_ind) {
return 1;
}
static int utfcharacters_aux (lua_State *L) {
static const unsigned char mask[4] = {0x80,0xE0,0xF0,0xF8};
static const unsigned char mequ[4] = {0x00,0xC0,0xE0,0xF0};
......@@ -141,8 +131,7 @@ static int utfcharacters_aux (lua_State *L) {
}
}
return utf_failed(L,ind+1); /* we found a follow byte! */
}
}
static int str_utfcharacters (lua_State *L) {
luaL_checkstring(L, 1);
......@@ -152,8 +141,6 @@ static int str_utfcharacters (lua_State *L) {
return 1;
}
static int utfvalues_aux (lua_State *L) {
size_t ls;
unsigned char i = 0;
......@@ -170,7 +157,7 @@ static int utfvalues_aux (lua_State *L) {
if (i<0x80) {
v = i;
} else if (i>=0xF0) {
if ((ind+3)<(int)ls && ((unsigned)*(s+ind+1))>=0x80
if ((ind+3)<(int)ls && ((unsigned)*(s+ind+1))>=0x80
&& ((unsigned)*(s+ind+2))>=0x80 && ((unsigned)*(s+ind+3))>=0x80) {
numbytes = 4;
j = ((unsigned)*(s+ind+1))-128;
......@@ -201,7 +188,6 @@ static int utfvalues_aux (lua_State *L) {
return 0; /* string ended */
}
static int str_utfvalues (lua_State *L) {
luaL_checkstring(L, 1);
lua_settop(L, 1);
......@@ -210,7 +196,6 @@ static int str_utfvalues (lua_State *L) {
return 1;
}
static int characterpairs_aux (lua_State *L) {
size_t ls;
char b[2];
......@@ -226,7 +211,7 @@ static int characterpairs_aux (lua_State *L) {
b[0] = *(s+ind); b[1] = 0;
lua_pushlstring(L, b, 1);
if (ind+1<(int)ls) {
b[0] = *(s+ind+1);
b[0] = *(s+ind+1);
lua_pushlstring(L, b, 1);
} else {
lua_pushlstring(L, b+1, 0);
......@@ -236,7 +221,6 @@ static int characterpairs_aux (lua_State *L) {
return 0; /* string ended */
}
static int str_characterpairs (lua_State *L) {
luaL_checkstring(L, 1);
lua_settop(L, 1);
......@@ -245,7 +229,6 @@ static int str_characterpairs (lua_State *L) {
return 1;
}
static int characters_aux (lua_State *L) {
size_t ls;
char b[2];
......@@ -261,7 +244,6 @@ static int characters_aux (lua_State *L) {
return 0; /* string ended */
}
static int str_characters (lua_State *L) {
luaL_checkstring(L, 1);
lua_settop(L, 1);
......@@ -332,56 +314,33 @@ static int str_split (lua_State *L) {
}
free(orig);
return 1;
}
}
static int str_bytetable (lua_State *L) {
size_t l;
int i;
const char *s = luaL_checklstring(L, 1, &l);
lua_createtable(L,l,0);
for (i=0;i<l;i++) {
lua_pushinteger(L,(unsigned char)*(s+i));
lua_rawseti(L,-2,i+1);
}
return 1;
}
static const luaL_Reg strlibext[] = {
{"utfvalues", str_utfvalues},
{"utfvalues", str_utfvalues},
{"utfcharacters", str_utfcharacters},
{"characters", str_characters},
{"characterpairs", str_characterpairs},
{"bytes", str_bytes},
{"bytepairs", str_bytepairs},
{"bytetable", str_bytetable},
{"explode", str_split},
/* {"dump", str_dump} already in luajit */
{NULL, NULL}
};
/* ------------------------------------------------------------------------ */
/* lj_libdef.h is generated by buildvm, it's not available on source */
/* #include "lua51/lj_libdef.h" */
/* LUALIB_API int luaopen_string(lua_State *L) */
/* { */
/* GCtab *mt; */
/* global_State *g; */
/* LJ_LIB_REG(L, LUA_STRLIBNAME, string); */
/* luaL_register(L, LUA_STRLIBNAME, strlib); */
/* //LJ_LIB_REG(L, LUA_STRLIBNAME, strlib); */
/* #if defined(LUA_COMPAT_GFIND) && !LJ_52 */
/* lua_getfield(L, -1, "gmatch"); */
/* lua_setfield(L, -2, "gfind"); */
/* #endif */
/* mt = lj_tab_new(L, 0, 1); */
/* /\* NOBARRIER: basemt is a GC root. *\/ */
/* g = G(L); */
/* setgcref(basemt_it(g, LJ_TSTR), obj2gco(mt)); */
/* settabV(L, lj_tab_setstr(L, mt, mmname_str(g, MM_index)), tabV(L->top-1)); */
/* mt->nomm = (uint8_t)(~(1u<<MM_index)); */
/* return 1; */
/* } */
/* void open_strlibext(lua_State *L) */
/* { */
/* int v; */
/* v = luaopen_string(L); */
/* } */
void open_strlibext(lua_State * L)
{
const luaL_Reg *lib;
......@@ -392,4 +351,3 @@ void open_strlibext(lua_State * L)
}
lua_pop(L,1);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment