Imported from ../lua-2.5.tar.gz.
[lua.git] / clients / lib / old / strlib.c
blob6a99871c492b20c59293f07f26cff028f9a5007c
1 /*
2 ** strlib.c
3 ** String library to LUA
4 */
6 char *rcs_strlib="$Id: strlib.c,v 1.23 1996/04/30 21:13:55 roberto Exp $";
8 #include <string.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <ctype.h>
12 #include <limits.h>
14 #include "lua.h"
15 #include "lualib.h"
18 void lua_arg_error(char *funcname)
20 char buff[100];
21 sprintf(buff, "incorrect arguments to function `%s'", funcname);
22 lua_error(buff);
25 char *lua_check_string (int numArg, char *funcname)
27 lua_Object o = lua_getparam(numArg);
28 if (!lua_isstring(o))
29 lua_arg_error(funcname);
30 return lua_getstring(o);
33 double lua_check_number (int numArg, char *funcname)
35 lua_Object o = lua_getparam(numArg);
36 if (!lua_isnumber(o))
37 lua_arg_error(funcname);
38 return lua_getnumber(o);
41 static int lua_opt_number (int numArg, int def, char *funcname)
43 return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
44 (int)lua_check_number(numArg, funcname);
47 char *luaI_addchar (int c)
49 static char *buff = NULL;
50 static int max = 0;
51 static int n = 0;
52 if (n >= max)
54 if (max == 0)
56 max = 100;
57 buff = (char *)malloc(max);
59 else
61 max *= 2;
62 buff = (char *)realloc(buff, max);
64 if (buff == NULL)
65 lua_error("memory overflow");
67 buff[n++] = c;
68 if (c == 0)
69 n = 0; /* prepare for next string */
70 return buff;
75 ** Return the position of the first caracter of a substring into a string
76 ** LUA interface:
77 ** n = strfind (string, substring, init, end)
79 static void str_find (void)
81 char *s1 = lua_check_string(1, "strfind");
82 char *s2 = lua_check_string(2, "strfind");
83 int init = lua_opt_number(3, 1, "strfind") - 1;
84 char *f = (init>=0 && init<=strlen(s1)) ? strstr(s1+init,s2) : NULL;
85 if (f != NULL)
87 int pos = f-s1+1;
88 if (lua_opt_number(4, INT_MAX, "strfind") >= pos+strlen(s2)-1)
89 lua_pushnumber (pos);
90 else
91 lua_pushnil();
93 else
94 lua_pushnil();
98 ** Return the string length
99 ** LUA interface:
100 ** n = strlen (string)
102 static void str_len (void)
104 char *s = lua_check_string(1, "strlen");
105 lua_pushnumber(strlen(s));
110 ** Return the substring of a string, from start to end
111 ** LUA interface:
112 ** substring = strsub (string, start, end)
114 static void str_sub (void)
116 char *s = lua_check_string(1, "strsub");
117 int start = (int)lua_check_number(2, "strsub");
118 int end = lua_opt_number(3, strlen(s), "strsub");
119 if (end < start || start < 1 || end > strlen(s))
120 lua_pushliteral("");
121 else
123 luaI_addchar(0);
124 while (start <= end)
125 luaI_addchar(s[start++ - 1]);
126 lua_pushstring (luaI_addchar(0));
131 ** Convert a string to lower case.
132 ** LUA interface:
133 ** lowercase = strlower (string)
135 static void str_lower (void)
137 char *s = lua_check_string(1, "strlower");
138 luaI_addchar(0);
139 while (*s)
140 luaI_addchar(tolower(*s++));
141 lua_pushstring(luaI_addchar(0));
146 ** Convert a string to upper case.
147 ** LUA interface:
148 ** uppercase = strupper (string)
150 static void str_upper (void)
152 char *s = lua_check_string(1, "strupper");
153 luaI_addchar(0);
154 while (*s)
155 luaI_addchar(toupper(*s++));
156 lua_pushstring(luaI_addchar(0));
160 ** get ascii value of a character in a string
162 static void str_ascii (void)
164 char *s = lua_check_string(1, "ascii");
165 int pos = lua_opt_number(2, 1, "ascii") - 1;
166 if (pos<0 || pos>=strlen(s))
167 lua_arg_error("ascii");
168 lua_pushnumber(s[pos]);
171 void luaI_addquoted (char *s)
173 luaI_addchar('"');
174 for (; *s; s++)
176 if (*s == '"' || *s == '\\' || *s == '\n')
177 luaI_addchar('\\');
178 luaI_addchar(*s);
180 luaI_addchar('"');
183 #define MAX_CONVERTION 2000
184 #define MAX_FORMAT 50
186 static void str_format (void)
188 int arg = 1;
189 char *strfrmt = lua_check_string(arg++, "format");
190 luaI_addchar(0); /* initialize */
191 while (*strfrmt)
193 if (*strfrmt != '%')
194 luaI_addchar(*strfrmt++);
195 else if (*++strfrmt == '%')
196 luaI_addchar(*strfrmt++); /* %% */
197 else
198 { /* format item */
199 char form[MAX_FORMAT]; /* store the format ('%...') */
200 char buff[MAX_CONVERTION]; /* store the formated value */
201 int size = 0;
202 int i = 0;
203 form[i++] = '%';
204 form[i] = *strfrmt++;
205 while (!isalpha(form[i]))
207 if (isdigit(form[i]))
209 size = size*10 + form[i]-'0';
210 if (size >= MAX_CONVERTION)
211 lua_error("format size/precision too long in function `format'");
213 else if (form[i] == '.')
214 size = 0; /* re-start */
215 if (++i >= MAX_FORMAT)
216 lua_error("bad format in function `format'");
217 form[i] = *strfrmt++;
219 form[i+1] = 0; /* ends string */
220 switch (form[i])
222 case 'q':
223 luaI_addquoted(lua_check_string(arg++, "format"));
224 buff[0] = '\0'; /* addchar already done */
225 break;
226 case 's':
228 char *s = lua_check_string(arg++, "format");
229 if (strlen(s) >= MAX_CONVERTION)
230 lua_error("string argument too long in function `format'");
231 sprintf(buff, form, s);
232 break;
234 case 'c': case 'd': case 'i': case 'o':
235 case 'u': case 'x': case 'X':
236 sprintf(buff, form, (int)lua_check_number(arg++, "format"));
237 break;
238 case 'e': case 'E': case 'f': case 'g':
239 sprintf(buff, form, lua_check_number(arg++, "format"));
240 break;
241 default: /* also treat cases 'pnLlh' */
242 lua_error("invalid format option in function `format'");
244 for (i=0; buff[i]; i++) /* move formated value to result */
245 luaI_addchar(buff[i]);
248 lua_pushstring(luaI_addchar(0)); /* push the result */
252 void luaI_openlib (struct lua_reg *l, int n)
254 int i;
255 for (i=0; i<n; i++)
256 lua_register(l[i].name, l[i].func);
259 static struct lua_reg strlib[] = {
260 {"strfind", str_find},
261 {"strlen", str_len},
262 {"strsub", str_sub},
263 {"strlower", str_lower},
264 {"strupper", str_upper},
265 {"ascii", str_ascii},
266 {"format", str_format}
271 ** Open string library
273 void strlib_open (void)
275 luaI_openlib(strlib, (sizeof(strlib)/sizeof(strlib[0])));