Ruby 2.0.0p247(2013-06-27revision41674)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

Data Structures | Macros | Functions
sprintf.c File Reference
`#include "ruby/ruby.h"`
`#include "ruby/re.h"`
`#include "ruby/encoding.h"`
`#include <math.h>`
`#include <stdarg.h>`
`#include "vsnprintf.c"`
Include dependency graph for sprintf.c:

Go to the source code of this file.

## Data Structures
struct | rb_printf_buffer_extra

## Macros
#define | BIT_DIGITS(N) (((N)*146)/485 + 1) /* log2(10) =~ 146/485 */
#define | BITSPERDIG (SIZEOF_BDIGITS*CHAR_BIT)
#define | EXTENDSIGN(n, l) (((~0 << (n)) >> (((n)*(l)) % BITSPERDIG)) & ~(~0 << (n)))
#define | FNONE 0
#define | FSHARP 1
#define | FMINUS 2
#define | FPLUS 4
#define | FZERO 8
#define | FSPACE 16
#define | FWIDTH 32
#define | FPREC 64
#define | FPREC0 128
#define | CHECK(l)
#define | PUSH(s, l)
#define | FILL(c, l)
#define | GETARG()
#define | GETPOSARG(n)
#define | GETNTHARG(nth) (((nth) >= argc) ? (rb_raise(rb_eArgError, "too few arguments"), 0) : argv[(nth)])
#define | GETNAMEARG(id, name, len, enc)
#define | GETNUM(n, val)
#define | GETASTER(val)
#define | CHECK_FOR_WIDTH(f)
#define | CHECK_FOR_FLAGS(f)
#define | FILE rb_printf_buffer
#define | __sbuf rb_printf_sbuf
#define | __sFILE rb_printf_sfile
#define | FLOATING_POINT 1
#define | BSD__dtoa ruby_dtoa
#define | BSD__hdtoa ruby_hdtoa
#define | f buffer.base

## Functions
static void | fmt_setup (char *, size_t, int, int, int, int)
static char * | remove_sign_bits (char *str, int base)
static char | sign_bits (int base, const char *p)
static VALUE | get_hash (volatile VALUE *hash, int argc, const VALUE *argv)
VALUE | rb_f_sprintf (int argc, const VALUE *argv)
VALUE | rb_str_format (int argc, const VALUE *argv, VALUE fmt)
static int | ruby__sfvwrite (register rb_printf_buffer *fp, register struct __suio *uio)
static char * | ruby__sfvextra (rb_printf_buffer *fp, size_t valsize, void *valp, long *sz, int sign)
VALUE | rb_enc_vsprintf (rb_encoding *enc, const char *fmt, va_list ap)
VALUE | rb_enc_sprintf (rb_encoding *enc, const char *format,...)
VALUE | rb_vsprintf (const char *fmt, va_list ap)
VALUE | rb_sprintf (const char *format,...)
VALUE | rb_str_vcatf (VALUE str, const char *fmt, va_list ap)
VALUE | rb_str_catf (VALUE str, const char *format,...)

## Macro Definition Documentation

#define __sbuf rb_printf_sbuf

Definition at line 1126 of file sprintf.c.

#define __sFILE rb_printf_sfile

Definition at line 1127 of file sprintf.c.

#define BIT_DIGITS | ( |  | N) |  | (((N)*146)/485 + 1) /* log2(10) =~ 146/485 */

Definition at line 24 of file sprintf.c.

Referenced by rb_str_format().

#define BITSPERDIG (SIZEOF_BDIGITS*CHAR_BIT)

Definition at line 25 of file sprintf.c.

#define BSD__dtoa ruby_dtoa

Definition at line 1145 of file sprintf.c.

#define BSD__hdtoa ruby_hdtoa

Definition at line 1146 of file sprintf.c.

#define CHECK | ( |  | l) |  |

Value:
do {\
int cr = ENC_CODERANGE(result);\
while (blen + (l) >= bsiz) {\
bsiz*=2;\
}\
rb_str_resize(result, bsiz);\
ENC_CODERANGE_SET(result, cr);\
buf = RSTRING_PTR(result);\
} while (0)

