diff options
Diffstat (limited to '')
| -rw-r--r-- | strlib.c | 135 |
1 files changed, 102 insertions, 33 deletions
| @@ -3,7 +3,7 @@ | |||
| 3 | ** String library to LUA | 3 | ** String library to LUA |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | char *rcs_strlib="$Id: strlib.c,v 1.13 1995/10/09 12:49:21 roberto Exp roberto $"; | 6 | char *rcs_strlib="$Id: strlib.c,v 1.14 1995/11/10 17:54:31 roberto Exp roberto $"; |
| 7 | 7 | ||
| 8 | #include <string.h> | 8 | #include <string.h> |
| 9 | #include <stdio.h> | 9 | #include <stdio.h> |
| @@ -37,13 +37,30 @@ float lua_check_number (int numArg, char *funcname) | |||
| 37 | return lua_getnumber(o); | 37 | return lua_getnumber(o); |
| 38 | } | 38 | } |
| 39 | 39 | ||
| 40 | static char *newstring (char *s) | 40 | char *luaI_addchar (int c) |
| 41 | { | 41 | { |
| 42 | char *ns = (char *)malloc(strlen(s)+1); | 42 | static char *buff = NULL; |
| 43 | if (ns == 0) | 43 | static int max = 0; |
| 44 | lua_error("not enough memory for new string"); | 44 | static int n = 0; |
| 45 | strcpy(ns, s); | 45 | if (n >= max) |
| 46 | return ns; | 46 | { |
| 47 | if (max == 0) | ||
| 48 | { | ||
| 49 | max = 100; | ||
| 50 | buff = (char *)malloc(max); | ||
| 51 | } | ||
| 52 | else | ||
| 53 | { | ||
| 54 | max *= 2; | ||
| 55 | buff = (char *)realloc(buff, max); | ||
| 56 | } | ||
| 57 | if (buff == NULL) | ||
| 58 | lua_error("memory overflow"); | ||
| 59 | } | ||
| 60 | buff[n++] = c; | ||
| 61 | if (c == 0) | ||
| 62 | n = 0; /* prepare for next string */ | ||
| 63 | return buff; | ||
| 47 | } | 64 | } |
| 48 | 65 | ||
| 49 | 66 | ||
| @@ -108,30 +125,17 @@ static void str_sub (void) | |||
| 108 | } | 125 | } |
| 109 | 126 | ||
| 110 | /* | 127 | /* |
| 111 | ** Convert a string according to given function. | ||
| 112 | */ | ||
| 113 | typedef int (*strfunc)(int s); | ||
| 114 | static void str_apply (strfunc f, char *funcname) | ||
| 115 | { | ||
| 116 | char *s, *c; | ||
| 117 | c = s = newstring(lua_check_string(1, funcname)); | ||
| 118 | while (*c != 0) | ||
| 119 | { | ||
| 120 | *c = f(*c); | ||
| 121 | c++; | ||
| 122 | } | ||
| 123 | lua_pushstring(s); | ||
| 124 | free(s); | ||
| 125 | } | ||
| 126 | |||
| 127 | /* | ||
| 128 | ** Convert a string to lower case. | 128 | ** Convert a string to lower case. |
| 129 | ** LUA interface: | 129 | ** LUA interface: |
| 130 | ** lowercase = strlower (string) | 130 | ** lowercase = strlower (string) |
| 131 | */ | 131 | */ |
| 132 | static void str_lower (void) | 132 | static void str_lower (void) |
| 133 | { | 133 | { |
| 134 | str_apply(tolower, "strlower"); | 134 | char *s = lua_check_string(1, "strlower"); |
| 135 | luaI_addchar(0); | ||
| 136 | while (*s) | ||
| 137 | luaI_addchar(tolower(*s++)); | ||
| 138 | lua_pushstring(luaI_addchar(0)); | ||
| 135 | } | 139 | } |
| 136 | 140 | ||
| 137 | 141 | ||
| @@ -142,7 +146,11 @@ static void str_lower (void) | |||
| 142 | */ | 146 | */ |
| 143 | static void str_upper (void) | 147 | static void str_upper (void) |
| 144 | { | 148 | { |
| 145 | str_apply(toupper, "strupper"); | 149 | char *s = lua_check_string(1, "strupper"); |
| 150 | luaI_addchar(0); | ||
| 151 | while (*s) | ||
| 152 | luaI_addchar(toupper(*s++)); | ||
| 153 | lua_pushstring(luaI_addchar(0)); | ||
| 146 | } | 154 | } |
| 147 | 155 | ||
| 148 | /* | 156 | /* |
| @@ -162,21 +170,81 @@ static void str_ascii (void) | |||
| 162 | /* | 170 | /* |
| 163 | ** converts one or more integers to chars in a string | 171 | ** converts one or more integers to chars in a string |
| 164 | */ | 172 | */ |
| 165 | #define maxparams 50 | ||
| 166 | static void str_int2str (void) | 173 | static void str_int2str (void) |
| 167 | { | 174 | { |
| 168 | char s[maxparams+1]; | ||
| 169 | int i = 0; | 175 | int i = 0; |
| 176 | luaI_addchar(0); | ||
| 170 | while (lua_getparam(++i) != LUA_NOOBJECT) | 177 | while (lua_getparam(++i) != LUA_NOOBJECT) |
| 178 | luaI_addchar((int)lua_check_number(i, "int2str")); | ||
| 179 | lua_pushstring(luaI_addchar(0)); | ||
| 180 | } | ||
| 181 | |||
| 182 | |||
| 183 | #define MAX_CONVERTION 2000 | ||
| 184 | #define MAX_FORMAT 50 | ||
| 185 | |||
| 186 | static void io_format (void) | ||
| 187 | { | ||
| 188 | int arg = 1; | ||
| 189 | char *strfrmt = lua_check_string(arg++, "format"); | ||
| 190 | luaI_addchar(0); /* initialize */ | ||
| 191 | while (*strfrmt) | ||
| 171 | { | 192 | { |
| 172 | if (i > maxparams) | 193 | if (*strfrmt != '%') |
| 173 | lua_error("too many parameters to function `int2str'"); | 194 | luaI_addchar(*strfrmt++); |
| 174 | s[i-1] = (int)lua_check_number(i, "int2str"); | 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])) | ||
| 206 | { | ||
| 207 | if (isdigit(form[i])) | ||
| 208 | { | ||
| 209 | size = size*10 + form[i]-'0'; | ||
| 210 | if (size >= MAX_CONVERTION) | ||
| 211 | lua_error("format size/precision too long in function `format'"); | ||
| 212 | } | ||
| 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++; | ||
| 218 | } | ||
| 219 | form[i+1] = 0; /* ends string */ | ||
| 220 | switch (form[i]) | ||
| 221 | { | ||
| 222 | case 's': | ||
| 223 | { | ||
| 224 | char *s = lua_check_string(arg++, "format"); | ||
| 225 | if (strlen(s) >= MAX_CONVERTION) | ||
| 226 | lua_error("string argument too long in function `format'"); | ||
| 227 | sprintf(buff, form, s); | ||
| 228 | break; | ||
| 229 | } | ||
| 230 | case 'c': case 'd': case 'i': case 'o': | ||
| 231 | case 'u': case 'x': case 'X': | ||
| 232 | sprintf(buff, form, (int)lua_check_number(arg++, "format")); | ||
| 233 | break; | ||
| 234 | case 'e': case 'E': case 'f': case 'g': | ||
| 235 | sprintf(buff, form, lua_check_number(arg++, "format")); | ||
| 236 | break; | ||
| 237 | default: /* also treat cases 'pnLlh' */ | ||
| 238 | lua_error("invalid format option in function `format'"); | ||
| 239 | } | ||
| 240 | for (i=0; buff[i]; i++) /* move formated value to result */ | ||
| 241 | luaI_addchar(buff[i]); | ||
| 242 | } | ||
| 175 | } | 243 | } |
| 176 | s[i-1] = 0; | 244 | lua_pushstring(luaI_addchar(0)); /* push the result */ |
| 177 | lua_pushstring(s); | ||
| 178 | } | 245 | } |
| 179 | 246 | ||
| 247 | |||
| 180 | /* | 248 | /* |
| 181 | ** Open string library | 249 | ** Open string library |
| 182 | */ | 250 | */ |
| @@ -189,4 +257,5 @@ void strlib_open (void) | |||
| 189 | lua_register ("strupper", str_upper); | 257 | lua_register ("strupper", str_upper); |
| 190 | lua_register ("ascii", str_ascii); | 258 | lua_register ("ascii", str_ascii); |
| 191 | lua_register ("int2str", str_int2str); | 259 | lua_register ("int2str", str_int2str); |
| 260 | lua_register ("format", io_format); | ||
| 192 | } | 261 | } |
