aboutsummaryrefslogtreecommitdiff
path: root/lauxlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-05-07 14:36:56 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2002-05-07 14:36:56 -0300
commitdea6b6da9422f34ad91c8f6ad9ad3ed650e95713 (patch)
tree3016b2fbcd67d75c71ee1b190aff2c24ada9b168 /lauxlib.c
parent71144e3ff0cb81bd9b8bb56d94dc76074c638c64 (diff)
downloadlua-dea6b6da9422f34ad91c8f6ad9ad3ed650e95713.tar.gz
lua-dea6b6da9422f34ad91c8f6ad9ad3ed650e95713.tar.bz2
lua-dea6b6da9422f34ad91c8f6ad9ad3ed650e95713.zip
new function `lua_vpushstr' to replace uses of `sprintf'
Diffstat (limited to 'lauxlib.c')
-rw-r--r--lauxlib.c34
1 files changed, 3 insertions, 31 deletions
diff --git a/lauxlib.c b/lauxlib.c
index 7be33892..2537ca8c 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.67 2002/05/01 20:40:42 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.68 2002/05/06 19:05:10 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -142,38 +142,10 @@ LUALIB_API void luaL_opennamedlib (lua_State *L, const char *libname,
142} 142}
143 143
144 144
145static void vstr (lua_State *L, const char *fmt, va_list argp) {
146 luaL_Buffer b;
147 luaL_buffinit(L, &b);
148 for (;;) {
149 const char *e = strchr(fmt, '%');
150 if (e == NULL) break;
151 luaL_addlstring(&b, fmt, e-fmt);
152 switch (*(e+1)) {
153 case 's':
154 luaL_addstring(&b, va_arg(argp, char *));
155 break;
156 case 'd':
157 lua_pushnumber(L, va_arg(argp, int));
158 luaL_addvalue (&b);
159 break;
160 case '%':
161 luaL_putchar(&b, '%');
162 break;
163 default:
164 lua_error(L, "invalid format option");
165 }
166 fmt = e+2;
167 }
168 luaL_addstring(&b, fmt);
169 luaL_pushresult(&b);
170}
171
172
173LUALIB_API void luaL_vstr (lua_State *L, const char *fmt, ...) { 145LUALIB_API void luaL_vstr (lua_State *L, const char *fmt, ...) {
174 va_list argp; 146 va_list argp;
175 va_start(argp, fmt); 147 va_start(argp, fmt);
176 vstr(L, fmt, argp); 148 lua_vpushstr(L, fmt, argp);
177 va_end(argp); 149 va_end(argp);
178} 150}
179 151
@@ -181,7 +153,7 @@ LUALIB_API void luaL_vstr (lua_State *L, const char *fmt, ...) {
181LUALIB_API int luaL_verror (lua_State *L, const char *fmt, ...) { 153LUALIB_API int luaL_verror (lua_State *L, const char *fmt, ...) {
182 va_list argp; 154 va_list argp;
183 va_start(argp, fmt); 155 va_start(argp, fmt);
184 vstr(L, fmt, argp); 156 lua_vpushstr(L, fmt, argp);
185 va_end(argp); 157 va_end(argp);
186 return lua_errorobj(L); 158 return lua_errorobj(L);
187} 159}