* except as arguments to the q option.
*/
static Varargs format( Varargs args ) {
LuaString fmt = args.checkstring( 1 );
final int n = fmt.length();
Buffer result = new Buffer(n);
int arg = 1;
int c;
for ( int i = 0; i < n; ) {
switch ( c = fmt.luaByte( i++ ) ) {
case '\n':
result.append( "\n" );
break;
default:
result.append( (byte) c );
break;
case L_ESC:
if ( i < n ) {
if ( ( c = fmt.luaByte( i ) ) == L_ESC ) {
++i;
result.append( (byte)L_ESC );
} else {
arg++;
FormatDesc fdsc = new FormatDesc(args, fmt, i );
i += fdsc.length;
switch ( fdsc.conversion ) {
case 'c':
fdsc.format( result, (byte)args.checkint( arg ) );
break;
case 'i':
case 'd':
fdsc.format( result, args.checkint( arg ) );
break;
case 'o':
case 'u':
case 'x':
case 'X':
fdsc.format( result, args.checklong( arg ) );
break;
case 'e':
case 'E':
case 'f':
case 'g':
case 'G':
fdsc.format( result, args.checkdouble( arg ) );
break;
case 'q':
addquoted( result, args.checkstring( arg ) );
break;
case 's': {
LuaString s = args.checkstring( arg );
if ( fdsc.precision == -1 && s.length() >= 100 ) {
result.append( s );
} else {
fdsc.format( result, s );
}
} break;
default:
error("invalid option '%"+(char)fdsc.conversion+"' to 'format'");
break;
}
}
}
}
}
return result.tostring();
}