14#include "ruby/internal/config.h"
25#include "internal/error.h"
26#include "internal/hash.h"
27#include "internal/numeric.h"
28#include "internal/object.h"
29#include "internal/sanitizers.h"
30#include "internal/symbol.h"
35#define BIT_DIGITS(N) (((N)*146)/485 + 1)
37static char *fmt_setup(
char*,
size_t,
int,
int,
int,
int);
38static char *ruby_ultoa(
unsigned long val,
char *endp,
int base,
int octzero);
41sign_bits(
int base,
const char *p)
47 if (*p ==
'X') c =
'F';
69 int cr = ENC_CODERANGE(result);\
70 while ((l) >= bsiz - blen) {\
72 if (bsiz<0) rb_raise(rb_eArgError, "too big specifier");\
74 rb_str_resize(result, bsiz);\
75 ENC_CODERANGE_SET(result, cr);\
76 buf = RSTRING_PTR(result);\
79#define PUSH(s, l) do { \
84#define PUSH_(s, l) do { \
85 memcpy(&buf[blen], (s), (l));\
89#define FILL(c, l) do { \
95#define FILL_(c, l) do { \
96 memset(&buf[blen], (c), (l));\
100#define GETARG() (!UNDEF_P(nextvalue) ? nextvalue : \
103#define GETNEXTARG() ( \
104 check_next_arg(posarg, nextarg), \
105 (posarg = nextarg++, GETNTHARG(posarg)))
107#define GETPOSARG(n) ( \
108 check_pos_arg(posarg, (n)), \
109 (posarg = -1, GETNTHARG(n)))
111#define GETNTHARG(nth) \
112 (((nth) >= argc) ? (rb_raise(rb_eArgError, "too few arguments"), 0) : argv[(nth)])
114#define CHECKNAMEARG(name, len, enc) ( \
115 check_name_arg(posarg, name, len, enc), \
118#define GETNUM(n, val) \
119 (!(p = get_num(p, end, enc, &(n))) ? \
120 rb_raise(rb_eArgError, #val " too big") : (void)0)
122#define GETASTER(val) do { \
127 tmp = GETPOSARG(n); \
130 tmp = GETNEXTARG(); \
133 (val) = NUM2INT(tmp); \
137get_num(
const char *p,
const char *end,
rb_encoding *enc,
int *valp)
141 if (MUL_OVERFLOW_INT_P(10, next_n))
144 if (INT_MAX - (*p -
'0') < next_n)
156check_next_arg(
int posarg,
int nextarg)
167check_pos_arg(
int posarg,
int n)
181check_name_arg(
int posarg,
const char *name,
int len,
rb_encoding *enc)
192get_hash(
volatile VALUE *hash,
int argc,
const VALUE *argv)
196 if (!UNDEF_P(*hash))
return *hash;
200 tmp = rb_check_hash_type(argv[1]);
204 return (*hash = tmp);
216 enum {default_float_precision = 6};
225 int width, prec, flags = FNONE;
234#define CHECK_FOR_WIDTH(f) \
235 if ((f) & FWIDTH) { \
236 rb_raise(rb_eArgError, "width given twice"); \
238 if ((f) & FPREC0) { \
239 rb_raise(rb_eArgError, "width after precision"); \
241#define CHECK_FOR_FLAGS(f) \
242 if ((f) & FWIDTH) { \
243 rb_raise(rb_eArgError, "flag after width"); \
245 if ((f) & FPREC0) { \
246 rb_raise(rb_eArgError, "flag after precision"); \
249#define update_coderange(partial) do { \
250 if (coderange != ENC_CODERANGE_BROKEN && scanned < blen \
251 && rb_enc_to_index(enc) ) { \
252 int cr = coderange; \
253 scanned += rb_str_coderange_scan_restartable(buf+scanned, buf+blen, enc, &cr); \
254 ENC_CODERANGE_SET(result, \
255 (partial && cr == ENC_CODERANGE_UNKNOWN ? \
256 ENC_CODERANGE_BROKEN : (coderange = cr))); \
262 enc = rb_enc_get(fmt);
265 fmt = rb_str_tmp_frozen_acquire(fmt);
271 rb_enc_associate(result, enc);
273 memset(buf, 0, bsiz);
276 for (; p < end; p++) {
281 for (t = p; t < end && *t !=
'%'; t++) ;
286 update_coderange(FALSE);
305 CHECK_FOR_FLAGS(flags);
311 CHECK_FOR_FLAGS(flags);
317 CHECK_FOR_FLAGS(flags);
323 CHECK_FOR_FLAGS(flags);
329 CHECK_FOR_FLAGS(flags);
334 case '1':
case '2':
case '3':
case '4':
335 case '5':
case '6':
case '7':
case '8':
case '9':
339 if (!UNDEF_P(nextvalue)) {
342 nextvalue = GETPOSARG(n);
346 CHECK_FOR_WIDTH(flags);
354 const char *start = p;
355 char term = (*p ==
'<') ?
'>' :
'}';
358 for (; p < end && *p != term; ) {
359 p += rb_enc_mbclen(p, end, enc);
364#if SIZEOF_INT < SIZEOF_SIZE_T
365 if ((
size_t)(p - start) >= INT_MAX) {
366 const int message_limit = 20;
369 "too long name (%"PRIuSIZE
" bytes) - %.*s...%c",
370 (
size_t)(p - start - 2), len, start, term);
373 len = (int)(p - start + 1);
378 CHECKNAMEARG(start, len, enc);
379 get_hash(&hash, argc, argv);
383 if (!
NIL_P(sym)) nextvalue = rb_hash_lookup2(hash, sym,
Qundef);
384 if (UNDEF_P(nextvalue)) {
386 sym = rb_sym_intern(start + 1,
390 nextvalue = rb_hash_default_value(hash, sym);
391 if (
NIL_P(nextvalue)) {
392 rb_key_err_raise(
rb_enc_sprintf(enc,
"key%.*s not found", len, start), hash, sym);
395 if (term ==
'}')
goto format_s;
401 CHECK_FOR_WIDTH(flags);
413 if (flags & FPREC0) {
416 flags |= FPREC|FPREC0;
429 GETNUM(prec, precision);
437 if (flags != FNONE) {
445 VALUE val = GETARG();
458 if (n >= 0) n = rb_enc_codelen((c = n), enc);
462 int encidx = rb_ascii8bit_appendable_encoding_index(enc, c);
463 if (encidx >= 0 && encidx != rb_enc_to_index(enc)) {
465 rb_enc_associate_index(result, encidx);
466 enc = rb_enc_from_index(encidx);
469 if (!(flags & FWIDTH)) {
474 else if ((flags & FMINUS)) {
476 CHECK(n + (width > 0 ? width : 0));
479 if (width > 0) FILL_(
' ', width);
483 CHECK(n + (width > 0 ? width : 0));
484 if (width > 0) FILL_(
' ', width);
495 VALUE arg = GETARG();
507 update_coderange(TRUE);
508 enc = rb_enc_check(result, str);
509 if (flags&(FPREC|FWIDTH)) {
514 if ((flags&FPREC) && (prec < slen)) {
521 if ((flags&FWIDTH) && (width > slen)) {
524 if (!(flags&FMINUS)) {
534 rb_enc_associate(result, enc);
540 rb_enc_associate(result, enc);
553 volatile VALUE val = GETARG();
555 char nbuf[BIT_DIGITS(SIZEOF_LONG*CHAR_BIT)+2], *s;
556 const char *prefix = 0;
557 int sign = 0, dots = 0;
560 int base, bignum = 0;
573 if (flags&(FPLUS|FSPACE)) sign = 1;
576 if (flags & FSHARP) {
581 prefix =
"0x";
break;
583 prefix =
"0X";
break;
585 prefix =
"0b";
break;
587 prefix =
"0B";
break;
603 val = rb_str_to_inum(val, 0, TRUE);
633 int numbits = ffs(base)-1;
635 size_t numdigits = rb_absint_numwords(val, numbits, &abs_nlz_bits);
637 if (INT_MAX-1 < numdigits)
652 else if (flags & FPLUS) {
656 else if (flags & FSPACE) {
671 if (numdigits == 0 ||
672 ((abs_nlz_bits != (
size_t)(numbits-1) ||
673 !rb_absint_singlebit_p(val)) &&
674 (!bignum ? v < 0 : BIGNUM_NEGATIVE_P(val))))
694 else if (flags & FPLUS) {
698 else if (flags & FSPACE) {
702 s = ruby_ultoa((
unsigned long)v, nbuf +
sizeof(nbuf), 10, 0);
703 len = (int)(nbuf +
sizeof(nbuf) - s);
706 tmp = rb_big2str(val, 10);
715 else if (flags & FPLUS) {
719 else if (flags & FSPACE) {
734 while ((c = (
int)(
unsigned char)*pp) != 0) {
735 *pp = rb_enc_toupper(c, enc);
739 if (prefix && !prefix[1]) {
743 else if (len == 1 && *s ==
'0') {
745 if (flags & FPREC) prec--;
747 else if ((flags & FPREC) && (prec > len)) {
751 else if (len == 1 && *s ==
'0') {
755 width -= (int)strlen(prefix);
757 if ((flags & (FZERO|FMINUS|FPREC)) == FZERO) {
763 if (!prefix && prec == 0 && len == 1 && *s ==
'0') len = 0;
768 if (!(flags&FMINUS)) {
772 if (sc) PUSH(&sc, 1);
774 int plen = (int)strlen(prefix);
777 if (dots) PUSH(
"..", 2);
780 if (!sign && valsign < 0) {
781 char c = sign_bits(base, p);
782 FILL_(c, prec - len);
784 else if ((flags & (FMINUS|FPREC)) != FMINUS) {
785 FILL_(
'0', prec - len);
796 VALUE val = GETARG(), num, den;
797 int sign = (flags&FPLUS) ? 1 : 0, zero = 0;
811 if (!(flags&FPREC)) prec = default_float_precision;
819 else if (BIGNUM_NEGATIVE_P(num)) {
821 num = rb_big_uminus(num);
825 num = rb_int_plus(num, rb_int_idiv(den,
INT2FIX(2)));
826 num = rb_int_idiv(num, den);
828 else if (prec >= 0) {
831 val = rb_int2str(num, 10);
833 if (prec >= len) len = prec + 1;
834 if (sign || (flags&FSPACE)) ++len;
836 fill = width > len ? width - len : 0;
838 if (fill && !(flags&(FMINUS|FZERO))) {
841 if (sign || (flags&FSPACE)) {
842 buf[blen++] = sign > 0 ?
'+' : sign < 0 ?
'-' :
' ';
844 if (fill && (flags&(FMINUS|FZERO)) == FZERO) {
850 PUSH_(t, len - prec);
861 else if (prec > len) {
862 FILL_(
'0', prec - len);
866 PUSH_(t + len - prec, prec);
868 if (fill && (flags&FMINUS)) {
883 VALUE val = GETARG();
887 if (!isfinite(fval)) {
899 need = (int)strlen(expr);
901 if (!isnan(fval) && fval < 0.0)
903 else if (flags & (FPLUS|FSPACE))
904 sign = (flags & FPLUS) ?
'+' :
' ';
907 if ((flags & FWIDTH) && need < width)
911 if (flags & FMINUS) {
913 buf[blen - need--] = sign;
914 memcpy(&buf[blen - need], expr, elen);
918 buf[blen - elen - 1] = sign;
919 memcpy(&buf[blen - elen], expr, elen);
925 char fbuf[2*BIT_DIGITS(SIZEOF_INT*CHAR_BIT)+10];
926 char *fmt = fmt_setup(fbuf,
sizeof(fbuf), *p, flags, width, prec);
939 update_coderange(FALSE);
941 rb_str_tmp_frozen_release(orig, fmt);
944 if (posarg >= 0 && nextarg < argc) {
945 const char *mesg =
"too many arguments for format string";
955fmt_setup(
char *buf,
size_t size,
int c,
int flags,
int width,
int prec)
962 buf = ruby_ultoa(prec, buf, 10, 0);
966 if (flags & FWIDTH) {
967 buf = ruby_ultoa(width, buf, 10, 0);
970 if (flags & FSPACE) *--buf =
' ';
971 if (flags & FZERO) *--buf =
'0';
972 if (flags & FMINUS) *--buf =
'-';
973 if (flags & FPLUS) *--buf =
'+';
974 if (flags & FSHARP) *--buf =
'#';
980#define FILE rb_printf_buffer
981#define __sbuf rb_printf_sbuf
982#define __sFILE rb_printf_sfile
987#if SIZEOF_LONG < SIZEOF_LONG_LONG
988# if SIZEOF_LONG_LONG == SIZEOF_VOIDP
993# define _HAVE_SANE_QUAD_
994# define quad_t LONG_LONG
995# define u_quad_t unsigned LONG_LONG
997#define FLOATING_POINT 1
998#define BSD__dtoa ruby_dtoa
999#define BSD__hdtoa ruby_hdtoa
1000#ifdef RUBY_PRI_VALUE_MARK
1001# define PRI_EXTRA_MARK RUBY_PRI_VALUE_MARK
1003#define lower_hexdigits (ruby_hexdigits+0)
1004#define upper_hexdigits (ruby_hexdigits+16)
1005#include "vsnprintf.c"
1008ruby_ultoa(
unsigned long val,
char *endp,
int base,
int flags)
1010 const char *xdigs = lower_hexdigits;
1011 int octzero = flags & FSHARP;
1012 return BSD__ultoa(val, endp, base, octzero, xdigs);
1015static int ruby_do_vsnprintf(
char *str,
size_t n,
const char *fmt, va_list ap);
1020 if (str && (ssize_t)n < 1)
1022 return ruby_do_vsnprintf(str, n, fmt, ap);
1026ruby_do_vsnprintf(
char *str,
size_t n,
const char *fmt, va_list ap)
1031 f._flags = __SWR | __SSTR;
1032 f._bf._base = f._p = (
unsigned char *)str;
1033 f._bf._size = f._w = str ? (n - 1) : 0;
1034 f.vwrite = BSD__sfvwrite;
1036 ret = BSD_vfprintf(&f, fmt, ap);
1038#if SIZEOF_SIZE_T > SIZEOF_INT
1039 if (n > INT_MAX)
return INT_MAX;
1050 if (str && (ssize_t)n < 1)
1054 ret = ruby_do_vsnprintf(str, n, fmt, ap);
1060 rb_printf_buffer base;
1061 volatile VALUE value;
1065ruby__sfvwrite(
register rb_printf_buffer *fp,
register struct __suio *uio)
1069 char *buf = (
char*)fp->_p;
1071 long blen = buf -
RSTRING_PTR(result), bsiz = fp->_w;
1073 if (
RBASIC(result)->klass) {
1076 if (uio->uio_resid == 0)
1078#if SIZE_MAX > LONG_MAX
1079 if (uio->uio_resid >= LONG_MAX)
1082 len = (long)uio->uio_resid;
1086 for (iov = uio->uio_iov; len > 0; ++iov) {
1087 MEMCPY(buf, iov->iov_base,
char, n = iov->iov_len);
1091 fp->_p = (
unsigned char *)buf;
1097ruby__sfvextra(rb_printf_buffer *fp,
size_t valsize,
void *valp,
long *sz,
int sign)
1103 if (valsize !=
sizeof(
VALUE))
return 0;
1104 value = *(
VALUE *)valp;
1105 if (
RBASIC(result)->klass) {
1109# define LITERAL(str) (*sz = rb_strlen_lit(str), str)
1112# define LITERAL_CASE(x) case Q##x: return LITERAL(#x)
1115 LITERAL_CASE(
false);
1123 if (sign ==
' ' && !rb_str_symname_p(value)) {
1124 value = rb_str_escape(value);
1129 if (sign ==
' ') value = QUOTE(value);
1131 enc = rb_enc_compatible(result, value);
1133 rb_enc_associate(result, enc);
1136 enc = rb_enc_get(result);
1140 *(
volatile VALUE *)valp = value;
1149ruby_vsprintf0(
VALUE result,
char *p,
const char *fmt, va_list ap)
1152#define f buffer.base
1159 f._flags = __SWR | __SSTR;
1162 f._bf._base = (
unsigned char *)result;
1163 f._p = (
unsigned char *)p;
1164 RBASIC_CLEAR_CLASS(result);
1165 f.vwrite = ruby__sfvwrite;
1166 f.vextra = ruby__sfvextra;
1168 BSD_vfprintf(&f, fmt, ap);
1169 RBASIC_SET_CLASS_RAW(result, klass);
1171 long blen = (
char *)f._p - p;
1172 if (scanned < blen) {
1183 const int initial_len = 120;
1193 rb_enc_associate(result, enc);
1195 ruby_vsprintf0(result,
RSTRING_PTR(result), fmt, ap);
1205 va_start(ap, format);
1224 va_start(ap, format);
1246 va_start(ap, format);
ruby_coderange_type
What rb_enc_str_coderange() returns.
static bool rb_enc_isprint(OnigCodePoint c, rb_encoding *enc)
Identical to rb_isprint(), except it additionally takes an encoding.
static bool rb_enc_isdigit(OnigCodePoint c, rb_encoding *enc)
Identical to rb_isdigit(), except it additionally takes an encoding.
VALUE rb_enc_vsprintf(rb_encoding *enc, const char *fmt, va_list ap)
Identical to rb_enc_sprintf(), except it takes a va_list instead of variadic arguments.
VALUE rb_enc_sprintf(rb_encoding *enc, const char *fmt,...)
Identical to rb_sprintf(), except it additionally takes an encoding.
#define TYPE(_)
Old name of rb_type.
#define RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
#define ENC_CODERANGE_7BIT
Old name of RUBY_ENC_CODERANGE_7BIT.
#define ENC_CODERANGE_VALID
Old name of RUBY_ENC_CODERANGE_VALID.
#define RFLOAT_VALUE
Old name of rb_float_value.
#define T_STRING
Old name of RUBY_T_STRING.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define T_FLOAT
Old name of RUBY_T_FLOAT.
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
#define ECONV_UNDEF_REPLACE
Old name of RUBY_ECONV_UNDEF_REPLACE.
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
#define ENC_CODERANGE(obj)
Old name of RB_ENC_CODERANGE.
#define ENC_CODERANGE_UNKNOWN
Old name of RUBY_ENC_CODERANGE_UNKNOWN.
#define FIXABLE
Old name of RB_FIXABLE.
#define LONG2FIX
Old name of RB_INT2FIX.
#define ECONV_INVALID_REPLACE
Old name of RUBY_ECONV_INVALID_REPLACE.
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
#define NUM2INT
Old name of RB_NUM2INT.
#define Qnil
Old name of RUBY_Qnil.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define NIL_P
Old name of RB_NIL_P.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define ENC_CODERANGE_SET(obj, cr)
Old name of RB_ENC_CODERANGE_SET.
#define SYMBOL_P
Old name of RB_SYMBOL_P.
#define ruby_debug
This variable controls whether the interpreter is in debug mode.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
VALUE rb_eRuntimeError
RuntimeError exception.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports always regardless of runtime -W flag.
VALUE rb_eArgError
ArgumentError exception.
void rb_enc_raise(rb_encoding *enc, VALUE exc, const char *fmt,...)
Identical to rb_raise(), except it additionally takes an encoding.
VALUE rb_Float(VALUE val)
This is the logic behind Kernel#Float.
VALUE rb_Integer(VALUE val)
This is the logic behind Kernel#Integer.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
static const char * rb_enc_name(rb_encoding *enc)
Queries the (canonical) name of the passed encoding.
static char * rb_enc_right_char_head(const char *s, const char *p, const char *e, rb_encoding *enc)
Queries the right boundary of a character.
static int rb_enc_mbcput(unsigned int c, void *buf, rb_encoding *enc)
Identical to rb_enc_uint_chr(), except it writes back to the passed buffer instead of allocating one.
static int rb_enc_mbminlen(rb_encoding *enc)
Queries the minimum number of bytes that the passed encoding needs to represent a character.
char * rb_enc_nth(const char *head, const char *tail, long nth, rb_encoding *enc)
Queries the n-th character.
VALUE rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags, VALUE ecopts)
Identical to rb_str_conv_enc(), except it additionally takes IO encoder options.
long rb_enc_strlen(const char *head, const char *tail, rb_encoding *enc)
Counts the number of characters of the passed string, according to the passed encoding.
long rb_str_coderange_scan_restartable(const char *str, const char *end, rb_encoding *enc, int *cr)
Scans the passed string until it finds something odd.
VALUE rb_check_symbol_cstr(const char *ptr, long len, rb_encoding *enc)
Identical to rb_check_id_cstr(), except for the return type.
#define INTEGER_PACK_BIG_ENDIAN
Big endian combination.
#define INTEGER_PACK_2COMP
Uses 2's complement representation.
VALUE rb_int_positive_pow(long x, unsigned long y)
Raises the passed x to the power of y.
VALUE rb_rational_num(VALUE rat)
Queries the numerator of the passed Rational.
VALUE rb_rational_den(VALUE rat)
Queries the denominator of the passed Rational.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
size_t rb_str_capacity(VALUE str)
Queries the capacity of the given string.
void rb_str_modify(VALUE str)
Declares that the string is about to be modified.
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
void rb_must_asciicompat(VALUE obj)
Asserts that the given string's encoding is (Ruby's definition of) ASCII compatible.
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
VALUE rb_str_resize(VALUE str, long len)
Overwrites the length of the string.
VALUE rb_str_buf_new(long capa)
Allocates a "string buffer".
VALUE rb_obj_as_string(VALUE obj)
Try converting an object to its stringised representation using its to_s method, if any.
VALUE rb_sym2str(VALUE id)
Identical to rb_id2str(), except it takes an instance of rb_cSymbol rather than an ID.
VALUE rb_str_format(int argc, const VALUE *argv, VALUE fmt)
Formats a string.
VALUE rb_f_sprintf(int argc, const VALUE *argv)
Identical to rb_str_format(), except how the arguments are arranged.
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
VALUE rb_str_vcatf(VALUE dst, const char *fmt, va_list ap)
Identical to rb_str_catf(), except it takes a va_list.
VALUE rb_vsprintf(const char *fmt, va_list ap)
Identical to rb_sprintf(), except it takes a va_list.
VALUE rb_str_catf(VALUE dst, const char *fmt,...)
Identical to rb_sprintf(), except it renders the output to the specified object rather than creating ...
#define rb_long2int
Just another name of rb_long2int_inline.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define RBASIC(obj)
Convenient casting macro.
#define StringValue(v)
Ensures that the parameter object is a String.
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
int ruby_vsnprintf(char *str, size_t n, char const *fmt, va_list ap)
Identical to ruby_snprintf(), except it takes a va_list.
int ruby_snprintf(char *str, size_t n, char const *fmt,...)
Our own locale-insensitive version of snprintf(3).
#define RTEST
This is an old name of RB_TEST.
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.