new functions: "ascii" and "int2str",
small "re-engineering" on parameter checking.
This commit is contained in:
159
strlib.c
159
strlib.c
@@ -3,9 +3,10 @@
|
|||||||
** String library to LUA
|
** String library to LUA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char *rcs_strlib="$Id: strlib.c,v 1.11 1995/02/02 20:05:37 roberto Exp roberto $";
|
char *rcs_strlib="$Id: strlib.c,v 1.12 1995/02/06 19:37:51 roberto Exp $";
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
@@ -13,9 +14,31 @@ char *rcs_strlib="$Id: strlib.c,v 1.11 1995/02/02 20:05:37 roberto Exp roberto $
|
|||||||
#include "lualib.h"
|
#include "lualib.h"
|
||||||
|
|
||||||
|
|
||||||
static char *newstring (lua_Object o)
|
static void str_error(char *funcname)
|
||||||
|
{
|
||||||
|
char buff[250];
|
||||||
|
sprintf(buff, "incorrect arguments to function `%s'", funcname);
|
||||||
|
lua_error(buff);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *check_and_get_string (int numArg, char *funcname)
|
||||||
|
{
|
||||||
|
lua_Object o = lua_getparam(numArg);
|
||||||
|
if (!(lua_isstring(o) || lua_isnumber(o)))
|
||||||
|
str_error(funcname);
|
||||||
|
return lua_getstring(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int check_and_get_int (int numArg, char *funcname)
|
||||||
|
{
|
||||||
|
lua_Object o = lua_getparam(numArg);
|
||||||
|
if (!lua_isnumber(o))
|
||||||
|
str_error(funcname);
|
||||||
|
return (int)lua_getnumber(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *newstring (char *s)
|
||||||
{
|
{
|
||||||
char *s = lua_getstring(o);
|
|
||||||
char *ns = (char *)malloc(strlen(s)+1);
|
char *ns = (char *)malloc(strlen(s)+1);
|
||||||
if (ns == 0)
|
if (ns == 0)
|
||||||
lua_error("not enough memory for new string");
|
lua_error("not enough memory for new string");
|
||||||
@@ -31,34 +54,17 @@ static char *newstring (lua_Object o)
|
|||||||
*/
|
*/
|
||||||
static void str_find (void)
|
static void str_find (void)
|
||||||
{
|
{
|
||||||
char *s1, *s2, *f;
|
char *s1 = check_and_get_string(1, "strfind");
|
||||||
int init;
|
char *s2 = check_and_get_string(2, "strfind");
|
||||||
lua_Object o1 = lua_getparam (1);
|
int init = (lua_getparam(3) == LUA_NOOBJECT) ? 0 :
|
||||||
lua_Object o2 = lua_getparam (2);
|
check_and_get_int(3, "strfind")-1;
|
||||||
lua_Object o3 = lua_getparam (3);
|
char *f = strstr(s1+init,s2);
|
||||||
lua_Object o4 = lua_getparam (4);
|
|
||||||
if (!lua_isstring(o1) || !lua_isstring(o2))
|
|
||||||
lua_error ("incorrect arguments to function `strfind'");
|
|
||||||
if (o3 == LUA_NOOBJECT)
|
|
||||||
init = 0;
|
|
||||||
else if (lua_isnumber(o3))
|
|
||||||
init = lua_getnumber(o3)-1;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
lua_error ("incorrect arguments to function `strfind'");
|
|
||||||
return; /* to avoid warnings */
|
|
||||||
}
|
|
||||||
s1 = lua_getstring(o1);
|
|
||||||
s2 = lua_getstring(o2);
|
|
||||||
f = strstr(s1+init,s2);
|
|
||||||
if (f != NULL)
|
if (f != NULL)
|
||||||
{
|
{
|
||||||
int pos = f-s1+1;
|
int pos = f-s1+1;
|
||||||
if (o4 == LUA_NOOBJECT)
|
if (lua_getparam (4) == LUA_NOOBJECT)
|
||||||
lua_pushnumber (pos);
|
lua_pushnumber (pos);
|
||||||
else if (!lua_isnumber(o4))
|
else if (check_and_get_int(4, "strfind") >= pos+strlen(s2)-1)
|
||||||
lua_error ("incorrect arguments to function `strfind'");
|
|
||||||
else if ((int)lua_getnumber(o4) >= pos+strlen(s2)-1)
|
|
||||||
lua_pushnumber (pos);
|
lua_pushnumber (pos);
|
||||||
else
|
else
|
||||||
lua_pushnil();
|
lua_pushnil();
|
||||||
@@ -74,10 +80,8 @@ static void str_find (void)
|
|||||||
*/
|
*/
|
||||||
static void str_len (void)
|
static void str_len (void)
|
||||||
{
|
{
|
||||||
lua_Object o = lua_getparam (1);
|
char *s = check_and_get_string(1, "strlen");
|
||||||
if (!lua_isstring(o))
|
lua_pushnumber(strlen(s));
|
||||||
lua_error ("incorrect arguments to function `strlen'");
|
|
||||||
lua_pushnumber(strlen(lua_getstring(o)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -88,25 +92,35 @@ static void str_len (void)
|
|||||||
*/
|
*/
|
||||||
static void str_sub (void)
|
static void str_sub (void)
|
||||||
{
|
{
|
||||||
int start, end;
|
char *s = check_and_get_string(1, "strsub");
|
||||||
char *s;
|
int start = check_and_get_int(2, "strsub");
|
||||||
lua_Object o1 = lua_getparam (1);
|
int end = (lua_getparam(3) == LUA_NOOBJECT) ? strlen(s) :
|
||||||
lua_Object o2 = lua_getparam (2);
|
check_and_get_int(3, "strsub");
|
||||||
lua_Object o3 = lua_getparam (3);
|
|
||||||
if (!lua_isstring(o1) || !lua_isnumber(o2))
|
|
||||||
lua_error ("incorrect arguments to function `strsub'");
|
|
||||||
if (o3 != LUA_NOOBJECT && !lua_isnumber(o3))
|
|
||||||
lua_error ("incorrect third argument to function `strsub'");
|
|
||||||
s = newstring(o1);
|
|
||||||
start = lua_getnumber (o2);
|
|
||||||
end = o3 == LUA_NOOBJECT ? strlen(s) : lua_getnumber (o3);
|
|
||||||
if (end < start || start < 1 || end > strlen(s))
|
if (end < start || start < 1 || end > strlen(s))
|
||||||
lua_pushliteral("");
|
lua_pushliteral("");
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
char temp = s[end];
|
||||||
s[end] = 0;
|
s[end] = 0;
|
||||||
lua_pushstring (&s[start-1]);
|
lua_pushstring (&s[start-1]);
|
||||||
|
s[end] = temp;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Convert a string according to given function.
|
||||||
|
*/
|
||||||
|
typedef int (*strfunc)(int s);
|
||||||
|
static void str_apply (strfunc f, char *funcname)
|
||||||
|
{
|
||||||
|
char *s, *c;
|
||||||
|
c = s = newstring(check_and_get_string(1, funcname));
|
||||||
|
while (*c != 0)
|
||||||
|
{
|
||||||
|
*c = f(*c);
|
||||||
|
c++;
|
||||||
|
}
|
||||||
|
lua_pushstring(s);
|
||||||
free(s);
|
free(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,18 +131,7 @@ static void str_sub (void)
|
|||||||
*/
|
*/
|
||||||
static void str_lower (void)
|
static void str_lower (void)
|
||||||
{
|
{
|
||||||
char *s, *c;
|
str_apply(tolower, "strlower");
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (!lua_isstring(o))
|
|
||||||
lua_error ("incorrect arguments to function `strlower'");
|
|
||||||
c = s = newstring(o);
|
|
||||||
while (*c != 0)
|
|
||||||
{
|
|
||||||
*c = tolower(*c);
|
|
||||||
c++;
|
|
||||||
}
|
|
||||||
lua_pushstring(s);
|
|
||||||
free(s);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -139,20 +142,40 @@ static void str_lower (void)
|
|||||||
*/
|
*/
|
||||||
static void str_upper (void)
|
static void str_upper (void)
|
||||||
{
|
{
|
||||||
char *s, *c;
|
str_apply(toupper, "strupper");
|
||||||
lua_Object o = lua_getparam (1);
|
|
||||||
if (!lua_isstring(o))
|
|
||||||
lua_error ("incorrect arguments to function `strlower'");
|
|
||||||
c = s = newstring(o);
|
|
||||||
while (*c != 0)
|
|
||||||
{
|
|
||||||
*c = toupper(*c);
|
|
||||||
c++;
|
|
||||||
}
|
|
||||||
lua_pushstring(s);
|
|
||||||
free(s);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** get ascii value of a character in a string
|
||||||
|
*/
|
||||||
|
static void str_ascii (void)
|
||||||
|
{
|
||||||
|
char *s = check_and_get_string(1, "ascii");
|
||||||
|
lua_Object o2 = lua_getparam(2);
|
||||||
|
int pos;
|
||||||
|
pos = (o2 == LUA_NOOBJECT) ? 0 : check_and_get_int(2, "ascii")-1;
|
||||||
|
if (pos<0 || pos>=strlen(s))
|
||||||
|
str_error("ascii");
|
||||||
|
lua_pushnumber(s[pos]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** converts one or more integers to chars in a string
|
||||||
|
*/
|
||||||
|
#define maxparams 50
|
||||||
|
static void str_int2str (void)
|
||||||
|
{
|
||||||
|
char s[maxparams+1];
|
||||||
|
int i = 0;
|
||||||
|
while (lua_getparam(++i) != LUA_NOOBJECT)
|
||||||
|
{
|
||||||
|
if (i > maxparams)
|
||||||
|
lua_error("too many parameters to function `int2str'");
|
||||||
|
s[i-1] = check_and_get_int(i, "int2str");
|
||||||
|
}
|
||||||
|
s[i-1] = 0;
|
||||||
|
lua_pushstring(s);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Open string library
|
** Open string library
|
||||||
@@ -164,4 +187,6 @@ void strlib_open (void)
|
|||||||
lua_register ("strsub", str_sub);
|
lua_register ("strsub", str_sub);
|
||||||
lua_register ("strlower", str_lower);
|
lua_register ("strlower", str_lower);
|
||||||
lua_register ("strupper", str_upper);
|
lua_register ("strupper", str_upper);
|
||||||
|
lua_register ("ascii", str_ascii);
|
||||||
|
lua_register ("int2str", str_int2str);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user