Definition at line 83 of file sprintf.c.

Referenced by rb_str_format(), and ruby__sfvwrite().

#define CHECK_FOR_FLAGS | ( |  | f) |  |

Value:
if ((f) & FWIDTH) { \
rb_raise(rb_eArgError, "flag after width"); \
} \
if ((f) & FPREC0) { \
rb_raise(rb_eArgError, "flag after precision"); \
}

Referenced by rb_str_format().

#define CHECK_FOR_WIDTH | ( |  | f) |  |

Value:
if ((f) & FWIDTH) { \
rb_raise(rb_eArgError, "width given twice"); \
} \
if ((f) & FPREC0) { \
rb_raise(rb_eArgError, "width after precision"); \
}

Referenced by rb_str_format().

#define EXTENDSIGN | ( |  | n,
|  |  | l
| ) |  | (((~0 << (n)) >> (((n)*(l)) % BITSPERDIG)) & ~(~0 << (n)))

Definition at line 26 of file sprintf.c.

Referenced by remove_sign_bits().

#define f buffer.base

Referenced by BigDecimal_divremain(), BigDecimal_mode(), BigDecimal_remainder(), BigDecimal_to_s(), BigDecimalCmp(), f_signbit(), flo_ceil(), flo_floor(), flo_round(), flo_truncate(), float_decode_internal(), float_rationalize(), float_to_r(), get_special_folder(), init_env(), int_round_0(), iseq_s_compile_file(), lgamma_r(), load_file_internal(), onigenc_property_list_init(), ossl_x509stctx_set_flags(), ossl_x509store_set_flags(), pack_pack(), parse_time2_cb(), rand_range(), rb_dlptr_initialize(), rb_dlptr_s_malloc(), rb_enc_vsprintf(), rb_feature_p(), rb_fiddle_ptr_initialize(), rb_fiddle_ptr_s_malloc(), rb_load_file(), rb_str_justify(), rb_str_vcatf(), rb_thread_blocking_region(), round(), ruby_init_loadpath_safe(), ruby_snprintf(), ruby_vsnprintf(), tgamma(), time_timespec(), trans_sweep(), VpInternalRound(), VpSqrt(), and VpVtoD().

#define f buffer.base

#define FILE rb_printf_buffer

Definition at line 1125 of file sprintf.c.

#define FILL | ( |  | c,
|  |  | l
| ) |  |

Value:
do { \
CHECK(l);\
memset(&buf[blen], (c), (l));\
blen += (l);\
} while (0)

Definition at line 99 of file sprintf.c.

Referenced by rb_str_format().

#define FLOATING_POINT 1

Definition at line 1144 of file sprintf.c.

#define FMINUS 2

Definition at line 75 of file sprintf.c.

Referenced by fmt_setup(), and rb_str_format().

#define FNONE 0

Definition at line 73 of file sprintf.c.

Referenced by rb_str_format().

#define FPLUS 4

Definition at line 76 of file sprintf.c.

Referenced by fmt_setup(), and rb_str_format().

#define FPREC 64

Definition at line 80 of file sprintf.c.

Referenced by fmt_setup(), and rb_str_format().

#define FPREC0 128

Definition at line 81 of file sprintf.c.

Referenced by rb_str_format().

#define FSHARP 1

Definition at line 74 of file sprintf.c.

Referenced by fmt_setup(), and rb_str_format().

#define FSPACE 16

Definition at line 78 of file sprintf.c.

Referenced by fmt_setup(), and rb_str_format().

#define FWIDTH 32

Definition at line 79 of file sprintf.c.

Referenced by fmt_setup(), and rb_str_format().

#define FZERO 8

Definition at line 77 of file sprintf.c.

Referenced by fmt_setup(), and rb_str_format().

#define GETARG | ( | ) |  |

