Ruby: sprintf.c File Reference

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

Data Structures | Macros | Functions
sprintf.c File Reference
`#include "ruby/internal/config.h"`
`#include <math.h>`
`#include <stdarg.h>`
`#include "id.h"`
`#include "internal.h"`
`#include "internal/error.h"`
`#include "internal/hash.h"`
`#include "internal/numeric.h"`
`#include "internal/object.h"`
`#include "internal/sanitizers.h"`
`#include "internal/symbol.h"`
`#include "internal/util.h"`
`#include "ruby/encoding.h"`
`#include "ruby/re.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 | PUSH_(s, l)
#define | FILL(c, 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 | CHECKNAMEARG(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 | PRI_EXTRA_MARK RUBY_PRI_VALUE_MARK
#define | lower_hexdigits (ruby_hexdigits+0)
#define | upper_hexdigits (ruby_hexdigits+16)
#define | LITERAL(str) (*sz = rb_strlen_lit(str), str)
#define | f buffer.base

## Functions
VALUE | rb_f_sprintf (int argc, const VALUE *argv)
VALUE | rb_str_format (int argc, const VALUE *argv, VALUE fmt)
int | ruby_vsnprintf (char *str, size_t n, const char *fmt, va_list ap)
int | ruby_snprintf (char *str, size_t n, char const *fmt,...)
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

## ◆ __sbuf

#define __sbuf rb_printf_sbuf

Definition at line 971 of file sprintf.c.

## ◆ __sFILE

#define __sFILE rb_printf_sfile

Definition at line 972 of file sprintf.c.

## ◆ BIT_DIGITS

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

Definition at line 35 of file sprintf.c.

## ◆ BSD__dtoa

#define BSD__dtoa ruby_dtoa

Definition at line 990 of file sprintf.c.

## ◆ BSD__hdtoa

#define BSD__hdtoa ruby_hdtoa

Definition at line 991 of file sprintf.c.

## ◆ CHECK

#define CHECK | ( |  | l | ) |

Value:
do {\
int cr = ENC_CODERANGE(result);\
while ((l) >= bsiz - blen) {\
bsiz*=2;\
if (bsiz<0) rb_raise(rb_eArgError, "too big specifier");\
}\
rb_str_resize(result, bsiz);\
ENC_CODERANGE_SET(result, cr);\
buf = RSTRING_PTR(result);\
} while (0)
RSTRING_PTR
#define RSTRING_PTR(string)
Definition: fbuffer.h:19
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2917
rb_eArgError
VALUE rb_eArgError
Definition: error.c:1058
ENC_CODERANGE
#define ENC_CODERANGE(obj)
Definition: encoding.h:97
ENC_CODERANGE_SET
#define ENC_CODERANGE_SET(obj, cr)
Definition: encoding.h:99
rb_str_resize
VALUE rb_str_resize(VALUE, long)
Definition: string.c:2859
buf
voidpf void * buf
Definition: ioapi.h:138

Definition at line 68 of file sprintf.c.

## ◆ 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"); \
}
FPREC0
#define FPREC0
Definition: sprintf.c:66
FWIDTH
#define FWIDTH
Definition: sprintf.c:64
f
#define f

## ◆ 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"); \
}

## ◆ CHECKNAMEARG

#define CHECKNAMEARG | ( |  | name,
|  |  | len,
|  |  | enc
| ) |  |

Value:
( \
check_name_arg(posarg, name, len, enc), \
posarg = -2)
len
uint8_t len
Definition: escape.c:17
name
const char * name
Definition: nkf.c:208

Definition at line 114 of file sprintf.c.

## ◆ f [1/2]

#define f buffer.base

## ◆ f [2/2]

#define f buffer.base

## ◆ FILE

#define FILE rb_printf_buffer

Definition at line 970 of file sprintf.c.

## ◆ FILL

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

Value:
do { \
if ((l) <= 0) break;\
CHECK(l);\
FILL_(c, l);\
} while (0)
CHECK
#define CHECK(l)
Definition: sprintf.c:68
FILL_
#define FILL_(c, l)
Definition: sprintf.c:95

Definition at line 89 of file sprintf.c.

## ◆ FILL_

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

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

Definition at line 95 of file sprintf.c.

## ◆ FLOATING_POINT

#define FLOATING_POINT 1

Definition at line 989 of file sprintf.c.

## ◆ FMINUS

#define FMINUS 2

Definition at line 60 of file sprintf.c.

## ◆ FNONE

#define FNONE 0

Definition at line 58 of file sprintf.c.

## ◆ FPLUS

#define FPLUS 4

Definition at line 61 of file sprintf.c.

## ◆ FPREC

#define FPREC 64

Definition at line 65 of file sprintf.c.

## ◆ FPREC0

#define FPREC0 128

Definition at line 66 of file sprintf.c.

## ◆ FSHARP

#define FSHARP 1

Definition at line 59 of file sprintf.c.

## ◆ FSPACE

#define FSPACE 16

Definition at line 63 of file sprintf.c.

## ◆ FWIDTH

#define FWIDTH 32

Definition at line 64 of file sprintf.c.

## ◆ FZERO

#define FZERO 8

Definition at line 62 of file sprintf.c.

## ◆ GETARG

#define GETARG | ( |  | ) |

Value:
(nextvalue != Qundef ? nextvalue : \
GETNEXTARG())
Qundef
#define Qundef
Definition: special_consts.h:53

Definition at line 100 of file sprintf.c.

## ◆ 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)
NUM2INT
#define NUM2INT
Definition: int.h:44
GETNEXTARG
#define GETNEXTARG()
Definition: sprintf.c:103
GETPOSARG
#define GETPOSARG(n)
Definition: sprintf.c:107
t
#define t
Definition: symbol.c:253

Definition at line 122 of file sprintf.c.

## ◆ GETNEXTARG

#define GETNEXTARG | ( |  | ) |

Value:
( \
check_next_arg(posarg, nextarg), \
(posarg = nextarg++, GETNTHARG(posarg)))
GETNTHARG
#define GETNTHARG(nth)
Definition: sprintf.c:111

Definition at line 103 of file sprintf.c.

## ◆ GETNTHARG

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

Definition at line 111 of file sprintf.c.

## ◆ GETNUM

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

Value:
(!(p = get_num(p, end, enc, &(n))) ? \
rb_raise(rb_eArgError, #val " too big") : (void)0)
get_num
#define get_num(num)
Definition: ripper.c:643

Definition at line 118 of file sprintf.c.

## ◆ GETPOSARG

#define GETPOSARG | ( |  | n | ) |

Value:
( \
check_pos_arg(posarg, (n)), \
(posarg = -1, GETNTHARG(n)))

Definition at line 107 of file sprintf.c.

## ◆ LITERAL

#define LITERAL | ( |  | str | ) | (*sz = rb_strlen_lit(str), str)

## ◆ lower_hexdigits

#define lower_hexdigits (ruby_hexdigits+0)

Definition at line 995 of file sprintf.c.

## ◆ PRI_EXTRA_MARK

#define PRI_EXTRA_MARK RUBY_PRI_VALUE_MARK

Definition at line 993 of file sprintf.c.

## ◆ PUSH

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

Value:
do { \
CHECK(l);\
PUSH_(s, l);\
} while (0)

Definition at line 79 of file sprintf.c.

## ◆ PUSH_

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

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

Definition at line 84 of file sprintf.c.

## ◆ upper_hexdigits

#define upper_hexdigits (ruby_hexdigits+16)

Definition at line 996 of file sprintf.c.

## Function Documentation

## ◆ rb_enc_sprintf()

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

Definition at line 1184 of file sprintf.c.

References rb_enc_vsprintf().

Referenced by rb_str_format(), rb_str_upto_each(), and rb_str_upto_endless_each().

## ◆ rb_enc_vsprintf()

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

Definition at line 1151 of file sprintf.c.

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

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

## ◆ rb_f_sprintf()

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

Definition at line 208 of file sprintf.c.

References argc, argv, GETNTHARG, and rb_str_format().

Referenced by rb_io_printf().

## ◆ rb_sprintf()

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

Definition at line 1203 of file sprintf.c.

References rb_vsprintf().

Referenced by Init_Bignum(), Init_dbm(), rb_any_to_s(), rb_attr(), rb_block_to_s(), rb_dump_literal(), rb_error_frozen_object(), rb_f_global_variables(), rb_insn_operand_intern(), rb_keyword_error_new(), rb_parser_show_bitstack(), rb_profile_frame_classpath(), rb_profile_frame_full_label(), rb_ractor_ensure_shareable(), rsock_raise_socket_error(), rsock_syserr_fail_host_port(), rsock_syserr_fail_path(), rsock_syserr_fail_raddrinfo(), and ruby_setenv().

## ◆ rb_str_catf()

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

Definition at line 1243 of file sprintf.c.

References rb_str_vcatf(), and str.

Referenced by rb_block_to_s(), rb_error_frozen_object(), rb_iseq_disasm_insn(), rb_parser_show_bitstack(), rb_parser_trace_lex_state(), rb_str_format(), rb_warn_deprecated(), rb_warn_deprecated_to_remove(), and rsock_inspect_sockaddr().

## ◆ rb_str_format()

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

Definition at line 214 of file sprintf.c.

References argc, argv, BIT_DIGITS, CHAR_BIT, CHECK, CHECK_FOR_FLAGS, CHECK_FOR_WIDTH, CHECKNAMEARG, ENC_CODERANGE, ENC_CODERANGE_7BIT, ENC_CODERANGE_BROKEN, ENC_CODERANGE_SET, ENC_CODERANGE_UNKNOWN, ffs(), FILL, FILL_, FIX2LONG, FIXABLE, FIXNUM_P, FMINUS, FNONE, FPLUS, FPREC, FPREC0, FSHARP, FSPACE, FWIDTH, FZERO, GETARG, GETASTER, GETNUM, GETPOSARG, int(), INT2FIX, INTEGER_PACK_2COMP, INTEGER_PACK_BIG_ENDIAN, isinf(), isnan, len, LONG2FIX, memcpy, NIL_P, NULL, num, NUM2INT, prefix, PRIsVALUE, PRIuSIZE, PUSH, PUSH_, Qnil, Qundef, rb_absint_numwords(), rb_absint_singlebit_p(), rb_big2str(), rb_big_uminus(), rb_check_string_type(), rb_check_symbol_cstr(), rb_dbl2big(), rb_eArgError, rb_enc_associate(), rb_enc_check(), rb_enc_codelen(), rb_enc_codepoint_len(), 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_sprintf(), rb_enc_strlen(), rb_enc_toupper(), rb_Float(), RB_GC_GUARD, rb_hash_default_value(), rb_hash_lookup2(), rb_inspect(), rb_int2str(), rb_int_idiv(), rb_int_mul(), rb_int_plus(), rb_int_positive_pow(), rb_Integer(), rb_integer_pack(), RB_INTEGER_TYPE_P, rb_long2int, rb_obj_as_string(), rb_raise(), rb_rational_den(), rb_rational_num(), rb_str_buf_new(), rb_str_capacity(), rb_str_catf(), rb_str_coderange_scan_restartable(), rb_str_new, rb_str_resize(), rb_str_set_len(), rb_str_tmp_frozen_acquire(), rb_str_tmp_frozen_release(), rb_str_to_inum(), rb_sym2str(), rb_sym_intern(), rb_warn(), RFLOAT_VALUE, RSTRING_GETMEM, RSTRING_LEN, RSTRING_PTR, RTEST, ruby_debug, ruby_digitmap, ruby_verbose, SIGNED_VALUE, str, StringValue, strlen(), sym, t, T_BIGNUM, T_FIXNUM, T_FLOAT, T_RATIONAL, T_STRING, term, TRUE, and TYPE.

Referenced by rb_f_sprintf(), rb_str_upto_each(), and rb_str_upto_endless_each().

## ◆ rb_str_vcatf()

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

Definition at line 1216 of file sprintf.c.

References __SSTR, __SWR, f, rb_str_capacity(), rb_str_modify(), rb_str_resize(), RBASIC, RSTRING_PTR, str, StringValue, and rb_printf_buffer_extra::value.

Referenced by rb_parser_fatal(), rb_parser_printf(), and rb_str_catf().

## ◆ rb_vsprintf()

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

Definition at line 1197 of file sprintf.c.

References NULL, and rb_enc_vsprintf().

Referenced by ole_raise(), rb_fatal(), rb_frozen_error_raise(), rb_name_error(), rb_name_error_str(), rb_sprintf(), and rb_vraise().

## ◆ ruby_snprintf()

int ruby_snprintf | ( | char * | str,
|  | size_t | n,
|  | char const * | fmt,
|  |  | ...
| ) |  |

Definition at line 1037 of file sprintf.c.

References EOF, and str.

## ◆ ruby_vsnprintf()

int ruby_vsnprintf | ( | char * | str,
|  | size_t | n,
|  | const char * | fmt,
|  | va_list | ap
| ) |  |

Definition at line 1010 of file sprintf.c.

References EOF, and str.