diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-04-02 12:30:27 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-04-02 12:30:27 -0300 |
commit | d00d2eaf51ffd07caa172d6e9fc3c796fd086403 (patch) | |
tree | fc8fe40e2d7da64135c28c2dc89338bd278c77c8 | |
parent | 11126422d9006e835a00cb1cbe2290a6820a401d (diff) | |
download | lua-d00d2eaf51ffd07caa172d6e9fc3c796fd086403.tar.gz lua-d00d2eaf51ffd07caa172d6e9fc3c796fd086403.tar.bz2 lua-d00d2eaf51ffd07caa172d6e9fc3c796fd086403.zip |
small changes in 'luaO_pushvfstring'
-rw-r--r-- | lobject.c | 28 |
1 files changed, 13 insertions, 15 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lobject.c,v 2.34 2009/11/26 11:39:20 roberto Exp roberto $ | 2 | ** $Id: lobject.c,v 2.35 2010/02/05 19:09:09 roberto Exp roberto $ |
3 | ** Some generic functions over Lua objects | 3 | ** Some generic functions over Lua objects |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -116,16 +116,15 @@ int luaO_str2d (const char *s, lua_Number *result) { | |||
116 | 116 | ||
117 | 117 | ||
118 | 118 | ||
119 | static void pushstr (lua_State *L, const char *str) { | 119 | static void pushstr (lua_State *L, const char *str, size_t l) { |
120 | setsvalue2s(L, L->top, luaS_new(L, str)); | 120 | setsvalue2s(L, L->top, luaS_newlstr(L, str, l)); |
121 | incr_top(L); | 121 | incr_top(L); |
122 | } | 122 | } |
123 | 123 | ||
124 | 124 | ||
125 | /* this function handles only `%d', `%c', %f, %p, and `%s' formats */ | 125 | /* this function handles only `%d', `%c', %f, %p, and `%s' formats */ |
126 | const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { | 126 | const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { |
127 | int n = 1; | 127 | int n = 0; |
128 | pushstr(L, ""); | ||
129 | for (;;) { | 128 | for (;;) { |
130 | const char *e = strchr(fmt, '%'); | 129 | const char *e = strchr(fmt, '%'); |
131 | if (e == NULL) break; | 130 | if (e == NULL) break; |
@@ -135,14 +134,13 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { | |||
135 | case 's': { | 134 | case 's': { |
136 | const char *s = va_arg(argp, char *); | 135 | const char *s = va_arg(argp, char *); |
137 | if (s == NULL) s = "(null)"; | 136 | if (s == NULL) s = "(null)"; |
138 | pushstr(L, s); | 137 | pushstr(L, s, strlen(s)); |
139 | break; | 138 | break; |
140 | } | 139 | } |
141 | case 'c': { | 140 | case 'c': { |
142 | char buff[2]; | 141 | char buff; |
143 | buff[0] = cast(char, va_arg(argp, int)); | 142 | buff = cast(char, va_arg(argp, int)); |
144 | buff[1] = '\0'; | 143 | pushstr(L, &buff, 1); |
145 | pushstr(L, buff); | ||
146 | break; | 144 | break; |
147 | } | 145 | } |
148 | case 'd': { | 146 | case 'd': { |
@@ -157,12 +155,12 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { | |||
157 | } | 155 | } |
158 | case 'p': { | 156 | case 'p': { |
159 | char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */ | 157 | char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */ |
160 | sprintf(buff, "%p", va_arg(argp, void *)); | 158 | int l = sprintf(buff, "%p", va_arg(argp, void *)); |
161 | pushstr(L, buff); | 159 | pushstr(L, buff, l); |
162 | break; | 160 | break; |
163 | } | 161 | } |
164 | case '%': { | 162 | case '%': { |
165 | pushstr(L, "%"); | 163 | pushstr(L, "%", 1); |
166 | break; | 164 | break; |
167 | } | 165 | } |
168 | default: { | 166 | default: { |
@@ -175,8 +173,8 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { | |||
175 | n += 2; | 173 | n += 2; |
176 | fmt = e+2; | 174 | fmt = e+2; |
177 | } | 175 | } |
178 | pushstr(L, fmt); | 176 | pushstr(L, fmt, strlen(fmt)); |
179 | luaV_concat(L, n+1); | 177 | if (n > 0) luaV_concat(L, n + 1); |
180 | return svalue(L->top - 1); | 178 | return svalue(L->top - 1); |
181 | } | 179 | } |
182 | 180 | ||