Ruby  2.1.10p492(2016-04-01revision54464)
Data Structures | Macros | Functions | Variables
sprintf.c File Reference
#include "ruby/ruby.h"
#include "ruby/re.h"
#include "ruby/encoding.h"
#include "internal.h"
#include <math.h>
#include <stdarg.h>
#include "vsnprintf.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 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 GETNEXTARG()
 
#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
 
#define f   buffer.base
 

Functions

static void fmt_setup (char *, size_t, int, int, int, int)
 
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,...)
 

Variables

const char ruby_digitmap []
 

Macro Definition Documentation

◆ __sbuf

#define __sbuf   rb_printf_sbuf

Definition at line 1105 of file sprintf.c.

◆ __sFILE

#define __sFILE   rb_printf_sfile

Definition at line 1106 of file sprintf.c.

◆ BIT_DIGITS

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

Definition at line 25 of file sprintf.c.

Referenced by rb_str_format().

◆ BSD__dtoa

#define BSD__dtoa   ruby_dtoa

Definition at line 1124 of file sprintf.c.

◆ BSD__hdtoa

#define BSD__hdtoa   ruby_hdtoa

Definition at line 1125 of file sprintf.c.

◆ CHECK

#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);\
} while (0)
static VALUE result
Definition: nkf.c:40
#define RSTRING_PTR(str)
Definition: ruby.h:845
#define ENC_CODERANGE(obj)
Definition: encoding.h:52

Definition at line 59 of file sprintf.c.

Referenced by rb_str_format(), and ruby__sfvwrite().

◆ CHECK_FOR_FLAGS

#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"); \
}
#define FPREC0
Definition: sprintf.c:57
#define f
#define FWIDTH
Definition: sprintf.c:55
VALUE rb_eArgError
Definition: error.c:549

Referenced by rb_str_format().

◆ CHECK_FOR_WIDTH

#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"); \
}
#define FPREC0
Definition: sprintf.c:57
#define f
#define FWIDTH
Definition: sprintf.c:55
VALUE rb_eArgError
Definition: error.c:549

Referenced by rb_str_format().

◆ f [1/2]

#define f   buffer.base

Referenced by BigDecimal_divremain(), BigDecimal_mode(), BigDecimal_remainder(), BigDecimal_to_s(), BigDecimalCmp(), broken_getc(), c_jd_to_weeknum(), c_valid_weeknum_p(), c_weeknum_to_jd(), cap_getc(), cap_ungetc(), check_bom(), console_set_echo(), d_lite_plus(), decode_day(), div_day(), div_df(), f_signbit(), flo_ceil(), flo_floor(), flo_round(), flo_truncate(), float_decode_internal(), float_to_r(), get_special_folder(), h_conv(), hex_getc(), init_env(), int_round_0(), iseq_s_compile_file(), kanji_convert(), lgamma_r(), load_file_internal(), loaded_feature_path_i(), m_wnumx(), mime_begin(), mime_begin_strict(), mime_getc(), mime_getc_buf(), mime_integrity(), mime_ungetc_buf(), nfc_getc(), nfc_ungetc(), noconvert(), num_step(), numchar_getc(), numchar_ungetc(), onig_set_verb_warn_func(), onig_set_warn_func(), onigenc_apply_all_case_fold_with_map(), onigenc_property_list_init(), ossl_x509stctx_set_flags(), ossl_x509store_set_flags(), parse_time2_cb(), prep_stdio(), rand_range(), rb_class_foreach_subclass(), rb_compile_cstr(), rb_compile_file(), rb_compile_string(), rb_dlptr_initialize(), rb_dlptr_s_malloc(), rb_enc_vsprintf(), rb_feature_p(), rb_fiddle_ptr_initialize(), rb_fiddle_ptr_s_malloc(), rb_file_s_basename(), rb_file_s_rename(), rb_find_file_ext_safe(), rb_find_file_safe(), rb_flt_rationalize(), rb_io_wait_readable(), rb_io_wait_writable(), rb_load_file(), rb_parser_compile_cstr(), rb_parser_compile_file(), rb_parser_compile_string(), rb_parser_compile_string_path(), rb_str_justify(), rb_str_vcatf(), rb_thread_blocking_region(), rb_w32_wrap_io_handle(), readline_s_set_input(), readline_s_set_output(), round(), rt__valid_weeknum_p(), ruby_enc_find_basename(), ruby_init_loadpath_safe(), ruby_snprintf(), ruby_vsnprintf(), set_iconv(), SHA256_Transform(), SHA512_Transform(), ss_apply_all_case_fold(), std_getc(), strscan_scan_full(), strscan_search_full(), tgamma(), time_timespec(), trans_sweep(), url_getc(), url_ungetc(), valid_weeknum_p(), VpActiveRound(), VpException(), VpInternalRound(), VpLeftRound(), VpMidRound(), VpSetException(), VpSqrt(), and VpVtoD().

