diff options
Diffstat (limited to 'lstrlib.c')
-rw-r--r-- | lstrlib.c | 64 |
1 files changed, 32 insertions, 32 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstrlib.c,v 1.88 2002/08/21 19:39:31 roberto Exp roberto $ | 2 | ** $Id: lstrlib.c,v 1.89 2002/08/23 19:45:24 roberto Exp roberto $ |
3 | ** Standard library for string operations and pattern-matching | 3 | ** Standard library for string operations and pattern-matching |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -28,7 +28,7 @@ typedef long sint32; /* a signed version for size_t */ | |||
28 | 28 | ||
29 | static int str_len (lua_State *L) { | 29 | static int str_len (lua_State *L) { |
30 | size_t l; | 30 | size_t l; |
31 | luaL_check_lstr(L, 1, &l); | 31 | luaL_checklstring(L, 1, &l); |
32 | lua_pushnumber(L, l); | 32 | lua_pushnumber(L, l); |
33 | return 1; | 33 | return 1; |
34 | } | 34 | } |
@@ -42,9 +42,9 @@ static sint32 posrelat (sint32 pos, size_t len) { | |||
42 | 42 | ||
43 | static int str_sub (lua_State *L) { | 43 | static int str_sub (lua_State *L) { |
44 | size_t l; | 44 | size_t l; |
45 | const char *s = luaL_check_lstr(L, 1, &l); | 45 | const char *s = luaL_checklstring(L, 1, &l); |
46 | sint32 start = posrelat(luaL_check_long(L, 2), l); | 46 | sint32 start = posrelat(luaL_checklong(L, 2), l); |
47 | sint32 end = posrelat(luaL_opt_long(L, 3, -1), l); | 47 | sint32 end = posrelat(luaL_optlong(L, 3, -1), l); |
48 | if (start < 1) start = 1; | 48 | if (start < 1) start = 1; |
49 | if (end > (sint32)l) end = l; | 49 | if (end > (sint32)l) end = l; |
50 | if (start <= end) | 50 | if (start <= end) |
@@ -58,7 +58,7 @@ static int str_lower (lua_State *L) { | |||
58 | size_t l; | 58 | size_t l; |
59 | size_t i; | 59 | size_t i; |
60 | luaL_Buffer b; | 60 | luaL_Buffer b; |
61 | const char *s = luaL_check_lstr(L, 1, &l); | 61 | const char *s = luaL_checklstring(L, 1, &l); |
62 | luaL_buffinit(L, &b); | 62 | luaL_buffinit(L, &b); |
63 | for (i=0; i<l; i++) | 63 | for (i=0; i<l; i++) |
64 | luaL_putchar(&b, tolower(uchar(s[i]))); | 64 | luaL_putchar(&b, tolower(uchar(s[i]))); |
@@ -71,7 +71,7 @@ static int str_upper (lua_State *L) { | |||
71 | size_t l; | 71 | size_t l; |
72 | size_t i; | 72 | size_t i; |
73 | luaL_Buffer b; | 73 | luaL_Buffer b; |
74 | const char *s = luaL_check_lstr(L, 1, &l); | 74 | const char *s = luaL_checklstring(L, 1, &l); |
75 | luaL_buffinit(L, &b); | 75 | luaL_buffinit(L, &b); |
76 | for (i=0; i<l; i++) | 76 | for (i=0; i<l; i++) |
77 | luaL_putchar(&b, toupper(uchar(s[i]))); | 77 | luaL_putchar(&b, toupper(uchar(s[i]))); |
@@ -82,8 +82,8 @@ static int str_upper (lua_State *L) { | |||
82 | static int str_rep (lua_State *L) { | 82 | static int str_rep (lua_State *L) { |
83 | size_t l; | 83 | size_t l; |
84 | luaL_Buffer b; | 84 | luaL_Buffer b; |
85 | const char *s = luaL_check_lstr(L, 1, &l); | 85 | const char *s = luaL_checklstring(L, 1, &l); |
86 | int n = luaL_check_int(L, 2); | 86 | int n = luaL_checkint(L, 2); |
87 | luaL_buffinit(L, &b); | 87 | luaL_buffinit(L, &b); |
88 | while (n-- > 0) | 88 | while (n-- > 0) |
89 | luaL_addlstring(&b, s, l); | 89 | luaL_addlstring(&b, s, l); |
@@ -94,9 +94,9 @@ static int str_rep (lua_State *L) { | |||
94 | 94 | ||
95 | static int str_byte (lua_State *L) { | 95 | static int str_byte (lua_State *L) { |
96 | size_t l; | 96 | size_t l; |
97 | const char *s = luaL_check_lstr(L, 1, &l); | 97 | const char *s = luaL_checklstring(L, 1, &l); |
98 | sint32 pos = posrelat(luaL_opt_long(L, 2, 1), l); | 98 | sint32 pos = posrelat(luaL_optlong(L, 2, 1), l); |
99 | luaL_arg_check(L, 0 < pos && (size_t)(pos) <= l, 2, "out of range"); | 99 | luaL_argcheck(L, 0 < pos && (size_t)(pos) <= l, 2, "out of range"); |
100 | lua_pushnumber(L, uchar(s[pos-1])); | 100 | lua_pushnumber(L, uchar(s[pos-1])); |
101 | return 1; | 101 | return 1; |
102 | } | 102 | } |
@@ -108,8 +108,8 @@ static int str_char (lua_State *L) { | |||
108 | luaL_Buffer b; | 108 | luaL_Buffer b; |
109 | luaL_buffinit(L, &b); | 109 | luaL_buffinit(L, &b); |
110 | for (i=1; i<=n; i++) { | 110 | for (i=1; i<=n; i++) { |
111 | int c = luaL_check_int(L, i); | 111 | int c = luaL_checkint(L, i); |
112 | luaL_arg_check(L, uchar(c) == c, i, "invalid value"); | 112 | luaL_argcheck(L, uchar(c) == c, i, "invalid value"); |
113 | luaL_putchar(&b, uchar(c)); | 113 | luaL_putchar(&b, uchar(c)); |
114 | } | 114 | } |
115 | luaL_pushresult(&b); | 115 | luaL_pushresult(&b); |
@@ -439,7 +439,7 @@ static void push_onecapture (MatchState *ms, int i) { | |||
439 | 439 | ||
440 | static int push_captures (MatchState *ms, const char *s, const char *e) { | 440 | static int push_captures (MatchState *ms, const char *s, const char *e) { |
441 | int i; | 441 | int i; |
442 | luaL_check_stack(ms->L, ms->level, "too many captures"); | 442 | luaL_checkstack(ms->L, ms->level, "too many captures"); |
443 | if (ms->level == 0 && s) { /* no explicit captures? */ | 443 | if (ms->level == 0 && s) { /* no explicit captures? */ |
444 | lua_pushlstring(ms->L, s, e-s); /* return whole match */ | 444 | lua_pushlstring(ms->L, s, e-s); /* return whole match */ |
445 | return 1; | 445 | return 1; |
@@ -454,10 +454,10 @@ static int push_captures (MatchState *ms, const char *s, const char *e) { | |||
454 | 454 | ||
455 | static int str_find (lua_State *L) { | 455 | static int str_find (lua_State *L) { |
456 | size_t l1, l2; | 456 | size_t l1, l2; |
457 | const char *s = luaL_check_lstr(L, 1, &l1); | 457 | const char *s = luaL_checklstring(L, 1, &l1); |
458 | const char *p = luaL_check_lstr(L, 2, &l2); | 458 | const char *p = luaL_checklstring(L, 2, &l2); |
459 | sint32 init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1; | 459 | sint32 init = posrelat(luaL_optlong(L, 3, 1), l1) - 1; |
460 | luaL_arg_check(L, 0 <= init && (size_t)(init) <= l1, 3, "out of range"); | 460 | luaL_argcheck(L, 0 <= init && (size_t)(init) <= l1, 3, "out of range"); |
461 | if (lua_toboolean(L, 4) || /* explicit request? */ | 461 | if (lua_toboolean(L, 4) || /* explicit request? */ |
462 | strpbrk(p, SPECIALS) == NULL) { /* or no special characters? */ | 462 | strpbrk(p, SPECIALS) == NULL) { /* or no special characters? */ |
463 | /* do a plain search */ | 463 | /* do a plain search */ |
@@ -517,8 +517,8 @@ static int gfind_aux (lua_State *L) { | |||
517 | 517 | ||
518 | 518 | ||
519 | static int gfind (lua_State *L) { | 519 | static int gfind (lua_State *L) { |
520 | luaL_check_string(L, 1); | 520 | luaL_checkstring(L, 1); |
521 | luaL_check_string(L, 2); | 521 | luaL_checkstring(L, 2); |
522 | lua_settop(L, 2); | 522 | lua_settop(L, 2); |
523 | lua_pushnumber(L, 0); | 523 | lua_pushnumber(L, 0); |
524 | lua_pushcclosure(L, gfind_aux, 3); | 524 | lua_pushcclosure(L, gfind_aux, 3); |
@@ -563,14 +563,14 @@ static void add_s (MatchState *ms, luaL_Buffer *b, | |||
563 | 563 | ||
564 | static int str_gsub (lua_State *L) { | 564 | static int str_gsub (lua_State *L) { |
565 | size_t srcl; | 565 | size_t srcl; |
566 | const char *src = luaL_check_lstr(L, 1, &srcl); | 566 | const char *src = luaL_checklstring(L, 1, &srcl); |
567 | const char *p = luaL_check_string(L, 2); | 567 | const char *p = luaL_checkstring(L, 2); |
568 | int max_s = luaL_opt_int(L, 4, srcl+1); | 568 | int max_s = luaL_optint(L, 4, srcl+1); |
569 | int anchor = (*p == '^') ? (p++, 1) : 0; | 569 | int anchor = (*p == '^') ? (p++, 1) : 0; |
570 | int n = 0; | 570 | int n = 0; |
571 | MatchState ms; | 571 | MatchState ms; |
572 | luaL_Buffer b; | 572 | luaL_Buffer b; |
573 | luaL_arg_check(L, | 573 | luaL_argcheck(L, |
574 | lua_gettop(L) >= 3 && (lua_isstring(L, 3) || lua_isfunction(L, 3)), | 574 | lua_gettop(L) >= 3 && (lua_isstring(L, 3) || lua_isfunction(L, 3)), |
575 | 3, "string or function expected"); | 575 | 3, "string or function expected"); |
576 | luaL_buffinit(L, &b); | 576 | luaL_buffinit(L, &b); |
@@ -609,7 +609,7 @@ static int str_gsub (lua_State *L) { | |||
609 | 609 | ||
610 | static void luaI_addquoted (lua_State *L, luaL_Buffer *b, int arg) { | 610 | static void luaI_addquoted (lua_State *L, luaL_Buffer *b, int arg) { |
611 | size_t l; | 611 | size_t l; |
612 | const char *s = luaL_check_lstr(L, arg, &l); | 612 | const char *s = luaL_checklstring(L, arg, &l); |
613 | luaL_putchar(b, '"'); | 613 | luaL_putchar(b, '"'); |
614 | while (l--) { | 614 | while (l--) { |
615 | switch (*s) { | 615 | switch (*s) { |
@@ -659,7 +659,7 @@ static const char *scanformat (lua_State *L, const char *strfrmt, | |||
659 | static int str_format (lua_State *L) { | 659 | static int str_format (lua_State *L) { |
660 | int arg = 1; | 660 | int arg = 1; |
661 | size_t sfl; | 661 | size_t sfl; |
662 | const char *strfrmt = luaL_check_lstr(L, arg, &sfl); | 662 | const char *strfrmt = luaL_checklstring(L, arg, &sfl); |
663 | const char *strfrmt_end = strfrmt+sfl; | 663 | const char *strfrmt_end = strfrmt+sfl; |
664 | luaL_Buffer b; | 664 | luaL_Buffer b; |
665 | luaL_buffinit(L, &b); | 665 | luaL_buffinit(L, &b); |
@@ -678,16 +678,16 @@ static int str_format (lua_State *L) { | |||
678 | strfrmt = scanformat(L, strfrmt, form, &hasprecision); | 678 | strfrmt = scanformat(L, strfrmt, form, &hasprecision); |
679 | switch (*strfrmt++) { | 679 | switch (*strfrmt++) { |
680 | case 'c': case 'd': case 'i': { | 680 | case 'c': case 'd': case 'i': { |
681 | sprintf(buff, form, luaL_check_int(L, arg)); | 681 | sprintf(buff, form, luaL_checkint(L, arg)); |
682 | break; | 682 | break; |
683 | } | 683 | } |
684 | case 'o': case 'u': case 'x': case 'X': { | 684 | case 'o': case 'u': case 'x': case 'X': { |
685 | sprintf(buff, form, (unsigned int)(luaL_check_number(L, arg))); | 685 | sprintf(buff, form, (unsigned int)(luaL_checknumber(L, arg))); |
686 | break; | 686 | break; |
687 | } | 687 | } |
688 | case 'e': case 'E': case 'f': | 688 | case 'e': case 'E': case 'f': |
689 | case 'g': case 'G': { | 689 | case 'g': case 'G': { |
690 | sprintf(buff, form, luaL_check_number(L, arg)); | 690 | sprintf(buff, form, luaL_checknumber(L, arg)); |
691 | break; | 691 | break; |
692 | } | 692 | } |
693 | case 'q': { | 693 | case 'q': { |
@@ -696,7 +696,7 @@ static int str_format (lua_State *L) { | |||
696 | } | 696 | } |
697 | case 's': { | 697 | case 's': { |
698 | size_t l; | 698 | size_t l; |
699 | const char *s = luaL_check_lstr(L, arg, &l); | 699 | const char *s = luaL_checklstring(L, arg, &l); |
700 | if (!hasprecision && l >= 100) { | 700 | if (!hasprecision && l >= 100) { |
701 | /* no precision and string is too long to be formatted; | 701 | /* no precision and string is too long to be formatted; |
702 | keep original string */ | 702 | keep original string */ |
@@ -741,7 +741,7 @@ static const luaL_reg strlib[] = { | |||
741 | ** Open string library | 741 | ** Open string library |
742 | */ | 742 | */ |
743 | LUALIB_API int lua_strlibopen (lua_State *L) { | 743 | LUALIB_API int lua_strlibopen (lua_State *L) { |
744 | luaL_opennamedlib(L, LUA_STRLIBNAME, strlib, 0); | 744 | luaL_openlib(L, LUA_STRLIBNAME, strlib, 0); |
745 | return 0; | 745 | return 0; |
746 | } | 746 | } |
747 | 747 | ||