From 212fdf861acfc0ecff0adec54501a0766183193d Mon Sep 17 00:00:00 2001 From: Waldemar Celes Date: Fri, 17 Dec 1993 16:41:19 -0200 Subject: [PATCH] String library to LUA --- strlib.c | 26 ++++++++++++++------------ strlib.h | 13 +++++++++++++ 2 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 strlib.h diff --git a/strlib.c b/strlib.c index efd01e9b..87622e9c 100644 --- 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 #include #include @@ -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 new file mode 100644 index 00000000..3e650be5 --- /dev/null +++ b/strlib.h @@ -0,0 +1,13 @@ +/* +** String library to LUA +** TeCGraf - PUC-Rio +** $Id: $ +*/ + + +#ifndef strlib_h + +void strlib_open (void); + +#endif +