Value:
(nextvalue != Qundef ? nextvalue : \
posarg == -1 ? \
(rb_raise(rb_eArgError, "unnumbered(%d) mixed with numbered", nextarg), 0) : \
posarg == -2 ? \
(rb_raise(rb_eArgError, "unnumbered(%d) mixed with named", nextarg), 0) : \
(posarg = nextarg++, GETNTHARG(posarg)))

Definition at line 105 of file sprintf.c.

Referenced by rb_str_format().

#define GETASTER | ( |  | val) |  |

Value:
do { \
t = p++; \
n = 0; \
GETNUM(n, (val)); \
if (*p == '$') { \
tmp = GETPOSARG(n); \
} \
else { \
tmp = GETARG(); \
p = t; \
} \
(val) = NUM2INT(tmp); \
} while (0)

Definition at line 141 of file sprintf.c.

Referenced by rb_str_format().

#define GETNAMEARG | ( |  | id,
|  |  | name,
|  |  | len,
|  |  | enc
| ) |  |

Value:
( \
posarg > 0 ? \
(rb_enc_raise((enc), rb_eArgError, "named%.*s after unnumbered(%d)", (len), (name), posarg), 0) : \
posarg == -1 ? \
(rb_enc_raise((enc), rb_eArgError, "named%.*s after numbered", (len), (name)), 0) : \
(posarg = -2, rb_hash_lookup2(get_hash(&hash, argc, argv), (id), Qundef)))

Definition at line 122 of file sprintf.c.

Referenced by rb_str_format().

#define GETNTHARG | ( |  | nth) |  | (((nth) >= argc) ? (rb_raise(rb_eArgError, "too few arguments"), 0) : argv[(nth)])

Definition at line 119 of file sprintf.c.

Referenced by rb_f_sprintf().

#define GETNUM | ( |  | n,
|  |  | val
| ) |  |

Value:
for (; p < end && rb_enc_isdigit(*p, enc); p++) { \
int next_n = 10 * (n) + (*p - '0'); \
if (next_n / 10 != (n)) {\
rb_raise(rb_eArgError, #val " too big"); \
} \
(n) = next_n; \
} \
if (p >= end) { \
rb_raise(rb_eArgError, "malformed format string - %%*[0-9]"); \
}

Definition at line 129 of file sprintf.c.

Referenced by rb_str_format().

#define GETPOSARG | ( |  | n) |  |

Value:
(posarg > 0 ? \
(rb_raise(rb_eArgError, "numbered(%d) after unnumbered(%d)", (n), posarg), 0) : \
posarg == -2 ? \
(rb_raise(rb_eArgError, "numbered(%d) after named", (n)), 0) : \
(((n) < 1) ? (rb_raise(rb_eArgError, "invalid index - %d$", (n)), 0) : \
(posarg = -1, GETNTHARG(n))))

Definition at line 112 of file sprintf.c.

Referenced by rb_str_format().

#define PUSH | ( |  | s,
|  |  | l
| ) |  |

Value:
do { \
CHECK(l);\
memcpy(&buf[blen], (s), (l));\
blen += (l);\
} while (0)

Definition at line 93 of file sprintf.c.

Referenced by rb_str_format().

## Function Documentation

static void fmt_setup | ( | char * | buf,
|  | size_t | size,
|  | int | c,
|  | int | flags,
|  | int | width,
|  | int | prec
| ) |  |
| static

Definition at line 1100 of file sprintf.c.

References c, FMINUS, FPLUS, FPREC, FSHARP, FSPACE, FWIDTH, FZERO, size, snprintf, and strlen().

Referenced by rb_str_format().

static VALUE get_hash | ( | volatile VALUE * | hash,
|  | int | argc,
|  | const VALUE * | argv
| ) |  |
| static

Definition at line 156 of file sprintf.c.

References hash(), NIL_P, Qundef, rb_check_hash_type(), rb_eArgError, and rb_raise().

VALUE rb_enc_sprintf | ( | rb_encoding * | enc,
|  | const char * | format,
|  |  | ...
| ) |  |

Definition at line 1251 of file sprintf.c.

References rb_enc_vsprintf(), and result.

Referenced by location_format(), mk_inspect(), of2str(), rb_str_upto(), and setup_exception().

