lua/strlib.c

157 lines
3.2 KiB
C
Raw Normal View History

1993-07-28 15:18:00 +02:00
/*
** strlib.c
** String library to LUA
*/
char *rcs_strlib="$Id: strlib.c,v 1.10 1995/02/02 18:54:58 roberto Exp $";
1993-12-17 19:41:19 +01:00
1993-07-28 15:18:00 +02:00
#include <string.h>
#include <strings.h>
#include <stdlib.h>
1993-07-28 15:18:00 +02:00
#include <ctype.h>
#include "lua.h"
#include "lualib.h"
1993-07-28 15:18:00 +02:00
/*
** Return the position of the first caracter of a substring into a string
** LUA interface:
** n = strfind (string, substring, init, end)
1993-07-28 15:18:00 +02:00
*/
static void str_find (void)
{
1993-12-17 19:41:19 +01:00
char *s1, *s2, *f;
int init;
1993-07-28 15:18:00 +02:00
lua_Object o1 = lua_getparam (1);
lua_Object o2 = lua_getparam (2);
lua_Object o3 = lua_getparam (3);
lua_Object o4 = lua_getparam (4);
1993-07-28 15:18:00 +02:00
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 */
}
1993-07-28 15:18:00 +02:00
s1 = lua_getstring(o1);
s2 = lua_getstring(o2);
f = strstr(s1+init,s2);
1993-12-17 19:41:19 +01:00
if (f != NULL)
{
int pos = f-s1+1;
if (o4 == LUA_NOOBJECT)
lua_pushnumber (pos);
else if (!lua_isnumber(o4))
lua_error ("incorrect arguments to function `strfind'");
else if ((int)lua_getnumber(o4) >= pos+strlen(s2)-1)
lua_pushnumber (pos);
else
lua_pushnil();
}
1993-12-17 19:41:19 +01:00
else
lua_pushnil();
1993-07-28 15:18:00 +02:00
}
/*
** Return the string length
** LUA interface:
** n = strlen (string)
*/
static void str_len (void)
{
lua_Object o = lua_getparam (1);
if (!lua_isstring(o))
lua_error ("incorrect arguments to function `strlen'");
1993-07-28 15:18:00 +02:00
lua_pushnumber(strlen(lua_getstring(o)));
}
/*
** Return the substring of a string, from start to end
** LUA interface:
** substring = strsub (string, start, end)
*/
static void str_sub (void)
{
int start, end;
char *s;
lua_Object o1 = lua_getparam (1);
lua_Object o2 = lua_getparam (2);
lua_Object o3 = lua_getparam (3);
1993-12-17 19:41:19 +01:00
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'");
1993-12-17 19:41:19 +01:00
s = lua_copystring(o1);
1993-07-28 15:18:00 +02:00
start = lua_getnumber (o2);
end = o3 == LUA_NOOBJECT ? strlen(s) : lua_getnumber (o3);
1993-07-28 15:18:00 +02:00
if (end < start || start < 1 || end > strlen(s))
1994-12-13 16:54:21 +01:00
lua_pushliteral("");
1993-07-28 15:18:00 +02:00
else
{
s[end] = 0;
lua_pushstring (&s[start-1]);
}
free(s);
1993-07-28 15:18:00 +02:00
}
/*
** Convert a string to lower case.
** LUA interface:
** lowercase = strlower (string)
*/
static void str_lower (void)
{
char *s, *c;
lua_Object o = lua_getparam (1);
if (!lua_isstring(o))
lua_error ("incorrect arguments to function `strlower'");
c = s = lua_copystring(o);
1993-07-28 15:18:00 +02:00
while (*c != 0)
{
*c = tolower(*c);
c++;
}
lua_pushstring(s);
free(s);
1993-07-28 15:18:00 +02:00
}
/*
** Convert a string to upper case.
** LUA interface:
** uppercase = strupper (string)
*/
static void str_upper (void)
{
char *s, *c;
lua_Object o = lua_getparam (1);
if (!lua_isstring(o))
lua_error ("incorrect arguments to function `strlower'");
c = s = lua_copystring(o);
1993-07-28 15:18:00 +02:00
while (*c != 0)
{
*c = toupper(*c);
c++;
}
lua_pushstring(s);
free(s);
1993-07-28 15:18:00 +02:00
}
/*
** Open string library
*/
void strlib_open (void)
{
lua_register ("strfind", str_find);
lua_register ("strlen", str_len);
lua_register ("strsub", str_sub);
lua_register ("strlower", str_lower);
lua_register ("strupper", str_upper);
}