Imported from ../lua-2.1.tar.gz.
[lua.git] / clients / lib / strlib.c
blob6b92a30bc1b1355320eea62ce4678a297b0d2bc6
1 /*
2 ** strlib.c
3 ** String library to LUA
4 */
6 char *rcs_strlib="$Id: strlib.c,v 1.12 1995/02/06 19:37:51 roberto Exp $";
8 #include <string.h>
9 #include <stdlib.h>
10 #include <ctype.h>
12 #include "lua.h"
13 #include "lualib.h"
16 static char *newstring (lua_Object o)
18 char *s = lua_getstring(o);
19 char *ns = (char *)malloc(strlen(s)+1);
20 if (ns == 0)
21 lua_error("not enough memory for new string");
22 strcpy(ns, s);
23 return ns;
28 ** Return the position of the first caracter of a substring into a string
29 ** LUA interface:
30 ** n = strfind (string, substring, init, end)
32 static void str_find (void)
34 char *s1, *s2, *f;
35 int init;
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)
43 init = 0;
44 else if (lua_isnumber(o3))
45 init = lua_getnumber(o3)-1;
46 else
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);
54 if (f != NULL)
56 int pos = f-s1+1;
57 if (o4 == LUA_NOOBJECT)
58 lua_pushnumber (pos);
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)
62 lua_pushnumber (pos);
63 else
64 lua_pushnil();
66 else
67 lua_pushnil();
71 ** Return the string length
72 ** LUA interface:
73 ** n = strlen (string)
75 static void str_len (void)
77 lua_Object o = lua_getparam (1);
78 if (!lua_isstring(o))
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
86 ** LUA interface:
87 ** substring = strsub (string, start, end)
89 static void str_sub (void)
91 int start, end;
92 char *s;
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'");
100 s = newstring(o1);
101 start = lua_getnumber (o2);
102 end = o3 == LUA_NOOBJECT ? strlen(s) : lua_getnumber (o3);
103 if (end < start || start < 1 || end > strlen(s))
104 lua_pushliteral("");
105 else
107 s[end] = 0;
108 lua_pushstring (&s[start-1]);
110 free(s);
114 ** Convert a string to lower case.
115 ** LUA interface:
116 ** lowercase = strlower (string)
118 static void str_lower (void)
120 char *s, *c;
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);
125 while (*c != 0)
127 *c = tolower(*c);
128 c++;
130 lua_pushstring(s);
131 free(s);
136 ** Convert a string to upper case.
137 ** LUA interface:
138 ** uppercase = strupper (string)
140 static void str_upper (void)
142 char *s, *c;
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);
147 while (*c != 0)
149 *c = toupper(*c);
150 c++;
152 lua_pushstring(s);
153 free(s);
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);