3 ** String library to LUA
6 char *rcs_strlib
="$Id: strlib.c,v 1.23 1996/04/30 21:13:55 roberto Exp $";
18 void lua_arg_error(char *funcname
)
21 sprintf(buff
, "incorrect arguments to function `%s'", funcname
);
25 char *lua_check_string (int numArg
, char *funcname
)
27 lua_Object o
= lua_getparam(numArg
);
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
);
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
;
57 buff
= (char *)malloc(max
);
62 buff
= (char *)realloc(buff
, max
);
65 lua_error("memory overflow");
69 n
= 0; /* prepare for next string */
75 ** Return the position of the first caracter of a substring into a string
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
;
88 if (lua_opt_number(4, INT_MAX
, "strfind") >= pos
+strlen(s2
)-1)
98 ** Return the string length
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
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
))
125 luaI_addchar(s
[start
++ - 1]);
126 lua_pushstring (luaI_addchar(0));
131 ** Convert a string to lower case.
133 ** lowercase = strlower (string)
135 static void str_lower (void)
137 char *s
= lua_check_string(1, "strlower");
140 luaI_addchar(tolower(*s
++));
141 lua_pushstring(luaI_addchar(0));
146 ** Convert a string to upper case.
148 ** uppercase = strupper (string)
150 static void str_upper (void)
152 char *s
= lua_check_string(1, "strupper");
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
)
176 if (*s
== '"' || *s
== '\\' || *s
== '\n')
183 #define MAX_CONVERTION 2000
184 #define MAX_FORMAT 50
186 static void str_format (void)
189 char *strfrmt
= lua_check_string(arg
++, "format");
190 luaI_addchar(0); /* initialize */
194 luaI_addchar(*strfrmt
++);
195 else if (*++strfrmt
== '%')
196 luaI_addchar(*strfrmt
++); /* %% */
199 char form
[MAX_FORMAT
]; /* store the format ('%...') */
200 char buff
[MAX_CONVERTION
]; /* store the formated value */
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 */
223 luaI_addquoted(lua_check_string(arg
++, "format"));
224 buff
[0] = '\0'; /* addchar already done */
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
);
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"));
238 case 'e': case 'E': case 'f': case 'g':
239 sprintf(buff
, form
, lua_check_number(arg
++, "format"));
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
)
256 lua_register(l
[i
].name
, l
[i
].func
);
259 static struct lua_reg strlib
[] = {
260 {"strfind", str_find
},
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])));