aboutsummaryrefslogtreecommitdiff
path: root/lstrlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'lstrlib.c')
-rw-r--r--lstrlib.c49
1 files changed, 27 insertions, 22 deletions
diff --git a/lstrlib.c b/lstrlib.c
index c3a424f9..d3709a83 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.147 2009/12/17 12:50:20 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.148 2010/01/04 16:37:19 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*/
@@ -65,12 +65,13 @@ static int str_sub (lua_State *L) {
65 65
66 66
67static int str_reverse (lua_State *L) { 67static int str_reverse (lua_State *L) {
68 size_t l; 68 size_t l, i;
69 luaL_Buffer b; 69 luaL_Buffer b;
70 const char *s = luaL_checklstring(L, 1, &l); 70 const char *s = luaL_checklstring(L, 1, &l);
71 luaL_buffinit(L, &b); 71 char *p = luaL_buffinitsize(L, &b, l);
72 while (l--) luaL_addchar(&b, s[l]); 72 for (i = 0; i < l; i++)
73 luaL_pushresult(&b); 73 p[i] = s[l - i - 1];
74 luaL_pushresultsize(&b, l);
74 return 1; 75 return 1;
75} 76}
76 77
@@ -80,10 +81,10 @@ static int str_lower (lua_State *L) {
80 size_t i; 81 size_t i;
81 luaL_Buffer b; 82 luaL_Buffer b;
82 const char *s = luaL_checklstring(L, 1, &l); 83 const char *s = luaL_checklstring(L, 1, &l);
83 luaL_buffinit(L, &b); 84 char *p = luaL_buffinitsize(L, &b, l);
84 for (i=0; i<l; i++) 85 for (i=0; i<l; i++)
85 luaL_addchar(&b, tolower(uchar(s[i]))); 86 p[i] = tolower(uchar(s[i]));
86 luaL_pushresult(&b); 87 luaL_pushresultsize(&b, l);
87 return 1; 88 return 1;
88} 89}
89 90
@@ -93,10 +94,10 @@ static int str_upper (lua_State *L) {
93 size_t i; 94 size_t i;
94 luaL_Buffer b; 95 luaL_Buffer b;
95 const char *s = luaL_checklstring(L, 1, &l); 96 const char *s = luaL_checklstring(L, 1, &l);
96 luaL_buffinit(L, &b); 97 char *p = luaL_buffinitsize(L, &b, l);
97 for (i=0; i<l; i++) 98 for (i=0; i<l; i++)
98 luaL_addchar(&b, toupper(uchar(s[i]))); 99 p[i] = toupper(uchar(s[i]));
99 luaL_pushresult(&b); 100 luaL_pushresultsize(&b, l);
100 return 1; 101 return 1;
101} 102}
102 103
@@ -136,13 +137,13 @@ static int str_char (lua_State *L) {
136 int n = lua_gettop(L); /* number of arguments */ 137 int n = lua_gettop(L); /* number of arguments */
137 int i; 138 int i;
138 luaL_Buffer b; 139 luaL_Buffer b;
139 luaL_buffinit(L, &b); 140 char *p = luaL_buffinitsize(L, &b, n);
140 for (i=1; i<=n; i++) { 141 for (i=1; i<=n; i++) {
141 int c = luaL_checkint(L, i); 142 int c = luaL_checkint(L, i);
142 luaL_argcheck(L, uchar(c) == c, i, "invalid value"); 143 luaL_argcheck(L, uchar(c) == c, i, "invalid value");
143 luaL_addchar(&b, uchar(c)); 144 p[i - 1] = uchar(c);
144 } 145 }
145 luaL_pushresult(&b); 146 luaL_pushresultsize(&b, n);
146 return 1; 147 return 1;
147} 148}
148 149
@@ -773,6 +774,9 @@ static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
773} 774}
774 775
775 776
777/*
778** add length modifier into integer formats
779*/
776static void addintlen (char *form) { 780static void addintlen (char *form) {
777 size_t l = strlen(form); 781 size_t l = strlen(form);
778 char spec = form[l - 1]; 782 char spec = form[l - 1];
@@ -796,12 +800,13 @@ static int str_format (lua_State *L) {
796 luaL_addchar(&b, *strfrmt++); /* %% */ 800 luaL_addchar(&b, *strfrmt++); /* %% */
797 else { /* format item */ 801 else { /* format item */
798 char form[MAX_FORMAT]; /* to store the format (`%...') */ 802 char form[MAX_FORMAT]; /* to store the format (`%...') */
799 char buff[MAX_ITEM]; /* to store the formatted item */ 803 char *buff = luaL_prepbuffsize(&b, MAX_ITEM); /* to put formatted item */
804 int nb = 0; /* number of bytes in added item */
800 arg++; 805 arg++;
801 strfrmt = scanformat(L, strfrmt, form); 806 strfrmt = scanformat(L, strfrmt, form);
802 switch (*strfrmt++) { 807 switch (*strfrmt++) {
803 case 'c': { 808 case 'c': {
804 sprintf(buff, form, luaL_checkint(L, arg)); 809 nb = sprintf(buff, form, luaL_checkint(L, arg));
805 break; 810 break;
806 } 811 }
807 case 'd': case 'i': 812 case 'd': case 'i':
@@ -810,17 +815,17 @@ static int str_format (lua_State *L) {
810 LUA_INTFRM_T r = (n < 0) ? (LUA_INTFRM_T)n : 815 LUA_INTFRM_T r = (n < 0) ? (LUA_INTFRM_T)n :
811 (LUA_INTFRM_T)(unsigned LUA_INTFRM_T)n; 816 (LUA_INTFRM_T)(unsigned LUA_INTFRM_T)n;
812 addintlen(form); 817 addintlen(form);
813 sprintf(buff, form, r); 818 nb = sprintf(buff, form, r);
814 break; 819 break;
815 } 820 }
816 case 'e': case 'E': case 'f': 821 case 'e': case 'E': case 'f':
817 case 'g': case 'G': { 822 case 'g': case 'G': {
818 sprintf(buff, form, (double)luaL_checknumber(L, arg)); 823 nb = sprintf(buff, form, (double)luaL_checknumber(L, arg));
819 break; 824 break;
820 } 825 }
821 case 'q': { 826 case 'q': {
822 addquoted(L, &b, arg); 827 addquoted(L, &b, arg);
823 continue; /* skip the 'addsize' at the end */ 828 break;
824 } 829 }
825 case 's': { 830 case 's': {
826 size_t l; 831 size_t l;
@@ -830,10 +835,10 @@ static int str_format (lua_State *L) {
830 keep original string */ 835 keep original string */
831 lua_pushvalue(L, arg); 836 lua_pushvalue(L, arg);
832 luaL_addvalue(&b); 837 luaL_addvalue(&b);
833 continue; /* skip the `addsize' at the end */ 838 break;
834 } 839 }
835 else { 840 else {
836 sprintf(buff, form, s); 841 nb = sprintf(buff, form, s);
837 break; 842 break;
838 } 843 }
839 } 844 }
@@ -842,7 +847,7 @@ static int str_format (lua_State *L) {
842 LUA_QL("format"), *(strfrmt - 1)); 847 LUA_QL("format"), *(strfrmt - 1));
843 } 848 }
844 } 849 }
845 luaL_addlstring(&b, buff, strlen(buff)); 850 luaL_addsize(&b, nb);
846 } 851 }
847 } 852 }
848 luaL_pushresult(&b); 853 luaL_pushresult(&b);