summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/luatexdir/lua/lstrlibext.c
diff options
context:
space:
mode:
Diffstat (limited to 'Build/source/texk/web2c/luatexdir/lua/lstrlibext.c')
-rw-r--r--Build/source/texk/web2c/luatexdir/lua/lstrlibext.c249
1 files changed, 130 insertions, 119 deletions
diff --git a/Build/source/texk/web2c/luatexdir/lua/lstrlibext.c b/Build/source/texk/web2c/luatexdir/lua/lstrlibext.c
index 8a474fd09c9..5d5c0d723ac 100644
--- a/Build/source/texk/web2c/luatexdir/lua/lstrlibext.c
+++ b/Build/source/texk/web2c/luatexdir/lua/lstrlibext.c
@@ -1,5 +1,5 @@
/* lstrlibext.c
-
+
Copyright 2012 Taco Hoekwater <taco@luatex.org>
This file is part of LuaTeX.
@@ -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 */
@@ -152,8 +123,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);
@@ -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;
@@ -180,7 +149,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;
@@ -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];
@@ -237,7 +203,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);
@@ -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,8 +345,19 @@ 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;
+ 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},
@@ -359,8 +366,13 @@ static const luaL_Reg strlibext[] = {
{"characterpairs", str_characterpairs},
{"bytes", str_bytes},
{"bytepairs", str_bytepairs},
+ {"bytetable", str_bytetable},
{"explode", str_split},
+#ifdef LuajitTeX
+ /* luajit has dump built in */
+#else
{"dump", str_dump},
+#endif
{NULL, NULL}
};
@@ -374,4 +386,3 @@ void open_strlibext(lua_State * L)
}
lua_pop(L,1);
}
-