diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2016-04-08 18:15:02 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2016-04-08 18:15:02 -0300 |
commit | e8e39a277f9f81e5cb2638a1d13d4d96e3731b16 (patch) | |
tree | 78995b008f3ad81b1618fb8b46615c7c2421d0d3 /lstrlib.c | |
parent | fff7d42a6963fc8edddd7349abe61f52e7d19e78 (diff) | |
download | lua-e8e39a277f9f81e5cb2638a1d13d4d96e3731b16.tar.gz lua-e8e39a277f9f81e5cb2638a1d13d4d96e3731b16.tar.bz2 lua-e8e39a277f9f81e5cb2638a1d13d4d96e3731b16.zip |
'string.format"%q"' now works for all basic types (nil, boolean,
numbers, and strings)
Diffstat (limited to 'lstrlib.c')
-rw-r--r-- | lstrlib.c | 42 |
1 files changed, 36 insertions, 6 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstrlib.c,v 1.243 2016/03/31 19:07:42 roberto Exp roberto $ | 2 | ** $Id: lstrlib.c,v 1.244 2016/04/07 15:40:07 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 | */ |
@@ -921,11 +921,9 @@ static int lua_number2strx (lua_State *L, char *buff, int sz, | |||
921 | #define MAX_FORMAT 32 | 921 | #define MAX_FORMAT 32 |
922 | 922 | ||
923 | 923 | ||
924 | static void addquoted (lua_State *L, luaL_Buffer *b, int arg) { | 924 | static void addquoted (luaL_Buffer *b, const char *s, size_t len) { |
925 | size_t l; | ||
926 | const char *s = luaL_checklstring(L, arg, &l); | ||
927 | luaL_addchar(b, '"'); | 925 | luaL_addchar(b, '"'); |
928 | while (l--) { | 926 | while (len--) { |
929 | if (*s == '"' || *s == '\\' || *s == '\n') { | 927 | if (*s == '"' || *s == '\\' || *s == '\n') { |
930 | luaL_addchar(b, '\\'); | 928 | luaL_addchar(b, '\\'); |
931 | luaL_addchar(b, *s); | 929 | luaL_addchar(b, *s); |
@@ -945,6 +943,38 @@ static void addquoted (lua_State *L, luaL_Buffer *b, int arg) { | |||
945 | luaL_addchar(b, '"'); | 943 | luaL_addchar(b, '"'); |
946 | } | 944 | } |
947 | 945 | ||
946 | |||
947 | static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { | ||
948 | switch (lua_type(L, arg)) { | ||
949 | case LUA_TSTRING: { | ||
950 | size_t len; | ||
951 | const char *s = lua_tolstring(L, arg, &len); | ||
952 | addquoted(b, s, len); | ||
953 | break; | ||
954 | } | ||
955 | case LUA_TNUMBER: { | ||
956 | if (!lua_isinteger(L, arg)) { /* write floats as hexa ('%a') */ | ||
957 | char *buff = luaL_prepbuffsize(b, MAX_ITEM); | ||
958 | lua_Number n = lua_tonumber(L, arg); | ||
959 | int nb = lua_number2strx(L, buff, MAX_ITEM, | ||
960 | "%" LUA_NUMBER_FRMLEN "a", n); | ||
961 | luaL_addsize(b, nb); | ||
962 | break; | ||
963 | } | ||
964 | /* else integers; write in "native" format *//* FALLTHROUGH */ | ||
965 | } | ||
966 | case LUA_TNIL: case LUA_TBOOLEAN: { | ||
967 | luaL_tolstring(L, arg, NULL); | ||
968 | luaL_addvalue(b); | ||
969 | break; | ||
970 | } | ||
971 | default: { | ||
972 | luaL_argerror(L, arg, "value has no literal form"); | ||
973 | } | ||
974 | } | ||
975 | } | ||
976 | |||
977 | |||
948 | static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { | 978 | static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { |
949 | const char *p = strfrmt; | 979 | const char *p = strfrmt; |
950 | while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */ | 980 | while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */ |
@@ -1024,7 +1054,7 @@ static int str_format (lua_State *L) { | |||
1024 | break; | 1054 | break; |
1025 | } | 1055 | } |
1026 | case 'q': { | 1056 | case 'q': { |
1027 | addquoted(L, &b, arg); | 1057 | addliteral(L, &b, arg); |
1028 | break; | 1058 | break; |
1029 | } | 1059 | } |
1030 | case 's': { | 1060 | case 's': { |