VALUE rb_enc_vsprintf | ( | rb_encoding * | enc,
|  | const char * | fmt,
|  | va_list | ap
| ) |  |

Definition at line 1218 of file sprintf.c.

References __SSTR, __SWR, BSD_vfprintf(), f, rb_cString, rb_eArgError, rb_enc_associate(), rb_enc_mbminlen, rb_enc_name, rb_raise(), rb_str_buf_new(), rb_str_resize(), RBASIC, result, RSTRING_PTR, ruby__sfvextra(), ruby__sfvwrite(), and rb_printf_buffer_extra::value.

Referenced by rb_enc_raise(), rb_enc_sprintf(), rb_loaderror(), rb_loaderror_with_path(), and rb_vsprintf().

VALUE rb_f_sprintf | ( | int | argc,
|  | const VALUE * | argv
| ) |  |

Definition at line 433 of file sprintf.c.

References GETNTHARG, and rb_str_format().

Referenced by Init_Object(), iso8601_timediv(), jisx0301_date(), name_err_mesg_to_str(), rb_f_printf(), rb_io_printf(), and syslog_write().

VALUE rb_sprintf | ( | const char * | format,
|  |  | ...
| ) |  |

Definition at line 1270 of file sprintf.c.

References rb_vsprintf(), and result.

Referenced by addrinfo_inspect(), argument_error(), econv_inspect(), enc_inspect(), esignal_init(), gc_profile_dump_on(), Init_curses(), Init_dbm(), insn_data_to_s_detail(), insn_operand_intern(), inspect_enumerator(), iow_inspect(), iseq_compile_each(), iseq_inspect(), load_encoding(), make_econv_exception(), make_inspectname(), make_name_for_block(), match_inspect(), mSyslog_inspect(), ole_cp2encoding(), ole_search_handler_method(), parser_set_encode(), path_inspect(), proc_to_s(), pst_inspect(), pty_open(), rb_any_to_s(), rb_arg_error_new(), rb_attr(), rb_dlcfunc_inspect(), rb_obj_inspect(), rb_stat_inspect(), rb_thread_current_status(), rb_thread_inspect(), rb_tmp_class_path(), rescue_callback(), ruby_setenv(), sockopt_inspect(), syserr_initialize(), and tracepoint_inspect().

VALUE rb_str_catf | ( | VALUE | str,
|  | const char * | format,
|  |  | ...
| ) |  |

Definition at line 1310 of file sprintf.c.

References rb_str_vcatf().

Referenced by addrinfo_inspect(), compile_snprintf(), econv_description(), flo_to_s(), insn_data_to_s_detail(), inspect_errno(), inspect_int(), inspect_sockaddr(), inspect_timeval_as_interval(), make_econv_exception(), make_inspectname(), match_inspect(), pst_message(), rb_io_inspect(), rb_iseq_disasm(), rb_iseq_disasm_insn(), rb_stat_inspect(), sockopt_inspect(), and warn_print().

VALUE rb_str_format | ( | int | argc,
|  | const VALUE * | argv,
|  | VALUE | fmt
| ) |  |

Definition at line 439 of file sprintf.c.

