diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-08-16 17:52:00 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-08-16 17:52:00 -0300 |
commit | c787dccd9b5c3e55547a2c4bb598c0276de65034 (patch) | |
tree | c4cdf2f7319fee48e048472a2044119f541e8da2 /lstrlib.c | |
parent | b44e35b773bcaa9891d80a117392911ab5f656e5 (diff) | |
download | lua-c787dccd9b5c3e55547a2c4bb598c0276de65034.tar.gz lua-c787dccd9b5c3e55547a2c4bb598c0276de65034.tar.bz2 lua-c787dccd9b5c3e55547a2c4bb598c0276de65034.zip |
"const" !!!
Diffstat (limited to 'lstrlib.c')
-rw-r--r-- | lstrlib.c | 91 |
1 files changed, 47 insertions, 44 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstrlib.c,v 1.32 1999/06/17 17:04:03 roberto Exp roberto $ | 2 | ** $Id: lstrlib.c,v 1.33 1999/08/10 12:55:56 roberto Exp roberto $ |
3 | ** Standard library for strings and pattern-matching | 3 | ** Standard library for strings and pattern-matching |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -16,16 +16,14 @@ | |||
16 | 16 | ||
17 | 17 | ||
18 | 18 | ||
19 | static void addnchar (char *s, int n) | 19 | static void addnchar (const char *s, int n) { |
20 | { | ||
21 | char *b = luaL_openspace(n); | 20 | char *b = luaL_openspace(n); |
22 | memcpy(b, s, n); | 21 | memcpy(b, s, n); |
23 | luaL_addsize(n); | 22 | luaL_addsize(n); |
24 | } | 23 | } |
25 | 24 | ||
26 | 25 | ||
27 | static void str_len (void) | 26 | static void str_len (void) { |
28 | { | ||
29 | long l; | 27 | long l; |
30 | luaL_check_lstr(1, &l); | 28 | luaL_check_lstr(1, &l); |
31 | lua_pushnumber(l); | 29 | lua_pushnumber(l); |
@@ -45,7 +43,7 @@ static long posrelat (long pos, long len) { | |||
45 | 43 | ||
46 | static void str_sub (void) { | 44 | static void str_sub (void) { |
47 | long l; | 45 | long l; |
48 | char *s = luaL_check_lstr(1, &l); | 46 | const char *s = luaL_check_lstr(1, &l); |
49 | long start = posrelat(luaL_check_long(2), l); | 47 | long start = posrelat(luaL_check_long(2), l); |
50 | long end = posrelat(luaL_opt_long(3, -1), l); | 48 | long end = posrelat(luaL_opt_long(3, -1), l); |
51 | if (start < 1) start = 1; | 49 | if (start < 1) start = 1; |
@@ -59,7 +57,7 @@ static void str_sub (void) { | |||
59 | static void str_lower (void) { | 57 | static void str_lower (void) { |
60 | long l; | 58 | long l; |
61 | int i; | 59 | int i; |
62 | char *s = luaL_check_lstr(1, &l); | 60 | const char *s = luaL_check_lstr(1, &l); |
63 | luaL_resetbuffer(); | 61 | luaL_resetbuffer(); |
64 | for (i=0; i<l; i++) | 62 | for (i=0; i<l; i++) |
65 | luaL_addchar(tolower((unsigned char)(s[i]))); | 63 | luaL_addchar(tolower((unsigned char)(s[i]))); |
@@ -70,17 +68,16 @@ static void str_lower (void) { | |||
70 | static void str_upper (void) { | 68 | static void str_upper (void) { |
71 | long l; | 69 | long l; |
72 | int i; | 70 | int i; |
73 | char *s = luaL_check_lstr(1, &l); | 71 | const char *s = luaL_check_lstr(1, &l); |
74 | luaL_resetbuffer(); | 72 | luaL_resetbuffer(); |
75 | for (i=0; i<l; i++) | 73 | for (i=0; i<l; i++) |
76 | luaL_addchar(toupper((unsigned char)(s[i]))); | 74 | luaL_addchar(toupper((unsigned char)(s[i]))); |
77 | closeandpush(); | 75 | closeandpush(); |
78 | } | 76 | } |
79 | 77 | ||
80 | static void str_rep (void) | 78 | static void str_rep (void) { |
81 | { | ||
82 | long l; | 79 | long l; |
83 | char *s = luaL_check_lstr(1, &l); | 80 | const char *s = luaL_check_lstr(1, &l); |
84 | int n = luaL_check_int(2); | 81 | int n = luaL_check_int(2); |
85 | luaL_resetbuffer(); | 82 | luaL_resetbuffer(); |
86 | while (n-- > 0) | 83 | while (n-- > 0) |
@@ -91,7 +88,7 @@ static void str_rep (void) | |||
91 | 88 | ||
92 | static void str_byte (void) { | 89 | static void str_byte (void) { |
93 | long l; | 90 | long l; |
94 | char *s = luaL_check_lstr(1, &l); | 91 | const char *s = luaL_check_lstr(1, &l); |
95 | long pos = posrelat(luaL_opt_long(2, 1), l); | 92 | long pos = posrelat(luaL_opt_long(2, 1), l); |
96 | luaL_arg_check(0<pos && pos<=l, 2, "out of range"); | 93 | luaL_arg_check(0<pos && pos<=l, 2, "out of range"); |
97 | lua_pushnumber((unsigned char)s[pos-1]); | 94 | lua_pushnumber((unsigned char)s[pos-1]); |
@@ -123,10 +120,10 @@ static void str_char (void) { | |||
123 | 120 | ||
124 | 121 | ||
125 | struct Capture { | 122 | struct Capture { |
126 | char *src_end; /* end ('\0') of source string */ | 123 | const char *src_end; /* end ('\0') of source string */ |
127 | int level; /* total number of captures (finished or unfinished) */ | 124 | int level; /* total number of captures (finished or unfinished) */ |
128 | struct { | 125 | struct { |
129 | char *init; | 126 | const char *init; |
130 | int len; /* -1 signals unfinished capture */ | 127 | int len; /* -1 signals unfinished capture */ |
131 | } capture[MAX_CAPT]; | 128 | } capture[MAX_CAPT]; |
132 | }; | 129 | }; |
@@ -163,7 +160,7 @@ static int capture_to_close (struct Capture *cap) { | |||
163 | } | 160 | } |
164 | 161 | ||
165 | 162 | ||
166 | char *luaI_classend (char *p) { | 163 | const char *luaI_classend (const char *p) { |
167 | switch (*p++) { | 164 | switch (*p++) { |
168 | case ESC: | 165 | case ESC: |
169 | if (*p == '\0') lua_error("incorrect pattern (ends with `%')"); | 166 | if (*p == '\0') lua_error("incorrect pattern (ends with `%')"); |
@@ -201,7 +198,7 @@ static int matchclass (int c, int cl) { | |||
201 | 198 | ||
202 | 199 | ||
203 | 200 | ||
204 | static int matchbracketclass (int c, char *p, char *endclass) { | 201 | static int matchbracketclass (int c, const char *p, const char *endclass) { |
205 | int sig = 1; | 202 | int sig = 1; |
206 | if (*(p+1) == '^') { | 203 | if (*(p+1) == '^') { |
207 | sig = 0; | 204 | sig = 0; |
@@ -225,7 +222,7 @@ static int matchbracketclass (int c, char *p, char *endclass) { | |||
225 | 222 | ||
226 | 223 | ||
227 | 224 | ||
228 | int luaI_singlematch (int c, char *p, char *ep) { | 225 | int luaI_singlematch (int c, const char *p, const char *ep) { |
229 | switch (*p) { | 226 | switch (*p) { |
230 | case '.': /* matches any char */ | 227 | case '.': /* matches any char */ |
231 | return 1; | 228 | return 1; |
@@ -239,10 +236,11 @@ int luaI_singlematch (int c, char *p, char *ep) { | |||
239 | } | 236 | } |
240 | 237 | ||
241 | 238 | ||
242 | static char *match (char *s, char *p, struct Capture *cap); | 239 | static const char *match (const char *s, const char *p, struct Capture *cap); |
243 | 240 | ||
244 | 241 | ||
245 | static char *matchbalance (char *s, char *p, struct Capture *cap) { | 242 | static const char *matchbalance (const char *s, const char *p, |
243 | struct Capture *cap) { | ||
246 | if (*p == 0 || *(p+1) == 0) | 244 | if (*p == 0 || *(p+1) == 0) |
247 | lua_error("unbalanced pattern"); | 245 | lua_error("unbalanced pattern"); |
248 | if (*s != *p) return NULL; | 246 | if (*s != *p) return NULL; |
@@ -261,13 +259,14 @@ static char *matchbalance (char *s, char *p, struct Capture *cap) { | |||
261 | } | 259 | } |
262 | 260 | ||
263 | 261 | ||
264 | static char *max_expand (char *s, char *p, char *ep, struct Capture *cap) { | 262 | static const char *max_expand (const char *s, const char *p, const char *ep, |
263 | struct Capture *cap) { | ||
265 | int i = 0; /* counts maximum expand for item */ | 264 | int i = 0; /* counts maximum expand for item */ |
266 | while ((s+i)<cap->src_end && luaI_singlematch((unsigned char)*(s+i), p, ep)) | 265 | while ((s+i)<cap->src_end && luaI_singlematch((unsigned char)*(s+i), p, ep)) |
267 | i++; | 266 | i++; |
268 | /* keeps trying to match mith the maximum repetitions */ | 267 | /* keeps trying to match mith the maximum repetitions */ |
269 | while (i>=0) { | 268 | while (i>=0) { |
270 | char *res = match((s+i), ep+1, cap); | 269 | const char *res = match((s+i), ep+1, cap); |
271 | if (res) return res; | 270 | if (res) return res; |
272 | i--; /* else didn't match; reduce 1 repetition to try again */ | 271 | i--; /* else didn't match; reduce 1 repetition to try again */ |
273 | } | 272 | } |
@@ -275,9 +274,10 @@ static char *max_expand (char *s, char *p, char *ep, struct Capture *cap) { | |||
275 | } | 274 | } |
276 | 275 | ||
277 | 276 | ||
278 | static char *min_expand (char *s, char *p, char *ep, struct Capture *cap) { | 277 | static const char *min_expand (const char *s, const char *p, const char *ep, |
278 | struct Capture *cap) { | ||
279 | for (;;) { | 279 | for (;;) { |
280 | char *res = match(s, ep+1, cap); | 280 | const char *res = match(s, ep+1, cap); |
281 | if (res != NULL) | 281 | if (res != NULL) |
282 | return res; | 282 | return res; |
283 | else if (s<cap->src_end && luaI_singlematch((unsigned char)*s, p, ep)) | 283 | else if (s<cap->src_end && luaI_singlematch((unsigned char)*s, p, ep)) |
@@ -287,8 +287,9 @@ static char *min_expand (char *s, char *p, char *ep, struct Capture *cap) { | |||
287 | } | 287 | } |
288 | 288 | ||
289 | 289 | ||
290 | static char *start_capt (char *s, char *p, struct Capture *cap) { | 290 | static const char *start_capt (const char *s, const char *p, |
291 | char *res; | 291 | struct Capture *cap) { |
292 | const char *res; | ||
292 | int level = cap->level; | 293 | int level = cap->level; |
293 | if (level >= MAX_CAPT) lua_error("too many captures"); | 294 | if (level >= MAX_CAPT) lua_error("too many captures"); |
294 | cap->capture[level].init = s; | 295 | cap->capture[level].init = s; |
@@ -300,9 +301,10 @@ static char *start_capt (char *s, char *p, struct Capture *cap) { | |||
300 | } | 301 | } |
301 | 302 | ||
302 | 303 | ||
303 | static char *end_capt (char *s, char *p, struct Capture *cap) { | 304 | static const char *end_capt (const char *s, const char *p, |
305 | struct Capture *cap) { | ||
304 | int l = capture_to_close(cap); | 306 | int l = capture_to_close(cap); |
305 | char *res; | 307 | const char *res; |
306 | cap->capture[l].len = s - cap->capture[l].init; /* close capture */ | 308 | cap->capture[l].len = s - cap->capture[l].init; /* close capture */ |
307 | if ((res = match(s, p+1, cap)) == NULL) /* match failed? */ | 309 | if ((res = match(s, p+1, cap)) == NULL) /* match failed? */ |
308 | cap->capture[l].len = -1; /* undo capture */ | 310 | cap->capture[l].len = -1; /* undo capture */ |
@@ -310,7 +312,8 @@ static char *end_capt (char *s, char *p, struct Capture *cap) { | |||
310 | } | 312 | } |
311 | 313 | ||
312 | 314 | ||
313 | static char *match_capture (char *s, int level, struct Capture *cap) { | 315 | static const char *match_capture (const char *s, int level, |
316 | struct Capture *cap) { | ||
314 | int l = check_cap(level, cap); | 317 | int l = check_cap(level, cap); |
315 | int len = cap->capture[l].len; | 318 | int len = cap->capture[l].len; |
316 | if (cap->src_end-s >= len && | 319 | if (cap->src_end-s >= len && |
@@ -320,7 +323,7 @@ static char *match_capture (char *s, int level, struct Capture *cap) { | |||
320 | } | 323 | } |
321 | 324 | ||
322 | 325 | ||
323 | static char *match (char *s, char *p, struct Capture *cap) { | 326 | static const char *match (const char *s, const char *p, struct Capture *cap) { |
324 | init: /* using goto's to optimize tail recursion */ | 327 | init: /* using goto's to optimize tail recursion */ |
325 | switch (*p) { | 328 | switch (*p) { |
326 | case '(': /* start capture */ | 329 | case '(': /* start capture */ |
@@ -346,11 +349,11 @@ static char *match (char *s, char *p, struct Capture *cap) { | |||
346 | return (s == cap->src_end) ? s : NULL; /* check end of string */ | 349 | return (s == cap->src_end) ? s : NULL; /* check end of string */ |
347 | else goto dflt; | 350 | else goto dflt; |
348 | default: dflt: { /* it is a pattern item */ | 351 | default: dflt: { /* it is a pattern item */ |
349 | char *ep = luaI_classend(p); /* points to what is next */ | 352 | const char *ep = luaI_classend(p); /* points to what is next */ |
350 | int m = s<cap->src_end && luaI_singlematch((unsigned char)*s, p, ep); | 353 | int m = s<cap->src_end && luaI_singlematch((unsigned char)*s, p, ep); |
351 | switch (*ep) { | 354 | switch (*ep) { |
352 | case '?': { /* optional */ | 355 | case '?': { /* optional */ |
353 | char *res; | 356 | const char *res; |
354 | if (m && ((res=match(s+1, ep+1, cap)) != NULL)) | 357 | if (m && ((res=match(s+1, ep+1, cap)) != NULL)) |
355 | return res; | 358 | return res; |
356 | p=ep+1; goto init; /* else return match(s, ep+1, cap); */ | 359 | p=ep+1; goto init; /* else return match(s, ep+1, cap); */ |
@@ -372,8 +375,8 @@ static char *match (char *s, char *p, struct Capture *cap) { | |||
372 | 375 | ||
373 | static void str_find (void) { | 376 | static void str_find (void) { |
374 | long l; | 377 | long l; |
375 | char *s = luaL_check_lstr(1, &l); | 378 | const char *s = luaL_check_lstr(1, &l); |
376 | char *p = luaL_check_string(2); | 379 | const char *p = luaL_check_string(2); |
377 | long init = posrelat(luaL_opt_long(3, 1), l) - 1; | 380 | long init = posrelat(luaL_opt_long(3, 1), l) - 1; |
378 | struct Capture cap; | 381 | struct Capture cap; |
379 | luaL_arg_check(0 <= init && init <= l, 3, "out of range"); | 382 | luaL_arg_check(0 <= init && init <= l, 3, "out of range"); |
@@ -388,10 +391,10 @@ static void str_find (void) { | |||
388 | } | 391 | } |
389 | else { | 392 | else { |
390 | int anchor = (*p == '^') ? (p++, 1) : 0; | 393 | int anchor = (*p == '^') ? (p++, 1) : 0; |
391 | char *s1=s+init; | 394 | const char *s1=s+init; |
392 | cap.src_end = s+l; | 395 | cap.src_end = s+l; |
393 | do { | 396 | do { |
394 | char *res; | 397 | const char *res; |
395 | cap.level = 0; | 398 | cap.level = 0; |
396 | if ((res=match(s1, p, &cap)) != NULL) { | 399 | if ((res=match(s1, p, &cap)) != NULL) { |
397 | lua_pushnumber(s1-s+1); /* start */ | 400 | lua_pushnumber(s1-s+1); /* start */ |
@@ -407,7 +410,7 @@ static void str_find (void) { | |||
407 | 410 | ||
408 | static void add_s (lua_Object newp, struct Capture *cap) { | 411 | static void add_s (lua_Object newp, struct Capture *cap) { |
409 | if (lua_isstring(newp)) { | 412 | if (lua_isstring(newp)) { |
410 | char *news = lua_getstring(newp); | 413 | const char *news = lua_getstring(newp); |
411 | int l = lua_strlen(newp); | 414 | int l = lua_strlen(newp); |
412 | int i; | 415 | int i; |
413 | for (i=0; i<l; i++) { | 416 | for (i=0; i<l; i++) { |
@@ -449,8 +452,8 @@ static void add_s (lua_Object newp, struct Capture *cap) { | |||
449 | 452 | ||
450 | static void str_gsub (void) { | 453 | static void str_gsub (void) { |
451 | long srcl; | 454 | long srcl; |
452 | char *src = luaL_check_lstr(1, &srcl); | 455 | const char *src = luaL_check_lstr(1, &srcl); |
453 | char *p = luaL_check_string(2); | 456 | const char *p = luaL_check_string(2); |
454 | lua_Object newp = lua_getparam(3); | 457 | lua_Object newp = lua_getparam(3); |
455 | int max_s = luaL_opt_int(4, srcl+1); | 458 | int max_s = luaL_opt_int(4, srcl+1); |
456 | int anchor = (*p == '^') ? (p++, 1) : 0; | 459 | int anchor = (*p == '^') ? (p++, 1) : 0; |
@@ -461,7 +464,7 @@ static void str_gsub (void) { | |||
461 | luaL_resetbuffer(); | 464 | luaL_resetbuffer(); |
462 | cap.src_end = src+srcl; | 465 | cap.src_end = src+srcl; |
463 | while (n < max_s) { | 466 | while (n < max_s) { |
464 | char *e; | 467 | const char *e; |
465 | cap.level = 0; | 468 | cap.level = 0; |
466 | e = match(src, p, &cap); | 469 | e = match(src, p, &cap); |
467 | if (e) { | 470 | if (e) { |
@@ -485,7 +488,7 @@ static void str_gsub (void) { | |||
485 | 488 | ||
486 | static void luaI_addquoted (int arg) { | 489 | static void luaI_addquoted (int arg) { |
487 | long l; | 490 | long l; |
488 | char *s = luaL_check_lstr(arg, &l); | 491 | const char *s = luaL_check_lstr(arg, &l); |
489 | luaL_addchar('"'); | 492 | luaL_addchar('"'); |
490 | while (l--) { | 493 | while (l--) { |
491 | switch (*s) { | 494 | switch (*s) { |
@@ -506,7 +509,7 @@ static void luaI_addquoted (int arg) { | |||
506 | 509 | ||
507 | static void str_format (void) { | 510 | static void str_format (void) { |
508 | int arg = 1; | 511 | int arg = 1; |
509 | char *strfrmt = luaL_check_string(arg); | 512 | const char *strfrmt = luaL_check_string(arg); |
510 | luaL_resetbuffer(); | 513 | luaL_resetbuffer(); |
511 | while (*strfrmt) { | 514 | while (*strfrmt) { |
512 | if (*strfrmt != '%') | 515 | if (*strfrmt != '%') |
@@ -517,7 +520,7 @@ static void str_format (void) { | |||
517 | struct Capture cap; | 520 | struct Capture cap; |
518 | char form[MAX_FORMAT]; /* to store the format ('%...') */ | 521 | char form[MAX_FORMAT]; /* to store the format ('%...') */ |
519 | char *buff; /* to store the formatted item */ | 522 | char *buff; /* to store the formatted item */ |
520 | char *initf = strfrmt; | 523 | const char *initf = strfrmt; |
521 | form[0] = '%'; | 524 | form[0] = '%'; |
522 | if (isdigit((unsigned char)*initf) && *(initf+1) == '$') { | 525 | if (isdigit((unsigned char)*initf) && *(initf+1) == '$') { |
523 | arg = *initf - '0'; | 526 | arg = *initf - '0'; |
@@ -548,7 +551,7 @@ static void str_format (void) { | |||
548 | continue; /* skip the "addsize" at the end */ | 551 | continue; /* skip the "addsize" at the end */ |
549 | case 's': { | 552 | case 's': { |
550 | long l; | 553 | long l; |
551 | char *s = luaL_check_lstr(arg, &l); | 554 | const char *s = luaL_check_lstr(arg, &l); |
552 | if (cap.capture[1].len == 0 && l >= 100) { | 555 | if (cap.capture[1].len == 0 && l >= 100) { |
553 | /* no precision and string is too big to be formatted; | 556 | /* no precision and string is too big to be formatted; |
554 | keep original string */ | 557 | keep original string */ |
@@ -570,7 +573,7 @@ static void str_format (void) { | |||
570 | } | 573 | } |
571 | 574 | ||
572 | 575 | ||
573 | static struct luaL_reg strlib[] = { | 576 | static const struct luaL_reg strlib[] = { |
574 | {"strlen", str_len}, | 577 | {"strlen", str_len}, |
575 | {"strsub", str_sub}, | 578 | {"strsub", str_sub}, |
576 | {"strlower", str_lower}, | 579 | {"strlower", str_lower}, |