24 #define BIT_DIGITS(N) (((N)*146)/485 + 1) 26 static void fmt_setup(
char*,
size_t,
int,
int,
int,
int);
35 if (*p ==
'X') c =
'F';
56 #define CHECK(l) do {\ 57 int cr = ENC_CODERANGE(result);\ 58 while (blen + (l) >= bsiz) {\ 61 rb_str_resize(result, bsiz);\ 62 ENC_CODERANGE_SET(result, cr);\ 63 buf = RSTRING_PTR(result);\ 66 #define PUSH(s, l) do { \ 71 #define PUSH_(s, l) do { \ 72 memcpy(&buf[blen], (s), (l));\ 76 #define FILL(c, l) do { \ 81 #define FILL_(c, l) do { \ 82 memset(&buf[blen], (c), (l));\ 86 #define GETARG() (nextvalue != Qundef ? nextvalue : \ 89 #define GETNEXTARG() ( \ 90 check_next_arg(posarg, nextarg), \ 91 (posarg = nextarg++, GETNTHARG(posarg))) 93 #define GETPOSARG(n) ( \ 94 check_pos_arg(posarg, (n)), \ 95 (posarg = -1, GETNTHARG(n))) 97 #define GETNTHARG(nth) \ 98 (((nth) >= argc) ? (rb_raise(rb_eArgError, "too few arguments"), 0) : argv[(nth)]) 100 #define CHECKNAMEARG(name, len, enc) ( \ 101 check_name_arg(posarg, name, len, enc), \ 104 #define GETNUM(n, val) \ 105 (!(p = get_num(p, end, enc, &(n))) ? \ 106 rb_raise(rb_eArgError, #val " too big") : (void)0) 108 #define GETASTER(val) do { \ 113 tmp = GETPOSARG(n); \ 116 tmp = GETNEXTARG(); \ 119 (val) = NUM2INT(tmp); \ 130 if (INT_MAX - (*p -
'0') < next_n)
190 return (*hash = tmp);
463 enum {default_float_precision = 6};
472 int width, prec, flags =
FNONE;
481 #define CHECK_FOR_WIDTH(f) \ 482 if ((f) & FWIDTH) { \ 483 rb_raise(rb_eArgError, "width given twice"); \ 485 if ((f) & FPREC0) { \ 486 rb_raise(rb_eArgError, "width after precision"); \ 488 #define CHECK_FOR_FLAGS(f) \ 489 if ((f) & FWIDTH) { \ 490 rb_raise(rb_eArgError, "flag after width"); \ 492 if ((f) & FPREC0) { \ 493 rb_raise(rb_eArgError, "flag after precision"); \ 509 memset(buf, 0, bsiz);
512 for (; p < end; p++) {
517 for (t = p; t < end && *t !=
'%'; t++) ;
570 case '1':
case '2':
case '3':
case '4':
571 case '5':
case '6':
case '7':
case '8':
case '9':
575 if (nextvalue !=
Qundef) {
590 const char *start = p;
591 char term = (*p ==
'<') ?
'>' :
'}';
594 for (; p < end && *p !=
term; ) {
600 #if SIZEOF_INT < SIZEOF_SIZE_T 601 if ((
size_t)(p - start) >= INT_MAX) {
602 const int message_limit = 20;
605 "too long name (%"PRIuSIZE" bytes) - %.*s...%c",
606 (
size_t)(p - start - 2), len, start, term);
609 len = (int)(p - start + 1);
620 if (nextvalue ==
Qundef) {
627 if (
NIL_P(nextvalue)) {
631 if (term ==
'}')
goto format_s;
671 if (flags !=
FNONE) {
704 else if ((flags &
FMINUS)) {
708 if (width > 1)
FILL(
' ', width-1);
711 if (width > 1)
FILL(
' ', width-1);
748 if ((flags&
FPREC) && (prec < slen)) {
755 if ((flags&
FWIDTH) && (width > slen)) {
795 const char *prefix = 0;
796 int sign = 0, dots = 0;
799 int base, bignum = 0;
820 prefix =
"0x";
break;
822 prefix =
"0X";
break;
824 prefix =
"0b";
break;
826 prefix =
"0B";
break;
872 int numbits =
ffs(base)-1;
876 if (INT_MAX-1 < numdigits)
891 else if (flags &
FPLUS) {
895 else if (flags &
FSPACE) {
910 if (numdigits == 0 ||
911 ((abs_nlz_bits != (
size_t)(numbits-1) ||
933 else if (flags &
FPLUS) {
937 else if (flags &
FSPACE) {
941 snprintf(nbuf,
sizeof(nbuf),
"%ld", v);
955 else if (flags &
FPLUS) {
959 else if (flags &
FSPACE) {
974 while ((c = (
int)(
unsigned char)*pp) != 0) {
979 if (prefix && !prefix[1]) {
983 else if (len == 1 && *s ==
'0') {
985 if (flags &
FPREC) prec--;
987 else if ((flags &
FPREC) && (prec > len)) {
991 else if (len == 1 && *s ==
'0') {
995 width -= (int)
strlen(prefix);
1003 if (!prefix && prec == 0 && len == 1 && *s ==
'0') len = 0;
1010 while (width-- > 0) {
1014 if (sc)
PUSH(&sc, 1);
1016 int plen = (int)
strlen(prefix);
1020 if (dots)
PUSH(
"..", 2);
1021 if (!sign && valsign < 0) {
1023 while (len < prec--) {
1027 else if ((flags & (FMINUS|
FPREC)) != FMINUS) {
1028 while (len < prec--) {
1035 while (width-- > 0) {
1044 int sign = (flags&
FPLUS) ? 1 : 0,
zero = 0;
1058 if (!(flags&
FPREC)) prec = default_float_precision;
1075 else if (prec >= 0) {
1080 if (prec >= len) len = prec + 1;
1082 if (prec > 0) ++
len;
1083 CHECK(len > width ? len : width);
1084 fill = width > len ? width -
len : 0;
1088 if (sign || (flags&FSPACE)) {
1089 buf[blen++] = sign > 0 ?
'+' : sign < 0 ?
'-' :
' ';
1091 if (fill && !(flags&FMINUS) && (flags&FZERO)) {
1097 PUSH_(t, len - prec);
1108 else if (prec > len) {
1109 FILL_(
'0', prec - len);
1112 else if (prec > 0) {
1113 PUSH_(t + len - prec, prec);
1115 if (fill && (flags&FMINUS)) {
1147 need = (int)
strlen(expr);
1150 if (!
isnan(fval) && fval < 0.0)
1153 sign = (flags &
FPLUS) ?
'+' :
' ';
1156 if ((flags &
FWIDTH) && need < width)
1162 buf[blen - need--] = sign;
1163 memcpy(&buf[blen - need], expr, elen);
1167 buf[blen - elen - 1] = sign;
1168 memcpy(&buf[blen - elen], expr, elen);
1173 fmt_setup(fbuf,
sizeof(fbuf), *p, flags, width, prec);
1175 if (*p !=
'e' && *p !=
'E') {
1181 need += (flags&
FPREC) ? prec : default_float_precision;
1182 if ((flags&
FWIDTH) && need < width)
1187 snprintf(&buf[blen], need, fbuf, fval);
1188 blen +=
strlen(&buf[blen]);
1199 if (posarg >= 0 && nextarg < argc) {
1200 const char *mesg =
"too many arguments for format string";
1213 char *end = buf +
size;
1215 if (flags &
FSHARP) *buf++ =
'#';
1216 if (flags &
FPLUS) *buf++ =
'+';
1217 if (flags &
FMINUS) *buf++ =
'-';
1218 if (flags &
FZERO) *buf++ =
'0';
1219 if (flags &
FSPACE) *buf++ =
' ';
1222 snprintf(buf, end - buf,
"%d", width);
1226 if (flags &
FPREC) {
1227 snprintf(buf, end - buf,
".%d", prec);
1236 #define FILE rb_printf_buffer 1237 #define __sbuf rb_printf_sbuf 1238 #define __sFILE rb_printf_sfile 1243 #if SIZEOF_LONG < SIZEOF_VOIDP 1244 # if SIZEOF_LONG_LONG == SIZEOF_VOIDP 1245 # define _HAVE_SANE_QUAD_ 1246 # define _HAVE_LLP64_ 1247 # define quad_t LONG_LONG 1248 # define u_quad_t unsigned LONG_LONG 1250 #elif SIZEOF_LONG != SIZEOF_LONG_LONG && SIZEOF_LONG_LONG == 8 1251 # define _HAVE_SANE_QUAD_ 1252 # define quad_t LONG_LONG 1253 # define u_quad_t unsigned LONG_LONG 1255 #define FLOATING_POINT 1 1256 #define BSD__dtoa ruby_dtoa 1257 #define BSD__hdtoa ruby_hdtoa 1258 #ifdef RUBY_PRI_VALUE_MARK 1259 # define PRI_EXTRA_MARK RUBY_PRI_VALUE_MARK 1261 #define lower_hexdigits (ruby_hexdigits+0) 1262 #define upper_hexdigits (ruby_hexdigits+16) 1274 f._bf._base = f._p = (
unsigned char *)str;
1275 f._bf._size = f._w = n - 1;
1308 char *
buf = (
char*)fp->_p;
1312 if (
RBASIC(result)->klass) {
1320 for (iov = uio->
uio_iov; len > 0; ++iov) {
1325 fp->_p = (
unsigned char *)
buf;
1337 if (valsize !=
sizeof(
VALUE))
return 0;
1338 value = *(
VALUE *)valp;
1339 if (
RBASIC(result)->klass) {
1344 # define LITERAL(str) (*sz = rb_strlen_lit(str), str) 1367 if (sign ==
' ') value =
QUOTE(value);
1378 *(
volatile VALUE *)valp = value;
1391 #define f buffer.base 1406 f._bf._base = (
unsigned char *)result;
1426 va_start(ap, format);
1445 va_start(ap, format);
1456 #define f buffer.base 1464 f._bf._base = (
unsigned char *)str;
1466 klass =
RBASIC(str)->klass;
1484 va_start(ap, format);
RUBY_EXTERN VALUE rb_cString
static VALUE rb_rational_num(VALUE rat)
int ruby_snprintf(char *str, size_t n, char const *fmt,...)
VALUE rb_int_plus(VALUE x, VALUE y)
#define RBASIC_CLEAR_CLASS(obj)
int rb_enc_codelen(int c, rb_encoding *enc)
int rb_integer_pack(VALUE val, void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
static VALUE get_hash(volatile VALUE *hash, int argc, const VALUE *argv)
rb_encoding * rb_enc_check(VALUE str1, VALUE str2)
RUBY_EXTERN VALUE rb_cFalseClass
void rb_enc_copy(VALUE obj1, VALUE obj2)
long rb_str_coderange_scan_restartable(const char *, const char *, rb_encoding *, int *)
size_t strlen(const char *)
VALUE rb_sym_intern(const char *ptr, long len, rb_encoding *enc)
static unsigned int hash(str, len) register const char *str
static int ruby__sfvwrite(register rb_printf_buffer *fp, register struct __suio *uio)
const char ruby_digitmap[]
#define ENC_CODERANGE_SET(obj, cr)
if(len<=MAX_WORD_LENGTH &&len >=MIN_WORD_LENGTH)
rb_encoding * rb_enc_compatible(VALUE str1, VALUE str2)
void rb_str_set_len(VALUE, long)
VALUE rb_int_mul(VALUE x, VALUE y)
unsigned int rb_enc_codepoint_len(const char *p, const char *e, int *len_p, rb_encoding *enc)
void rb_raise(VALUE exc, const char *fmt,...)
#define RSTRING_GETMEM(str, ptrvar, lenvar)
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
int rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc)
VALUE rb_enc_vsprintf(rb_encoding *enc, const char *fmt, va_list ap)
VALUE rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags, VALUE ecopts)
#define INTEGER_PACK_2COMP
#define ENC_CODERANGE_7BIT
VALUE rb_enc_sprintf(rb_encoding *enc, const char *format,...)
VALUE rb_str_format(int argc, const VALUE *argv, VALUE fmt)
char * rb_enc_nth(const char *, const char *, long, rb_encoding *)
int rb_enc_toupper(int c, rb_encoding *enc)
VALUE rb_hash_default_value(VALUE hash, VALUE key)
static char sign_bits(int base, const char *p)
int rb_absint_singlebit_p(VALUE val)
#define RBASIC_SET_CLASS_RAW(obj, cls)
#define RB_TYPE_P(obj, type)
static const char * ruby__sfvextra(rb_printf_buffer *fp, size_t valsize, void *valp, long *sz, int sign)
VALUE rb_int_idiv(VALUE x, VALUE y)
VALUE rb_dbl2big(double d)
static int BSD__sfvwrite(register FILE *fp, register struct __suio *uio)
VALUE rb_str_to_inum(VALUE str, int base, int badcheck)
#define ECONV_INVALID_REPLACE
VALUE rb_obj_as_string(VALUE)
RUBY_EXTERN VALUE rb_cTrueClass
VALUE rb_int2str(VALUE num, int base)
static const char * get_num(const char *p, const char *end, rb_encoding *enc, int *valp)
VALUE rb_vsprintf(const char *fmt, va_list ap)
#define BIGNUM_NEGATIVE_P(b)
#define ENC_CODERANGE_UNKNOWN
#define rb_enc_isprint(c, enc)
#define MEMCPY(p1, p2, type, n)
RUBY_EXTERN int isinf(double)
#define ENC_CODERANGE_BROKEN
void rb_enc_raise(rb_encoding *enc, VALUE exc, const char *fmt,...)
VALUE rb_check_symbol_cstr(const char *ptr, long len, rb_encoding *enc)
#define rb_enc_mbminlen(enc)
#define CHECK_FOR_WIDTH(f)
VALUE rb_big2str(VALUE x, int base)
RUBY_EXTERN VALUE rb_int_positive_pow(long x, unsigned long y)
VALUE rb_str_resize(VALUE, long)
static VALUE rb_rational_den(VALUE rat)
VALUE rb_sprintf(const char *format,...)
VALUE rb_f_sprintf(int argc, const VALUE *argv)
VALUE rb_check_hash_type(VALUE hash)
unsigned char buf[MIME_BUF_SIZE]
#define rb_enc_isdigit(c, enc)
static void fmt_setup(char *, size_t, int, int, int, int)
RUBY_EXTERN VALUE rb_cInteger
#define INTEGER_PACK_BIG_ENDIAN
register unsigned int len
#define StringValueCStr(v)
#define rb_enc_right_char_head(s, p, e, enc)
#define ECONV_UNDEF_REPLACE
void rb_str_modify(VALUE)
static void check_next_arg(int posarg, int nextarg)
rb_encoding * rb_enc_get(VALUE obj)
VALUE rb_big_uminus(VALUE x)
VALUE rb_hash_lookup2(VALUE hash, VALUE key, VALUE def)
long rb_enc_strlen(const char *, const char *, rb_encoding *)
static double float_value(VALUE v)
static ssize_t BSD_vfprintf(FILE *fp, const char *fmt0, va_list ap)
VALUE rb_str_vcatf(VALUE str, const char *fmt, va_list ap)
VALUE rb_str_catf(VALUE str, const char *format,...)
VALUE rb_check_string_type(VALUE)
size_t rb_str_capacity(VALUE str)
static void check_name_arg(int posarg, const char *name, int len, rb_encoding *enc)
size_t rb_absint_numwords(VALUE val, size_t word_numbits, size_t *nlz_bits_ret)
#define CHECKNAMEARG(name, len, enc)
#define CHECK_FOR_FLAGS(f)
int ruby_vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
RUBY_EXTERN VALUE rb_cSymbol
#define rb_enc_mbcput(c, buf, enc)
VALUE rb_str_buf_new(long)
#define MUL_OVERFLOW_INT_P(a, b)
#define RB_INTEGER_TYPE_P(obj)
void rb_warn(const char *fmt,...)
RUBY_EXTERN VALUE rb_cNilClass
static void check_pos_arg(int posarg, int n)
VALUE rb_str_new(const char *, long)