Ruby: sprintf.c Source File

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

sprintf.c
Go to the documentation of this file.
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 "internal/util.h"
32#include "ruby/encoding.h"
33#include "ruby/re.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() (nextvalue != Qundef ? 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 *
137 get_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 (*hash != Qundef) 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
208 rb_f_sprintf(int argc, const VALUE *argv)
209{
210 return rb_str_format(argc - 1, argv + 1, GETNTHARG(0));
211}
212
213 VALUE
214 rb_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 int 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 ++argc;
250 --argv;
251 StringValue(fmt);
252 enc = rb_enc_get(fmt);
253 orig = fmt;
254 fmt = rb_str_tmp_frozen_acquire(fmt);
255 p = RSTRING_PTR(fmt);
256 end = p + RSTRING_LEN(fmt);
257 blen = 0;
258 bsiz = 120;
259 result = rb_str_buf_new(bsiz);
260 rb_enc_associate(result, enc);
261 buf = RSTRING_PTR(result);
262 memset(buf, 0, bsiz);
263 ENC_CODERANGE_SET(result, coderange);
264
265 for (; p < end; p++) {
266 const char *t;
267 int n;
268 VALUE sym = Qnil;
269
270 for (t = p; t < end && *t != '%'; t++) ;
271 if (t + 1 == end) {
272 rb_raise(rb_eArgError, "incomplete format specifier; use %%%% (double %%) instead");
273 }
274 PUSH(p, t - p);
275 if (coderange != ENC_CODERANGE_BROKEN && scanned < blen) {
276 scanned += rb_str_coderange_scan_restartable(buf+scanned, buf+blen, enc, &coderange);
277 ENC_CODERANGE_SET(result, coderange);
278 }
279 if (t >= end) {
280 /* end of fmt string */
281 goto sprint_exit;
282 }
283 p = t + 1; /* skip `%' */
284
285 width = prec = -1;
286 nextvalue = Qundef;
287 retry:
288 switch (*p) {
289 default:
290 if (rb_enc_isprint(*p, enc))
291 rb_raise(rb_eArgError, "malformed format string - %%%c", *p);
292 else
293 rb_raise(rb_eArgError, "malformed format string");
294 break;
295
296 case ' ':
297 CHECK_FOR_FLAGS(flags);
298 flags |= FSPACE;
299 p++;
300 goto retry;
301
302 case '#':
303 CHECK_FOR_FLAGS(flags);
304 flags |= FSHARP;
305 p++;
306 goto retry;
307
308 case '+':
309 CHECK_FOR_FLAGS(flags);
310 flags |= FPLUS;
311 p++;
312 goto retry;
313
314 case '-':
315 CHECK_FOR_FLAGS(flags);
316 flags |= FMINUS;
317 p++;
318 goto retry;
319
320 case '0':
321 CHECK_FOR_FLAGS(flags);
322 flags |= FZERO;
323 p++;
324 goto retry;
325
326 case '1': case '2': case '3': case '4':
327 case '5': case '6': case '7': case '8': case '9':
328 n = 0;
329 GETNUM(n, width);
330 if (*p == '$') {
331 if (nextvalue != Qundef) {
332 rb_raise(rb_eArgError, "value given twice - %d$", n);
333 }
334 nextvalue = GETPOSARG(n);
335 p++;
336 goto retry;
337 }
338 CHECK_FOR_WIDTH(flags);
339 width = n;
340 flags |= FWIDTH;
341 goto retry;
342
343 case '<':
344 case '{':
345 {
346 const char *start = p;
347 char term = (*p == '<') ? '>' : '}';
348 int len;
349
350 for (; p < end && *p != term; ) {
351 p += rb_enc_mbclen(p, end, enc);
352 }
353 if (p >= end) {
354 rb_raise(rb_eArgError, "malformed name - unmatched parenthesis");
355 }
356#if SIZEOF_INT < SIZEOF_SIZE_T
357 if ((size_t)(p - start) >= INT_MAX) {
358 const int message_limit = 20;
359 len = (int)(rb_enc_right_char_head(start, start + message_limit, p, enc) - start);
360 rb_enc_raise(enc, rb_eArgError,
361 "too long name (%"PRIuSIZE" bytes) - %.*s...%c",
362 (size_t)(p - start - 2), len, start, term);
363 }
364#endif
365 len = (int)(p - start + 1); /* including parenthesis */
366 if (sym != Qnil) {
367 rb_enc_raise(enc, rb_eArgError, "named%.*s after <%"PRIsVALUE">",
368 len, start, rb_sym2str(sym));
369 }
370 CHECKNAMEARG(start, len, enc);
371 get_hash(&hash, argc, argv);
372 sym = rb_check_symbol_cstr(start + 1,
373 len - 2 /* without parenthesis */,
374 enc);
375 if (!NIL_P(sym)) nextvalue = rb_hash_lookup2(hash, sym, Qundef);
376 if (nextvalue == Qundef) {
377 if (NIL_P(sym)) {
378 sym = rb_sym_intern(start + 1,
379 len - 2 /* without parenthesis */,
380 enc);
381 }
382 nextvalue = rb_hash_default_value(hash, sym);
383 if (NIL_P(nextvalue)) {
384 rb_key_err_raise(rb_enc_sprintf(enc, "key%.*s not found", len, start), hash, sym);
385 }
386 }
387 if (term == '}') goto format_s;
388 p++;
389 goto retry;
390 }
391
392 case '*':
393 CHECK_FOR_WIDTH(flags);
394 flags |= FWIDTH;
395 GETASTER(width);
396 if (width < 0) {
397 flags |= FMINUS;
398 width = -width;
399 if (width < 0) rb_raise(rb_eArgError, "width too big");
400 }
401 p++;
402 goto retry;
403
404 case '.':
405 if (flags & FPREC0) {
406 rb_raise(rb_eArgError, "precision given twice");
407 }
408 flags |= FPREC|FPREC0;
409
410 prec = 0;
411 p++;
412 if (*p == '*') {
413 GETASTER(prec);
414 if (prec < 0) { /* ignore negative precision */
415 flags &= ~FPREC;
416 }
417 p++;
418 goto retry;
419 }
420
421 GETNUM(prec, precision);
422 goto retry;
423
424 case '\n':
425 case '\0':
426 p--;
427 /* fall through */
428 case '%':
429 if (flags != FNONE) {
430 rb_raise(rb_eArgError, "invalid format character - %%");
431 }
432 PUSH("%", 1);
433 break;
434
435 case 'c':
436 {
437 VALUE val = GETARG();
438 VALUE tmp;
439 unsigned int c;
440 int n;
441
442 tmp = rb_check_string_type(val);
443 if (!NIL_P(tmp)) {
444 if (rb_enc_strlen(RSTRING_PTR(tmp),RSTRING_END(tmp),enc) != 1) {
445 rb_raise(rb_eArgError, "%%c requires a character");
446 }
447 c = rb_enc_codepoint_len(RSTRING_PTR(tmp), RSTRING_END(tmp), &n, enc);
448 RB_GC_GUARD(tmp);
449 }
450 else {
451 c = NUM2INT(val);
452 n = rb_enc_codelen(c, enc);
453 }
454 if (n <= 0) {
455 rb_raise(rb_eArgError, "invalid character");
456 }
457 if (!(flags & FWIDTH)) {
458 CHECK(n);
459 rb_enc_mbcput(c, &buf[blen], enc);
460 blen += n;
461 }
462 else if ((flags & FMINUS)) {
463 CHECK(n);
464 rb_enc_mbcput(c, &buf[blen], enc);
465 blen += n;
466 if (width > 1) FILL(' ', width-1);
467 }
468 else {
469 if (width > 1) FILL(' ', width-1);
470 CHECK(n);
471 rb_enc_mbcput(c, &buf[blen], enc);
472 blen += n;
473 }
474 }
475 break;
476
477 case 's':
478 case 'p':
479 format_s:
480 {
481 VALUE arg = GETARG();
482 long len, slen;
483
484 if (*p == 'p') {
485 str = rb_inspect(arg);
486 }
487 else {
488 str = rb_obj_as_string(arg);
489 }
490 len = RSTRING_LEN(str);
491 rb_str_set_len(result, blen);
492 if (coderange != ENC_CODERANGE_BROKEN && scanned < blen) {
493 int cr = coderange;
494 scanned += rb_str_coderange_scan_restartable(buf+scanned, buf+blen, enc, &cr);
495 ENC_CODERANGE_SET(result,
496 (cr == ENC_CODERANGE_UNKNOWN ?
497 ENC_CODERANGE_BROKEN : (coderange = cr)));
498 }
499 enc = rb_enc_check(result, str);
500 if (flags&(FPREC|FWIDTH)) {
501 slen = rb_enc_strlen(RSTRING_PTR(str),RSTRING_END(str),enc);
502 if (slen < 0) {
503 rb_raise(rb_eArgError, "invalid mbstring sequence");
504 }
505 if ((flags&FPREC) && (prec < slen)) {
506 char *p = rb_enc_nth(RSTRING_PTR(str), RSTRING_END(str),
507 prec, enc);
508 slen = prec;
509 len = p - RSTRING_PTR(str);
510 }
511 /* need to adjust multi-byte string pos */
512 if ((flags&FWIDTH) && (width > slen)) {
513 width -= (int)slen;
514 if (!(flags&FMINUS)) {
515 FILL(' ', width);
516 width = 0;
517 }
518 CHECK(len);
519 memcpy(&buf[blen], RSTRING_PTR(str), len);
520 RB_GC_GUARD(str);
521 blen += len;
522 if (flags&FMINUS) {
523 FILL(' ', width);
524 }
525 rb_enc_associate(result, enc);
526 break;
527 }
528 }
529 PUSH(RSTRING_PTR(str), len);
530 RB_GC_GUARD(str);
531 rb_enc_associate(result, enc);
532 }
533 break;
534
535 case 'd':
536 case 'i':
537 case 'o':
538 case 'x':
539 case 'X':
540 case 'b':
541 case 'B':
542 case 'u':
543 {
544 volatile VALUE val = GETARG();
545 int valsign;
546 char nbuf[BIT_DIGITS(SIZEOF_LONG*CHAR_BIT)+2], *s;
547 const char *prefix = 0;
548 int sign = 0, dots = 0;
549 char sc = 0;
550 long v = 0;
551 int base, bignum = 0;
552 int len;
553
554 switch (*p) {
555 case 'd':
556 case 'i':
557 case 'u':
558 sign = 1; break;
559 case 'o':
560 case 'x':
561 case 'X':
562 case 'b':
563 case 'B':
564 if (flags&(FPLUS|FSPACE)) sign = 1;
565 break;
566 }
567 if (flags & FSHARP) {
568 switch (*p) {
569 case 'o':
570 prefix = "0"; break;
571 case 'x':
572 prefix = "0x"; break;
573 case 'X':
574 prefix = "0X"; break;
575 case 'b':
576 prefix = "0b"; break;
577 case 'B':
578 prefix = "0B"; break;
579 }
580 }
581
582 bin_retry:
583 switch (TYPE(val)) {
584 case T_FLOAT:
585 if (FIXABLE(RFLOAT_VALUE(val))) {
586 val = LONG2FIX((long)RFLOAT_VALUE(val));
587 goto bin_retry;
588 }
589 val = rb_dbl2big(RFLOAT_VALUE(val));
590 if (FIXNUM_P(val)) goto bin_retry;
591 bignum = 1;
592 break;
593 case T_STRING:
594 val = rb_str_to_inum(val, 0, TRUE);
595 goto bin_retry;
596 case T_BIGNUM:
597 bignum = 1;
598 break;
599 case T_FIXNUM:
600 v = FIX2LONG(val);
601 break;
602 default:
603 val = rb_Integer(val);
604 goto bin_retry;
605 }
606
607 switch (*p) {
608 case 'o':
609 base = 8; break;
610 case 'x':
611 case 'X':
612 base = 16; break;
613 case 'b':
614 case 'B':
615 base = 2; break;
616 case 'u':
617 case 'd':
618 case 'i':
619 default:
620 base = 10; break;
621 }
622
623 if (base != 10) {
624 int numbits = ffs(base)-1;
625 size_t abs_nlz_bits;
626 size_t numdigits = rb_absint_numwords(val, numbits, &abs_nlz_bits);
627 long i;
628 if (INT_MAX-1 < numdigits) /* INT_MAX is used because rb_long2int is used later. */
629 rb_raise(rb_eArgError, "size too big");
630 if (sign) {
631 if (numdigits == 0)
632 numdigits = 1;
633 tmp = rb_str_new(NULL, numdigits);
634 valsign = rb_integer_pack(val, RSTRING_PTR(tmp), RSTRING_LEN(tmp),
635 1, CHAR_BIT-numbits, INTEGER_PACK_BIG_ENDIAN);
636 for (i = 0; i < RSTRING_LEN(tmp); i++)
637 RSTRING_PTR(tmp)[i] = ruby_digitmap[((unsigned char *)RSTRING_PTR(tmp))[i]];
638 s = RSTRING_PTR(tmp);
639 if (valsign < 0) {
640 sc = '-';
641 width--;
642 }
643 else if (flags & FPLUS) {
644 sc = '+';
645 width--;
646 }
647 else if (flags & FSPACE) {
648 sc = ' ';
649 width--;
650 }
651 }
652 else {
653 /* Following conditional "numdigits++" guarantees the
654 * most significant digit as
655 * - '1'(bin), '7'(oct) or 'f'(hex) for negative numbers
656 * - '0' for zero
657 * - not '0' for positive numbers.
658 *
659 * It also guarantees the most significant two
660 * digits will not be '11'(bin), '77'(oct), 'ff'(hex)
661 * or '00'. */
662 if (numdigits == 0 ||
663 ((abs_nlz_bits != (size_t)(numbits-1) ||
664 !rb_absint_singlebit_p(val)) &&
665 (!bignum ? v < 0 : BIGNUM_NEGATIVE_P(val))))
666 numdigits++;
667 tmp = rb_str_new(NULL, numdigits);
668 valsign = rb_integer_pack(val, RSTRING_PTR(tmp), RSTRING_LEN(tmp),
669 1, CHAR_BIT-numbits, INTEGER_PACK_2COMP | INTEGER_PACK_BIG_ENDIAN);
670 for (i = 0; i < RSTRING_LEN(tmp); i++)
671 RSTRING_PTR(tmp)[i] = ruby_digitmap[((unsigned char *)RSTRING_PTR(tmp))[i]];
672 s = RSTRING_PTR(tmp);
673 dots = valsign < 0;
674 }
675 len = rb_long2int(RSTRING_END(tmp) - s);
676 }
677 else if (!bignum) {
678 valsign = 1;
679 if (v < 0) {
680 v = -v;
681 sc = '-';
682 width--;
683 valsign = -1;
684 }
685 else if (flags & FPLUS) {
686 sc = '+';
687 width--;
688 }
689 else if (flags & FSPACE) {
690 sc = ' ';
691 width--;
692 }
693 s = ruby_ultoa((unsigned long)v, nbuf + sizeof(nbuf), 10, 0);
694 len = (int)(nbuf + sizeof(nbuf) - s);
695 }
696 else {
697 tmp = rb_big2str(val, 10);
698 s = RSTRING_PTR(tmp);
699 valsign = 1;
700 if (s[0] == '-') {
701 s++;
702 sc = '-';
703 width--;
704 valsign = -1;
705 }
706 else if (flags & FPLUS) {
707 sc = '+';
708 width--;
709 }
710 else if (flags & FSPACE) {
711 sc = ' ';
712 width--;
713 }
714 len = rb_long2int(RSTRING_END(tmp) - s);
715 }
716
717 if (dots) {
718 prec -= 2;
719 width -= 2;
720 }
721
722 if (*p == 'X') {
723 char *pp = s;
724 int c;
725 while ((c = (int)(unsigned char)*pp) != 0) {
726 *pp = rb_enc_toupper(c, enc);
727 pp++;
728 }
729 }
730 if (prefix && !prefix[1]) { /* octal */
731 if (dots) {
732 prefix = 0;
733 }
734 else if (len == 1 && *s == '0') {
735 len = 0;
736 if (flags & FPREC) prec--;
737 }
738 else if ((flags & FPREC) && (prec > len)) {
739 prefix = 0;
740 }
741 }
742 else if (len == 1 && *s == '0') {
743 prefix = 0;
744 }
745 if (prefix) {
746 width -= (int)strlen(prefix);
747 }
748 if ((flags & (FZERO|FMINUS|FPREC)) == FZERO) {
749 prec = width;
750 width = 0;
751 }
752 else {
753 if (prec < len) {
754 if (!prefix && prec == 0 && len == 1 && *s == '0') len = 0;
755 prec = len;
756 }
757 width -= prec;
758 }
759 if (!(flags&FMINUS)) {
760 FILL(' ', width);
761 width = 0;
762 }
763 if (sc) PUSH(&sc, 1);
764 if (prefix) {
765 int plen = (int)strlen(prefix);
766 PUSH(prefix, plen);
767 }
768 if (dots) PUSH("..", 2);
769 if (prec > len) {
770 CHECK(prec - len);
771 if (!sign && valsign < 0) {
772 char c = sign_bits(base, p);
773 FILL_(c, prec - len);
774 }
775 else if ((flags & (FMINUS|FPREC)) != FMINUS) {
776 FILL_('0', prec - len);
777 }
778 }
779 PUSH(s, len);
780 RB_GC_GUARD(tmp);
781 FILL(' ', width);
782 }
783 break;
784
785 case 'f':
786 {
787 VALUE val = GETARG(), num, den;
788 int sign = (flags&FPLUS) ? 1 : 0, zero = 0;
789 long len, fill;
790 if (RB_INTEGER_TYPE_P(val)) {
791 den = INT2FIX(1);
792 num = val;
793 }
794 else if (RB_TYPE_P(val, T_RATIONAL)) {
795 den = rb_rational_den(val);
796 num = rb_rational_num(val);
797 }
798 else {
799 nextvalue = val;
800 goto float_value;
801 }
802 if (!(flags&FPREC)) prec = default_float_precision;
803 if (FIXNUM_P(num)) {
804 if ((SIGNED_VALUE)num < 0) {
805 long n = -FIX2LONG(num);
806 num = LONG2FIX(n);
807 sign = -1;
808 }
809 }
810 else if (BIGNUM_NEGATIVE_P(num)) {
811 sign = -1;
812 num = rb_big_uminus(num);
813 }
814 if (den != INT2FIX(1)) {
815 num = rb_int_mul(num, rb_int_positive_pow(10, prec));
816 num = rb_int_plus(num, rb_int_idiv(den, INT2FIX(2)));
817 num = rb_int_idiv(num, den);
818 }
819 else if (prec >= 0) {
820 zero = prec;
821 }
822 val = rb_int2str(num, 10);
823 len = RSTRING_LEN(val) + zero;
824 if (prec >= len) len = prec + 1; /* integer part 0 */
825 if (sign || (flags&FSPACE)) ++len;
826 if (prec > 0) ++len; /* period */
827 fill = width > len ? width - len : 0;
828 CHECK(fill + len);
829 if (fill && !(flags&(FMINUS|FZERO))) {
830 FILL_(' ', fill);
831 }
832 if (sign || (flags&FSPACE)) {
833 buf[blen++] = sign > 0 ? '+' : sign < 0 ? '-' : ' ';
834 }
835 if (fill && (flags&(FMINUS|FZERO)) == FZERO) {
836 FILL_('0', fill);
837 }
838 len = RSTRING_LEN(val) + zero;
839 t = RSTRING_PTR(val);
840 if (len > prec) {
841 PUSH_(t, len - prec);
842 }
843 else {
844 buf[blen++] = '0';
845 }
846 if (prec > 0) {
847 buf[blen++] = '.';
848 }
849 if (zero) {
850 FILL_('0', zero);
851 }
852 else if (prec > len) {
853 FILL_('0', prec - len);
854 PUSH_(t, len);
855 }
856 else if (prec > 0) {
857 PUSH_(t + len - prec, prec);
858 }
859 if (fill && (flags&FMINUS)) {
860 FILL_(' ', fill);
861 }
862 RB_GC_GUARD(val);
863 break;
864 }
865 case 'g':
866 case 'G':
867 case 'e':
868 case 'E':
869 /* TODO: rational support */
870 case 'a':
871 case 'A':
872 float_value:
873 {
874 VALUE val = GETARG();
875 double fval;
876
877 fval = RFLOAT_VALUE(rb_Float(val));
878 if (isnan(fval) || isinf(fval)) {
879 const char *expr;
880 int need;
881 int elen;
882 char sign = '\0';
883
884 if (isnan(fval)) {
885 expr = "NaN";
886 }
887 else {
888 expr = "Inf";
889 }
890 need = (int)strlen(expr);
891 elen = need;
892 if (!isnan(fval) && fval < 0.0)
893 sign = '-';
894 else if (flags & (FPLUS|FSPACE))
895 sign = (flags & FPLUS) ? '+' : ' ';
896 if (sign)
897 ++need;
898 if ((flags & FWIDTH) && need < width)
899 need = width;
900
901 FILL(' ', need);
902 if (flags & FMINUS) {
903 if (sign)
904 buf[blen - need--] = sign;
905 memcpy(&buf[blen - need], expr, elen);
906 }
907 else {
908 if (sign)
909 buf[blen - elen - 1] = sign;
910 memcpy(&buf[blen - elen], expr, elen);
911 }
912 break;
913 }
914 else {
915 int cr = ENC_CODERANGE(result);
916 char fbuf[2*BIT_DIGITS(SIZEOF_INT*CHAR_BIT)+10];
917 char *fmt = fmt_setup(fbuf, sizeof(fbuf), *p, flags, width, prec);
918 rb_str_set_len(result, blen);
919 rb_str_catf(result, fmt, fval);
920 ENC_CODERANGE_SET(result, cr);
921 bsiz = rb_str_capacity(result);
922 RSTRING_GETMEM(result, buf, blen);
923 }
924 }
925 break;
926 }
927 flags = FNONE;
928 }
929
930 sprint_exit:
931 rb_str_tmp_frozen_release(orig, fmt);
932 /* XXX - We cannot validate the number of arguments if (digit)$ style used.
933 */
934 if (posarg >= 0 && nextarg < argc) {
935 const char *mesg = "too many arguments for format string";
936 if (RTEST(ruby_debug)) rb_raise(rb_eArgError, "%s", mesg);
937 if (RTEST(ruby_verbose)) rb_warn("%s", mesg);
938 }
939 rb_str_resize(result, blen);
940
941 return result;
942}
943
944static char *
945fmt_setup(char *buf, size_t size, int c, int flags, int width, int prec)
946{
947 buf += size;
948 *--buf = '\0';
949 *--buf = c;
950
951 if (flags & FPREC) {
952 buf = ruby_ultoa(prec, buf, 10, 0);
953 *--buf = '.';
954 }
955
956 if (flags & FWIDTH) {
957 buf = ruby_ultoa(width, buf, 10, 0);
958 }
959
960 if (flags & FSPACE) *--buf = ' ';
961 if (flags & FZERO) *--buf = '0';
962 if (flags & FMINUS) *--buf = '-';
963 if (flags & FPLUS) *--buf = '+';
964 if (flags & FSHARP) *--buf = '#';
965 *--buf = '%';
966 return buf;
967}
968
969#undef FILE
970#define FILE rb_printf_buffer
971#define __sbuf rb_printf_sbuf
972#define __sFILE rb_printf_sfile
973#undef feof
974#undef ferror
975#undef clearerr
976#undef fileno
977#if SIZEOF_LONG < SIZEOF_VOIDP
978# if SIZEOF_LONG_LONG == SIZEOF_VOIDP
979# define _HAVE_SANE_QUAD_
980# define _HAVE_LLP64_
981# define quad_t LONG_LONG
982# define u_quad_t unsigned LONG_LONG
983# endif
984#elif SIZEOF_LONG != SIZEOF_LONG_LONG && SIZEOF_LONG_LONG == 8
985# define _HAVE_SANE_QUAD_
986# define quad_t LONG_LONG
987# define u_quad_t unsigned LONG_LONG
988#endif
989#define FLOATING_POINT 1
990#define BSD__dtoa ruby_dtoa
991#define BSD__hdtoa ruby_hdtoa
992#ifdef RUBY_PRI_VALUE_MARK
993# define PRI_EXTRA_MARK RUBY_PRI_VALUE_MARK
994#endif
995#define lower_hexdigits (ruby_hexdigits+0)
996#define upper_hexdigits (ruby_hexdigits+16)
997#include "vsnprintf.c"
998
999static char *
1000ruby_ultoa(unsigned long val, char *endp, int base, int flags)
1001{
1002 const char *xdigs = lower_hexdigits;
1003 int octzero = flags & FSHARP;
1004 return BSD__ultoa(val, endp, base, octzero, xdigs);
1005}
1006
1007static int ruby_do_vsnprintf(char *str, size_t n, const char *fmt, va_list ap);
1008
1009int
1010 ruby_vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
1011{
1012 if (str && (ssize_t)n < 1)
1013 return (EOF);
1014 return ruby_do_vsnprintf(str, n, fmt, ap);
1015}
1016
1017static int
1018ruby_do_vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
1019{
1020 ssize_t ret;
1021 rb_printf_buffer f;
1022
1023 f._flags = __SWR | __SSTR;
1024 f._bf._base = f._p = (unsigned char *)str;
1025 f._bf._size = f._w = str ? (n - 1) : 0;
1026 f.vwrite = BSD__sfvwrite;
1027 f.vextra = 0;
1028 ret = BSD_vfprintf(&f, fmt, ap);
1029 if (str) *f._p = 0;
1030#if SIZEOF_SIZE_T > SIZEOF_INT
1031 if (n > INT_MAX) return INT_MAX;
1032#endif
1033 return (int)ret;
1034}
1035
1036int
1037 ruby_snprintf(char *str, size_t n, char const *fmt, ...)
1038{
1039 int ret;
1040 va_list ap;
1041
1042 if (str && (ssize_t)n < 1)
1043 return (EOF);
1044
1045 va_start(ap, fmt);
1046 ret = ruby_do_vsnprintf(str, n, fmt, ap);
1047 va_end(ap);
1048 return ret;
1049}
1050
1051 typedef struct {
1052 rb_printf_buffer base;
1053 volatile VALUE value;
1054} rb_printf_buffer_extra;
1055
1056static int
1057ruby__sfvwrite(register rb_printf_buffer *fp, register struct __suio *uio)
1058{
1059 struct __siov *iov;
1060 VALUE result = (VALUE)fp->_bf._base;
1061 char *buf = (char*)fp->_p;
1062 long len, n;
1063 long blen = buf - RSTRING_PTR(result), bsiz = fp->_w;
1064
1065 if (RBASIC(result)->klass) {
1066 rb_raise(rb_eRuntimeError, "rb_vsprintf reentered");
1067 }
1068 if (uio->uio_resid == 0)
1069 return 0;
1070#if SIZE_MAX > LONG_MAX
1071 if (uio->uio_resid >= LONG_MAX)
1072 rb_raise(rb_eRuntimeError, "too big string");
1073#endif
1074 len = (long)uio->uio_resid;
1075 CHECK(len);
1076 buf += blen;
1077 fp->_w = bsiz;
1078 for (iov = uio->uio_iov; len > 0; ++iov) {
1079 MEMCPY(buf, iov->iov_base, char, n = iov->iov_len);
1080 buf += n;
1081 len -= n;
1082 }
1083 fp->_p = (unsigned char *)buf;
1084 rb_str_set_len(result, buf - RSTRING_PTR(result));
1085 return 0;
1086}
1087
1088static const char *
1089ruby__sfvextra(rb_printf_buffer *fp, size_t valsize, void *valp, long *sz, int sign)
1090{
1091 VALUE value, result = (VALUE)fp->_bf._base;
1092 rb_encoding *enc;
1093 char *cp;
1094
1095 if (valsize != sizeof(VALUE)) return 0;
1096 value = *(VALUE *)valp;
1097 if (RBASIC(result)->klass) {
1098 rb_raise(rb_eRuntimeError, "rb_vsprintf reentered");
1099 }
1100 if (sign == '+') {
1101 if (RB_TYPE_P(value, T_CLASS)) {
1102# define LITERAL(str) (*sz = rb_strlen_lit(str), str)
1103
1104 if (value == rb_cNilClass) {
1105 return LITERAL("nil");
1106 }
1107 else if (value == rb_cInteger) {
1108 return LITERAL("Integer");
1109 }
1110 else if (value == rb_cSymbol) {
1111 return LITERAL("Symbol");
1112 }
1113 else if (value == rb_cTrueClass) {
1114 return LITERAL("true");
1115 }
1116 else if (value == rb_cFalseClass) {
1117 return LITERAL("false");
1118 }
1119# undef LITERAL
1120 }
1121 value = rb_inspect(value);
1122 }
1123 else if (SYMBOL_P(value)) {
1124 value = rb_sym2str(value);
1125 if (sign == ' ' && !rb_str_symname_p(value)) {
1126 value = rb_str_inspect(value);
1127 }
1128 }
1129 else {
1130 value = rb_obj_as_string(value);
1131 if (sign == ' ') value = QUOTE(value);
1132 }
1133 enc = rb_enc_compatible(result, value);
1134 if (enc) {
1135 rb_enc_associate(result, enc);
1136 }
1137 else {
1138 enc = rb_enc_get(result);
1139 value = rb_str_conv_enc_opts(value, rb_enc_get(value), enc,
1140 ECONV_UNDEF_REPLACE|ECONV_INVALID_REPLACE,
1141 Qnil);
1142 *(volatile VALUE *)valp = value;
1143 }
1144 StringValueCStr(value);
1145 RSTRING_GETMEM(value, cp, *sz);
1146 ((rb_printf_buffer_extra *)fp)->value = value;
1147 return cp;
1148}
1149
1150 VALUE
1151 rb_enc_vsprintf(rb_encoding *enc, const char *fmt, va_list ap)
1152{
1153 rb_printf_buffer_extra buffer;
1154#define f buffer.base
1155 VALUE result;
1156
1157 f._flags = __SWR | __SSTR;
1158 f._bf._size = 0;
1159 f._w = 120;
1160 result = rb_str_buf_new(f._w);
1161 if (enc) {
1162 if (rb_enc_mbminlen(enc) > 1) {
1163 /* the implementation deeply depends on plain char */
1164 rb_raise(rb_eArgError, "cannot construct wchar_t based encoding string: %s",
1165 rb_enc_name(enc));
1166 }
1167 rb_enc_associate(result, enc);
1168 }
1169 f._bf._base = (unsigned char *)result;
1170 f._p = (unsigned char *)RSTRING_PTR(result);
1171 RBASIC_CLEAR_CLASS(result);
1172 f.vwrite = ruby__sfvwrite;
1173 f.vextra = ruby__sfvextra;
1174 buffer.value = 0;
1175 BSD_vfprintf(&f, fmt, ap);
1176 RBASIC_SET_CLASS_RAW(result, rb_cString);
1177 rb_str_resize(result, (char *)f._p - RSTRING_PTR(result));
1178#undef f
1179
1180 return result;
1181}
1182
1183 VALUE
1184 rb_enc_sprintf(rb_encoding *enc, const char *format, ...)
1185{
1186 VALUE result;
1187 va_list ap;
1188
1189 va_start(ap, format);
1190 result = rb_enc_vsprintf(enc, format, ap);
1191 va_end(ap);
1192
1193 return result;
1194}
1195
1196 VALUE
1197 rb_vsprintf(const char *fmt, va_list ap)
1198{
1199 return rb_enc_vsprintf(NULL, fmt, ap);
1200}
1201
1202 VALUE
1203 rb_sprintf(const char *format, ...)
1204{
1205 VALUE result;
1206 va_list ap;
1207
1208 va_start(ap, format);
1209 result = rb_vsprintf(format, ap);
1210 va_end(ap);
1211
1212 return result;
1213}
1214
1215 VALUE
1216 rb_str_vcatf(VALUE str, const char *fmt, va_list ap)
1217{
1218 rb_printf_buffer_extra buffer;
1219#define f buffer.base
1220 VALUE klass;
1221
1222 StringValue(str);
1223 rb_str_modify(str);
1224 f._flags = __SWR | __SSTR;
1225 f._bf._size = 0;
1226 f._w = rb_str_capacity(str);
1227 f._bf._base = (unsigned char *)str;
1228 f._p = (unsigned char *)RSTRING_END(str);
1229 klass = RBASIC(str)->klass;
1230 RBASIC_CLEAR_CLASS(str);
1231 f.vwrite = ruby__sfvwrite;
1232 f.vextra = ruby__sfvextra;
1233 buffer.value = 0;
1234 BSD_vfprintf(&f, fmt, ap);
1235 RBASIC_SET_CLASS_RAW(str, klass);
1236 rb_str_resize(str, (char *)f._p - RSTRING_PTR(str));
1237#undef f
1238
1239 return str;
1240}
1241
1242 VALUE
1243 rb_str_catf(VALUE str, const char *format, ...)
1244{
1245 va_list ap;
1246
1247 va_start(ap, format);
1248 str = rb_str_vcatf(str, format, ap);
1249 va_end(ap);
1250
1251 return str;
1252}
rb_integer_pack
int rb_integer_pack(VALUE val, void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
Definition: bignum.c:3553
rb_str_to_inum
VALUE rb_str_to_inum(VALUE str, int base, int badcheck)
Definition: bignum.c:4274
rb_absint_numwords
size_t rb_absint_numwords(VALUE val, size_t word_numbits, size_t *nlz_bits_ret)
Definition: bignum.c:3388
rb_absint_singlebit_p
int rb_absint_singlebit_p(VALUE val)
Definition: bignum.c:3452
rb_big2str
VALUE rb_big2str(VALUE x, int base)
Definition: bignum.c:5090
rb_dbl2big
VALUE rb_dbl2big(double d)
Definition: bignum.c:5248
ruby_digitmap
const char ruby_digitmap[]
Definition: bignum.c:48
rb_big_uminus
VALUE rb_big_uminus(VALUE x)
Definition: bignum.c:5551
MUL_OVERFLOW_INT_P
#define MUL_OVERFLOW_INT_P(a, b)
Definition: bits.h:126
rb_int_positive_pow
VALUE rb_int_positive_pow(long x, unsigned long y)
Definition: numeric.c:4022
RFLOAT_VALUE
#define RFLOAT_VALUE
Definition: double.h:28
rb_enc_associate
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
Definition: encoding.c:1064
rb_enc_codepoint_len
unsigned int rb_enc_codepoint_len(const char *p, const char *e, int *len_p, rb_encoding *enc)
Definition: encoding.c:1266
rb_enc_get
rb_encoding * rb_enc_get(VALUE obj)
Definition: encoding.c:1070
rb_enc_check
rb_encoding * rb_enc_check(VALUE str1, VALUE str2)
Definition: encoding.c:1089
rb_enc_mbclen
int rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc)
Definition: encoding.c:1218
rb_enc_compatible
rb_encoding * rb_enc_compatible(VALUE str1, VALUE str2)
Definition: encoding.c:1172
rb_enc_toupper
int rb_enc_toupper(int c, rb_encoding *enc)
Definition: encoding.c:1304
rb_enc_codelen
int rb_enc_codelen(int c, rb_encoding *enc)
Definition: encoding.c:1287
num
big_t * num
Definition: enough.c:232
sym
#define sym(name)
Definition: enumerator.c:4007
len
uint8_t len
Definition: escape.c:17
str
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
RSTRING_LEN
#define RSTRING_LEN(string)
Definition: fbuffer.h:22
RSTRING_PTR
#define RSTRING_PTR(string)
Definition: fbuffer.h:19
memcpy
#define memcpy(d, s, n)
Definition: ffi_common.h:55
PRIsVALUE
#define PRIsVALUE
Definition: function.c:10
rb_cInteger
VALUE rb_cInteger
Definition: numeric.c:191
rb_cSymbol
VALUE rb_cSymbol
Definition: string.c:81
rb_cString
VALUE rb_cString
Definition: string.c:80
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2917
rb_eRuntimeError
VALUE rb_eRuntimeError
Definition: error.c:1055
rb_warn
void rb_warn(const char *fmt,...)
Definition: error.c:408
rb_eArgError
VALUE rb_eArgError
Definition: error.c:1058
rb_Float
VALUE rb_Float(VALUE)
Equivalent to Kernel#Float in Ruby.
Definition: object.c:3531
rb_cNilClass
VALUE rb_cNilClass
NilClass class.
Definition: object.c:53
rb_Integer
VALUE rb_Integer(VALUE)
Equivalent to Kernel#Integer in Ruby.
Definition: object.c:3138
rb_cFalseClass
VALUE rb_cFalseClass
FalseClass class.
Definition: object.c:55
rb_inspect
VALUE rb_inspect(VALUE)
Convenient wrapper of Object::inspect.
Definition: object.c:585
rb_cTrueClass
VALUE rb_cTrueClass
TrueClass class.
Definition: object.c:54
prefix
unsigned short prefix[65536]
Definition: gun.c:163
rb_hash_default_value
VALUE rb_hash_default_value(VALUE hash, VALUE key)
Definition: hash.c:1999
rb_check_hash_type
VALUE rb_check_hash_type(VALUE hash)
Definition: hash.c:1860
rb_hash_lookup2
VALUE rb_hash_lookup2(VALUE hash, VALUE key, VALUE def)
Definition: hash.c:2059
term
const char term
Definition: id.c:37
id.h
encoding.h
ENC_CODERANGE_7BIT
#define ENC_CODERANGE_7BIT
Definition: encoding.h:93
rb_enc_mbcput
#define rb_enc_mbcput(c, buf, enc)
Definition: encoding.h:208
ECONV_UNDEF_REPLACE
#define ECONV_UNDEF_REPLACE
Definition: encoding.h:387
ENC_CODERANGE
#define ENC_CODERANGE(obj)
Definition: encoding.h:97
ENC_CODERANGE_UNKNOWN
#define ENC_CODERANGE_UNKNOWN
Definition: encoding.h:92
rb_str_conv_enc_opts
VALUE rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags, VALUE ecopts)
Definition: string.c:984
rb_enc_name
#define rb_enc_name(enc)
Definition: encoding.h:168
ECONV_INVALID_REPLACE
#define ECONV_INVALID_REPLACE
Definition: encoding.h:385
rb_enc_isdigit
#define rb_enc_isdigit(c, enc)
Definition: encoding.h:229
rb_enc_isprint
#define rb_enc_isprint(c, enc)
Definition: encoding.h:227
rb_enc_mbminlen
#define rb_enc_mbminlen(enc)
Definition: encoding.h:171
rb_check_symbol_cstr
VALUE rb_check_symbol_cstr(const char *ptr, long len, rb_encoding *enc)
Definition: symbol.c:1166
rb_enc_strlen
long rb_enc_strlen(const char *, const char *, rb_encoding *)
Definition: string.c:1887
ENC_CODERANGE_BROKEN
#define ENC_CODERANGE_BROKEN
Definition: encoding.h:95
rb_str_coderange_scan_restartable
long rb_str_coderange_scan_restartable(const char *, const char *, rb_encoding *, int *)
Definition: string.c:617
rb_enc_right_char_head
#define rb_enc_right_char_head(s, p, e, enc)
Definition: encoding.h:214
ENC_CODERANGE_SET
#define ENC_CODERANGE_SET(obj, cr)
Definition: encoding.h:99
rb_enc_nth
char * rb_enc_nth(const char *, const char *, long, rb_encoding *)
Definition: string.c:2538
FIXABLE
#define FIXABLE
Definition: fixnum.h:25
config.h
Thin wrapper to ruby/config.h.
ruby_debug
#define ruby_debug
Definition: error.h:69
ruby_verbose
#define ruby_verbose
Definition: error.h:68
INTEGER_PACK_BIG_ENDIAN
#define INTEGER_PACK_BIG_ENDIAN
Definition: bignum.h:94
INTEGER_PACK_2COMP
#define INTEGER_PACK_2COMP
Definition: bignum.h:85
rb_rational_num
VALUE rb_rational_num(VALUE rat)
Definition: rational.c:1977
rb_rational_den
VALUE rb_rational_den(VALUE rat)
Definition: rational.c:1983
rb_str_resize
VALUE rb_str_resize(VALUE, long)
Definition: string.c:2859
rb_str_new
#define rb_str_new(str, len)
Definition: string.h:213
rb_str_set_len
void rb_str_set_len(VALUE, long)
Definition: string.c:2842
rb_str_modify
void rb_str_modify(VALUE)
Definition: string.c:2262
rb_str_buf_new
VALUE rb_str_buf_new(long)
Definition: string.c:1398
rb_check_string_type
VALUE rb_check_string_type(VALUE)
Definition: string.c:2462
rb_str_inspect
VALUE rb_str_inspect(VALUE)
Definition: string.c:6199
rb_obj_as_string
VALUE rb_obj_as_string(VALUE)
Definition: string.c:1529
rb_str_capacity
size_t rb_str_capacity(VALUE)
Definition: string.c:773
rb_sym2str
VALUE rb_sym2str(VALUE)
Definition: symbol.c:927
ffs
int ffs(int)
Definition: ffs.c:6
re.h
NUM2INT
#define NUM2INT
Definition: int.h:44
hash.h
Internal header for Hash.
numeric.h
Internal header for Numeric.
rb_int2str
VALUE rb_int2str(VALUE num, int base)
Definition: numeric.c:3549
rb_int_idiv
VALUE rb_int_idiv(VALUE x, VALUE y)
Definition: numeric.c:3830
rb_int_plus
VALUE rb_int_plus(VALUE x, VALUE y)
Definition: numeric.c:3597
rb_int_mul
VALUE rb_int_mul(VALUE x, VALUE y)
Definition: numeric.c:3686
object.h
Internal header for Object.
rb_str_symname_p
int rb_str_symname_p(VALUE)
Definition: string.c:11027
rb_str_tmp_frozen_acquire
VALUE rb_str_tmp_frozen_acquire(VALUE str)
Definition: string.c:1287
rb_str_tmp_frozen_release
void rb_str_tmp_frozen_release(VALUE str, VALUE tmp)
Definition: string.c:1294
rb_sym_intern
VALUE rb_sym_intern(const char *ptr, long len, rb_encoding *enc)
Definition: symbol.c:1191
util.h
Internal header corresponding util.c.
internal.h
PRIuSIZE
#define PRIuSIZE
Definition: inttypes.h:127
size
voidpf void uLong size
Definition: ioapi.h:138
long
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque
int
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
buf
voidpf void * buf
Definition: ioapi.h:138
isinf
int isinf(double n)
Definition: isinf.c:56
CHAR_BIT
#define CHAR_BIT
Definition: limits.h:44
LONG_MAX
#define LONG_MAX
Definition: limits.h:36
INT2FIX
#define INT2FIX
Definition: long.h:48
LONG2FIX
#define LONG2FIX
Definition: long.h:49
rb_long2int
#define rb_long2int
Definition: long.h:62
FIX2LONG
#define FIX2LONG
Definition: long.h:46
math.h
Internal header for Math.
MEMCPY
#define MEMCPY(p1, p2, type, n)
Definition: memory.h:129
RB_GC_GUARD
#define RB_GC_GUARD(v)
Definition: memory.h:91
name
const char * name
Definition: nkf.c:208
TRUE
#define TRUE
Definition: nkf.h:175
rb_enc_raise
#define rb_enc_raise
Definition: parser.c:21
RBASIC
#define RBASIC(obj)
Definition: rbasic.h:34
NULL
#define NULL
Definition: regenc.h:69
get_num
#define get_num(num)
Definition: ripper.c:643
StringValue
#define StringValue(v)
Definition: rstring.h:50
RSTRING_GETMEM
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Definition: rstring.h:211
StringValueCStr
#define StringValueCStr(v)
Definition: rstring.h:52
argc
int argc
Definition: ruby.c:240
argv
char ** argv
Definition: ruby.c:241
RB_INTEGER_TYPE_P
#define RB_INTEGER_TYPE_P(obj)
Definition: ruby_missing.h:15
sanitizers.h
Internal header for ASAN / MSAN / etc.
Qundef
#define Qundef
Definition: special_consts.h:53
RTEST
#define RTEST
Definition: special_consts.h:42
Qnil
#define Qnil
Definition: special_consts.h:51
NIL_P
#define NIL_P
Definition: special_consts.h:46
FIXNUM_P
#define FIXNUM_P
Definition: special_consts.h:44
rb_str_catf
VALUE rb_str_catf(VALUE str, const char *format,...)
Definition: sprintf.c:1243
rb_sprintf
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1203
rb_str_format
VALUE rb_str_format(int argc, const VALUE *argv, VALUE fmt)
Definition: sprintf.c:214
rb_enc_vsprintf
VALUE rb_enc_vsprintf(rb_encoding *enc, const char *fmt, va_list ap)
Definition: sprintf.c:1151
rb_f_sprintf
VALUE rb_f_sprintf(int argc, const VALUE *argv)
Definition: sprintf.c:208
GETASTER
#define GETASTER(val)
Definition: sprintf.c:122
rb_enc_sprintf
VALUE rb_enc_sprintf(rb_encoding *enc, const char *format,...)
Definition: sprintf.c:1184
FILL
#define FILL(c, l)
Definition: sprintf.c:89
CHECK_FOR_FLAGS
#define CHECK_FOR_FLAGS(f)
FZERO
#define FZERO
Definition: sprintf.c:62
rb_str_vcatf
VALUE rb_str_vcatf(VALUE str, const char *fmt, va_list ap)
Definition: sprintf.c:1216
GETNUM
#define GETNUM(n, val)
Definition: sprintf.c:118
ruby_vsnprintf
int ruby_vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
Definition: sprintf.c:1010
FPREC0
#define FPREC0
Definition: sprintf.c:66
rb_vsprintf
VALUE rb_vsprintf(const char *fmt, va_list ap)
Definition: sprintf.c:1197
FWIDTH
#define FWIDTH
Definition: sprintf.c:64
GETNTHARG
#define GETNTHARG(nth)
Definition: sprintf.c:111
f
#define f
GETPOSARG
#define GETPOSARG(n)
Definition: sprintf.c:107
PUSH
#define PUSH(s, l)
Definition: sprintf.c:79
CHECK_FOR_WIDTH
#define CHECK_FOR_WIDTH(f)
CHECKNAMEARG
#define CHECKNAMEARG(name, len, enc)
Definition: sprintf.c:114
FMINUS
#define FMINUS
Definition: sprintf.c:60
GETARG
#define GETARG()
Definition: sprintf.c:100
CHECK
#define CHECK(l)
Definition: sprintf.c:68
LITERAL
#define LITERAL(str)
FSPACE
#define FSPACE
Definition: sprintf.c:63
FNONE
#define FNONE
Definition: sprintf.c:58
PUSH_
#define PUSH_(s, l)
Definition: sprintf.c:84
FPLUS
#define FPLUS
Definition: sprintf.c:61
FILL_
#define FILL_(c, l)
Definition: sprintf.c:95
FPREC
#define FPREC
Definition: sprintf.c:65
FSHARP
#define FSHARP
Definition: sprintf.c:59
lower_hexdigits
#define lower_hexdigits
Definition: sprintf.c:995
BIT_DIGITS
#define BIT_DIGITS(N)
Definition: sprintf.c:35
ruby_snprintf
int ruby_snprintf(char *str, size_t n, char const *fmt,...)
Definition: sprintf.c:1037
stdarg.h
Defines old _.
strlen
size_t strlen(const char *)
__siov
Definition: vsnprintf.c:226
__siov::iov_len
size_t iov_len
Definition: vsnprintf.c:228
__siov::iov_base
const void * iov_base
Definition: vsnprintf.c:227
__suio
Definition: vsnprintf.c:230
__suio::uio_iov
struct __siov * uio_iov
Definition: vsnprintf.c:231
__suio::uio_resid
size_t uio_resid
Definition: vsnprintf.c:233
OnigEncodingTypeST
Definition: onigmo.h:160
rb_printf_buffer_extra
Definition: sprintf.c:1051
rb_printf_buffer_extra::value
volatile VALUE value
Definition: sprintf.c:1053
rb_printf_buffer_extra::base
rb_printf_buffer base
Definition: sprintf.c:1052
t
#define t
Definition: symbol.c:253
VALUE
unsigned long VALUE
Definition: value.h:38
SIGNED_VALUE
#define SIGNED_VALUE
Definition: value.h:40
TYPE
#define TYPE(_)
Definition: value_type.h:105
T_STRING
#define T_STRING
Definition: value_type.h:77
T_FLOAT
#define T_FLOAT
Definition: value_type.h:63
T_BIGNUM
#define T_BIGNUM
Definition: value_type.h:56
T_FIXNUM
#define T_FIXNUM
Definition: value_type.h:62
T_RATIONAL
#define T_RATIONAL
Definition: value_type.h:75
T_CLASS
#define T_CLASS
Definition: value_type.h:57
SYMBOL_P
#define SYMBOL_P
Definition: value_type.h:87
vsnprintf.c
EOF
#define EOF
Definition: vsnprintf.c:203
__SSTR
#define __SSTR
Definition: vsnprintf.c:196
__SWR
#define __SWR
Definition: vsnprintf.c:189
isnan
#define isnan(x)
Definition: win32.h:346
if
if((ID)(DISPID) nameid !=nameid)
Definition: win32ole.c:357