diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-03-26 19:23:15 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-03-26 19:23:15 -0300 |
| commit | 264f8c5e7bd168de2f0ca07399e6fc70d5a820d3 (patch) | |
| tree | 2cb28f94f0b7dd131ee16679df813a0b52a852c7 /strlib.c | |
| parent | 9e9e2ea287a24871e6e35faef52ae236bf85f8ae (diff) | |
| download | lua-264f8c5e7bd168de2f0ca07399e6fc70d5a820d3.tar.gz lua-264f8c5e7bd168de2f0ca07399e6fc70d5a820d3.tar.bz2 lua-264f8c5e7bd168de2f0ca07399e6fc70d5a820d3.zip | |
new (internal?) functions to manipulate userdata
Diffstat (limited to 'strlib.c')
| -rw-r--r-- | strlib.c | 44 |
1 files changed, 24 insertions, 20 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.36 1997/03/17 17:01:10 roberto Exp roberto $"; | 6 | char *rcs_strlib="$Id: strlib.c,v 1.37 1997/03/18 15:30:50 roberto Exp roberto $"; |
| 7 | 7 | ||
| 8 | #include <string.h> | 8 | #include <string.h> |
| 9 | #include <stdio.h> | 9 | #include <stdio.h> |
| @@ -24,7 +24,7 @@ struct lbuff { | |||
| 24 | static struct lbuff lbuffer = {NULL, 0, 0}; | 24 | static struct lbuff lbuffer = {NULL, 0, 0}; |
| 25 | 25 | ||
| 26 | 26 | ||
| 27 | static char *lua_strbuffer (unsigned long size) | 27 | static char *luaL_strbuffer (unsigned long size) |
| 28 | { | 28 | { |
| 29 | if (size > lbuffer.max) { | 29 | if (size > lbuffer.max) { |
| 30 | /* ANSI "realloc" doesn't need this test, but some machines (Sun!) | 30 | /* ANSI "realloc" doesn't need this test, but some machines (Sun!) |
| @@ -39,20 +39,24 @@ static char *lua_strbuffer (unsigned long size) | |||
| 39 | 39 | ||
| 40 | static char *openspace (unsigned long size) | 40 | static char *openspace (unsigned long size) |
| 41 | { | 41 | { |
| 42 | char *buff = lua_strbuffer(lbuffer.size+size); | 42 | char *buff = luaL_strbuffer(lbuffer.size+size); |
| 43 | return buff+lbuffer.size; | 43 | return buff+lbuffer.size; |
| 44 | } | 44 | } |
| 45 | 45 | ||
| 46 | char *luaI_addchar (int c) | 46 | char *luaI_addchar (int c) |
| 47 | { | 47 | { |
| 48 | if (lbuffer.size >= lbuffer.max) | 48 | if (lbuffer.size >= lbuffer.max) |
| 49 | lua_strbuffer(lbuffer.max == 0 ? 100 : lbuffer.max*2); | 49 | luaL_strbuffer(lbuffer.max == 0 ? 100 : lbuffer.max*2); |
| 50 | lbuffer.b[lbuffer.size++] = c; | 50 | lbuffer.b[lbuffer.size++] = c; |
| 51 | if (c == 0) | ||
| 52 | lbuffer.size = 0; /* prepare for next string */ | ||
| 53 | return lbuffer.b; | 51 | return lbuffer.b; |
| 54 | } | 52 | } |
| 55 | 53 | ||
| 54 | void luaI_emptybuff (void) | ||
| 55 | { | ||
| 56 | lbuffer.size = 0; /* prepare for next string */ | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 56 | static void addnchar (char *s, int n) | 60 | static void addnchar (char *s, int n) |
| 57 | { | 61 | { |
| 58 | char *b = openspace(n); | 62 | char *b = openspace(n); |
| @@ -75,7 +79,7 @@ static void str_tok (void) | |||
| 75 | lua_Object t = lua_createtable(); | 79 | lua_Object t = lua_createtable(); |
| 76 | int i = 1; | 80 | int i = 1; |
| 77 | /* As strtok changes s1, and s1 is "constant", make a copy of it */ | 81 | /* As strtok changes s1, and s1 is "constant", make a copy of it */ |
| 78 | s1 = strcpy(lua_strbuffer(strlen(s1+1)), s1); | 82 | s1 = strcpy(luaL_strbuffer(strlen(s1+1)), s1); |
| 79 | while ((s1 = strtok(s1, del)) != NULL) { | 83 | while ((s1 = strtok(s1, del)) != NULL) { |
| 80 | lua_pushobject(t); | 84 | lua_pushobject(t); |
| 81 | lua_pushnumber(i++); | 85 | lua_pushnumber(i++); |
| @@ -105,7 +109,7 @@ static void str_sub (void) | |||
| 105 | long start = (long)luaL_check_number(2, "strsub"); | 109 | long start = (long)luaL_check_number(2, "strsub"); |
| 106 | long end = (long)luaL_opt_number(3, strlen(s), "strsub"); | 110 | long end = (long)luaL_opt_number(3, strlen(s), "strsub"); |
| 107 | if (1 <= start && start <= end && end <= strlen(s)) { | 111 | if (1 <= start && start <= end && end <= strlen(s)) { |
| 108 | luaI_addchar(0); | 112 | luaI_emptybuff(); |
| 109 | addnchar(s+start-1, end-start+1); | 113 | addnchar(s+start-1, end-start+1); |
| 110 | lua_pushstring(luaI_addchar(0)); | 114 | lua_pushstring(luaI_addchar(0)); |
| 111 | } | 115 | } |
| @@ -118,7 +122,7 @@ static void str_sub (void) | |||
| 118 | static void str_lower (void) | 122 | static void str_lower (void) |
| 119 | { | 123 | { |
| 120 | char *s = luaL_check_string(1, "strlower"); | 124 | char *s = luaL_check_string(1, "strlower"); |
| 121 | luaI_addchar(0); | 125 | luaI_emptybuff(); |
| 122 | while (*s) | 126 | while (*s) |
| 123 | luaI_addchar(tolower((unsigned char)*s++)); | 127 | luaI_addchar(tolower((unsigned char)*s++)); |
| 124 | lua_pushstring(luaI_addchar(0)); | 128 | lua_pushstring(luaI_addchar(0)); |
| @@ -130,7 +134,7 @@ static void str_lower (void) | |||
| 130 | static void str_upper (void) | 134 | static void str_upper (void) |
| 131 | { | 135 | { |
| 132 | char *s = luaL_check_string(1, "strupper"); | 136 | char *s = luaL_check_string(1, "strupper"); |
| 133 | luaI_addchar(0); | 137 | luaI_emptybuff(); |
| 134 | while (*s) | 138 | while (*s) |
| 135 | luaI_addchar(toupper((unsigned char)*s++)); | 139 | luaI_addchar(toupper((unsigned char)*s++)); |
| 136 | lua_pushstring(luaI_addchar(0)); | 140 | lua_pushstring(luaI_addchar(0)); |
| @@ -140,7 +144,7 @@ static void str_rep (void) | |||
| 140 | { | 144 | { |
| 141 | char *s = luaL_check_string(1, "strrep"); | 145 | char *s = luaL_check_string(1, "strrep"); |
| 142 | int n = (int)luaL_check_number(2, "strrep"); | 146 | int n = (int)luaL_check_number(2, "strrep"); |
| 143 | luaI_addchar(0); | 147 | luaI_emptybuff(); |
| 144 | while (n-- > 0) | 148 | while (n-- > 0) |
| 145 | addstr(s); | 149 | addstr(s); |
| 146 | lua_pushstring(luaI_addchar(0)); | 150 | lua_pushstring(luaI_addchar(0)); |
| @@ -168,7 +172,7 @@ static char *bracket_end (char *p) | |||
| 168 | return (*p == 0) ? NULL : strchr((*p=='^') ? p+2 : p+1, ']'); | 172 | return (*p == 0) ? NULL : strchr((*p=='^') ? p+2 : p+1, ']'); |
| 169 | } | 173 | } |
| 170 | 174 | ||
| 171 | char *item_end (char *p) | 175 | char *luaL_item_end (char *p) |
| 172 | { | 176 | { |
| 173 | switch (*p++) { | 177 | switch (*p++) { |
| 174 | case '\0': return p-1; | 178 | case '\0': return p-1; |
| @@ -202,7 +206,7 @@ static int matchclass (int c, int cl) | |||
| 202 | return (islower((unsigned char)cl) ? res : !res); | 206 | return (islower((unsigned char)cl) ? res : !res); |
| 203 | } | 207 | } |
| 204 | 208 | ||
| 205 | int singlematch (int c, char *p) | 209 | int luaL_singlematch (int c, char *p) |
| 206 | { | 210 | { |
| 207 | if (c == 0) return 0; | 211 | if (c == 0) return 0; |
| 208 | switch (*p) { | 212 | switch (*p) { |
| @@ -324,8 +328,8 @@ static char *match (char *s, char *p, int level) | |||
| 324 | } | 328 | } |
| 325 | else goto dflt; | 329 | else goto dflt; |
| 326 | default: dflt: { /* it is a pattern item */ | 330 | default: dflt: { /* it is a pattern item */ |
| 327 | int m = singlematch(*s, p); | 331 | int m = luaL_singlematch(*s, p); |
| 328 | char *ep = item_end(p); /* get what is next */ | 332 | char *ep = luaL_item_end(p); /* get what is next */ |
| 329 | switch (*ep) { | 333 | switch (*ep) { |
| 330 | case '*': { /* repetition */ | 334 | case '*': { /* repetition */ |
| 331 | char *res; | 335 | char *res; |
| @@ -427,7 +431,7 @@ static void str_gsub (void) | |||
| 427 | int max_s = (int)luaL_opt_number(4, strlen(src)+1, "gsub"); | 431 | int max_s = (int)luaL_opt_number(4, strlen(src)+1, "gsub"); |
| 428 | int anchor = (*p == '^') ? (p++, 1) : 0; | 432 | int anchor = (*p == '^') ? (p++, 1) : 0; |
| 429 | int n = 0; | 433 | int n = 0; |
| 430 | luaI_addchar(0); | 434 | luaI_emptybuff(); |
| 431 | while (n < max_s) { | 435 | while (n < max_s) { |
| 432 | char *e = match(src, p, 0); | 436 | char *e = match(src, p, 0); |
| 433 | if (e) { | 437 | if (e) { |
| @@ -450,10 +454,10 @@ static void str_set (void) | |||
| 450 | { | 454 | { |
| 451 | char *item = luaL_check_string(1, "strset"); | 455 | char *item = luaL_check_string(1, "strset"); |
| 452 | int i; | 456 | int i; |
| 453 | luaL_arg_check(*item_end(item) == 0, "strset", 1, "wrong format"); | 457 | luaL_arg_check(*luaL_item_end(item) == 0, "strset", 1, "wrong format"); |
| 454 | luaI_addchar(0); | 458 | luaI_emptybuff(); |
| 455 | for (i=1; i<256; i++) /* 0 cannot be part of a set */ | 459 | for (i=1; i<256; i++) /* 0 cannot be part of a set */ |
| 456 | if (singlematch(i, item)) | 460 | if (luaL_singlematch(i, item)) |
| 457 | luaI_addchar(i); | 461 | luaI_addchar(i); |
| 458 | lua_pushstring(luaI_addchar(0)); | 462 | lua_pushstring(luaI_addchar(0)); |
| 459 | } | 463 | } |
| @@ -476,7 +480,7 @@ static void str_format (void) | |||
| 476 | { | 480 | { |
| 477 | int arg = 1; | 481 | int arg = 1; |
| 478 | char *strfrmt = luaL_check_string(arg++, "format"); | 482 | char *strfrmt = luaL_check_string(arg++, "format"); |
| 479 | luaI_addchar(0); /* initialize */ | 483 | luaI_emptybuff(); /* initialize */ |
| 480 | while (*strfrmt) { | 484 | while (*strfrmt) { |
| 481 | if (*strfrmt != '%') | 485 | if (*strfrmt != '%') |
| 482 | luaI_addchar(*strfrmt++); | 486 | luaI_addchar(*strfrmt++); |
