mirror of
https://github.com/lua/lua.git
synced 2025-01-28 06:03:00 +08:00
new functionality for "format": "%d$...".
This commit is contained in:
parent
4321fde2a7
commit
da4dbe65b2
13
manual.tex
13
manual.tex
@ -1,4 +1,4 @@
|
|||||||
% $Id: manual.tex,v 2.9 1997/07/01 17:41:34 roberto Exp roberto $
|
% $Id: manual.tex,v 2.10 1997/07/02 17:09:48 roberto Exp roberto $
|
||||||
|
|
||||||
\documentstyle[fullpage,11pt,bnf]{article}
|
\documentstyle[fullpage,11pt,bnf]{article}
|
||||||
|
|
||||||
@ -38,7 +38,7 @@ Waldemar Celes
|
|||||||
\tecgraf\ --- Computer Science Department --- PUC-Rio
|
\tecgraf\ --- Computer Science Department --- PUC-Rio
|
||||||
}
|
}
|
||||||
|
|
||||||
\date{\small \verb$Date: 1997/07/01 17:41:34 $}
|
\date{\small \verb$Date: 1997/07/02 17:09:48 $}
|
||||||
|
|
||||||
\maketitle
|
\maketitle
|
||||||
|
|
||||||
@ -1885,6 +1885,15 @@ will produce the string:
|
|||||||
new line"
|
new line"
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
|
Conversions can be applied to the n-th argument in the argument list,
|
||||||
|
rather than the next unused argument.
|
||||||
|
In this case, the conversion character \verb|%| is replaced
|
||||||
|
by the sequence \verb|%d$|, where \verb|d| is a
|
||||||
|
decimal digit in the range [1,9],
|
||||||
|
giving the position of the argument in the argument list.
|
||||||
|
For instance, the call \verb|format("%2$d -> %1$03d", 1, 34)| will
|
||||||
|
result in \verb|"34 -> 001"|.
|
||||||
|
|
||||||
The options \verb|c|, \verb|d|, \verb|E|, \verb|e|, \verb|f|,
|
The options \verb|c|, \verb|d|, \verb|E|, \verb|e|, \verb|f|,
|
||||||
\verb|g| \verb|i|, \verb|o|, \verb|u|, \verb|X|, and \verb|x| all
|
\verb|g| \verb|i|, \verb|o|, \verb|u|, \verb|X|, and \verb|x| all
|
||||||
expect a number as argument,
|
expect a number as argument,
|
||||||
|
26
strlib.c
26
strlib.c
@ -3,7 +3,7 @@
|
|||||||
** String library to LUA
|
** String library to LUA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char *rcs_strlib="$Id: strlib.c,v 1.45 1997/06/19 17:45:28 roberto Exp roberto $";
|
char *rcs_strlib="$Id: strlib.c,v 1.46 1997/06/19 18:49:40 roberto Exp roberto $";
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -468,7 +468,7 @@ void luaI_addquoted (char *s)
|
|||||||
static void str_format (void)
|
static void str_format (void)
|
||||||
{
|
{
|
||||||
int arg = 1;
|
int arg = 1;
|
||||||
char *strfrmt = luaL_check_string(arg++);
|
char *strfrmt = luaL_check_string(arg);
|
||||||
luaI_emptybuff(); /* initialize */
|
luaI_emptybuff(); /* initialize */
|
||||||
while (*strfrmt) {
|
while (*strfrmt) {
|
||||||
if (*strfrmt != '%')
|
if (*strfrmt != '%')
|
||||||
@ -478,29 +478,35 @@ static void str_format (void)
|
|||||||
else { /* format item */
|
else { /* format item */
|
||||||
char form[MAX_FORMAT]; /* store the format ('%...') */
|
char form[MAX_FORMAT]; /* store the format ('%...') */
|
||||||
char *buff;
|
char *buff;
|
||||||
char *initf = strfrmt-1; /* -1 to include % */
|
char *initf = strfrmt;
|
||||||
strfrmt = match(strfrmt, "[-+ #]*(%d*)%.?(%d*)", 0);
|
form[0] = '%';
|
||||||
|
strfrmt = match(strfrmt, "%d?%$?[-+ #]*(%d*)%.?(%d*)", 0);
|
||||||
if (capture[0].len > 3 || capture[1].len > 3) /* < 1000? */
|
if (capture[0].len > 3 || capture[1].len > 3) /* < 1000? */
|
||||||
lua_error("invalid format (width or precision too long)");
|
lua_error("invalid format (width or precision too long)");
|
||||||
strncpy(form, initf, strfrmt-initf+1); /* +1 to include convertion */
|
if (isdigit((unsigned char)initf[0]) && initf[1] == '$') {
|
||||||
form[strfrmt-initf+1] = 0;
|
arg = initf[0] - '0';
|
||||||
|
initf += 2; /* skip the 'n$' */
|
||||||
|
}
|
||||||
|
arg++;
|
||||||
|
strncpy(form+1, initf, strfrmt-initf+1); /* +1 to include convertion */
|
||||||
|
form[strfrmt-initf+2] = 0;
|
||||||
buff = openspace(1000); /* to store the formated value */
|
buff = openspace(1000); /* to store the formated value */
|
||||||
switch (*strfrmt++) {
|
switch (*strfrmt++) {
|
||||||
case 'q':
|
case 'q':
|
||||||
luaI_addquoted(luaL_check_string(arg++));
|
luaI_addquoted(luaL_check_string(arg));
|
||||||
continue;
|
continue;
|
||||||
case 's': {
|
case 's': {
|
||||||
char *s = luaL_check_string(arg++);
|
char *s = luaL_check_string(arg);
|
||||||
buff = openspace(strlen(s));
|
buff = openspace(strlen(s));
|
||||||
sprintf(buff, form, s);
|
sprintf(buff, form, s);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'c': case 'd': case 'i': case 'o':
|
case 'c': case 'd': case 'i': case 'o':
|
||||||
case 'u': case 'x': case 'X':
|
case 'u': case 'x': case 'X':
|
||||||
sprintf(buff, form, (int)luaL_check_number(arg++));
|
sprintf(buff, form, (int)luaL_check_number(arg));
|
||||||
break;
|
break;
|
||||||
case 'e': case 'E': case 'f': case 'g':
|
case 'e': case 'E': case 'f': case 'g':
|
||||||
sprintf(buff, form, luaL_check_number(arg++));
|
sprintf(buff, form, luaL_check_number(arg));
|
||||||
break;
|
break;
|
||||||
default: /* also treat cases 'pnLlh' */
|
default: /* also treat cases 'pnLlh' */
|
||||||
lua_error("invalid format option in function `format'");
|
lua_error("invalid format option in function `format'");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user