2000-05-09 00:53:16 -04:00
|
|
|
/**********************************************************************
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
sprintf.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
$Date$
|
|
|
|
created at: Fri Oct 15 10:39:26 JST 1993
|
|
|
|
|
2002-04-18 04:46:18 -04:00
|
|
|
Copyright (C) 1993-2002 Yukihiro Matsumoto
|
2000-05-01 05:42:38 -04:00
|
|
|
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
|
2000-05-09 00:53:16 -04:00
|
|
|
Copyright (C) 2000 Information-technology Promotion Agency, Japan
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2000-05-09 00:53:16 -04:00
|
|
|
**********************************************************************/
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
#include "ruby.h"
|
|
|
|
#include <ctype.h>
|
1999-08-13 01:45:20 -04:00
|
|
|
#include <math.h>
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
#define BIT_DIGITS(N) (((N)*146)/485 + 1) /* log2(10) =~ 146/485 */
|
|
|
|
|
1999-09-01 05:48:03 -04:00
|
|
|
static void fmt_setup _((char*,int,int,int,int));
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
static char*
|
|
|
|
remove_sign_bits(str, base)
|
|
|
|
char *str;
|
|
|
|
int base;
|
|
|
|
{
|
|
|
|
char *s, *t, *end;
|
2002-08-28 04:05:23 -04:00
|
|
|
unsigned long len;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
s = t = str;
|
2002-04-10 04:45:26 -04:00
|
|
|
len = strlen(str);
|
|
|
|
end = str + len;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (base == 16) {
|
2002-04-10 04:45:26 -04:00
|
|
|
while (t<end && *t == 'f') {
|
|
|
|
t++;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (base == 8) {
|
2002-04-10 04:45:26 -04:00
|
|
|
while (t<end && *t == '7') {
|
|
|
|
t++;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (base == 2) {
|
2002-04-10 04:45:26 -04:00
|
|
|
while (t<end && *t == '1') {
|
|
|
|
t++;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
while (*t) *s++ = *t++;
|
|
|
|
*s = '\0';
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define FNONE 0
|
|
|
|
#define FSHARP 1
|
|
|
|
#define FMINUS 2
|
|
|
|
#define FPLUS 4
|
|
|
|
#define FZERO 8
|
1999-01-19 23:59:39 -05:00
|
|
|
#define FSPACE 16
|
|
|
|
#define FWIDTH 32
|
|
|
|
#define FPREC 64
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-16 09:44:19 -04:00
|
|
|
#define CHECK(l) do {\
|
1998-01-16 07:13:05 -05:00
|
|
|
while (blen + (l) >= bsiz) {\
|
|
|
|
bsiz*=2;\
|
2002-10-16 09:44:19 -04:00
|
|
|
}\
|
|
|
|
rb_str_resize(result, bsiz);\
|
|
|
|
buf = RSTRING(result)->ptr;\
|
|
|
|
} while (0)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-04-18 04:46:18 -04:00
|
|
|
#define PUSH(s, l) do { \
|
1998-01-16 07:13:05 -05:00
|
|
|
CHECK(l);\
|
|
|
|
memcpy(&buf[blen], s, l);\
|
|
|
|
blen += (l);\
|
2002-04-18 04:46:18 -04:00
|
|
|
} while (0)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-16 09:44:19 -04:00
|
|
|
#define GETARG() (nextvalue != Qundef ? nextvalue : \
|
|
|
|
posarg < 0 ? \
|
|
|
|
(rb_raise(rb_eArgError, "unnumbered(%d) mixed with numbered", nextarg), 0) : \
|
|
|
|
(posarg = nextarg++, GETNTHARG(posarg)))
|
|
|
|
|
|
|
|
#define GETPOSARG(n) (posarg > 0 ? \
|
|
|
|
(rb_raise(rb_eArgError, "numbered(%d) after unnumbered(%d)", n, posarg), 0) : \
|
|
|
|
((n < 1) ? (rb_raise(rb_eArgError, "invalid index - %d$", n), 0) : \
|
|
|
|
(posarg = -1, GETNTHARG(n))))
|
|
|
|
|
|
|
|
#define GETNTHARG(nth) \
|
|
|
|
((nth >= argc) ? (rb_raise(rb_eArgError, "too few argument."), 0) : argv[nth])
|
1999-11-10 23:08:26 -05:00
|
|
|
|
2002-04-18 04:46:18 -04:00
|
|
|
#define GETASTER(val) do { \
|
1999-11-10 23:08:26 -05:00
|
|
|
t = p++; \
|
|
|
|
n = 0; \
|
|
|
|
for (; p < end && ISDIGIT(*p); p++) { \
|
|
|
|
n = 10 * n + (*p - '0'); \
|
|
|
|
} \
|
|
|
|
if (p >= end) { \
|
|
|
|
rb_raise(rb_eArgError, "malformed format string - %%*[0-9]"); \
|
|
|
|
} \
|
|
|
|
if (*p == '$') { \
|
2002-10-16 09:44:19 -04:00
|
|
|
tmp = GETPOSARG(n); \
|
1999-11-10 23:08:26 -05:00
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
tmp = GETARG(); \
|
|
|
|
p = t; \
|
|
|
|
} \
|
|
|
|
val = NUM2INT(tmp); \
|
2002-04-18 04:46:18 -04:00
|
|
|
} while (0)
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE
|
|
|
|
rb_f_sprintf(argc, argv)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
{
|
|
|
|
VALUE fmt;
|
|
|
|
char *buf, *p, *end;
|
|
|
|
int blen, bsiz;
|
|
|
|
VALUE result;
|
|
|
|
|
|
|
|
int width, prec, flags = FNONE;
|
2002-10-16 09:44:19 -04:00
|
|
|
int nextarg = 1;
|
|
|
|
int posarg = 0;
|
1999-12-06 04:04:03 -05:00
|
|
|
int tainted = 0;
|
2002-10-16 09:44:19 -04:00
|
|
|
VALUE nextvalue;
|
1999-01-19 23:59:39 -05:00
|
|
|
VALUE tmp;
|
|
|
|
VALUE str;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2002-10-16 09:44:19 -04:00
|
|
|
fmt = GETNTHARG(0);
|
1999-12-06 04:04:03 -05:00
|
|
|
if (OBJ_TAINTED(fmt)) tainted = 1;
|
2001-05-02 00:22:21 -04:00
|
|
|
StringValue(fmt);
|
|
|
|
p = RSTRING(fmt)->ptr;
|
|
|
|
end = p + RSTRING(fmt)->len;
|
1998-01-16 07:13:05 -05:00
|
|
|
blen = 0;
|
|
|
|
bsiz = 120;
|
2002-10-16 09:44:19 -04:00
|
|
|
result = rb_str_buf_new(bsiz);
|
|
|
|
buf = RSTRING(result)->ptr;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
for (; p < end; p++) {
|
1998-01-16 07:13:05 -05:00
|
|
|
char *t;
|
1999-11-10 23:08:26 -05:00
|
|
|
int n;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
for (t = p; t < end && *t != '%'; t++) ;
|
|
|
|
PUSH(p, t - p);
|
|
|
|
if (t >= end) {
|
|
|
|
/* end of fmt string */
|
|
|
|
goto sprint_exit;
|
|
|
|
}
|
|
|
|
p = t + 1; /* skip `%' */
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
width = prec = -1;
|
2002-10-16 09:44:19 -04:00
|
|
|
nextvalue = Qundef;
|
1998-01-16 07:13:05 -05:00
|
|
|
retry:
|
|
|
|
switch (*p) {
|
|
|
|
default:
|
1999-01-19 23:59:39 -05:00
|
|
|
if (ISPRINT(*p))
|
|
|
|
rb_raise(rb_eArgError, "malformed format string - %%%c", *p);
|
1998-01-16 07:13:05 -05:00
|
|
|
else
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eArgError, "malformed format string");
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ' ':
|
1999-01-19 23:59:39 -05:00
|
|
|
flags |= FSPACE;
|
1998-01-16 07:13:05 -05:00
|
|
|
p++;
|
|
|
|
goto retry;
|
|
|
|
|
|
|
|
case '#':
|
|
|
|
flags |= FSHARP;
|
|
|
|
p++;
|
|
|
|
goto retry;
|
|
|
|
|
|
|
|
case '+':
|
|
|
|
flags |= FPLUS;
|
|
|
|
p++;
|
|
|
|
goto retry;
|
|
|
|
|
|
|
|
case '-':
|
|
|
|
flags |= FMINUS;
|
|
|
|
p++;
|
|
|
|
goto retry;
|
|
|
|
|
|
|
|
case '0':
|
|
|
|
flags |= FZERO;
|
|
|
|
p++;
|
|
|
|
goto retry;
|
|
|
|
|
|
|
|
case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
1999-11-10 23:08:26 -05:00
|
|
|
n = 0;
|
1999-01-19 23:59:39 -05:00
|
|
|
for (; p < end && ISDIGIT(*p); p++) {
|
1999-11-10 23:08:26 -05:00
|
|
|
n = 10 * n + (*p - '0');
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
if (p >= end) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eArgError, "malformed format string - %%[0-9]");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-11-10 23:08:26 -05:00
|
|
|
if (*p == '$') {
|
2002-10-16 09:44:19 -04:00
|
|
|
if (nextvalue != Qundef) {
|
|
|
|
rb_raise(rb_eArgError, "value given twice - %d$", n);
|
|
|
|
}
|
|
|
|
nextvalue = GETPOSARG(n);
|
1999-11-10 23:08:26 -05:00
|
|
|
p++;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
width = n;
|
|
|
|
flags |= FWIDTH;
|
1998-01-16 07:13:05 -05:00
|
|
|
goto retry;
|
|
|
|
|
|
|
|
case '*':
|
|
|
|
if (flags & FWIDTH) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eArgError, "width given twice");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
flags |= FWIDTH;
|
1999-11-10 23:08:26 -05:00
|
|
|
GETASTER(width);
|
1998-01-16 07:13:05 -05:00
|
|
|
if (width < 0) {
|
|
|
|
flags |= FMINUS;
|
1999-01-19 23:59:39 -05:00
|
|
|
width = -width;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
p++;
|
|
|
|
goto retry;
|
|
|
|
|
|
|
|
case '.':
|
|
|
|
if (flags & FPREC) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eArgError, "precision given twice");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
2000-06-19 04:38:11 -04:00
|
|
|
flags |= FPREC;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
prec = 0;
|
|
|
|
p++;
|
|
|
|
if (*p == '*') {
|
1999-11-10 23:08:26 -05:00
|
|
|
GETASTER(prec);
|
2000-06-19 04:38:11 -04:00
|
|
|
if (prec < 0) { /* ignore negative precision */
|
|
|
|
flags &= ~FPREC;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
p++;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
for (; p < end && ISDIGIT(*p); p++) {
|
1998-01-16 07:13:05 -05:00
|
|
|
prec = 10 * prec + (*p - '0');
|
|
|
|
}
|
|
|
|
if (p >= end) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eArgError, "malformed format string - %%.[0-9]");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
goto retry;
|
|
|
|
|
|
|
|
case '\n':
|
|
|
|
p--;
|
|
|
|
case '\0':
|
|
|
|
case '%':
|
1999-09-01 05:48:03 -04:00
|
|
|
if (flags != FNONE) {
|
|
|
|
rb_raise(rb_eArgError, "illegal format character - %%");
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
PUSH("%", 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
{
|
|
|
|
VALUE val = GETARG();
|
|
|
|
char c;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (!(flags & FMINUS))
|
|
|
|
while (--width > 0)
|
|
|
|
PUSH(" ", 1);
|
1998-01-16 07:13:05 -05:00
|
|
|
c = NUM2INT(val) & 0xff;
|
|
|
|
PUSH(&c, 1);
|
1999-01-19 23:59:39 -05:00
|
|
|
while (--width > 0)
|
|
|
|
PUSH(" ", 1);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
{
|
|
|
|
VALUE arg = GETARG();
|
2002-08-21 11:47:54 -04:00
|
|
|
long len;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
str = rb_obj_as_string(arg);
|
1999-12-06 04:04:03 -05:00
|
|
|
if (OBJ_TAINTED(str)) tainted = 1;
|
1998-01-16 07:13:05 -05:00
|
|
|
len = RSTRING(str)->len;
|
|
|
|
if (flags&FPREC) {
|
|
|
|
if (prec < len) {
|
2000-12-12 02:42:35 -05:00
|
|
|
len = prec;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (flags&FWIDTH) {
|
|
|
|
if (width > len) {
|
|
|
|
CHECK(width);
|
1999-01-19 23:59:39 -05:00
|
|
|
width -= len;
|
1998-01-16 07:13:05 -05:00
|
|
|
if (!(flags&FMINUS)) {
|
|
|
|
while (width--) {
|
|
|
|
buf[blen++] = ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memcpy(&buf[blen], RSTRING(str)->ptr, len);
|
|
|
|
blen += len;
|
|
|
|
if (flags&FMINUS) {
|
|
|
|
while (width--) {
|
|
|
|
buf[blen++] = ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-01-10 04:52:13 -05:00
|
|
|
PUSH(RSTRING(str)->ptr, len);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
case 'd':
|
|
|
|
case 'i':
|
1998-01-16 07:13:05 -05:00
|
|
|
case 'o':
|
|
|
|
case 'x':
|
1999-01-19 23:59:39 -05:00
|
|
|
case 'X':
|
|
|
|
case 'b':
|
2001-10-22 02:48:18 -04:00
|
|
|
case 'B':
|
1998-01-16 07:13:05 -05:00
|
|
|
case 'u':
|
|
|
|
{
|
|
|
|
volatile VALUE val = GETARG();
|
|
|
|
char fbuf[32], nbuf[64], *s, *t;
|
1999-01-19 23:59:39 -05:00
|
|
|
char *prefix = 0;
|
|
|
|
int sign = 0;
|
|
|
|
char sc = 0;
|
|
|
|
long v;
|
|
|
|
int base, bignum = 0;
|
|
|
|
int len, pos;
|
|
|
|
|
|
|
|
switch (*p) {
|
|
|
|
case 'd':
|
|
|
|
case 'i':
|
|
|
|
sign = 1; break;
|
|
|
|
case 'o':
|
|
|
|
case 'x':
|
|
|
|
case 'X':
|
|
|
|
case 'b':
|
2001-10-22 02:48:18 -04:00
|
|
|
case 'B':
|
1999-01-19 23:59:39 -05:00
|
|
|
case 'u':
|
|
|
|
default:
|
|
|
|
if (flags&(FPLUS|FSPACE)) sign = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (flags & FSHARP) {
|
|
|
|
if (*p == 'o') prefix = "0";
|
|
|
|
else if (*p == 'x') prefix = "0x";
|
|
|
|
else if (*p == 'X') prefix = "0X";
|
|
|
|
else if (*p == 'b') prefix = "0b";
|
2001-10-22 02:48:18 -04:00
|
|
|
else if (*p == 'B') prefix = "0B";
|
1999-01-19 23:59:39 -05:00
|
|
|
if (prefix) {
|
|
|
|
width -= strlen(prefix);
|
|
|
|
}
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
bin_retry:
|
|
|
|
switch (TYPE(val)) {
|
|
|
|
case T_FLOAT:
|
1999-01-19 23:59:39 -05:00
|
|
|
val = rb_dbl2big(RFLOAT(val)->value);
|
|
|
|
if (FIXNUM_P(val)) goto bin_retry;
|
1998-01-16 07:13:05 -05:00
|
|
|
bignum = 1;
|
|
|
|
break;
|
|
|
|
case T_STRING:
|
2002-02-01 05:23:22 -05:00
|
|
|
val = rb_str_to_inum(val, 0, Qtrue);
|
1998-01-16 07:13:05 -05:00
|
|
|
goto bin_retry;
|
|
|
|
case T_BIGNUM:
|
|
|
|
bignum = 1;
|
|
|
|
break;
|
2000-01-04 23:41:21 -05:00
|
|
|
case T_FIXNUM:
|
|
|
|
v = FIX2LONG(val);
|
1998-01-16 07:13:05 -05:00
|
|
|
break;
|
2002-04-10 04:45:26 -04:00
|
|
|
default:
|
2002-06-04 03:34:19 -04:00
|
|
|
val = rb_Integer(val);
|
|
|
|
goto bin_retry;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if (*p == 'u' || *p == 'd' || *p == 'i') base = 10;
|
|
|
|
else if (*p == 'x' || *p == 'X') base = 16;
|
1998-01-16 07:13:05 -05:00
|
|
|
else if (*p == 'o') base = 8;
|
2001-10-22 02:48:18 -04:00
|
|
|
else if (*p == 'b' || *p == 'B') base = 2;
|
1998-01-16 07:13:05 -05:00
|
|
|
if (!bignum) {
|
1999-01-19 23:59:39 -05:00
|
|
|
if (base == 2) {
|
|
|
|
val = rb_int2big(v);
|
|
|
|
goto bin_retry;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
if (sign) {
|
|
|
|
char c = *p;
|
|
|
|
if (c == 'i') c = 'd'; /* %d and %i are identical */
|
1998-01-16 07:13:05 -05:00
|
|
|
if (v < 0) {
|
1999-01-19 23:59:39 -05:00
|
|
|
v = -v;
|
|
|
|
sc = '-';
|
|
|
|
width--;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
else if (flags & FPLUS) {
|
|
|
|
sc = '+';
|
|
|
|
width--;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
else if (flags & FSPACE) {
|
|
|
|
sc = ' ';
|
|
|
|
width--;
|
|
|
|
}
|
|
|
|
sprintf(fbuf, "%%l%c", c);
|
|
|
|
sprintf(nbuf, fbuf, v);
|
1998-01-16 07:13:05 -05:00
|
|
|
s = nbuf;
|
1999-01-19 23:59:39 -05:00
|
|
|
goto format_integer;
|
|
|
|
}
|
|
|
|
s = nbuf;
|
|
|
|
if (v < 0) {
|
2002-03-25 01:18:07 -05:00
|
|
|
if (base == 10) {
|
|
|
|
rb_warning("negative number for %%u specifier");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
strcpy(s, "..");
|
|
|
|
s += 2;
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
}
|
|
|
|
sprintf(fbuf, "%%l%c", *p);
|
|
|
|
sprintf(s, fbuf, v);
|
|
|
|
if (v < 0) {
|
|
|
|
char d = 0;
|
|
|
|
|
|
|
|
remove_sign_bits(s, base);
|
|
|
|
switch (base) {
|
|
|
|
case 16:
|
2002-04-10 04:45:26 -04:00
|
|
|
d = 'f'; break;
|
1999-01-19 23:59:39 -05:00
|
|
|
case 8:
|
|
|
|
d = '7'; break;
|
|
|
|
}
|
|
|
|
if (d && *s != d) {
|
|
|
|
memmove(s+1, s, strlen(s)+1);
|
|
|
|
*s = d;
|
|
|
|
}
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
s = nbuf;
|
|
|
|
goto format_integer;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
|
|
|
|
if (sign) {
|
|
|
|
val = rb_big2str(val, base);
|
|
|
|
s = RSTRING(val)->ptr;
|
|
|
|
if (s[0] == '-') {
|
|
|
|
s++;
|
|
|
|
sc = '-';
|
|
|
|
width--;
|
|
|
|
}
|
|
|
|
else if (flags & FPLUS) {
|
|
|
|
sc = '+';
|
|
|
|
width--;
|
|
|
|
}
|
|
|
|
else if (flags & FSPACE) {
|
|
|
|
sc = ' ';
|
|
|
|
width--;
|
|
|
|
}
|
|
|
|
goto format_integer;
|
|
|
|
}
|
|
|
|
if (!RBIGNUM(val)->sign) {
|
|
|
|
val = rb_big_clone(val);
|
|
|
|
rb_big_2comp(val);
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
val = rb_big2str(val, base);
|
1998-01-16 07:13:05 -05:00
|
|
|
s = RSTRING(val)->ptr;
|
1999-01-19 23:59:39 -05:00
|
|
|
if (*s == '-') {
|
2002-03-25 01:18:07 -05:00
|
|
|
if (base == 10) {
|
|
|
|
rb_warning("negative number for %%u specifier");
|
2002-04-10 04:45:26 -04:00
|
|
|
s++;
|
2002-03-25 01:18:07 -05:00
|
|
|
}
|
|
|
|
else {
|
2002-04-10 04:45:26 -04:00
|
|
|
remove_sign_bits(++s, base);
|
|
|
|
val = rb_str_new(0, 3+strlen(s));
|
|
|
|
t = RSTRING(val)->ptr;
|
2002-03-25 01:18:07 -05:00
|
|
|
strcpy(t, "..");
|
|
|
|
t += 2;
|
2002-04-10 04:45:26 -04:00
|
|
|
switch (base) {
|
|
|
|
case 16:
|
|
|
|
if (s[0] != 'f') strcpy(t++, "f"); break;
|
|
|
|
case 8:
|
|
|
|
if (s[0] != '7') strcpy(t++, "7"); break;
|
|
|
|
case 2:
|
|
|
|
if (s[0] != '1') strcpy(t++, "1"); break;
|
|
|
|
}
|
|
|
|
strcpy(t, s);
|
|
|
|
bignum = 2;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
s = RSTRING(val)->ptr;
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
format_integer:
|
|
|
|
pos = -1;
|
|
|
|
len = strlen(s);
|
|
|
|
|
|
|
|
if (*p == 'X') {
|
|
|
|
char *pp = s;
|
|
|
|
while (*pp) {
|
|
|
|
*pp = toupper(*pp);
|
|
|
|
pp++;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
if (prec < len) prec = len;
|
|
|
|
width -= prec;
|
|
|
|
if (!(flags&(FZERO|FMINUS)) && s[0] != '.') {
|
|
|
|
CHECK(width);
|
|
|
|
while (width-->0) {
|
|
|
|
buf[blen++] = ' ';
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
if (sc) PUSH(&sc, 1);
|
|
|
|
if (prefix) {
|
|
|
|
int plen = strlen(prefix);
|
2001-01-10 04:52:13 -05:00
|
|
|
PUSH(prefix, plen);
|
1999-01-19 23:59:39 -05:00
|
|
|
if (pos) pos += plen;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
if (!(flags & FMINUS)) {
|
|
|
|
char c = ' ';
|
|
|
|
|
|
|
|
if (s[0] == '.') {
|
|
|
|
c = '.';
|
|
|
|
if ((flags & FPREC) && prec > len) {
|
|
|
|
pos = blen;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pos = blen + 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (flags & FZERO) c = '0';
|
|
|
|
CHECK(width);
|
|
|
|
while (width-->0) {
|
|
|
|
buf[blen++] = c;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
CHECK(prec - len);
|
|
|
|
while (len < prec--) {
|
|
|
|
buf[blen++] = s[0]=='.'?'.':'0';
|
|
|
|
}
|
2001-01-10 04:52:13 -05:00
|
|
|
PUSH(s, len);
|
1999-01-19 23:59:39 -05:00
|
|
|
CHECK(width);
|
|
|
|
while (width-->0) {
|
|
|
|
buf[blen++] = ' ';
|
|
|
|
}
|
|
|
|
if (pos >= 0 && buf[pos] == '.') {
|
|
|
|
char c = '.';
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
switch (base) {
|
|
|
|
case 16:
|
1999-01-19 23:59:39 -05:00
|
|
|
if (*p == 'X') c = 'F';
|
|
|
|
else c = 'f';
|
|
|
|
break;
|
1998-01-16 07:13:05 -05:00
|
|
|
case 8:
|
1999-01-19 23:59:39 -05:00
|
|
|
c = '7'; break;
|
2001-01-15 02:01:00 -05:00
|
|
|
case 2:
|
1999-01-19 23:59:39 -05:00
|
|
|
c = '1'; break;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-01-19 23:59:39 -05:00
|
|
|
s = &buf[pos];
|
|
|
|
while (*s && *s == '.') {
|
|
|
|
*s++ = c;
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
case 'g':
|
1999-08-13 01:45:20 -04:00
|
|
|
case 'G':
|
1998-01-16 07:13:05 -05:00
|
|
|
case 'e':
|
1999-01-19 23:59:39 -05:00
|
|
|
case 'E':
|
1998-01-16 07:13:05 -05:00
|
|
|
{
|
|
|
|
VALUE val = GETARG();
|
|
|
|
double fval;
|
1999-08-13 01:45:20 -04:00
|
|
|
int i, need = 6;
|
1998-01-16 07:13:05 -05:00
|
|
|
char fbuf[32];
|
|
|
|
|
2001-09-03 01:37:42 -04:00
|
|
|
fval = RFLOAT(rb_Float(val))->value;
|
1998-01-16 07:13:05 -05:00
|
|
|
fmt_setup(fbuf, *p, flags, width, prec);
|
1999-08-13 01:45:20 -04:00
|
|
|
need = 0;
|
|
|
|
if (*p != 'e' && *p != 'E') {
|
|
|
|
i = INT_MIN;
|
|
|
|
frexp(fval, &i);
|
|
|
|
if (i > 0)
|
|
|
|
need = BIT_DIGITS(i);
|
|
|
|
}
|
|
|
|
need += (flags&FPREC) ? prec : 6;
|
|
|
|
if ((flags&FWIDTH) && need < width)
|
|
|
|
need = width;
|
|
|
|
need += 20;
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
CHECK(need);
|
1998-01-16 07:13:05 -05:00
|
|
|
sprintf(&buf[blen], fbuf, fval);
|
|
|
|
blen += strlen(&buf[blen]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
flags = FNONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
sprint_exit:
|
1999-11-10 23:08:26 -05:00
|
|
|
#if 0
|
|
|
|
/* XXX - We cannot validiate the number of arguments because
|
|
|
|
* the format string may contain `n$'-style argument selector.
|
|
|
|
*/
|
|
|
|
if (RTEST(ruby_verbose) && nextarg < argc) {
|
1999-01-19 23:59:39 -05:00
|
|
|
rb_raise(rb_eArgError, "too many argument for format string");
|
1998-01-16 07:13:05 -05:00
|
|
|
}
|
1999-11-10 23:08:26 -05:00
|
|
|
#endif
|
2002-10-16 09:44:19 -04:00
|
|
|
rb_str_resize(result, blen);
|
1998-01-16 07:13:05 -05:00
|
|
|
|
1999-12-06 04:04:03 -05:00
|
|
|
if (tainted) OBJ_TAINT(result);
|
1998-01-16 07:13:05 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fmt_setup(buf, c, flags, width, prec)
|
1999-09-01 05:48:03 -04:00
|
|
|
char *buf;
|
|
|
|
int c;
|
1998-01-16 07:13:05 -05:00
|
|
|
int flags, width, prec;
|
|
|
|
{
|
|
|
|
*buf++ = '%';
|
|
|
|
if (flags & FSHARP) *buf++ = '#';
|
|
|
|
if (flags & FPLUS) *buf++ = '+';
|
|
|
|
if (flags & FMINUS) *buf++ = '-';
|
|
|
|
if (flags & FZERO) *buf++ = '0';
|
1999-01-19 23:59:39 -05:00
|
|
|
if (flags & FSPACE) *buf++ = ' ';
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
if (flags & FWIDTH) {
|
|
|
|
sprintf(buf, "%d", width);
|
|
|
|
buf += strlen(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & FPREC) {
|
|
|
|
sprintf(buf, ".%d", prec);
|
|
|
|
buf += strlen(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
*buf++ = c;
|
|
|
|
*buf = '\0';
|
|
|
|
}
|