diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-03-17 14:02:29 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-03-17 14:02:29 -0300 |
commit | eea734aa881002e90bd9130171a2b94cd9dc3267 (patch) | |
tree | b2816a614fca723d8c0b06e96cd093438e6e098b /strlib.c | |
parent | b6d91e24e23edfe98ad732660fd456e91658edb9 (diff) | |
download | lua-eea734aa881002e90bd9130171a2b94cd9dc3267.tar.gz lua-eea734aa881002e90bd9130171a2b94cd9dc3267.tar.bz2 lua-eea734aa881002e90bd9130171a2b94cd9dc3267.zip |
new module 'auxlib' centralizes functions to get/check parameters.
Diffstat (limited to 'strlib.c')
-rw-r--r-- | strlib.c | 93 |
1 files changed, 29 insertions, 64 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.34 1996/11/22 13:08:02 roberto Exp roberto $"; | 6 | char *rcs_strlib="$Id: strlib.c,v 1.35 1997/02/21 15:21:34 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <string.h> | 8 | #include <string.h> |
9 | #include <stdio.h> | 9 | #include <stdio.h> |
@@ -42,41 +42,6 @@ static char *openspace (unsigned long size) | |||
42 | return buff+lbuffer.size; | 42 | return buff+lbuffer.size; |
43 | } | 43 | } |
44 | 44 | ||
45 | void lua_arg_check(int cond, char *funcname) | ||
46 | { | ||
47 | if (!cond) { | ||
48 | char buff[100]; | ||
49 | sprintf(buff, "incorrect argument to function `%s'", funcname); | ||
50 | lua_error(buff); | ||
51 | } | ||
52 | } | ||
53 | |||
54 | char *lua_check_string (int numArg, char *funcname) | ||
55 | { | ||
56 | lua_Object o = lua_getparam(numArg); | ||
57 | lua_arg_check(lua_isstring(o), funcname); | ||
58 | return lua_getstring(o); | ||
59 | } | ||
60 | |||
61 | char *lua_opt_string (int numArg, char *def, char *funcname) | ||
62 | { | ||
63 | return (lua_getparam(numArg) == LUA_NOOBJECT) ? def : | ||
64 | lua_check_string(numArg, funcname); | ||
65 | } | ||
66 | |||
67 | double lua_check_number (int numArg, char *funcname) | ||
68 | { | ||
69 | lua_Object o = lua_getparam(numArg); | ||
70 | lua_arg_check(lua_isnumber(o), funcname); | ||
71 | return lua_getnumber(o); | ||
72 | } | ||
73 | |||
74 | long lua_opt_number (int numArg, long def, char *funcname) | ||
75 | { | ||
76 | return (lua_getparam(numArg) == LUA_NOOBJECT) ? def : | ||
77 | (long)lua_check_number(numArg, funcname); | ||
78 | } | ||
79 | |||
80 | char *luaI_addchar (int c) | 45 | char *luaI_addchar (int c) |
81 | { | 46 | { |
82 | if (lbuffer.size >= lbuffer.max) | 47 | if (lbuffer.size >= lbuffer.max) |
@@ -104,8 +69,8 @@ static void addstr (char *s) | |||
104 | */ | 69 | */ |
105 | static void str_tok (void) | 70 | static void str_tok (void) |
106 | { | 71 | { |
107 | char *s1 = lua_check_string(1, "strtok"); | 72 | char *s1 = luaL_check_string(1, "strtok"); |
108 | char *del = lua_check_string(2, "strtok"); | 73 | char *del = luaL_check_string(2, "strtok"); |
109 | lua_Object t = lua_createtable(); | 74 | lua_Object t = lua_createtable(); |
110 | int i = 1; | 75 | int i = 1; |
111 | /* As strtok changes s1, and s1 is "constant", make a copy of it */ | 76 | /* As strtok changes s1, and s1 is "constant", make a copy of it */ |
@@ -127,7 +92,7 @@ static void str_tok (void) | |||
127 | */ | 92 | */ |
128 | static void str_len (void) | 93 | static void str_len (void) |
129 | { | 94 | { |
130 | lua_pushnumber(strlen(lua_check_string(1, "strlen"))); | 95 | lua_pushnumber(strlen(luaL_check_string(1, "strlen"))); |
131 | } | 96 | } |
132 | 97 | ||
133 | /* | 98 | /* |
@@ -135,9 +100,9 @@ static void str_len (void) | |||
135 | */ | 100 | */ |
136 | static void str_sub (void) | 101 | static void str_sub (void) |
137 | { | 102 | { |
138 | char *s = lua_check_string(1, "strsub"); | 103 | char *s = luaL_check_string(1, "strsub"); |
139 | long start = (long)lua_check_number(2, "strsub"); | 104 | long start = (long)luaL_check_number(2, "strsub"); |
140 | long end = lua_opt_number(3, strlen(s), "strsub"); | 105 | long end = (long)luaL_opt_number(3, strlen(s), "strsub"); |
141 | if (1 <= start && start <= end && end <= strlen(s)) { | 106 | if (1 <= start && start <= end && end <= strlen(s)) { |
142 | luaI_addchar(0); | 107 | luaI_addchar(0); |
143 | addnchar(s+start-1, end-start+1); | 108 | addnchar(s+start-1, end-start+1); |
@@ -151,7 +116,7 @@ static void str_sub (void) | |||
151 | */ | 116 | */ |
152 | static void str_lower (void) | 117 | static void str_lower (void) |
153 | { | 118 | { |
154 | char *s = lua_check_string(1, "strlower"); | 119 | char *s = luaL_check_string(1, "strlower"); |
155 | luaI_addchar(0); | 120 | luaI_addchar(0); |
156 | while (*s) | 121 | while (*s) |
157 | luaI_addchar(tolower((unsigned char)*s++)); | 122 | luaI_addchar(tolower((unsigned char)*s++)); |
@@ -163,7 +128,7 @@ static void str_lower (void) | |||
163 | */ | 128 | */ |
164 | static void str_upper (void) | 129 | static void str_upper (void) |
165 | { | 130 | { |
166 | char *s = lua_check_string(1, "strupper"); | 131 | char *s = luaL_check_string(1, "strupper"); |
167 | luaI_addchar(0); | 132 | luaI_addchar(0); |
168 | while (*s) | 133 | while (*s) |
169 | luaI_addchar(toupper((unsigned char)*s++)); | 134 | luaI_addchar(toupper((unsigned char)*s++)); |
@@ -172,8 +137,8 @@ static void str_upper (void) | |||
172 | 137 | ||
173 | static void str_rep (void) | 138 | static void str_rep (void) |
174 | { | 139 | { |
175 | char *s = lua_check_string(1, "strrep"); | 140 | char *s = luaL_check_string(1, "strrep"); |
176 | int n = (int)lua_check_number(2, "strrep"); | 141 | int n = (int)luaL_check_number(2, "strrep"); |
177 | luaI_addchar(0); | 142 | luaI_addchar(0); |
178 | while (n-- > 0) | 143 | while (n-- > 0) |
179 | addstr(s); | 144 | addstr(s); |
@@ -185,9 +150,9 @@ static void str_rep (void) | |||
185 | */ | 150 | */ |
186 | static void str_ascii (void) | 151 | static void str_ascii (void) |
187 | { | 152 | { |
188 | char *s = lua_check_string(1, "ascii"); | 153 | char *s = luaL_check_string(1, "ascii"); |
189 | long pos = lua_opt_number(2, 1, "ascii") - 1; | 154 | long pos = (long)luaL_opt_number(2, 1, "ascii") - 1; |
190 | lua_arg_check(0<=pos && pos<strlen(s), "ascii"); | 155 | luaL_arg_check(0<=pos && pos<strlen(s), "ascii", 2, "out of range"); |
191 | lua_pushnumber((unsigned char)s[pos]); | 156 | lua_pushnumber((unsigned char)s[pos]); |
192 | } | 157 | } |
193 | 158 | ||
@@ -394,10 +359,10 @@ static char *match (char *s, char *p, int level) | |||
394 | 359 | ||
395 | static void str_find (void) | 360 | static void str_find (void) |
396 | { | 361 | { |
397 | char *s = lua_check_string(1, "strfind"); | 362 | char *s = luaL_check_string(1, "strfind"); |
398 | char *p = lua_check_string(2, "strfind"); | 363 | char *p = luaL_check_string(2, "strfind"); |
399 | long init = lua_opt_number(3, 1, "strfind") - 1; | 364 | long init = (long)luaL_opt_number(3, 1, "strfind") - 1; |
400 | lua_arg_check(0 <= init && init <= strlen(s), "strfind"); | 365 | luaL_arg_check(0 <= init && init <= strlen(s), "strfind", 3, "out of range"); |
401 | if (lua_getparam(4) != LUA_NOOBJECT || | 366 | if (lua_getparam(4) != LUA_NOOBJECT || |
402 | strpbrk(p, SPECIALS) == NULL) { /* no special caracters? */ | 367 | strpbrk(p, SPECIALS) == NULL) { /* no special caracters? */ |
403 | char *s2 = strstr(s+init, p); | 368 | char *s2 = strstr(s+init, p); |
@@ -450,15 +415,15 @@ static void add_s (lua_Object newp) | |||
450 | addstr(lua_isstring(res) ? lua_getstring(res) : ""); | 415 | addstr(lua_isstring(res) ? lua_getstring(res) : ""); |
451 | lua_endblock(); | 416 | lua_endblock(); |
452 | } | 417 | } |
453 | else lua_arg_check(0, "gsub"); | 418 | else luaL_arg_check(0, "gsub", 3, NULL); |
454 | } | 419 | } |
455 | 420 | ||
456 | static void str_gsub (void) | 421 | static void str_gsub (void) |
457 | { | 422 | { |
458 | char *src = lua_check_string(1, "gsub"); | 423 | char *src = luaL_check_string(1, "gsub"); |
459 | char *p = lua_check_string(2, "gsub"); | 424 | char *p = luaL_check_string(2, "gsub"); |
460 | lua_Object newp = lua_getparam(3); | 425 | lua_Object newp = lua_getparam(3); |
461 | int max_s = lua_opt_number(4, strlen(src)+1, "gsub"); | 426 | int max_s = (int)luaL_opt_number(4, strlen(src)+1, "gsub"); |
462 | int anchor = (*p == '^') ? (p++, 1) : 0; | 427 | int anchor = (*p == '^') ? (p++, 1) : 0; |
463 | int n = 0; | 428 | int n = 0; |
464 | luaI_addchar(0); | 429 | luaI_addchar(0); |
@@ -482,9 +447,9 @@ static void str_gsub (void) | |||
482 | 447 | ||
483 | static void str_set (void) | 448 | static void str_set (void) |
484 | { | 449 | { |
485 | char *item = lua_check_string(1, "strset"); | 450 | char *item = luaL_check_string(1, "strset"); |
486 | int i; | 451 | int i; |
487 | lua_arg_check(*item_end(item) == 0, "strset"); | 452 | luaL_arg_check(*item_end(item) == 0, "strset", 1, "wrong format"); |
488 | luaI_addchar(0); | 453 | luaI_addchar(0); |
489 | for (i=1; i<256; i++) /* 0 cannot be part of a set */ | 454 | for (i=1; i<256; i++) /* 0 cannot be part of a set */ |
490 | if (singlematch(i, item)) | 455 | if (singlematch(i, item)) |
@@ -509,7 +474,7 @@ void luaI_addquoted (char *s) | |||
509 | static void str_format (void) | 474 | static void str_format (void) |
510 | { | 475 | { |
511 | int arg = 1; | 476 | int arg = 1; |
512 | char *strfrmt = lua_check_string(arg++, "format"); | 477 | char *strfrmt = luaL_check_string(arg++, "format"); |
513 | luaI_addchar(0); /* initialize */ | 478 | luaI_addchar(0); /* initialize */ |
514 | while (*strfrmt) { | 479 | while (*strfrmt) { |
515 | if (*strfrmt != '%') | 480 | if (*strfrmt != '%') |
@@ -528,20 +493,20 @@ static void str_format (void) | |||
528 | buff = openspace(1000); /* to store the formated value */ | 493 | buff = openspace(1000); /* to store the formated value */ |
529 | switch (*strfrmt++) { | 494 | switch (*strfrmt++) { |
530 | case 'q': | 495 | case 'q': |
531 | luaI_addquoted(lua_check_string(arg++, "format")); | 496 | luaI_addquoted(luaL_check_string(arg++, "format")); |
532 | continue; | 497 | continue; |
533 | case 's': { | 498 | case 's': { |
534 | char *s = lua_check_string(arg++, "format"); | 499 | char *s = luaL_check_string(arg++, "format"); |
535 | buff = openspace(strlen(s)); | 500 | buff = openspace(strlen(s)); |
536 | sprintf(buff, form, s); | 501 | sprintf(buff, form, s); |
537 | break; | 502 | break; |
538 | } | 503 | } |
539 | case 'c': case 'd': case 'i': case 'o': | 504 | case 'c': case 'd': case 'i': case 'o': |
540 | case 'u': case 'x': case 'X': | 505 | case 'u': case 'x': case 'X': |
541 | sprintf(buff, form, (int)lua_check_number(arg++, "format")); | 506 | sprintf(buff, form, (int)luaL_check_number(arg++, "format")); |
542 | break; | 507 | break; |
543 | case 'e': case 'E': case 'f': case 'g': | 508 | case 'e': case 'E': case 'f': case 'g': |
544 | sprintf(buff, form, lua_check_number(arg++, "format")); | 509 | sprintf(buff, form, luaL_check_number(arg++, "format")); |
545 | break; | 510 | break; |
546 | default: /* also treat cases 'pnLlh' */ | 511 | default: /* also treat cases 'pnLlh' */ |
547 | lua_error("invalid format option in function `format'"); | 512 | lua_error("invalid format option in function `format'"); |