diff options
| author | Waldemar Celes <celes@tecgraf.puc-rio.br> | 1993-12-17 16:41:19 -0200 |
|---|---|---|
| committer | Waldemar Celes <celes@tecgraf.puc-rio.br> | 1993-12-17 16:41:19 -0200 |
| commit | 212fdf861acfc0ecff0adec54501a0766183193d (patch) | |
| tree | 1250acd202ea523681d589e13c96d87d4be8b543 | |
| parent | 26c3684c4f8614de517f5e2ed550972a5dcb5771 (diff) | |
| download | lua-github-212fdf861acfc0ecff0adec54501a0766183193d.tar.gz | |
String library to LUA
| -rw-r--r-- | strlib.c | 26 | ||||
| -rw-r--r-- | strlib.h | 13 |
2 files changed, 27 insertions, 12 deletions
@@ -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 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 + |
