Ruby: sprintf.c Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

sprintf.c
1/**********************************************************************
2
3 sprintf.c -
4
5 $Author$
6 created at: Fri Oct 15 10:39:26 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9 Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10 Copyright (C) 2000 Information-technology Promotion Agency, Japan
11
12**********************************************************************/
13
14#include "ruby/internal/config.h"
15
16#include <math.h>
17#include <stdarg.h>
18
19#ifdef HAVE_IEEEFP_H
20# include <ieeefp.h>
21#endif
22
23#include "id.h"
24#include "internal.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"
31#include "ruby/encoding.h"
32#include "ruby/re.h"
33#include "ruby/util.h"
34
35#define BIT_DIGITS(N) (((N)*146)/485 + 1) /* log2(10) =~ 146/485 */
36
37static char *fmt_setup(char*,size_t,int,int,int,int);
38static char *ruby_ultoa(unsigned long val, char *endp, int base, int octzero);
39
40static char
41sign_bits(int base, const char *p)
42{
43 char c = '.';
44
45 switch (base) {
46 case 16:
47 if (*p == 'X') c = 'F';
48 else c = 'f';
49 break;
50 case 8:
51 c = '7'; break;
52 case 2:
53 c = '1'; break;
54 }
55 return c;
56}
57
58#define FNONE 0
59#define FSHARP 1
60#define FMINUS 2
61#define FPLUS 4
62#define FZERO 8
63#define FSPACE 16
64#define FWIDTH 32
65#define FPREC 64
66#define FPREC0 128
67
68#define CHECK(l) do {\
69 int cr = ENC_CODERANGE(result);\
70 while ((l) >= bsiz - blen) {\
71 bsiz*=2;\
72 if (bsiz<0) rb_raise(rb_eArgError, "too big specifier");\
73 }\
74 rb_str_resize(result, bsiz);\
75 ENC_CODERANGE_SET(result, cr);\
76 buf = RSTRING_PTR(result);\
77} while (0)
78
79#define PUSH(s, l) do { \
80 CHECK(l);\
81 PUSH_(s, l);\
82} while (0)
83
84#define PUSH_(s, l) do { \
85 memcpy(&buf[blen], (s), (l));\
86 blen += (l);\
87} while (0)
88
89#define FILL(c, l) do { \
90 if ((l) <= 0) break;\
91 CHECK(l);\
92 FILL_(c, l);\
93} while (0)
94
95#define FILL_(c, l) do { \
96 memset(&buf[blen], (c), (l));\
97 blen += (l);\
98} while (0)
99
100#define GETARG() (!UNDEF_P(nextvalue) ? nextvalue : \
101 GETNEXTARG())
102
103#define GETNEXTARG() ( \
104 check_next_arg(posarg, nextarg), \
105 (posarg = nextarg++, GETNTHARG(posarg)))
106
107#define GETPOSARG(n) ( \
108 check_pos_arg(posarg, (n)), \
109 (posarg = -1, GETNTHARG(n)))
110
111#define GETNTHARG(nth) \
112 (((nth) >= argc) ? (rb_raise(rb_eArgError, "too few arguments"), 0) : argv[(nth)])
113
114#define CHECKNAMEARG(name, len, enc) ( \
115 check_name_arg(posarg, name, len, enc), \
116 posarg = -2)
117
118#define GETNUM(n, val) \
119 (!(p = get_num(p, end, enc, &(n))) ? \
120 rb_raise(rb_eArgError, #val " too big") : (void)0)
121
122#define GETASTER(val) do { \
123 t = p++; \
124 n = 0; \
125 GETNUM(n, val); \
126 if (*p == '$') { \
127 tmp = GETPOSARG(n); \
128 } \
129 else { \
130 tmp = GETNEXTARG(); \
131 p = t; \
132 } \
133 (val) = NUM2INT(tmp); \
134} while (0)
135
136static const char *
137get_num(const char *p, const char *end, rb_encoding *enc, int *valp)
138{
139 int next_n = *valp;
140 for (; p < end && rb_enc_isdigit(*p, enc); p++) {
141 if (MUL_OVERFLOW_INT_P(10, next_n))
142 return NULL;
143 next_n *= 10;
144 if (INT_MAX - (*p - '0') < next_n)
145 return NULL;
146 next_n += *p - '0';
147 }
148 if (p >= end) {
149 rb_raise(rb_eArgError, "malformed format string - %%*[0-9]");
150 }
151 *valp = next_n;
152 return p;
153}
154
155static void
156check_next_arg(int posarg, int nextarg)
157{
158 switch (posarg) {
159 case -1:
160 rb_raise(rb_eArgError, "unnumbered(%d) mixed with numbered", nextarg);
161 case -2:
162 rb_raise(rb_eArgError, "unnumbered(%d) mixed with named", nextarg);
163 }
164}
165
166static void
167check_pos_arg(int posarg, int n)
168{
169 if (posarg > 0) {
170 rb_raise(rb_eArgError, "numbered(%d) after unnumbered(%d)", n, posarg);
171 }
172 if (posarg == -2) {
173 rb_raise(rb_eArgError, "numbered(%d) after named", n);
174 }
175 if (n < 1) {
176 rb_raise(rb_eArgError, "invalid index - %d$", n);
177 }
178}
179
180static void
181check_name_arg(int posarg, const char *name, int len, rb_encoding *enc)
182{
183 if (posarg > 0) {
184 rb_enc_raise(enc, rb_eArgError, "named%.*s after unnumbered(%d)", len, name, posarg);
185 }
186 if (posarg == -1) {
187 rb_enc_raise(enc, rb_eArgError, "named%.*s after numbered", len, name);
188 }
189}
190
191static VALUE
192get_hash(volatile VALUE *hash, int argc, const VALUE *argv)
193{
194 VALUE tmp;
195
196 if (!UNDEF_P(*hash)) return *hash;
197 if (argc != 2) {
198 rb_raise(rb_eArgError, "one hash required");
199 }
200 tmp = rb_check_hash_type(argv[1]);
201 if (NIL_P(tmp)) {
202 rb_raise(rb_eArgError, "one hash required");
203 }
204 return (*hash = tmp);
205}
206
207 VALUE
208rb_f_sprintf(int argc, const VALUE *argv)
209{
210 return rb_str_format(argc - 1, argv + 1, GETNTHARG(0));
211}
212
213 VALUE
214rb_str_format(int argc, const VALUE *argv, VALUE fmt)
215{
216 enum {default_float_precision = 6};
217 rb_encoding *enc;
218 const char *p, *end;
219 char *buf;
220 long blen, bsiz;
221 VALUE result;
222
223 long scanned = 0;
224 enum ruby_coderange_type coderange = ENC_CODERANGE_7BIT;
225 int width, prec, flags = FNONE;
226 int nextarg = 1;
227 int posarg = 0;
228 VALUE nextvalue;
229 VALUE tmp;
230 VALUE orig;
231 VALUE str;
232 volatile VALUE hash = Qundef;
233
234#define CHECK_FOR_WIDTH(f) \
235 if ((f) & FWIDTH) { \
236 rb_raise(rb_eArgError, "width given twice"); \
237 } \
238 if ((f) & FPREC0) { \
239 rb_raise(rb_eArgError, "width after precision"); \
240 }
241#define CHECK_FOR_FLAGS(f) \
242 if ((f) & FWIDTH) { \
243 rb_raise(rb_eArgError, "flag after width"); \
244 } \
245 if ((f) & FPREC0) { \
246 rb_raise(rb_eArgError, "flag after precision"); \
247 }
248
249#define update_coderange(partial) do { \
250 if (coderange != ENC_CODERANGE_BROKEN && scanned < blen \
251 && rb_enc_to_index(enc) /* != ENCINDEX_ASCII_8BIT */) { \
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))); \
257 } \
258 } while (0)
259 ++argc;
260 --argv;
261 StringValue(fmt);
262 enc = rb_enc_get(fmt);
263 rb_must_asciicompat(fmt);
264 orig = fmt;
265 fmt = rb_str_tmp_frozen_acquire(fmt);
266 p = RSTRING_PTR(fmt);
267 end = p + RSTRING_LEN(fmt);
268 blen = 0;
269 bsiz = 120;
270 result = rb_str_buf_new(bsiz);
271 rb_enc_associate(result, enc);
272 buf = RSTRING_PTR(result);
273 memset(buf, 0, bsiz);
274 ENC_CODERANGE_SET(result, coderange);
275
276 for (; p < end; p++) {
277 const char *t;
278 int n;
279 VALUE sym = Qnil;
280
281 for (t = p; t < end && *t != '%'; t++) ;
282 if (t + 1 == end) {
283 rb_raise(rb_eArgError, "incomplete format specifier; use %%%% (double %%) instead");
284 }
285 PUSH(p, t - p);
286 update_coderange(FALSE);
287 if (t >= end) {
288 /* end of fmt string */
289 goto sprint_exit;
290 }
291 p = t + 1; /* skip `%' */
292
293 width = prec = -1;
294 nextvalue = Qundef;
295 retry:
296 switch (*p) {
297 default:
298 if (rb_enc_isprint(*p, enc))
299 rb_raise(rb_eArgError, "malformed format string - %%%c", *p);
300 else
301 rb_raise(rb_eArgError, "malformed format string");
302 break;
303
304 case ' ':
305 CHECK_FOR_FLAGS(flags);
306 flags |= FSPACE;
307 p++;
308 goto retry;
309
310 case '#':
311 CHECK_FOR_FLAGS(flags);
312 flags |= FSHARP;
313 p++;
314 goto retry;
315
316 case '+':
317 CHECK_FOR_FLAGS(flags);
318 flags |= FPLUS;
319 p++;
320 goto retry;
321
322 case '-':
323 CHECK_FOR_FLAGS(flags);
324 flags |= FMINUS;
325 p++;
326 goto retry;
327
328 case '0':
329 CHECK_FOR_FLAGS(flags);
330 flags |= FZERO;
331 p++;
332 goto retry;
333
334 case '1': case '2': case '3': case '4':
335 case '5': case '6': case '7': case '8': case '9':
336 n = 0;
337 GETNUM(n, width);
338 if (*p == '$') {
339 if (!UNDEF_P(nextvalue)) {
340 rb_raise(rb_eArgError, "value given twice - %d$", n);
341 }
342 nextvalue = GETPOSARG(n);
343 p++;
344 goto retry;
345 }
346 CHECK_FOR_WIDTH(flags);
347 width = n;
348 flags |= FWIDTH;
349 goto retry;
350
351 case '<':
352 case '{':
353 {
354 const char *start = p;
355 char term = (*p == '<') ? '>' : '}';
356 int len;
357
358 for (; p < end && *p != term; ) {
359 p += rb_enc_mbclen(p, end, enc);
360 }
361 if (p >= end) {
362 rb_raise(rb_eArgError, "malformed name - unmatched parenthesis");
363 }
364#if SIZEOF_INT < SIZEOF_SIZE_T
365 if ((size_t)(p - start) >= INT_MAX) {
366 const int message_limit = 20;
367 len = (int)(rb_enc_right_char_head(start, start + message_limit, p, enc) - start);
368 rb_enc_raise(enc, rb_eArgError,
369 "too long name (%"PRIuSIZE" bytes) - %.*s...%c",
370 (size_t)(p - start - 2), len, start, term);
371 }
372#endif
373 len = (int)(p - start + 1); /* including parenthesis */
374 if (sym != Qnil) {
375 rb_enc_raise(enc, rb_eArgError, "named%.*s after <%"PRIsVALUE">",
376 len, start, rb_sym2str(sym));
377 }
378 CHECKNAMEARG(start, len, enc);
379 get_hash(&hash, argc, argv);
380 sym = rb_check_symbol_cstr(start + 1,
381 len - 2 /* without parenthesis */,
382 enc);
383 if (!NIL_P(sym)) nextvalue = rb_hash_lookup2(hash, sym, Qundef);
384 if (UNDEF_P(nextvalue)) {
385 if (NIL_P(sym)) {
386 sym = rb_sym_intern(start + 1,
387 len - 2 /* without parenthesis */,
388 enc);
389 }
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);
393 }
394 }
395 if (term == '}') goto format_s;
396 p++;
397 goto retry;
398 }
399
400 case '*':
401 CHECK_FOR_WIDTH(flags);
402 flags |= FWIDTH;
403 GETASTER(width);
404 if (width < 0) {
405 flags |= FMINUS;
406 width = -width;
407 if (width < 0) rb_raise(rb_eArgError, "width too big");
408 }
409 p++;
410 goto retry;
411
412 case '.':
413 if (flags & FPREC0) {
414 rb_raise(rb_eArgError, "precision given twice");
415 }
416 flags |= FPREC|FPREC0;
417
418 prec = 0;
419 p++;
420 if (*p == '*') {
421 GETASTER(prec);
422 if (prec < 0) { /* ignore negative precision */
423 flags &= ~FPREC;
424 }
425 p++;
426 goto retry;
427 }
428
429 GETNUM(prec, precision);
430 goto retry;
431
432 case '\n':
433 case '\0':
434 p--;
435 /* fall through */
436 case '%':
437 if (flags != FNONE) {
438 rb_raise(rb_eArgError, "invalid format character - %%");
439 }
440 PUSH("%", 1);
441 break;
442
443 case 'c':
444 {
445 VALUE val = GETARG();
446 VALUE tmp;
447 unsigned int c;
448 int n;
449
450 tmp = rb_check_string_type(val);
451 if (!NIL_P(tmp)) {
452 flags |= FPREC;
453 prec = 1;
454 str = tmp;
455 goto format_s1;
456 }
457 n = NUM2INT(val);
458 if (n >= 0) n = rb_enc_codelen((c = n), enc);
459 if (n <= 0) {
460 rb_raise(rb_eArgError, "invalid character");
461 }
462 int encidx = rb_ascii8bit_appendable_encoding_index(enc, c);
463 if (encidx >= 0 && encidx != rb_enc_to_index(enc)) {
464 /* special case */
465 rb_enc_associate_index(result, encidx);
466 enc = rb_enc_from_index(encidx);
467 coderange = ENC_CODERANGE_VALID;
468 }
469 if (!(flags & FWIDTH)) {
470 CHECK(n);
471 rb_enc_mbcput(c, &buf[blen], enc);
472 blen += n;
473 }
474 else if ((flags & FMINUS)) {
475 --width;
476 CHECK(n + (width > 0 ? width : 0));
477 rb_enc_mbcput(c, &buf[blen], enc);
478 blen += n;
479 if (width > 0) FILL_(' ', width);
480 }
481 else {
482 --width;
483 CHECK(n + (width > 0 ? width : 0));
484 if (width > 0) FILL_(' ', width);
485 rb_enc_mbcput(c, &buf[blen], enc);
486 blen += n;
487 }
488 }
489 break;
490
491 case 's':
492 case 'p':
493 format_s:
494 {
495 VALUE arg = GETARG();
496 long len, slen;
497
498 if (*p == 'p') {
499 str = rb_inspect(arg);
500 }
501 else {
502 str = rb_obj_as_string(arg);
503 }
504 format_s1:
505 len = RSTRING_LEN(str);
506 rb_str_set_len(result, blen);
507 update_coderange(TRUE);
508 enc = rb_enc_check(result, str);
509 if (flags&(FPREC|FWIDTH)) {
510 slen = rb_enc_strlen(RSTRING_PTR(str),RSTRING_END(str),enc);
511 if (slen < 0) {
512 rb_raise(rb_eArgError, "invalid mbstring sequence");
513 }
514 if ((flags&FPREC) && (prec < slen)) {
515 char *p = rb_enc_nth(RSTRING_PTR(str), RSTRING_END(str),
516 prec, enc);
517 slen = prec;
518 len = p - RSTRING_PTR(str);
519 }
520 /* need to adjust multi-byte string pos */
521 if ((flags&FWIDTH) && (width > slen)) {
522 width -= (int)slen;
523 CHECK(len + width);
524 if (!(flags&FMINUS)) {
525 FILL_(' ', width);
526 width = 0;
527 }
528 memcpy(&buf[blen], RSTRING_PTR(str), len);
529 RB_GC_GUARD(str);
530 blen += len;
531 if (flags&FMINUS) {
532 FILL_(' ', width);
533 }
534 rb_enc_associate(result, enc);
535 break;
536 }
537 }
538 PUSH(RSTRING_PTR(str), len);
539 RB_GC_GUARD(str);
540 rb_enc_associate(result, enc);
541 }
542 break;
543
544 case 'd':
545 case 'i':
546 case 'o':
547 case 'x':
548 case 'X':
549 case 'b':
550 case 'B':
551 case 'u':
552 {
553 volatile VALUE val = GETARG();
554 int valsign;
555 char nbuf[BIT_DIGITS(SIZEOF_LONG*CHAR_BIT)+2], *s;
556 const char *prefix = 0;
557 int sign = 0, dots = 0;
558 char sc = 0;
559 long v = 0;
560 int base, bignum = 0;
561 int len;
562
563 switch (*p) {
564 case 'd':
565 case 'i':
566 case 'u':
567 sign = 1; break;
568 case 'o':
569 case 'x':
570 case 'X':
571 case 'b':
572 case 'B':
573 if (flags&(FPLUS|FSPACE)) sign = 1;
574 break;
575 }
576 if (flags & FSHARP) {
577 switch (*p) {
578 case 'o':
579 prefix = "0"; break;
580 case 'x':
581 prefix = "0x"; break;
582 case 'X':
583 prefix = "0X"; break;
584 case 'b':
585 prefix = "0b"; break;
586 case 'B':
587 prefix = "0B"; break;
588 }
589 }
590
591 bin_retry:
592 switch (TYPE(val)) {
593 case T_FLOAT:
594 if (FIXABLE(RFLOAT_VALUE(val))) {
595 val = LONG2FIX((long)RFLOAT_VALUE(val));
596 goto bin_retry;
597 }
598 val = rb_dbl2big(RFLOAT_VALUE(val));
599 if (FIXNUM_P(val)) goto bin_retry;
600 bignum = 1;
601 break;
602 case T_STRING:
603 val = rb_str_to_inum(val, 0, TRUE);
604 goto bin_retry;
605 case T_BIGNUM:
606 bignum = 1;
607 break;
608 case T_FIXNUM:
609 v = FIX2LONG(val);
610 break;
611 default:
612 val = rb_Integer(val);
613 goto bin_retry;
614 }
615
616 switch (*p) {
617 case 'o':
618 base = 8; break;
619 case 'x':
620 case 'X':
621 base = 16; break;
622 case 'b':
623 case 'B':
624 base = 2; break;
625 case 'u':
626 case 'd':
627 case 'i':
628 default:
629 base = 10; break;
630 }
631
632 if (base != 10) {
633 int numbits = ffs(base)-1;
634 size_t abs_nlz_bits;
635 size_t numdigits = rb_absint_numwords(val, numbits, &abs_nlz_bits);
636 long i;
637 if (INT_MAX-1 < numdigits) /* INT_MAX is used because rb_long2int is used later. */
638 rb_raise(rb_eArgError, "size too big");
639 if (sign) {
640 if (numdigits == 0)
641 numdigits = 1;
642 tmp = rb_str_new(NULL, numdigits);
643 valsign = rb_integer_pack(val, RSTRING_PTR(tmp), RSTRING_LEN(tmp),
644 1, CHAR_BIT-numbits, INTEGER_PACK_BIG_ENDIAN);
645 for (i = 0; i < RSTRING_LEN(tmp); i++)
646 RSTRING_PTR(tmp)[i] = ruby_digitmap[((unsigned char *)RSTRING_PTR(tmp))[i]];
647 s = RSTRING_PTR(tmp);
648 if (valsign < 0) {
649 sc = '-';
650 width--;
651 }
652 else if (flags & FPLUS) {
653 sc = '+';
654 width--;
655 }
656 else if (flags & FSPACE) {
657 sc = ' ';
658 width--;
659 }
660 }
661 else {
662 /* Following conditional "numdigits++" guarantees the
663 * most significant digit as
664 * - '1'(bin), '7'(oct) or 'f'(hex) for negative numbers
665 * - '0' for zero
666 * - not '0' for positive numbers.
667 *
668 * It also guarantees the most significant two
669 * digits will not be '11'(bin), '77'(oct), 'ff'(hex)
670 * or '00'. */
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))))
675 numdigits++;
676 tmp = rb_str_new(NULL, numdigits);
677 valsign = rb_integer_pack(val, RSTRING_PTR(tmp), RSTRING_LEN(tmp),
678 1, CHAR_BIT-numbits, INTEGER_PACK_2COMP | INTEGER_PACK_BIG_ENDIAN);
679 for (i = 0; i < RSTRING_LEN(tmp); i++)
680 RSTRING_PTR(tmp)[i] = ruby_digitmap[((unsigned char *)RSTRING_PTR(tmp))[i]];
681 s = RSTRING_PTR(tmp);
682 dots = valsign < 0;
683 }
684 len = rb_long2int(RSTRING_END(tmp) - s);
685 }
686 else if (!bignum) {
687 valsign = 1;
688 if (v < 0) {
689 v = -v;
690 sc = '-';
691 width--;
692 valsign = -1;
693 }
694 else if (flags & FPLUS) {
695 sc = '+';
696 width--;
697 }
698 else if (flags & FSPACE) {
699 sc = ' ';
700 width--;
701 }
702 s = ruby_ultoa((unsigned long)v, nbuf + sizeof(nbuf), 10, 0);
703 len = (int)(nbuf + sizeof(nbuf) - s);
704 }
705 else {
706 tmp = rb_big2str(val, 10);
707 s = RSTRING_PTR(tmp);
708 valsign = 1;
709 if (s[0] == '-') {
710 s++;
711 sc = '-';
712 width--;
713 valsign = -1;
714 }
715 else if (flags & FPLUS) {
716 sc = '+';
717 width--;
718 }
719 else if (flags & FSPACE) {
720 sc = ' ';
721 width--;
722 }
723 len = rb_long2int(RSTRING_END(tmp) - s);
724 }
725
726 if (dots) {
727 prec -= 2;
728 width -= 2;
729 }
730
731 if (*p == 'X') {
732 char *pp = s;
733 int c;
734 while ((c = (int)(unsigned char)*pp) != 0) {
735 *pp = rb_enc_toupper(c, enc);
736 pp++;
737 }
738 }
739 if (prefix && !prefix[1]) { /* octal */
740 if (dots) {
741 prefix = 0;
742 }
743 else if (len == 1 && *s == '0') {
744 len = 0;
745 if (flags & FPREC) prec--;
746 }
747 else if ((flags & FPREC) && (prec > len)) {
748 prefix = 0;
749 }
750 }
751 else if (len == 1 && *s == '0') {
752 prefix = 0;
753 }
754 if (prefix) {
755 width -= (int)strlen(prefix);
756 }
757 if ((flags & (FZERO|FMINUS|FPREC)) == FZERO) {
758 prec = width;
759 width = 0;
760 }
761 else {
762 if (prec < len) {
763 if (!prefix && prec == 0 && len == 1 && *s == '0') len = 0;
764 prec = len;
765 }
766 width -= prec;
767 }
768 if (!(flags&FMINUS)) {
769 FILL(' ', width);
770 width = 0;
771 }
772 if (sc) PUSH(&sc, 1);
773 if (prefix) {
774 int plen = (int)strlen(prefix);
775 PUSH(prefix, plen);
776 }
777 if (dots) PUSH("..", 2);
778 if (prec > len) {
779 CHECK(prec - len);
780 if (!sign && valsign < 0) {
781 char c = sign_bits(base, p);
782 FILL_(c, prec - len);
783 }
784 else if ((flags & (FMINUS|FPREC)) != FMINUS) {
785 FILL_('0', prec - len);
786 }
787 }
788 PUSH(s, len);
789 RB_GC_GUARD(tmp);
790 FILL(' ', width);
791 }
792 break;
793
794 case 'f':
795 {
796 VALUE val = GETARG(), num, den;
797 int sign = (flags&FPLUS) ? 1 : 0, zero = 0;
798 long len, fill;
799 if (RB_INTEGER_TYPE_P(val)) {
800 den = INT2FIX(1);
801 num = val;
802 }
803 else if (RB_TYPE_P(val, T_RATIONAL)) {
804 den = rb_rational_den(val);
805 num = rb_rational_num(val);
806 }
807 else {
808 nextvalue = val;
809 goto float_value;
810 }
811 if (!(flags&FPREC)) prec = default_float_precision;
812 if (FIXNUM_P(num)) {
813 if ((SIGNED_VALUE)num < 0) {
814 long n = -FIX2LONG(num);
815 num = LONG2FIX(n);
816 sign = -1;
817 }
818 }
819 else if (BIGNUM_NEGATIVE_P(num)) {
820 sign = -1;
821 num = rb_big_uminus(num);
822 }
823 if (den != INT2FIX(1)) {
824 num = rb_int_mul(num, rb_int_positive_pow(10, prec));
825 num = rb_int_plus(num, rb_int_idiv(den, INT2FIX(2)));
826 num = rb_int_idiv(num, den);
827 }
828 else if (prec >= 0) {
829 zero = prec;
830 }
831 val = rb_int2str(num, 10);
832 len = RSTRING_LEN(val) + zero;
833 if (prec >= len) len = prec + 1; /* integer part 0 */
834 if (sign || (flags&FSPACE)) ++len;
835 if (prec > 0) ++len; /* period */
836 fill = width > len ? width - len : 0;
837 CHECK(fill + len);
838 if (fill && !(flags&(FMINUS|FZERO))) {
839 FILL_(' ', fill);
840 }
841 if (sign || (flags&FSPACE)) {
842 buf[blen++] = sign > 0 ? '+' : sign < 0 ? '-' : ' ';
843 }
844 if (fill && (flags&(FMINUS|FZERO)) == FZERO) {
845 FILL_('0', fill);
846 }
847 len = RSTRING_LEN(val) + zero;
848 t = RSTRING_PTR(val);
849 if (len > prec) {
850 PUSH_(t, len - prec);
851 }
852 else {
853 buf[blen++] = '0';
854 }
855 if (prec > 0) {
856 buf[blen++] = '.';
857 }
858 if (zero) {
859 FILL_('0', zero);
860 }
861 else if (prec > len) {
862 FILL_('0', prec - len);
863 PUSH_(t, len);
864 }
865 else if (prec > 0) {
866 PUSH_(t + len - prec, prec);
867 }
868 if (fill && (flags&FMINUS)) {
869 FILL_(' ', fill);
870 }
871 RB_GC_GUARD(val);
872 break;
873 }
874 case 'g':
875 case 'G':
876 case 'e':
877 case 'E':
878 /* TODO: rational support */
879 case 'a':
880 case 'A':
881 float_value:
882 {
883 VALUE val = GETARG();
884 double fval;
885
886 fval = RFLOAT_VALUE(rb_Float(val));
887 if (!isfinite(fval)) {
888 const char *expr;
889 int need;
890 int elen;
891 char sign = '\0';
892
893 if (isnan(fval)) {
894 expr = "NaN";
895 }
896 else {
897 expr = "Inf";
898 }
899 need = (int)strlen(expr);
900 elen = need;
901 if (!isnan(fval) && fval < 0.0)
902 sign = '-';
903 else if (flags & (FPLUS|FSPACE))
904 sign = (flags & FPLUS) ? '+' : ' ';
905 if (sign)
906 ++need;
907 if ((flags & FWIDTH) && need < width)
908 need = width;
909
910 FILL(' ', need);
911 if (flags & FMINUS) {
912 if (sign)
913 buf[blen - need--] = sign;
914 memcpy(&buf[blen - need], expr, elen);
915 }
916 else {
917 if (sign)
918 buf[blen - elen - 1] = sign;
919 memcpy(&buf[blen - elen], expr, elen);
920 }
921 break;
922 }
923 else {
924 int cr = ENC_CODERANGE(result);
925 char fbuf[2*BIT_DIGITS(SIZEOF_INT*CHAR_BIT)+10];
926 char *fmt = fmt_setup(fbuf, sizeof(fbuf), *p, flags, width, prec);
927 rb_str_set_len(result, blen);
928 rb_str_catf(result, fmt, fval);
929 ENC_CODERANGE_SET(result, cr);
930 bsiz = rb_str_capacity(result);
931 RSTRING_GETMEM(result, buf, blen);
932 }
933 }
934 break;
935 }
936 flags = FNONE;
937 }
938
939 update_coderange(FALSE);
940 sprint_exit:
941 rb_str_tmp_frozen_release(orig, fmt);
942 /* XXX - We cannot validate the number of arguments if (digit)$ style used.
943 */
944 if (posarg >= 0 && nextarg < argc) {
945 const char *mesg = "too many arguments for format string";
946 if (RTEST(ruby_debug)) rb_raise(rb_eArgError, "%s", mesg);
947 if (RTEST(ruby_verbose)) rb_warn("%s", mesg);
948 }
949 rb_str_resize(result, blen);
950
951 return result;
952}
953
954static char *
955fmt_setup(char *buf, size_t size, int c, int flags, int width, int prec)
956{
957 buf += size;
958 *--buf = '\0';
959 *--buf = c;
960
961 if (flags & FPREC) {
962 buf = ruby_ultoa(prec, buf, 10, 0);
963 *--buf = '.';
964 }
965
966 if (flags & FWIDTH) {
967 buf = ruby_ultoa(width, buf, 10, 0);
968 }
969
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 = '#';
975 *--buf = '%';
976 return buf;
977}
978
979#undef FILE
980#define FILE rb_printf_buffer
981#define __sbuf rb_printf_sbuf
982#define __sFILE rb_printf_sfile
983#undef feof
984#undef ferror
985#undef clearerr
986#undef fileno
987#if SIZEOF_LONG < SIZEOF_LONG_LONG
988# if SIZEOF_LONG_LONG == SIZEOF_VOIDP
989/* actually this doesn't mean a pointer is strictly 64bit, but just
990 * quad_t size */
991# define _HAVE_LLP64_
992# endif
993# define _HAVE_SANE_QUAD_
994# define quad_t LONG_LONG
995# define u_quad_t unsigned LONG_LONG
996#endif
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
1002#endif
1003#define lower_hexdigits (ruby_hexdigits+0)
1004#define upper_hexdigits (ruby_hexdigits+16)
1005#include "vsnprintf.c"
1006
1007static char *
1008ruby_ultoa(unsigned long val, char *endp, int base, int flags)
1009{
1010 const char *xdigs = lower_hexdigits;
1011 int octzero = flags & FSHARP;
1012 return BSD__ultoa(val, endp, base, octzero, xdigs);
1013}
1014
1015static int ruby_do_vsnprintf(char *str, size_t n, const char *fmt, va_list ap);
1016
1017int
1018ruby_vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
1019{
1020 if (str && (ssize_t)n < 1)
1021 return (EOF);
1022 return ruby_do_vsnprintf(str, n, fmt, ap);
1023}
1024
1025static int
1026ruby_do_vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
1027{
1028 ssize_t ret;
1029 rb_printf_buffer f;
1030
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;
1035 f.vextra = 0;
1036 ret = BSD_vfprintf(&f, fmt, ap);
1037 if (str) *f._p = 0;
1038#if SIZEOF_SIZE_T > SIZEOF_INT
1039 if (n > INT_MAX) return INT_MAX;
1040#endif
1041 return (int)ret;
1042}
1043
1044int
1045ruby_snprintf(char *str, size_t n, char const *fmt, ...)
1046{
1047 int ret;
1048 va_list ap;
1049
1050 if (str && (ssize_t)n < 1)
1051 return (EOF);
1052
1053 va_start(ap, fmt);
1054 ret = ruby_do_vsnprintf(str, n, fmt, ap);
1055 va_end(ap);
1056 return ret;
1057}
1058
1059 typedef struct {
1060 rb_printf_buffer base;
1061 volatile VALUE value;
1062} rb_printf_buffer_extra;
1063
1064static int
1065ruby__sfvwrite(register rb_printf_buffer *fp, register struct __suio *uio)
1066{
1067 struct __siov *iov;
1068 VALUE result = (VALUE)fp->_bf._base;
1069 char *buf = (char*)fp->_p;
1070 long len, n;
1071 long blen = buf - RSTRING_PTR(result), bsiz = fp->_w;
1072
1073 if (RBASIC(result)->klass) {
1074 rb_raise(rb_eRuntimeError, "rb_vsprintf reentered");
1075 }
1076 if (uio->uio_resid == 0)
1077 return 0;
1078#if SIZE_MAX > LONG_MAX
1079 if (uio->uio_resid >= LONG_MAX)
1080 rb_raise(rb_eRuntimeError, "too big string");
1081#endif
1082 len = (long)uio->uio_resid;
1083 CHECK(len);
1084 buf += blen;
1085 fp->_w = bsiz;
1086 for (iov = uio->uio_iov; len > 0; ++iov) {
1087 MEMCPY(buf, iov->iov_base, char, n = iov->iov_len);
1088 buf += n;
1089 len -= n;
1090 }
1091 fp->_p = (unsigned char *)buf;
1092 rb_str_set_len(result, buf - RSTRING_PTR(result));
1093 return 0;
1094}
1095
1096static const char *
1097ruby__sfvextra(rb_printf_buffer *fp, size_t valsize, void *valp, long *sz, int sign)
1098{
1099 VALUE value, result = (VALUE)fp->_bf._base;
1100 rb_encoding *enc;
1101 char *cp;
1102
1103 if (valsize != sizeof(VALUE)) return 0;
1104 value = *(VALUE *)valp;
1105 if (RBASIC(result)->klass) {
1106 rb_raise(rb_eRuntimeError, "rb_vsprintf reentered");
1107 }
1108 if (sign == '+') {
1109# define LITERAL(str) (*sz = rb_strlen_lit(str), str)
1110 /* optimize special const cases */
1111 switch (value) {
1112# define LITERAL_CASE(x) case Q##x: return LITERAL(#x)
1113 LITERAL_CASE(nil);
1114 LITERAL_CASE(true);
1115 LITERAL_CASE(false);
1116# undef LITERAL_CASE
1117 }
1118# undef LITERAL
1119 value = rb_inspect(value);
1120 }
1121 else if (SYMBOL_P(value)) {
1122 value = rb_sym2str(value);
1123 if (sign == ' ' && !rb_str_symname_p(value)) {
1124 value = rb_str_escape(value);
1125 }
1126 }
1127 else {
1128 value = rb_obj_as_string(value);
1129 if (sign == ' ') value = QUOTE(value);
1130 }
1131 enc = rb_enc_compatible(result, value);
1132 if (enc) {
1133 rb_enc_associate(result, enc);
1134 }
1135 else {
1136 enc = rb_enc_get(result);
1137 value = rb_str_conv_enc_opts(value, rb_enc_get(value), enc,
1138 ECONV_UNDEF_REPLACE|ECONV_INVALID_REPLACE,
1139 Qnil);
1140 *(volatile VALUE *)valp = value;
1141 }
1142 StringValueCStr(value);
1143 RSTRING_GETMEM(value, cp, *sz);
1144 ((rb_printf_buffer_extra *)fp)->value = value;
1145 return cp;
1146}
1147
1148static void
1149ruby_vsprintf0(VALUE result, char *p, const char *fmt, va_list ap)
1150{
1151 rb_printf_buffer_extra buffer;
1152#define f buffer.base
1153 VALUE klass = RBASIC(result)->klass;
1154 int coderange = ENC_CODERANGE(result);
1155 long scanned = 0;
1156
1157 if (coderange != ENC_CODERANGE_UNKNOWN) scanned = p - RSTRING_PTR(result);
1158
1159 f._flags = __SWR | __SSTR;
1160 f._bf._size = 0;
1161 f._w = rb_str_capacity(result);
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;
1167 buffer.value = 0;
1168 BSD_vfprintf(&f, fmt, ap);
1169 RBASIC_SET_CLASS_RAW(result, klass);
1170 p = RSTRING_PTR(result);
1171 long blen = (char *)f._p - p;
1172 if (scanned < blen) {
1173 rb_str_coderange_scan_restartable(p + scanned, p + blen, rb_enc_get(result), &coderange);
1174 ENC_CODERANGE_SET(result, coderange);
1175 }
1176 rb_str_resize(result, blen);
1177#undef f
1178}
1179
1180 VALUE
1181rb_enc_vsprintf(rb_encoding *enc, const char *fmt, va_list ap)
1182{
1183 const int initial_len = 120;
1184 VALUE result;
1185
1186 result = rb_str_buf_new(initial_len);
1187 if (enc) {
1188 if (rb_enc_mbminlen(enc) > 1) {
1189 /* the implementation deeply depends on plain char */
1190 rb_raise(rb_eArgError, "cannot construct wchar_t based encoding string: %s",
1191 rb_enc_name(enc));
1192 }
1193 rb_enc_associate(result, enc);
1194 }
1195 ruby_vsprintf0(result, RSTRING_PTR(result), fmt, ap);
1196 return result;
1197}
1198
1199 VALUE
1200rb_enc_sprintf(rb_encoding *enc, const char *format, ...)
1201{
1202 VALUE result;
1203 va_list ap;
1204
1205 va_start(ap, format);
1206 result = rb_enc_vsprintf(enc, format, ap);
1207 va_end(ap);
1208
1209 return result;
1210}
1211
1212 VALUE
1213rb_vsprintf(const char *fmt, va_list ap)
1214{
1215 return rb_enc_vsprintf(NULL, fmt, ap);
1216}
1217
1218 VALUE
1219rb_sprintf(const char *format, ...)
1220{
1221 VALUE result;
1222 va_list ap;
1223
1224 va_start(ap, format);
1225 result = rb_vsprintf(format, ap);
1226 va_end(ap);
1227
1228 return result;
1229}
1230
1231 VALUE
1232rb_str_vcatf(VALUE str, const char *fmt, va_list ap)
1233{
1234 StringValue(str);
1235 rb_str_modify(str);
1236 ruby_vsprintf0(str, RSTRING_END(str), fmt, ap);
1237
1238 return str;
1239}
1240
1241 VALUE
1242rb_str_catf(VALUE str, const char *format, ...)
1243{
1244 va_list ap;
1245
1246 va_start(ap, format);
1247 str = rb_str_vcatf(str, format, ap);
1248 va_end(ap);
1249
1250 return str;
1251}
ruby_coderange_type
ruby_coderange_type
What rb_enc_str_coderange() returns.
Definition coderange.h:33
rb_enc_isprint
static bool rb_enc_isprint(OnigCodePoint c, rb_encoding *enc)
Identical to rb_isprint(), except it additionally takes an encoding.
Definition ctype.h:180
rb_enc_isdigit
static bool rb_enc_isdigit(OnigCodePoint c, rb_encoding *enc)
Identical to rb_isdigit(), except it additionally takes an encoding.
Definition ctype.h:208
rb_enc_vsprintf
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.
Definition sprintf.c:1181
rb_enc_sprintf
VALUE rb_enc_sprintf(rb_encoding *enc, const char *fmt,...)
Identical to rb_sprintf(), except it additionally takes an encoding.
Definition sprintf.c:1200
TYPE
#define TYPE(_)
Old name of rb_type.
Definition value_type.h:107
RB_INTEGER_TYPE_P
#define RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
Definition value_type.h:87
ENC_CODERANGE_7BIT
#define ENC_CODERANGE_7BIT
Old name of RUBY_ENC_CODERANGE_7BIT.
Definition coderange.h:180
ENC_CODERANGE_VALID
#define ENC_CODERANGE_VALID
Old name of RUBY_ENC_CODERANGE_VALID.
Definition coderange.h:181
RFLOAT_VALUE
#define RFLOAT_VALUE
Old name of rb_float_value.
Definition double.h:28
T_STRING
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
Qundef
#define Qundef
Old name of RUBY_Qundef.
Definition special_consts.h:62
INT2FIX
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
T_FLOAT
#define T_FLOAT
Old name of RUBY_T_FLOAT.
Definition value_type.h:64
T_BIGNUM
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
Definition value_type.h:57
ECONV_UNDEF_REPLACE
#define ECONV_UNDEF_REPLACE
Old name of RUBY_ECONV_UNDEF_REPLACE.
Definition transcode.h:526
T_FIXNUM
#define T_FIXNUM
Old name of RUBY_T_FIXNUM.
Definition value_type.h:63
ENC_CODERANGE
#define ENC_CODERANGE(obj)
Old name of RB_ENC_CODERANGE.
Definition coderange.h:184
ENC_CODERANGE_UNKNOWN
#define ENC_CODERANGE_UNKNOWN
Old name of RUBY_ENC_CODERANGE_UNKNOWN.
Definition coderange.h:179
FIXABLE
#define FIXABLE
Old name of RB_FIXABLE.
Definition fixnum.h:25
LONG2FIX
#define LONG2FIX
Old name of RB_INT2FIX.
Definition long.h:49
ECONV_INVALID_REPLACE
#define ECONV_INVALID_REPLACE
Old name of RUBY_ECONV_INVALID_REPLACE.
Definition transcode.h:524
T_RATIONAL
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
Definition value_type.h:76
NUM2INT
#define NUM2INT
Old name of RB_NUM2INT.
Definition int.h:44
Qnil
#define Qnil
Old name of RUBY_Qnil.
Definition special_consts.h:60
FIX2LONG
#define FIX2LONG
Old name of RB_FIX2LONG.
Definition long.h:46
NIL_P
#define NIL_P
Old name of RB_NIL_P.
Definition special_consts.h:55
FIXNUM_P
#define FIXNUM_P
Old name of RB_FIXNUM_P.
Definition special_consts.h:53
ENC_CODERANGE_SET
#define ENC_CODERANGE_SET(obj, cr)
Old name of RB_ENC_CODERANGE_SET.
Definition coderange.h:186
SYMBOL_P
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
ruby_debug
#define ruby_debug
This variable controls whether the interpreter is in debug mode.
Definition error.h:470
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
Definition error.c:3150
ruby_verbose
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
Definition error.h:459
rb_eRuntimeError
VALUE rb_eRuntimeError
RuntimeError exception.
Definition error.c:1089
rb_warn
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports always regardless of runtime -W flag.
Definition error.c:411
rb_eArgError
VALUE rb_eArgError
ArgumentError exception.
Definition error.c:1092
rb_enc_raise
void rb_enc_raise(rb_encoding *enc, VALUE exc, const char *fmt,...)
Identical to rb_raise(), except it additionally takes an encoding.
Definition error.c:3131
rb_Float
VALUE rb_Float(VALUE val)
This is the logic behind Kernel#Float.
Definition object.c:3533
rb_Integer
VALUE rb_Integer(VALUE val)
This is the logic behind Kernel#Integer.
Definition object.c:3102
rb_inspect
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
Definition object.c:601
encoding.h
Encoding relates APIs.
rb_enc_name
static const char * rb_enc_name(rb_encoding *enc)
Queries the (canonical) name of the passed encoding.
Definition encoding.h:433
rb_enc_right_char_head
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.
Definition encoding.h:720
rb_enc_mbcput
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.
Definition encoding.h:659
rb_enc_mbminlen
static int rb_enc_mbminlen(rb_encoding *enc)
Queries the minimum number of bytes that the passed encoding needs to represent a character.
Definition encoding.h:448
rb_enc_nth
char * rb_enc_nth(const char *head, const char *tail, long nth, rb_encoding *enc)
Queries the n-th character.
Definition string.c:2716
rb_str_conv_enc_opts
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.
Definition string.c:1093
rb_enc_strlen
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.
Definition string.c:2060
rb_str_coderange_scan_restartable
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.
Definition string.c:719
rb_check_symbol_cstr
VALUE rb_check_symbol_cstr(const char *ptr, long len, rb_encoding *enc)
Identical to rb_check_id_cstr(), except for the return type.
Definition symbol.c:1192
INTEGER_PACK_BIG_ENDIAN
#define INTEGER_PACK_BIG_ENDIAN
Big endian combination.
Definition bignum.h:572
INTEGER_PACK_2COMP
#define INTEGER_PACK_2COMP
Uses 2's complement representation.
Definition bignum.h:549
rb_int_positive_pow
VALUE rb_int_positive_pow(long x, unsigned long y)
Raises the passed x to the power of y.
Definition numeric.c:4492
rb_rational_num
VALUE rb_rational_num(VALUE rat)
Queries the numerator of the passed Rational.
Definition rational.c:1984
rb_rational_den
VALUE rb_rational_den(VALUE rat)
Queries the denominator of the passed Rational.
Definition rational.c:1990
rb_str_new
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1498
rb_str_capacity
size_t rb_str_capacity(VALUE str)
Queries the capacity of the given string.
Definition string.c:871
rb_str_modify
void rb_str_modify(VALUE str)
Declares that the string is about to be modified.
Definition string.c:2437
rb_str_set_len
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
Definition string.c:3020
rb_must_asciicompat
void rb_must_asciicompat(VALUE obj)
Asserts that the given string's encoding is (Ruby's definition of) ASCII compatible.
Definition string.c:2489
rb_check_string_type
VALUE rb_check_string_type(VALUE obj)
Try converting an object to its stringised representation using its to_str method,...
Definition string.c:2640
rb_str_resize
VALUE rb_str_resize(VALUE str, long len)
Overwrites the length of the string.
Definition string.c:3064
rb_str_buf_new
VALUE rb_str_buf_new(long capa)
Allocates a "string buffer".
Definition string.c:1532
rb_obj_as_string
VALUE rb_obj_as_string(VALUE obj)
Try converting an object to its stringised representation using its to_s method, if any.
Definition string.c:1682
rb_sym2str
VALUE rb_sym2str(VALUE id)
Identical to rb_id2str(), except it takes an instance of rb_cSymbol rather than an ID.
Definition symbol.c:943
re.h
util.h
rb_str_format
VALUE rb_str_format(int argc, const VALUE *argv, VALUE fmt)
Formats a string.
Definition sprintf.c:214
rb_f_sprintf
VALUE rb_f_sprintf(int argc, const VALUE *argv)
Identical to rb_str_format(), except how the arguments are arranged.
Definition sprintf.c:208
rb_sprintf
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
Definition sprintf.c:1219
rb_str_vcatf
VALUE rb_str_vcatf(VALUE dst, const char *fmt, va_list ap)
Identical to rb_str_catf(), except it takes a va_list.
Definition sprintf.c:1232
rb_vsprintf
VALUE rb_vsprintf(const char *fmt, va_list ap)
Identical to rb_sprintf(), except it takes a va_list.
Definition sprintf.c:1213
rb_str_catf
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 ...
Definition sprintf.c:1242
rb_long2int
#define rb_long2int
Just another name of rb_long2int_inline.
Definition long.h:62
MEMCPY
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:366
RB_GC_GUARD
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:161
RBASIC
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
StringValue
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:72
RSTRING_END
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
Definition rstring.h:528
RSTRING_GETMEM
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
Definition rstring.h:574
RSTRING_LEN
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
Definition rstring.h:484
RSTRING_PTR
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
Definition rstring.h:498
StringValueCStr
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
Definition rstring.h:95
ruby_vsnprintf
int ruby_vsnprintf(char *str, size_t n, char const *fmt, va_list ap)
Identical to ruby_snprintf(), except it takes a va_list.
Definition sprintf.c:1018
ruby_snprintf
int ruby_snprintf(char *str, size_t n, char const *fmt,...)
Our own locale-insensitive version of snprintf(3).
Definition sprintf.c:1045
RTEST
#define RTEST
This is an old name of RB_TEST.
Definition special_consts.h:51
stdarg.h
Defines old _.
__siov
Definition vsnprintf.c:217
__suio
Definition vsnprintf.c:221
OnigEncodingTypeST
Definition onigmo.h:160
rb_printf_buffer_extra
Definition sprintf.c:1059
SIGNED_VALUE
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
Definition value.h:63
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
RB_TYPE_P
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:375