1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
/*
** strlib.c
** String library to LUA
*/
char *rcs_strlib="$Id: strlib.c,v 1.11 1995/02/02 20:05:37 roberto Exp roberto $";
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "lua.h"
#include "lualib.h"
static char *newstring (lua_Object o)
{
char *s = lua_getstring(o);
char *ns = (char *)malloc(strlen(s)+1);
if (ns == 0)
lua_error("not enough memory for new string");
strcpy(ns, s);
return ns;
}
/*
** Return the position of the first caracter of a substring into a string
** LUA interface:
** n = strfind (string, substring, init, end)
*/
static void str_find (void)
{
char *s1, *s2, *f;
int init;
lua_Object o1 = lua_getparam (1);
lua_Object o2 = lua_getparam (2);
lua_Object o3 = lua_getparam (3);
lua_Object o4 = lua_getparam (4);
if (!lua_isstring(o1) || !lua_isstring(o2))
lua_error ("incorrect arguments to function `strfind'");
if (o3 == LUA_NOOBJECT)
init = 0;
else if (lua_isnumber(o3))
init = lua_getnumber(o3)-1;
else
{
lua_error ("incorrect arguments to function `strfind'");
return; /* to avoid warnings */
}
s1 = lua_getstring(o1);
s2 = lua_getstring(o2);
f = strstr(s1+init,s2);
if (f != NULL)
{
int pos = f-s1+1;
if (o4 == LUA_NOOBJECT)
lua_pushnumber (pos);
else if (!lua_isnumber(o4))
lua_error ("incorrect arguments to function `strfind'");
else if ((int)lua_getnumber(o4) >= pos+strlen(s2)-1)
lua_pushnumber (pos);
else
lua_pushnil();
}
else
lua_pushnil();
}
/*
** Return the string length
** LUA interface:
** n = strlen (string)
*/
static void str_len (void)
{
lua_Object o = lua_getparam (1);
if (!lua_isstring(o))
lua_error ("incorrect arguments to function `strlen'");
lua_pushnumber(strlen(lua_getstring(o)));
}
/*
** Return the substring of a string, from start to end
** LUA interface:
** substring = strsub (string, start, end)
*/
static void str_sub (void)
{
int start, end;
char *s;
lua_Object o1 = lua_getparam (1);
lua_Object o2 = lua_getparam (2);
lua_Object o3 = lua_getparam (3);
if (!lua_isstring(o1) || !lua_isnumber(o2))
lua_error ("incorrect arguments to function `strsub'");
if (o3 != LUA_NOOBJECT && !lua_isnumber(o3))
lua_error ("incorrect third argument to function `strsub'");
s = newstring(o1);
start = lua_getnumber (o2);
end = o3 == LUA_NOOBJECT ? strlen(s) : lua_getnumber (o3);
if (end < start || start < 1 || end > strlen(s))
lua_pushliteral("");
else
{
s[end] = 0;
lua_pushstring (&s[start-1]);
}
free(s);
}
/*
** Convert a string to lower case.
** LUA interface:
** lowercase = strlower (string)
*/
static void str_lower (void)
{
char *s, *c;
lua_Object o = lua_getparam (1);
if (!lua_isstring(o))
lua_error ("incorrect arguments to function `strlower'");
c = s = newstring(o);
while (*c != 0)
{
*c = tolower(*c);
c++;
}
lua_pushstring(s);
free(s);
}
/*
** Convert a string to upper case.
** LUA interface:
** uppercase = strupper (string)
*/
static void str_upper (void)
{
char *s, *c;
lua_Object o = lua_getparam (1);
if (!lua_isstring(o))
lua_error ("incorrect arguments to function `strlower'");
c = s = newstring(o);
while (*c != 0)
{
*c = toupper(*c);
c++;
}
lua_pushstring(s);
free(s);
}
/*
** Open string library
*/
void strlib_open (void)
{
lua_register ("strfind", str_find);
lua_register ("strlen", str_len);
lua_register ("strsub", str_sub);
lua_register ("strlower", str_lower);
lua_register ("strupper", str_upper);
}
|