◆ f [2/2]

#define f   buffer.base

◆ FILE

#define FILE   rb_printf_buffer

Definition at line 1104 of file sprintf.c.

◆ FILL

#define FILL (   c,
 
)
Value:
do { \
CHECK(l);\
memset(&buf[blen], (c), (l));\
blen += (l);\
} while (0)
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4308

Definition at line 75 of file sprintf.c.

Referenced by rb_str_format().

◆ FLOATING_POINT

#define FLOATING_POINT   1

Definition at line 1123 of file sprintf.c.

◆ FMINUS

#define FMINUS   2

Definition at line 51 of file sprintf.c.

Referenced by fmt_setup(), and rb_str_format().

◆ FNONE

#define FNONE   0

Definition at line 49 of file sprintf.c.

Referenced by rb_str_format().

◆ FPLUS

#define FPLUS   4

Definition at line 52 of file sprintf.c.

Referenced by fmt_setup(), and rb_str_format().

◆ FPREC

#define FPREC   64

Definition at line 56 of file sprintf.c.

Referenced by fmt_setup(), and rb_str_format().

◆ FPREC0

#define FPREC0   128

Definition at line 57 of file sprintf.c.

Referenced by rb_str_format().

◆ FSHARP

#define FSHARP   1

Definition at line 50 of file sprintf.c.

Referenced by fmt_setup(), and rb_str_format().

◆ FSPACE

#define FSPACE   16

Definition at line 54 of file sprintf.c.

Referenced by fmt_setup(), and rb_str_format().

◆ FWIDTH

#define FWIDTH   32

Definition at line 55 of file sprintf.c.

Referenced by fmt_setup(), and rb_str_format().

◆ FZERO

#define FZERO   8

Definition at line 53 of file sprintf.c.

Referenced by fmt_setup(), and rb_str_format().

◆ GETARG

#define GETARG ( )
Value:
(nextvalue != Qundef ? nextvalue : \
GETNEXTARG())
#define Qundef
Definition: ruby.h:428

Definition at line 81 of file sprintf.c.

Referenced by rb_str_format().

◆ GETASTER

#define GETASTER (   val)
Value:
do { \
t = p++; \
n = 0; \
GETNUM(n, (val)); \
if (*p == '$') { \
tmp = GETPOSARG(n); \
} \
else { \
tmp = GETNEXTARG(); \
p = t; \
} \
(val) = NUM2INT(tmp); \
} while (0)
#define NUM2INT(x)
Definition: ruby.h:630
#define GETNEXTARG()
Definition: sprintf.c:84
int t(void)
Definition: conftest.c:13
#define val
#define GETPOSARG(n)
Definition: sprintf.c:91

Definition at line 123 of file sprintf.c.

Referenced by rb_str_format().

◆ GETNAMEARG

#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)))
static VALUE get_hash(volatile VALUE *hash, int argc, const VALUE *argv)
Definition: sprintf.c:138
int argc
Definition: ruby.c:131
void rb_enc_raise(rb_encoding *enc, VALUE exc, const char *fmt,...)
Definition: error.c:1844
VALUE rb_hash_lookup2(VALUE hash, VALUE key, VALUE def)
Definition: hash.c:717
static unsigned int hash(const char *str, unsigned int len)
Definition: lex.c:56
const char * name
Definition: nkf.c:208
#define Qundef
Definition: ruby.h:428
VALUE rb_eArgError
Definition: error.c:549
char ** argv
Definition: ruby.c:132

Definition at line 101 of file sprintf.c.

Referenced by rb_str_format().

◆ GETNEXTARG

#define GETNEXTARG ( )
Value:
( \
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)))
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1857
#define GETNTHARG(nth)
Definition: sprintf.c:98
VALUE rb_eArgError
Definition: error.c:549

Definition at line 84 of file sprintf.c.

◆ GETNTHARG

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

Definition at line 98 of file sprintf.c.

Referenced by rb_f_sprintf().

◆ GETNUM