References arg, argc, argv, BIT_DIGITS, buf, c, CHECK, CHECK_FOR_FLAGS, CHECK_FOR_WIDTH, ENC_CODERANGE_7BIT, ENC_CODERANGE_BROKEN, ENC_CODERANGE_SET, ENC_CODERANGE_UNKNOWN, expr, fbuf, FILL, FIX2LONG, FIXABLE, FIXNUM_P, FMINUS, fmt_setup(), FNONE, FPLUS, FPREC, FPREC0, FSHARP, FSPACE, FWIDTH, FZERO, GETARG, GETASTER, GETNAMEARG, GETNUM, GETPOSARG, hash(), i, ID2SYM, isinf(), isnan, LONG2FIX, NIL_P, NUM2INT, OBJ_TAINT, OBJ_TAINTED, p, PRIdSIZE, PUSH, Qundef, rb_big2str(), rb_big2str0(), rb_big_2comp(), rb_big_clone(), rb_check_id_cstr(), rb_check_string_type(), rb_dbl2big(), rb_eArgError, rb_eKeyError, rb_enc_associate(), rb_enc_check(), rb_enc_codelen(), rb_enc_codepoint_len(), rb_enc_copy(), rb_enc_get(), rb_enc_isprint, rb_enc_mbclen(), rb_enc_mbcput, rb_enc_nth(), rb_enc_raise(), rb_enc_right_char_head, rb_enc_strlen(), rb_enc_toupper(), rb_Float(), RB_GC_GUARD, rb_id2name(), rb_inspect(), rb_int2big(), rb_Integer(), rb_long2int, rb_obj_as_string(), rb_raise(), rb_str_buf_new(), rb_str_coderange_scan_restartable(), rb_str_new4, rb_str_resize(), rb_str_set_len(), rb_str_to_inum(), rb_warn(), rb_warning(), RBIGNUM_SIGN, remove_sign_bits(), result, RFLOAT_VALUE, RSTRING_END, RSTRING_LEN, RSTRING_PTR, RTEST, ruby_debug, ruby_verbose, sign_bits(), snprintf, StringValue, strlen(), T_BIGNUM, T_FIXNUM, T_FLOAT, T_STRING, TRUE, TYPE, v, and val.

Referenced by date_strftime_with_tmx(), rb_f_sprintf(), rb_str_format_m(), rb_str_upto(), and rb_strftime_with_timespec().

VALUE rb_str_vcatf | ( | VALUE | str,
|  | const char * | fmt,
|  | va_list | ap
| ) |  |

Definition at line 1283 of file sprintf.c.

References __SSTR, __SWR, BSD_vfprintf(), f, rb_str_capacity(), rb_str_modify(), rb_str_resize(), RBASIC, RSTRING_END, RSTRING_PTR, ruby__sfvextra(), ruby__sfvwrite(), StringValue, and rb_printf_buffer_extra::value.

Referenced by compile_snprintf(), rb_str_catf(), and warn_print().

VALUE rb_vsprintf | ( | const char * | fmt,
|  | va_list | ap
| ) |  |

Definition at line 1264 of file sprintf.c.

References NULL, and rb_enc_vsprintf().

Referenced by rb_compile_error_append(), rb_fatal(), rb_name_error(), rb_name_error_str(), rb_raise(), and rb_sprintf().

static char* remove_sign_bits | ( | char * | str,
|  | int | base
| ) |  |
| static

Definition at line 31 of file sprintf.c.

References EXTENDSIGN, and strlen().

Referenced by rb_str_format().

static char* ruby__sfvextra | ( | rb_printf_buffer * | fp,
|  | size_t | valsize,
|  | void * | valp,
|  | long * | sz,
|  | int | sign
| ) |  |
| static

Definition at line 1182 of file sprintf.c.

References ECONV_INVALID_REPLACE, ECONV_UNDEF_REPLACE, if(), OBJ_INFECT, Qnil, rb_enc_associate(), rb_enc_compatible(), rb_enc_get(), rb_eRuntimeError, rb_inspect(), rb_obj_as_string(), rb_raise(), rb_str_conv_enc_opts(), RBASIC, result, RSTRING_GETMEM, and StringValueCStr.

Referenced by rb_enc_vsprintf(), and rb_str_vcatf().

static int ruby__sfvwrite | ( | register rb_printf_buffer * | fp,
|  | register struct __suio * | uio
| ) |  |
| static

Definition at line 1155 of file sprintf.c.

References buf, CHECK, __siov::iov_base, __siov::iov_len, MEMCPY, rb_eRuntimeError, rb_raise(), rb_str_set_len(), RBASIC, result, RSTRING_PTR, __suio::uio_iov, and __suio::uio_resid.

Referenced by rb_enc_vsprintf(), and rb_str_vcatf().

static char sign_bits | ( | int | base,
|  | const char * | p
| ) |  |
| static

Definition at line 56 of file sprintf.c.

References c.

Referenced by rb_str_format().