3 ** String library to LUA
6 char *rcs_strlib
="$Id: strlib.c,v 1.12 1995/02/06 19:37:51 roberto Exp $";
16 static char *newstring (lua_Object o
)
18 char *s
= lua_getstring(o
);
19 char *ns
= (char *)malloc(strlen(s
)+1);
21 lua_error("not enough memory for new string");
28 ** Return the position of the first caracter of a substring into a string
30 ** n = strfind (string, substring, init, end)
32 static void str_find (void)
36 lua_Object o1
= lua_getparam (1);
37 lua_Object o2
= lua_getparam (2);
38 lua_Object o3
= lua_getparam (3);
39 lua_Object o4
= lua_getparam (4);
40 if (!lua_isstring(o1
) || !lua_isstring(o2
))
41 lua_error ("incorrect arguments to function `strfind'");
42 if (o3
== LUA_NOOBJECT
)
44 else if (lua_isnumber(o3
))
45 init
= lua_getnumber(o3
)-1;
48 lua_error ("incorrect arguments to function `strfind'");
49 return; /* to avoid warnings */
51 s1
= lua_getstring(o1
);
52 s2
= lua_getstring(o2
);
53 f
= strstr(s1
+init
,s2
);
57 if (o4
== LUA_NOOBJECT
)
59 else if (!lua_isnumber(o4
))
60 lua_error ("incorrect arguments to function `strfind'");
61 else if ((int)lua_getnumber(o4
) >= pos
+strlen(s2
)-1)
71 ** Return the string length
73 ** n = strlen (string)
75 static void str_len (void)
77 lua_Object o
= lua_getparam (1);
79 lua_error ("incorrect arguments to function `strlen'");
80 lua_pushnumber(strlen(lua_getstring(o
)));
85 ** Return the substring of a string, from start to end
87 ** substring = strsub (string, start, end)
89 static void str_sub (void)
93 lua_Object o1
= lua_getparam (1);
94 lua_Object o2
= lua_getparam (2);
95 lua_Object o3
= lua_getparam (3);
96 if (!lua_isstring(o1
) || !lua_isnumber(o2
))
97 lua_error ("incorrect arguments to function `strsub'");
98 if (o3
!= LUA_NOOBJECT
&& !lua_isnumber(o3
))
99 lua_error ("incorrect third argument to function `strsub'");
101 start
= lua_getnumber (o2
);
102 end
= o3
== LUA_NOOBJECT
? strlen(s
) : lua_getnumber (o3
);
103 if (end
< start
|| start
< 1 || end
> strlen(s
))
108 lua_pushstring (&s
[start
-1]);
114 ** Convert a string to lower case.
116 ** lowercase = strlower (string)
118 static void str_lower (void)
121 lua_Object o
= lua_getparam (1);
122 if (!lua_isstring(o
))
123 lua_error ("incorrect arguments to function `strlower'");
124 c
= s
= newstring(o
);
136 ** Convert a string to upper case.
138 ** uppercase = strupper (string)
140 static void str_upper (void)
143 lua_Object o
= lua_getparam (1);
144 if (!lua_isstring(o
))
145 lua_error ("incorrect arguments to function `strlower'");
146 c
= s
= newstring(o
);
158 ** Open string library
160 void strlib_open (void)
162 lua_register ("strfind", str_find
);
163 lua_register ("strlen", str_len
);
164 lua_register ("strsub", str_sub
);
165 lua_register ("strlower", str_lower
);
166 lua_register ("strupper", str_upper
);