#define GETNUM (   n,
  val 
)
Value:
for (; p < end && rb_enc_isdigit(*p, enc); p++) { \
int next_n = (n); \
if (MUL_OVERFLOW_INT_P(10, next_n)) \
rb_raise(rb_eArgError, #val " too big"); \
next_n *= 10; \
if (INT_MAX - (*p - '0') < next_n) \
rb_raise(rb_eArgError, #val " too big"); \
next_n += *p - '0'; \
(n) = next_n; \
} \
if (p >= end) { \
rb_raise(rb_eArgError, "malformed format string - %%*[0-9]"); \
}
#define val
#define rb_enc_isdigit(c, enc)
Definition: encoding.h:186
#define MUL_OVERFLOW_INT_P(a, b)
Definition: internal.h:70
VALUE rb_eArgError
Definition: error.c:549

Definition at line 108 of file sprintf.c.

Referenced by rb_str_format().

◆ GETPOSARG

#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))))
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1857
#define GETNTHARG(nth)
Definition: sprintf.c:98
VALUE rb_eArgError
Definition: error.c:549

Definition at line 91 of file sprintf.c.

Referenced by rb_str_format().

◆ PUSH

#define PUSH (   s,
 
)
Value:
do { \
CHECK(l);\
memcpy(&buf[blen], (s), (l));\
blen += (l);\
} while (0)
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4308

Definition at line 69 of file sprintf.c.

Referenced by rb_str_format().

Function Documentation

◆ fmt_setup()

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

Definition at line 1079 of file sprintf.c.

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

Referenced by rb_str_format().

◆ get_hash()

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

Definition at line 138 of file sprintf.c.

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

◆ rb_enc_sprintf()

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

Definition at line 1231 of file sprintf.c.

References rb_enc_vsprintf(), and result.

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

◆ rb_enc_vsprintf()

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

◆ rb_f_sprintf()

VALUE rb_f_sprintf ( int  argc,
const VALUE argv 
)

◆ rb_sprintf()

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

◆ rb_str_catf()

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

◆ rb_str_format()

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

Definition at line 421 of file sprintf.c.

References argc, argv, BIT_DIGITS, buf, CHAR_BIT, CHECK, CHECK_FOR_FLAGS, CHECK_FOR_WIDTH, ENC_CODERANGE_7BIT, ENC_CODERANGE_BROKEN, ENC_CODERANGE_SET, ENC_CODERANGE_UNKNOWN, ffs(), FILL, FIX2LONG, FIXABLE, FIXNUM_P, FMINUS, fmt_setup(), FNONE, FPLUS, FPREC, FPREC0, FSHARP, FSPACE, FWIDTH, FZERO, GETARG, GETASTER, GETNAMEARG, GETNUM, GETPOSARG, hash(), ID2SYM, INTEGER_PACK_2COMP, INTEGER_PACK_BIG_ENDIAN, isinf(), isnan, LONG2FIX, NIL_P, NULL, NUM2INT, OBJ_TAINT, OBJ_TAINTED, PRIdSIZE, PUSH, Qundef, rb_absint_numwords(), rb_absint_singlebit_p(), rb_big2str(), 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_Integer(), rb_integer_pack(), rb_long2int, rb_obj_as_string(), rb_raise(), rb_str_buf_new(), rb_str_coderange_scan_restartable(), rb_str_new(), rb_str_new4, rb_str_resize(), rb_str_set_len(), rb_str_to_inum(), rb_warn(), RBIGNUM_NEGATIVE_P, result, RFLOAT_VALUE, RSTRING_END, RSTRING_LEN, RSTRING_PTR, RTEST, ruby_debug, ruby_digitmap, ruby_verbose, sign_bits(), snprintf, StringValue, strlen(), t(), T_BIGNUM, T_FIXNUM, T_FLOAT, T_STRING, TRUE, TYPE, and val.

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

◆ rb_str_vcatf()

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

◆ rb_vsprintf()

VALUE rb_vsprintf ( const char *  fmt,
va_list  ap 
)

◆ ruby__sfvextra()

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

◆ ruby__sfvwrite()

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

◆ sign_bits()

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

Definition at line 32 of file sprintf.c.

Referenced by rb_str_format().

Variable Documentation

◆ ruby_digitmap

const char ruby_digitmap[]

Definition at line 36 of file bignum.c.

Referenced by big2str_2bdigits(), big2str_base_poweroftwo(), rb_fix2str(), and rb_str_format().