commit 212fdf861acfc0ecff0adec54501a0766183193d
parent 26c3684c4f8614de517f5e2ed550972a5dcb5771
Author: Waldemar Celes <celes@tecgraf.puc-rio.br>
Date: Fri, 17 Dec 1993 16:41:01 -0200
String library to LUA
Diffstat:
2 files changed, 27 insertions(+), 12 deletions(-)
diff --git a/strlib.c b/strlib.c
@@ -1,12 +1,10 @@
/*
** strlib.c
** String library to LUA
-**
-** Waldemar Celes Filho
-** TeCGraf - PUC-Rio
-** 19 May 93
*/
+char *rcs_strlib="$Id: $";
+
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
@@ -21,16 +19,18 @@
*/
static void str_find (void)
{
- int n;
- char *s1, *s2;
+ char *s1, *s2, *f;
lua_Object o1 = lua_getparam (1);
lua_Object o2 = lua_getparam (2);
if (!lua_isstring(o1) || !lua_isstring(o2))
{ lua_error ("incorrect arguments to function `strfind'"); return; }
s1 = lua_getstring(o1);
s2 = lua_getstring(o2);
- n = strstr(s1,s2) - s1 + 1;
- lua_pushnumber (n);
+ f = strstr(s1,s2);
+ if (f != NULL)
+ lua_pushnumber (f-s1+1);
+ else
+ lua_pushnil();
}
/*
@@ -59,13 +59,15 @@ static void str_sub (void)
lua_Object o1 = lua_getparam (1);
lua_Object o2 = lua_getparam (2);
lua_Object o3 = lua_getparam (3);
- if (!lua_isstring(o1) || !lua_isnumber(o2) || !lua_isnumber(o3))
+ if (!lua_isstring(o1) || !lua_isnumber(o2))
{ lua_error ("incorrect arguments to function `strsub'"); return; }
- s = strdup (lua_getstring(o1));
+ if (o3 != NULL && !lua_isnumber(o3))
+ { lua_error ("incorrect third argument to function `strsub'"); return; }
+ s = lua_copystring(o1);
start = lua_getnumber (o2);
- end = lua_getnumber (o3);
+ end = o3 == NULL ? strlen(s) : lua_getnumber (o3);
if (end < start || start < 1 || end > strlen(s))
- lua_pushstring ("");
+ lua_pushstring("");
else
{
s[end] = 0;
diff --git a/strlib.h b/strlib.h
@@ -0,0 +1,13 @@
+/*
+** String library to LUA
+** TeCGraf - PUC-Rio
+** $Id: $
+*/
+
+
+#ifndef strlib_h
+
+void strlib_open (void);
+
+#endif
+