|
|
@ -3,7 +3,7 @@ |
|
|
|
** String library to LUA |
|
|
|
*/ |
|
|
|
|
|
|
|
char *rcs_strlib="$Id: strlib.c,v 1.34 1996/11/22 13:08:02 roberto Exp roberto $"; |
|
|
|
char *rcs_strlib="$Id: strlib.c,v 1.35 1997/02/21 15:21:34 roberto Exp roberto $"; |
|
|
|
|
|
|
|
#include <string.h> |
|
|
|
#include <stdio.h> |
|
|
@ -42,41 +42,6 @@ static char *openspace (unsigned long size) |
|
|
|
return buff+lbuffer.size; |
|
|
|
} |
|
|
|
|
|
|
|
void lua_arg_check(int cond, char *funcname) |
|
|
|
{ |
|
|
|
if (!cond) { |
|
|
|
char buff[100]; |
|
|
|
sprintf(buff, "incorrect argument to function `%s'", funcname); |
|
|
|
lua_error(buff); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
char *lua_check_string (int numArg, char *funcname) |
|
|
|
{ |
|
|
|
lua_Object o = lua_getparam(numArg); |
|
|
|
lua_arg_check(lua_isstring(o), funcname); |
|
|
|
return lua_getstring(o); |
|
|
|
} |
|
|
|
|
|
|
|
char *lua_opt_string (int numArg, char *def, char *funcname) |
|
|
|
{ |
|
|
|
return (lua_getparam(numArg) == LUA_NOOBJECT) ? def : |
|
|
|
lua_check_string(numArg, funcname); |
|
|
|
} |
|
|
|
|
|
|
|
double lua_check_number (int numArg, char *funcname) |
|
|
|
{ |
|
|
|
lua_Object o = lua_getparam(numArg); |
|
|
|
lua_arg_check(lua_isnumber(o), funcname); |
|
|
|
return lua_getnumber(o); |
|
|
|
} |
|
|
|
|
|
|
|
long lua_opt_number (int numArg, long def, char *funcname) |
|
|
|
{ |
|
|
|
return (lua_getparam(numArg) == LUA_NOOBJECT) ? def : |
|
|
|
(long)lua_check_number(numArg, funcname); |
|
|
|
} |
|
|
|
|
|
|
|
char *luaI_addchar (int c) |
|
|
|
{ |
|
|
|
if (lbuffer.size >= lbuffer.max) |
|
|
@ -104,8 +69,8 @@ static void addstr (char *s) |
|
|
|
*/ |
|
|
|
static void str_tok (void) |
|
|
|
{ |
|
|
|
char *s1 = lua_check_string(1, "strtok"); |
|
|
|
char *del = lua_check_string(2, "strtok"); |
|
|
|
char *s1 = luaL_check_string(1, "strtok"); |
|
|
|
char *del = luaL_check_string(2, "strtok"); |
|
|
|
lua_Object t = lua_createtable(); |
|
|
|
int i = 1; |
|
|
|
/* As strtok changes s1, and s1 is "constant", make a copy of it */ |
|
|
@ -127,7 +92,7 @@ static void str_tok (void) |
|
|
|
*/ |
|
|
|
static void str_len (void) |
|
|
|
{ |
|
|
|
lua_pushnumber(strlen(lua_check_string(1, "strlen"))); |
|
|
|
lua_pushnumber(strlen(luaL_check_string(1, "strlen"))); |
|
|
|
} |
|
|
|
|
|
|
|
/*
|
|
|
@ -135,9 +100,9 @@ static void str_len (void) |
|
|
|
*/ |
|
|
|
static void str_sub (void) |
|
|
|
{ |
|
|
|
char *s = lua_check_string(1, "strsub"); |
|
|
|
long start = (long)lua_check_number(2, "strsub"); |
|
|
|
long end = lua_opt_number(3, strlen(s), "strsub"); |
|
|
|
char *s = luaL_check_string(1, "strsub"); |
|
|
|
long start = (long)luaL_check_number(2, "strsub"); |
|
|
|
long end = (long)luaL_opt_number(3, strlen(s), "strsub"); |
|
|
|
if (1 <= start && start <= end && end <= strlen(s)) { |
|
|
|
luaI_addchar(0); |
|
|
|
addnchar(s+start-1, end-start+1); |
|
|
@ -151,7 +116,7 @@ static void str_sub (void) |
|
|
|
*/ |
|
|
|
static void str_lower (void) |
|
|
|
{ |
|
|
|
char *s = lua_check_string(1, "strlower"); |
|
|
|
char *s = luaL_check_string(1, "strlower"); |
|
|
|
luaI_addchar(0); |
|
|
|
while (*s) |
|
|
|
luaI_addchar(tolower((unsigned char)*s++)); |
|
|
@ -163,7 +128,7 @@ static void str_lower (void) |
|
|
|
*/ |
|
|
|
static void str_upper (void) |
|
|
|
{ |
|
|
|
char *s = lua_check_string(1, "strupper"); |
|
|
|
char *s = luaL_check_string(1, "strupper"); |
|
|
|
luaI_addchar(0); |
|
|
|
while (*s) |
|
|
|
luaI_addchar(toupper((unsigned char)*s++)); |
|
|
@ -172,8 +137,8 @@ static void str_upper (void) |
|
|
|
|
|
|
|
static void str_rep (void) |
|
|
|
{ |
|
|
|
char *s = lua_check_string(1, "strrep"); |
|
|
|
int n = (int)lua_check_number(2, "strrep"); |
|
|
|
char *s = luaL_check_string(1, "strrep"); |
|
|
|
int n = (int)luaL_check_number(2, "strrep"); |
|
|
|
luaI_addchar(0); |
|
|
|
while (n-- > 0) |
|
|
|
addstr(s); |
|
|
@ -185,9 +150,9 @@ static void str_rep (void) |
|
|
|
*/ |
|
|
|
static void str_ascii (void) |
|
|
|
{ |
|
|
|
char *s = lua_check_string(1, "ascii"); |
|
|
|
long pos = lua_opt_number(2, 1, "ascii") - 1; |
|
|
|
lua_arg_check(0<=pos && pos<strlen(s), "ascii"); |
|
|
|
char *s = luaL_check_string(1, "ascii"); |
|
|
|
long pos = (long)luaL_opt_number(2, 1, "ascii") - 1; |
|
|
|
luaL_arg_check(0<=pos && pos<strlen(s), "ascii", 2, "out of range"); |
|
|
|
lua_pushnumber((unsigned char)s[pos]); |
|
|
|
} |
|
|
|
|
|
|
@ -394,10 +359,10 @@ static char *match (char *s, char *p, int level) |
|
|
|
|
|
|
|
static void str_find (void) |
|
|
|
{ |
|
|
|
char *s = lua_check_string(1, "strfind"); |
|
|
|
char *p = lua_check_string(2, "strfind"); |
|
|
|
long init = lua_opt_number(3, 1, "strfind") - 1; |
|
|
|
lua_arg_check(0 <= init && init <= strlen(s), "strfind"); |
|
|
|
char *s = luaL_check_string(1, "strfind"); |
|
|
|
char *p = luaL_check_string(2, "strfind"); |
|
|
|
long init = (long)luaL_opt_number(3, 1, "strfind") - 1; |
|
|
|
luaL_arg_check(0 <= init && init <= strlen(s), "strfind", 3, "out of range"); |
|
|
|
if (lua_getparam(4) != LUA_NOOBJECT || |
|
|
|
strpbrk(p, SPECIALS) == NULL) { /* no special caracters? */ |
|
|
|
char *s2 = strstr(s+init, p); |
|
|
@ -450,15 +415,15 @@ static void add_s (lua_Object newp) |
|
|
|
addstr(lua_isstring(res) ? lua_getstring(res) : ""); |
|
|
|
lua_endblock(); |
|
|
|
} |
|
|
|
else lua_arg_check(0, "gsub"); |
|
|
|
else luaL_arg_check(0, "gsub", 3, NULL); |
|
|
|
} |
|
|
|
|
|
|
|
static void str_gsub (void) |
|
|
|
{ |
|
|
|
char *src = lua_check_string(1, "gsub"); |
|
|
|
char *p = lua_check_string(2, "gsub"); |
|
|
|
char *src = luaL_check_string(1, "gsub"); |
|
|
|
char *p = luaL_check_string(2, "gsub"); |
|
|
|
lua_Object newp = lua_getparam(3); |
|
|
|
int max_s = lua_opt_number(4, strlen(src)+1, "gsub"); |
|
|
|
int max_s = (int)luaL_opt_number(4, strlen(src)+1, "gsub"); |
|
|
|
int anchor = (*p == '^') ? (p++, 1) : 0; |
|
|
|
int n = 0; |
|
|
|
luaI_addchar(0); |
|
|
@ -482,9 +447,9 @@ static void str_gsub (void) |
|
|
|
|
|
|
|
static void str_set (void) |
|
|
|
{ |
|
|
|
char *item = lua_check_string(1, "strset"); |
|
|
|
char *item = luaL_check_string(1, "strset"); |
|
|
|
int i; |
|
|
|
lua_arg_check(*item_end(item) == 0, "strset"); |
|
|
|
luaL_arg_check(*item_end(item) == 0, "strset", 1, "wrong format"); |
|
|
|
luaI_addchar(0); |
|
|
|
for (i=1; i<256; i++) /* 0 cannot be part of a set */ |
|
|
|
if (singlematch(i, item)) |
|
|
@ -509,7 +474,7 @@ void luaI_addquoted (char *s) |
|
|
|
static void str_format (void) |
|
|
|
{ |
|
|
|
int arg = 1; |
|
|
|
char *strfrmt = lua_check_string(arg++, "format"); |
|
|
|
char *strfrmt = luaL_check_string(arg++, "format"); |
|
|
|
luaI_addchar(0); /* initialize */ |
|
|
|
while (*strfrmt) { |
|
|
|
if (*strfrmt != '%') |
|
|
@ -528,20 +493,20 @@ static void str_format (void) |
|
|
|
buff = openspace(1000); /* to store the formated value */ |
|
|
|
switch (*strfrmt++) { |
|
|
|
case 'q': |
|
|
|
luaI_addquoted(lua_check_string(arg++, "format")); |
|
|
|
luaI_addquoted(luaL_check_string(arg++, "format")); |
|
|
|
continue; |
|
|
|
case 's': { |
|
|
|
char *s = lua_check_string(arg++, "format"); |
|
|
|
char *s = luaL_check_string(arg++, "format"); |
|
|
|
buff = openspace(strlen(s)); |
|
|
|
sprintf(buff, form, s); |
|
|
|
break; |
|
|
|
} |
|
|
|
case 'c': case 'd': case 'i': case 'o': |
|
|
|
case 'u': case 'x': case 'X': |
|
|
|
sprintf(buff, form, (int)lua_check_number(arg++, "format")); |
|
|
|
sprintf(buff, form, (int)luaL_check_number(arg++, "format")); |
|
|
|
break; |
|
|
|
case 'e': case 'E': case 'f': case 'g': |
|
|
|
sprintf(buff, form, lua_check_number(arg++, "format")); |
|
|
|
sprintf(buff, form, luaL_check_number(arg++, "format")); |
|
|
|
break; |
|
|
|
default: /* also treat cases 'pnLlh' */ |
|
|
|
lua_error("invalid format option in function `format'"); |
|
|
|