aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWaldemar Celes <celes@tecgraf.puc-rio.br>1993-12-17 16:41:19 -0200
committerWaldemar Celes <celes@tecgraf.puc-rio.br>1993-12-17 16:41:19 -0200
commit212fdf861acfc0ecff0adec54501a0766183193d (patch)
tree1250acd202ea523681d589e13c96d87d4be8b543
parent26c3684c4f8614de517f5e2ed550972a5dcb5771 (diff)
downloadlua-212fdf861acfc0ecff0adec54501a0766183193d.tar.gz
lua-212fdf861acfc0ecff0adec54501a0766183193d.tar.bz2
lua-212fdf861acfc0ecff0adec54501a0766183193d.zip
String library to LUA
-rw-r--r--strlib.c26
-rw-r--r--strlib.h13
2 files changed, 27 insertions, 12 deletions
diff --git a/strlib.c b/strlib.c
index efd01e9b..87622e9c 100644
--- a/strlib.c
+++ b/strlib.c
@@ -1,12 +1,10 @@
1/* 1/*
2** strlib.c 2** strlib.c
3** String library to LUA 3** String library to LUA
4**
5** Waldemar Celes Filho
6** TeCGraf - PUC-Rio
7** 19 May 93
8*/ 4*/
9 5
6char *rcs_strlib="$Id: $";
7
10#include <stdlib.h> 8#include <stdlib.h>
11#include <string.h> 9#include <string.h>
12#include <ctype.h> 10#include <ctype.h>
@@ -21,16 +19,18 @@
21*/ 19*/
22static void str_find (void) 20static void str_find (void)
23{ 21{
24 int n; 22 char *s1, *s2, *f;
25 char *s1, *s2;
26 lua_Object o1 = lua_getparam (1); 23 lua_Object o1 = lua_getparam (1);
27 lua_Object o2 = lua_getparam (2); 24 lua_Object o2 = lua_getparam (2);
28 if (!lua_isstring(o1) || !lua_isstring(o2)) 25 if (!lua_isstring(o1) || !lua_isstring(o2))
29 { lua_error ("incorrect arguments to function `strfind'"); return; } 26 { lua_error ("incorrect arguments to function `strfind'"); return; }
30 s1 = lua_getstring(o1); 27 s1 = lua_getstring(o1);
31 s2 = lua_getstring(o2); 28 s2 = lua_getstring(o2);
32 n = strstr(s1,s2) - s1 + 1; 29 f = strstr(s1,s2);
33 lua_pushnumber (n); 30 if (f != NULL)
31 lua_pushnumber (f-s1+1);
32 else
33 lua_pushnil();
34} 34}
35 35
36/* 36/*
@@ -59,13 +59,15 @@ static void str_sub (void)
59 lua_Object o1 = lua_getparam (1); 59 lua_Object o1 = lua_getparam (1);
60 lua_Object o2 = lua_getparam (2); 60 lua_Object o2 = lua_getparam (2);
61 lua_Object o3 = lua_getparam (3); 61 lua_Object o3 = lua_getparam (3);
62 if (!lua_isstring(o1) || !lua_isnumber(o2) || !lua_isnumber(o3)) 62 if (!lua_isstring(o1) || !lua_isnumber(o2))
63 { lua_error ("incorrect arguments to function `strsub'"); return; } 63 { lua_error ("incorrect arguments to function `strsub'"); return; }
64 s = strdup (lua_getstring(o1)); 64 if (o3 != NULL && !lua_isnumber(o3))
65 { lua_error ("incorrect third argument to function `strsub'"); return; }
66 s = lua_copystring(o1);
65 start = lua_getnumber (o2); 67 start = lua_getnumber (o2);
66 end = lua_getnumber (o3); 68 end = o3 == NULL ? strlen(s) : lua_getnumber (o3);
67 if (end < start || start < 1 || end > strlen(s)) 69 if (end < start || start < 1 || end > strlen(s))
68 lua_pushstring (""); 70 lua_pushstring("");
69 else 71 else
70 { 72 {
71 s[end] = 0; 73 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 @@
1/*
2** String library to LUA
3** TeCGraf - PUC-Rio
4** $Id: $
5*/
6
7
8#ifndef strlib_h
9
10void strlib_open (void);
11
12#endif
13