Ruby 2.1.5p273(2014-11-13revision48405)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

sprintf.c
Go to the documentation of this file.
1 /**********************************************************************
2
3 sprintf.c -
4
5 $Author: nagachika $
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/ruby.h"
15 #include "ruby/re.h"
16 #include "ruby/encoding.h"
17 #include "internal.h"
18 #include <math.h>
19 #include <stdarg.h>
20
21 #ifdef HAVE_IEEEFP_H
22 #include <ieeefp.h>
23 #endif
24
25 #define BIT_DIGITS(N) (((N)*146)/485 + 1) /* log2(10) =~ 146/485 */
26
27 extern const char ruby_digitmap[];
28
29 static void fmt_setup(char*,size_t,int,int,int,int);
30
31 static char
32 sign_bits(int base, const char *p)
33 {
34 char c = '.';
35
36 switch (base) {
37 case 16:
38 if (*p == 'X') c = 'F';
39 else c = 'f';
40 break;
41 case 8:
42 c = '7'; break;
43 case 2:
44 c = '1'; break;
45 }
46 return c;
47 }
48
49 #define FNONE 0
50 #define FSHARP 1
51 #define FMINUS 2
52 #define FPLUS 4
53 #define FZERO 8
54 #define FSPACE 16
55 #define FWIDTH 32
56 #define FPREC 64
57 #define FPREC0 128
58
59 #define CHECK(l) do {\
60 int cr = ENC_CODERANGE(result);\
61 while (blen + (l) >= bsiz) {\
62 bsiz*=2;\
63 }\
64 rb_str_resize(result, bsiz);\
65 ENC_CODERANGE_SET(result, cr);\
66 buf = RSTRING_PTR(result);\
67 } while (0)
68
69 #define PUSH(s, l) do { \
70 CHECK(l);\
71 memcpy(&buf[blen], (s), (l));\
72 blen += (l);\
73 } while (0)
74
75 #define FILL(c, l) do { \
76 CHECK(l);\
77 memset(&buf[blen], (c), (l));\
78 blen += (l);\
79 } while (0)
80
81 #define GETARG() (nextvalue != Qundef ? nextvalue : \
82 GETNEXTARG())
83
84 #define GETNEXTARG() ( \
85 posarg == -1 ? \
86 (rb_raise(rb_eArgError, "unnumbered(%d) mixed with numbered", nextarg), 0) : \
87 posarg == -2 ? \
88 (rb_raise(rb_eArgError, "unnumbered(%d) mixed with named", nextarg), 0) : \
89 (posarg = nextarg++, GETNTHARG(posarg)))
90
91 #define GETPOSARG(n) (posarg > 0 ? \
92 (rb_raise(rb_eArgError, "numbered(%d) after unnumbered(%d)", (n), posarg), 0) : \
93 posarg == -2 ? \
94 (rb_raise(rb_eArgError, "numbered(%d) after named", (n)), 0) : \
95 (((n) < 1) ? (rb_raise(rb_eArgError, "invalid index - %d$", (n)), 0) : \
96 (posarg = -1, GETNTHARG(n))))
97
98 #define GETNTHARG(nth) \
99 (((nth) >= argc) ? (rb_raise(rb_eArgError, "too few arguments"), 0) : argv[(nth)])
100
101 #define GETNAMEARG(id, name, len, enc) ( \
102 posarg > 0 ? \
103 (rb_enc_raise((enc), rb_eArgError, "named%.*s after unnumbered(%d)", (len), (name), posarg), 0) : \
104 posarg == -1 ? \
105 (rb_enc_raise((enc), rb_eArgError, "named%.*s after numbered", (len), (name)), 0) : \
106 (posarg = -2, rb_hash_lookup2(get_hash(&hash, argc, argv), (id), Qundef)))
107
108 #define GETNUM(n, val) \
109 for (; p < end && rb_enc_isdigit(*p, enc); p++) { \
110 int next_n = (n); \
111 if (MUL_OVERFLOW_INT_P(10, next_n)) \
112 rb_raise(rb_eArgError, #val " too big"); \
113 next_n *= 10; \
114 if (INT_MAX - (*p - '0') < next_n) \
115 rb_raise(rb_eArgError, #val " too big"); \
116 next_n += *p - '0'; \
117 (n) = next_n; \
118 } \
119 if (p >= end) { \
120 rb_raise(rb_eArgError, "malformed format string - %%*[0-9]"); \
121 }
122
123 #define GETASTER(val) do { \
124 t = p++; \
125 n = 0; \
126 GETNUM(n, (val)); \
127 if (*p == '$') { \
128 tmp = GETPOSARG(n); \
129 } \
130 else { \
131 tmp = GETNEXTARG(); \
132 p = t; \
133 } \
134 (val) = NUM2INT(tmp); \
135 } while (0)
136
137 static VALUE
138 get_hash(volatile VALUE *hash, int argc, const VALUE *argv)
139 {
140 VALUE tmp;
141
142 if (*hash != Qundef) return *hash;
143 if (argc != 2) {
144 rb_raise(rb_eArgError, "one hash required");
145 }
146 tmp = rb_check_hash_type(argv[1]);
147 if (NIL_P(tmp)) {
148 rb_raise(rb_eArgError, "one hash required");
149 }
150 return (*hash = tmp);
151 }
152
153 /*
154 * call-seq:
155 * format(format_string [, arguments...] ) -> string
156 * sprintf(format_string [, arguments...] ) -> string
157 *
158 * Returns the string resulting from applying <i>format_string</i> to
159 * any additional arguments. Within the format string, any characters
160 * other than format sequences are copied to the result.
161 *
162 * The syntax of a format sequence is follows.
163 *
164 * %[flags][width][.precision]type
165 *
166 * A format
167 * sequence consists of a percent sign, followed by optional flags,
168 * width, and precision indicators, then terminated with a field type
169 * character. The field type controls how the corresponding
170 * <code>sprintf</code> argument is to be interpreted, while the flags
171 * modify that interpretation.
172 *
173 * The field type characters are:
174 *
175 * Field | Integer Format
176 * ------+--------------------------------------------------------------
177 * b | Convert argument as a binary number.
178 * | Negative numbers will be displayed as a two's complement
179 * | prefixed with `..1'.
180 * B | Equivalent to `b', but uses an uppercase 0B for prefix
181 * | in the alternative format by #.
182 * d | Convert argument as a decimal number.
183 * i | Identical to `d'.
184 * o | Convert argument as an octal number.
185 * | Negative numbers will be displayed as a two's complement
186 * | prefixed with `..7'.
187 * u | Identical to `d'.
188 * x | Convert argument as a hexadecimal number.
189 * | Negative numbers will be displayed as a two's complement
190 * | prefixed with `..f' (representing an infinite string of
191 * | leading 'ff's).
192 * X | Equivalent to `x', but uses uppercase letters.
193 *
194 * Field | Float Format
195 * ------+--------------------------------------------------------------
196 * e | Convert floating point argument into exponential notation
197 * | with one digit before the decimal point as [-]d.dddddde[+-]dd.
198 * | The precision specifies the number of digits after the decimal
199 * | point (defaulting to six).
200 * E | Equivalent to `e', but uses an uppercase E to indicate
201 * | the exponent.
202 * f | Convert floating point argument as [-]ddd.dddddd,
203 * | where the precision specifies the number of digits after
204 * | the decimal point.
205 * g | Convert a floating point number using exponential form
206 * | if the exponent is less than -4 or greater than or
207 * | equal to the precision, or in dd.dddd form otherwise.
208 * | The precision specifies the number of significant digits.
209 * G | Equivalent to `g', but use an uppercase `E' in exponent form.
210 * a | Convert floating point argument as [-]0xh.hhhhp[+-]dd,
211 * | which is consisted from optional sign, "0x", fraction part
212 * | as hexadecimal, "p", and exponential part as decimal.
213 * A | Equivalent to `a', but use uppercase `X' and `P'.
214 *
215 * Field | Other Format
216 * ------+--------------------------------------------------------------
217 * c | Argument is the numeric code for a single character or
218 * | a single character string itself.
219 * p | The valuing of argument.inspect.
220 * s | Argument is a string to be substituted. If the format
221 * | sequence contains a precision, at most that many characters
222 * | will be copied.
223 * % | A percent sign itself will be displayed. No argument taken.
224 *
225 * The flags modifies the behavior of the formats.
226 * The flag characters are:
227 *
228 * Flag | Applies to | Meaning
229 * ---------+---------------+-----------------------------------------
230 * space | bBdiouxX | Leave a space at the start of
231 * | aAeEfgG | non-negative numbers.
232 * | (numeric fmt) | For `o', `x', `X', `b' and `B', use
233 * | | a minus sign with absolute value for
234 * | | negative values.
235 * ---------+---------------+-----------------------------------------
236 * (digit)$ | all | Specifies the absolute argument number
237 * | | for this field. Absolute and relative
238 * | | argument numbers cannot be mixed in a
239 * | | sprintf string.
240 * ---------+---------------+-----------------------------------------
241 * # | bBoxX | Use an alternative format.
242 * | aAeEfgG | For the conversions `o', increase the precision
243 * | | until the first digit will be `0' if
244 * | | it is not formatted as complements.
245 * | | For the conversions `x', `X', `b' and `B'
246 * | | on non-zero, prefix the result with ``0x'',
247 * | | ``0X'', ``0b'' and ``0B'', respectively.
248 * | | For `a', `A', `e', `E', `f', `g', and 'G',
249 * | | force a decimal point to be added,
250 * | | even if no digits follow.
251 * | | For `g' and 'G', do not remove trailing zeros.
252 * ---------+---------------+-----------------------------------------
253 * + | bBdiouxX | Add a leading plus sign to non-negative
254 * | aAeEfgG | numbers.
255 * | (numeric fmt) | For `o', `x', `X', `b' and `B', use
256 * | | a minus sign with absolute value for
257 * | | negative values.
258 * ---------+---------------+-----------------------------------------
259 * - | all | Left-justify the result of this conversion.
260 * ---------+---------------+-----------------------------------------
261 * 0 (zero) | bBdiouxX | Pad with zeros, not spaces.
262 * | aAeEfgG | For `o', `x', `X', `b' and `B', radix-1
263 * | (numeric fmt) | is used for negative numbers formatted as
264 * | | complements.
265 * ---------+---------------+-----------------------------------------
266 * * | all | Use the next argument as the field width.
267 * | | If negative, left-justify the result. If the
268 * | | asterisk is followed by a number and a dollar
269 * | | sign, use the indicated argument as the width.
270 *
271 * Examples of flags:
272 *
273 * # `+' and space flag specifies the sign of non-negative numbers.
274 * sprintf("%d", 123) #=> "123"
275 * sprintf("%+d", 123) #=> "+123"
276 * sprintf("% d", 123) #=> " 123"
277 *
278 * # `#' flag for `o' increases number of digits to show `0'.
279 * # `+' and space flag changes format of negative numbers.
280 * sprintf("%o", 123) #=> "173"
281 * sprintf("%#o", 123) #=> "0173"
282 * sprintf("%+o", -123) #=> "-173"
283 * sprintf("%o", -123) #=> "..7605"
284 * sprintf("%#o", -123) #=> "..7605"
285 *
286 * # `#' flag for `x' add a prefix `0x' for non-zero numbers.
287 * # `+' and space flag disables complements for negative numbers.
288 * sprintf("%x", 123) #=> "7b"
289 * sprintf("%#x", 123) #=> "0x7b"
290 * sprintf("%+x", -123) #=> "-7b"
291 * sprintf("%x", -123) #=> "..f85"
292 * sprintf("%#x", -123) #=> "0x..f85"
293 * sprintf("%#x", 0) #=> "0"
294 *
295 * # `#' for `X' uses the prefix `0X'.
296 * sprintf("%X", 123) #=> "7B"
297 * sprintf("%#X", 123) #=> "0X7B"
298 *
299 * # `#' flag for `b' add a prefix `0b' for non-zero numbers.
300 * # `+' and space flag disables complements for negative numbers.
301 * sprintf("%b", 123) #=> "1111011"
302 * sprintf("%#b", 123) #=> "0b1111011"
303 * sprintf("%+b", -123) #=> "-1111011"
304 * sprintf("%b", -123) #=> "..10000101"
305 * sprintf("%#b", -123) #=> "0b..10000101"
306 * sprintf("%#b", 0) #=> "0"
307 *
308 * # `#' for `B' uses the prefix `0B'.
309 * sprintf("%B", 123) #=> "1111011"
310 * sprintf("%#B", 123) #=> "0B1111011"
311 *
312 * # `#' for `e' forces to show the decimal point.
313 * sprintf("%.0e", 1) #=> "1e+00"
314 * sprintf("%#.0e", 1) #=> "1.e+00"
315 *
316 * # `#' for `f' forces to show the decimal point.
317 * sprintf("%.0f", 1234) #=> "1234"
318 * sprintf("%#.0f", 1234) #=> "1234."
319 *
320 * # `#' for `g' forces to show the decimal point.
321 * # It also disables stripping lowest zeros.
322 * sprintf("%g", 123.4) #=> "123.4"
323 * sprintf("%#g", 123.4) #=> "123.400"
324 * sprintf("%g", 123456) #=> "123456"
325 * sprintf("%#g", 123456) #=> "123456."
326 *
327 * The field width is an optional integer, followed optionally by a
328 * period and a precision. The width specifies the minimum number of
329 * characters that will be written to the result for this field.
330 *
331 * Examples of width:
332 *
333 * # padding is done by spaces, width=20
334 * # 0 or radix-1. <------------------>
335 * sprintf("%20d", 123) #=> " 123"
336 * sprintf("%+20d", 123) #=> " +123"
337 * sprintf("%020d", 123) #=> "00000000000000000123"
338 * sprintf("%+020d", 123) #=> "+0000000000000000123"
339 * sprintf("% 020d", 123) #=> " 0000000000000000123"
340 * sprintf("%-20d", 123) #=> "123 "
341 * sprintf("%-+20d", 123) #=> "+123 "
342 * sprintf("%- 20d", 123) #=> " 123 "
343 * sprintf("%020x", -123) #=> "..ffffffffffffffff85"
344 *
345 * For
346 * numeric fields, the precision controls the number of decimal places
347 * displayed. For string fields, the precision determines the maximum
348 * number of characters to be copied from the string. (Thus, the format
349 * sequence <code>%10.10s</code> will always contribute exactly ten
350 * characters to the result.)
351 *
352 * Examples of precisions:
353 *
354 * # precision for `d', 'o', 'x' and 'b' is
355 * # minimum number of digits <------>
356 * sprintf("%20.8d", 123) #=> " 00000123"
357 * sprintf("%20.8o", 123) #=> " 00000173"
358 * sprintf("%20.8x", 123) #=> " 0000007b"
359 * sprintf("%20.8b", 123) #=> " 01111011"
360 * sprintf("%20.8d", -123) #=> " -00000123"
361 * sprintf("%20.8o", -123) #=> " ..777605"
362 * sprintf("%20.8x", -123) #=> " ..ffff85"
363 * sprintf("%20.8b", -11) #=> " ..110101"
364 *
365 * # "0x" and "0b" for `#x' and `#b' is not counted for
366 * # precision but "0" for `#o' is counted. <------>
367 * sprintf("%#20.8d", 123) #=> " 00000123"
368 * sprintf("%#20.8o", 123) #=> " 00000173"
369 * sprintf("%#20.8x", 123) #=> " 0x0000007b"
370 * sprintf("%#20.8b", 123) #=> " 0b01111011"
371 * sprintf("%#20.8d", -123) #=> " -00000123"
372 * sprintf("%#20.8o", -123) #=> " ..777605"
373 * sprintf("%#20.8x", -123) #=> " 0x..ffff85"
374 * sprintf("%#20.8b", -11) #=> " 0b..110101"
375 *
376 * # precision for `e' is number of
377 * # digits after the decimal point <------>
378 * sprintf("%20.8e", 1234.56789) #=> " 1.23456789e+03"
379 *
380 * # precision for `f' is number of
381 * # digits after the decimal point <------>
382 * sprintf("%20.8f", 1234.56789) #=> " 1234.56789000"
383 *
384 * # precision for `g' is number of
385 * # significant digits <------->
386 * sprintf("%20.8g", 1234.56789) #=> " 1234.5679"
387 *
388 * # <------->
389 * sprintf("%20.8g", 123456789) #=> " 1.2345679e+08"
390 *
391 * # precision for `s' is
392 * # maximum number of characters <------>
393 * sprintf("%20.8s", "string test") #=> " string t"
394 *
395 * Examples:
396 *
397 * sprintf("%d %04x", 123, 123) #=> "123 007b"
398 * sprintf("%08b '%4s'", 123, 123) #=> "01111011 ' 123'"
399 * sprintf("%1$*2$s %2$d %1$s", "hello", 8) #=> " hello 8 hello"
400 * sprintf("%1$*2$s %2$d", "hello", -8) #=> "hello -8"
401 * sprintf("%+g:% g:%-g", 1.23, 1.23, 1.23) #=> "+1.23: 1.23:1.23"
402 * sprintf("%u", -123) #=> "-123"
403 *
404 * For more complex formatting, Ruby supports a reference by name.
405 * %<name>s style uses format style, but %{name} style doesn't.
406 *
407 * Examples:
408 * sprintf("%<foo>d : %<bar>f", { :foo => 1, :bar => 2 })
409 * #=> 1 : 2.000000
410 * sprintf("%{foo}f", { :foo => 1 })
411 * # => "1f"
412 */
413
414 VALUE
415 rb_f_sprintf(int argc, const VALUE *argv)
416 {
417 return rb_str_format(argc - 1, argv + 1, GETNTHARG(0));
418 }
419
420 VALUE
421 rb_str_format(int argc, const VALUE *argv, VALUE fmt)
422 {
423 rb_encoding *enc;
424 const char *p, *end;
425 char *buf;
426 long blen, bsiz;
427 VALUE result;
428
429 long scanned = 0;
430 int coderange = ENC_CODERANGE_7BIT;
431 int width, prec, flags = FNONE;
432 int nextarg = 1;
433 int posarg = 0;
434 int tainted = 0;
435 VALUE nextvalue;
436 VALUE tmp;
437 VALUE str;
438 volatile VALUE hash = Qundef;
439
440 #define CHECK_FOR_WIDTH(f) \
441 if ((f) & FWIDTH) { \
442 rb_raise(rb_eArgError, "width given twice"); \
443 } \
444 if ((f) & FPREC0) { \
445 rb_raise(rb_eArgError, "width after precision"); \
446 }
447 #define CHECK_FOR_FLAGS(f) \
448 if ((f) & FWIDTH) { \
449 rb_raise(rb_eArgError, "flag after width"); \
450 } \
451 if ((f) & FPREC0) { \
452 rb_raise(rb_eArgError, "flag after precision"); \
453 }
454
455 ++argc;
456 --argv;
457 if (OBJ_TAINTED(fmt)) tainted = 1;
458 StringValue(fmt);
459 enc = rb_enc_get(fmt);
460 fmt = rb_str_new4(fmt);
461 p = RSTRING_PTR(fmt);
462 end = p + RSTRING_LEN(fmt);
463 blen = 0;
464 bsiz = 120;
465 result = rb_str_buf_new(bsiz);
466 rb_enc_copy(result, fmt);
467 buf = RSTRING_PTR(result);
468 memset(buf, 0, bsiz);
469 ENC_CODERANGE_SET(result, coderange);
470
471 for (; p < end; p++) {
472 const char *t;
473 int n;
474 ID id = 0;
475
476 for (t = p; t < end && *t != '%'; t++) ;
477 PUSH(p, t - p);
478 if (coderange != ENC_CODERANGE_BROKEN && scanned < blen) {
479 scanned += rb_str_coderange_scan_restartable(buf+scanned, buf+blen, enc, &coderange);
480 ENC_CODERANGE_SET(result, coderange);
481 }
482 if (t >= end) {
483 /* end of fmt string */
484 goto sprint_exit;
485 }
486 p = t + 1; /* skip `%' */
487
488 width = prec = -1;
489 nextvalue = Qundef;
490 retry:
491 switch (*p) {
492 default:
493 if (rb_enc_isprint(*p, enc))
494 rb_raise(rb_eArgError, "malformed format string - %%%c", *p);
495 else
496 rb_raise(rb_eArgError, "malformed format string");
497 break;
498
499 case ' ':
500 CHECK_FOR_FLAGS(flags);
501 flags |= FSPACE;
502 p++;
503 goto retry;
504
505 case '#':
506 CHECK_FOR_FLAGS(flags);
507 flags |= FSHARP;
508 p++;
509 goto retry;
510
511 case '+':
512 CHECK_FOR_FLAGS(flags);
513 flags |= FPLUS;
514 p++;
515 goto retry;
516
517 case '-':
518 CHECK_FOR_FLAGS(flags);
519 flags |= FMINUS;
520 p++;
521 goto retry;
522
523 case '0':
524 CHECK_FOR_FLAGS(flags);
525 flags |= FZERO;
526 p++;
527 goto retry;
528
529 case '1': case '2': case '3': case '4':
530 case '5': case '6': case '7': case '8': case '9':
531 n = 0;
532 GETNUM(n, width);
533 if (*p == '$') {
534 if (nextvalue != Qundef) {
535 rb_raise(rb_eArgError, "value given twice - %d$", n);
536 }
537 nextvalue = GETPOSARG(n);
538 p++;
539 goto retry;
540 }
541 CHECK_FOR_WIDTH(flags);
542 width = n;
543 flags |= FWIDTH;
544 goto retry;
545
546 case '<':
547 case '{':
548 {
549 const char *start = p;
550 char term = (*p == '<') ? '>' : '}';
551 int len;
552
553 for (; p < end && *p != term; ) {
554 p += rb_enc_mbclen(p, end, enc);
555 }
556 if (p >= end) {
557 rb_raise(rb_eArgError, "malformed name - unmatched parenthesis");
558 }
559 #if SIZEOF_INT < SIZEOF_SIZE_T
560 if ((size_t)(p - start) >= INT_MAX) {
561 const int message_limit = 20;
562 len = (int)(rb_enc_right_char_head(start, start + message_limit, p, enc) - start);
563 rb_enc_raise(enc, rb_eArgError,
564 "too long name (%"PRIdSIZE" bytes) - %.*s...%c",
565 (size_t)(p - start - 2), len, start, term);
566 }
567 #endif
568 len = (int)(p - start + 1); /* including parenthesis */
569 if (id) {
570 rb_enc_raise(enc, rb_eArgError, "named%.*s after <%s>",
571 len, start, rb_id2name(id));
572 }
573 nextvalue = GETNAMEARG((id = rb_check_id_cstr(start + 1,
574 len - 2 /* without parenthesis */,
575 enc),
576 ID2SYM(id)),
577 start, len, enc);
578 if (nextvalue == Qundef) {
579 rb_enc_raise(enc, rb_eKeyError, "key%.*s not found", len, start);
580 }
581 if (term == '}') goto format_s;
582 p++;
583 goto retry;
584 }
585
586 case '*':
587 CHECK_FOR_WIDTH(flags);
588 flags |= FWIDTH;
589 GETASTER(width);
590 if (width < 0) {
591 flags |= FMINUS;
592 width = -width;
593 }
594 p++;
595 goto retry;
596
597 case '.':
598 if (flags & FPREC0) {
599 rb_raise(rb_eArgError, "precision given twice");
600 }
601 flags |= FPREC|FPREC0;
602
603 prec = 0;
604 p++;
605 if (*p == '*') {
606 GETASTER(prec);
607 if (prec < 0) { /* ignore negative precision */
608 flags &= ~FPREC;
609 }
610 p++;
611 goto retry;
612 }
613
614 GETNUM(prec, precision);
615 goto retry;
616
617 case '\n':
618 case '\0':
619 p--;
620 case '%':
621 if (flags != FNONE) {
622 rb_raise(rb_eArgError, "invalid format character - %%");
623 }
624 PUSH("%", 1);
625 break;
626
627 case 'c':
628 {
629 VALUE val = GETARG();
630 VALUE tmp;
631 unsigned int c;
632 int n;
633
634 tmp = rb_check_string_type(val);
635 if (!NIL_P(tmp)) {
636 if (rb_enc_strlen(RSTRING_PTR(tmp),RSTRING_END(tmp),enc) != 1) {
637 rb_raise(rb_eArgError, "%%c requires a character");
638 }
639 c = rb_enc_codepoint_len(RSTRING_PTR(tmp), RSTRING_END(tmp), &n, enc);
640 RB_GC_GUARD(tmp);
641 }
642 else {
643 c = NUM2INT(val);
644 n = rb_enc_codelen(c, enc);
645 }
646 if (n <= 0) {
647 rb_raise(rb_eArgError, "invalid character");
648 }
649 if (!(flags & FWIDTH)) {
650 CHECK(n);
651 rb_enc_mbcput(c, &buf[blen], enc);
652 blen += n;
653 }
654 else if ((flags & FMINUS)) {
655 CHECK(n);
656 rb_enc_mbcput(c, &buf[blen], enc);
657 blen += n;
658 FILL(' ', width-1);
659 }
660 else {
661 FILL(' ', width-1);
662 CHECK(n);
663 rb_enc_mbcput(c, &buf[blen], enc);
664 blen += n;
665 }
666 }
667 break;
668
669 case 's':
670 case 'p':
671 format_s:
672 {
673 VALUE arg = GETARG();
674 long len, slen;
675
676 if (*p == 'p') arg = rb_inspect(arg);
677 str = rb_obj_as_string(arg);
678 if (OBJ_TAINTED(str)) tainted = 1;
679 len = RSTRING_LEN(str);
680 rb_str_set_len(result, blen);
681 if (coderange != ENC_CODERANGE_BROKEN && scanned < blen) {
682 int cr = coderange;
683 scanned += rb_str_coderange_scan_restartable(buf+scanned, buf+blen, enc, &cr);
684 ENC_CODERANGE_SET(result,
685 (cr == ENC_CODERANGE_UNKNOWN ?
686 ENC_CODERANGE_BROKEN : (coderange = cr)));
687 }
688 enc = rb_enc_check(result, str);
689 if (flags&(FPREC|FWIDTH)) {
690 slen = rb_enc_strlen(RSTRING_PTR(str),RSTRING_END(str),enc);
691 if (slen < 0) {
692 rb_raise(rb_eArgError, "invalid mbstring sequence");
693 }
694 if ((flags&FPREC) && (prec < slen)) {
695 char *p = rb_enc_nth(RSTRING_PTR(str), RSTRING_END(str),
696 prec, enc);
697 slen = prec;
698 len = p - RSTRING_PTR(str);
699 }
700 /* need to adjust multi-byte string pos */
701 if ((flags&FWIDTH) && (width > slen)) {
702 width -= (int)slen;
703 if (!(flags&FMINUS)) {
704 CHECK(width);
705 while (width--) {
706 buf[blen++] = ' ';
707 }
708 }
709 CHECK(len);
710 memcpy(&buf[blen], RSTRING_PTR(str), len);
711 RB_GC_GUARD(str);
712 blen += len;
713 if (flags&FMINUS) {
714 CHECK(width);
715 while (width--) {
716 buf[blen++] = ' ';
717 }
718 }
719 rb_enc_associate(result, enc);
720 break;
721 }
722 }
723 PUSH(RSTRING_PTR(str), len);
724 RB_GC_GUARD(str);
725 rb_enc_associate(result, enc);
726 }
727 break;
728
729 case 'd':
730 case 'i':
731 case 'o':
732 case 'x':
733 case 'X':
734 case 'b':
735 case 'B':
736 case 'u':
737 {
738 volatile VALUE val = GETARG();
739 int valsign;
740 char nbuf[64], *s;
741 const char *prefix = 0;
742 int sign = 0, dots = 0;
743 char sc = 0;
744 long v = 0;
745 int base, bignum = 0;
746 int len;
747
748 switch (*p) {
749 case 'd':
750 case 'i':
751 case 'u':
752 sign = 1; break;
753 case 'o':
754 case 'x':
755 case 'X':
756 case 'b':
757 case 'B':
758 if (flags&(FPLUS|FSPACE)) sign = 1;
759 break;
760 }
761 if (flags & FSHARP) {
762 switch (*p) {
763 case 'o':
764 prefix = "0"; break;
765 case 'x':
766 prefix = "0x"; break;
767 case 'X':
768 prefix = "0X"; break;
769 case 'b':
770 prefix = "0b"; break;
771 case 'B':
772 prefix = "0B"; break;
773 }
774 }
775
776 bin_retry:
777 switch (TYPE(val)) {
778 case T_FLOAT:
779 if (FIXABLE(RFLOAT_VALUE(val))) {
780 val = LONG2FIX((long)RFLOAT_VALUE(val));
781 goto bin_retry;
782 }
783 val = rb_dbl2big(RFLOAT_VALUE(val));
784 if (FIXNUM_P(val)) goto bin_retry;
785 bignum = 1;
786 break;
787 case T_STRING:
788 val = rb_str_to_inum(val, 0, TRUE);
789 goto bin_retry;
790 case T_BIGNUM:
791 bignum = 1;
792 break;
793 case T_FIXNUM:
794 v = FIX2LONG(val);
795 break;
796 default:
797 val = rb_Integer(val);
798 goto bin_retry;
799 }
800
801 switch (*p) {
802 case 'o':
803 base = 8; break;
804 case 'x':
805 case 'X':
806 base = 16; break;
807 case 'b':
808 case 'B':
809 base = 2; break;
810 case 'u':
811 case 'd':
812 case 'i':
813 default:
814 base = 10; break;
815 }
816
817 if (base != 10) {
818 int numbits = ffs(base)-1;
819 size_t abs_nlz_bits;
820 size_t numdigits = rb_absint_numwords(val, numbits, &abs_nlz_bits);
821 long i;
822 if (INT_MAX-1 < numdigits) /* INT_MAX is used because rb_long2int is used later. */
823 rb_raise(rb_eArgError, "size too big");
824 if (sign) {
825 if (numdigits == 0)
826 numdigits = 1;
827 tmp = rb_str_new(NULL, numdigits);
828 valsign = rb_integer_pack(val, RSTRING_PTR(tmp), RSTRING_LEN(tmp),
829 1, CHAR_BIT-numbits, INTEGER_PACK_BIG_ENDIAN);
830 for (i = 0; i < RSTRING_LEN(tmp); i++)
831 RSTRING_PTR(tmp)[i] = ruby_digitmap[((unsigned char *)RSTRING_PTR(tmp))[i]];
832 s = RSTRING_PTR(tmp);
833 if (valsign < 0) {
834 sc = '-';
835 width--;
836 }
837 else if (flags & FPLUS) {
838 sc = '+';
839 width--;
840 }
841 else if (flags & FSPACE) {
842 sc = ' ';
843 width--;
844 }
845 }
846 else {
847 /* Following conditional "numdigits++" guarantees the
848 * most significant digit as
849 * - '1'(bin), '7'(oct) or 'f'(hex) for negative numbers
850 * - '0' for zero
851 * - not '0' for positive numbers.
852 *
853 * It also guarantees the most significant two
854 * digits will not be '11'(bin), '77'(oct), 'ff'(hex)
855 * or '00'. */
856 if (numdigits == 0 ||
857 ((abs_nlz_bits != (size_t)(numbits-1) ||
858 !rb_absint_singlebit_p(val)) &&
859 (!bignum ? v < 0 : RBIGNUM_NEGATIVE_P(val))))
860 numdigits++;
861 tmp = rb_str_new(NULL, numdigits);
862 valsign = rb_integer_pack(val, RSTRING_PTR(tmp), RSTRING_LEN(tmp),
863 1, CHAR_BIT-numbits, INTEGER_PACK_2COMP | INTEGER_PACK_BIG_ENDIAN);
864 for (i = 0; i < RSTRING_LEN(tmp); i++)
865 RSTRING_PTR(tmp)[i] = ruby_digitmap[((unsigned char *)RSTRING_PTR(tmp))[i]];
866 s = RSTRING_PTR(tmp);
867 dots = valsign < 0;
868 }
869 len = rb_long2int(RSTRING_END(tmp) - s);
870 }
871 else if (!bignum) {
872 valsign = 1;
873 if (v < 0) {
874 v = -v;
875 sc = '-';
876 width--;
877 valsign = -1;
878 }
879 else if (flags & FPLUS) {
880 sc = '+';
881 width--;
882 }
883 else if (flags & FSPACE) {
884 sc = ' ';
885 width--;
886 }
887 snprintf(nbuf, sizeof(nbuf), "%ld", v);
888 s = nbuf;
889 len = (int)strlen(s);
890 }
891 else {
892 tmp = rb_big2str(val, 10);
893 s = RSTRING_PTR(tmp);
894 valsign = 1;
895 if (s[0] == '-') {
896 s++;
897 sc = '-';
898 width--;
899 valsign = -1;
900 }
901 else if (flags & FPLUS) {
902 sc = '+';
903 width--;
904 }
905 else if (flags & FSPACE) {
906 sc = ' ';
907 width--;
908 }
909 len = rb_long2int(RSTRING_END(tmp) - s);
910 }
911
912 if (dots) {
913 prec -= 2;
914 width -= 2;
915 }
916
917 if (*p == 'X') {
918 char *pp = s;
919 int c;
920 while ((c = (int)(unsigned char)*pp) != 0) {
921 *pp = rb_enc_toupper(c, enc);
922 pp++;
923 }
924 }
925 if (prefix && !prefix[1]) { /* octal */
926 if (dots) {
927 prefix = 0;
928 }
929 else if (len == 1 && *s == '0') {
930 len = 0;
931 if (flags & FPREC) prec--;
932 }
933 else if ((flags & FPREC) && (prec > len)) {
934 prefix = 0;
935 }
936 }
937 else if (len == 1 && *s == '0') {
938 prefix = 0;
939 }
940 if (prefix) {
941 width -= (int)strlen(prefix);
942 }
943 if ((flags & (FZERO|FMINUS|FPREC)) == FZERO) {
944 prec = width;
945 width = 0;
946 }
947 else {
948 if (prec < len) {
949 if (!prefix && prec == 0 && len == 1 && *s == '0') len = 0;
950 prec = len;
951 }
952 width -= prec;
953 }
954 if (!(flags&FMINUS)) {
955 CHECK(width);
956 while (width-- > 0) {
957 buf[blen++] = ' ';
958 }
959 }
960 if (sc) PUSH(&sc, 1);
961 if (prefix) {
962 int plen = (int)strlen(prefix);
963 PUSH(prefix, plen);
964 }
965 CHECK(prec - len);
966 if (dots) PUSH("..", 2);
967 if (!sign && valsign < 0) {
968 char c = sign_bits(base, p);
969 while (len < prec--) {
970 buf[blen++] = c;
971 }
972 }
973 else if ((flags & (FMINUS|FPREC)) != FMINUS) {
974 while (len < prec--) {
975 buf[blen++] = '0';
976 }
977 }
978 PUSH(s, len);
979 RB_GC_GUARD(tmp);
980 CHECK(width);
981 while (width-- > 0) {
982 buf[blen++] = ' ';
983 }
984 }
985 break;
986
987 case 'f':
988 case 'g':
989 case 'G':
990 case 'e':
991 case 'E':
992 case 'a':
993 case 'A':
994 {
995 VALUE val = GETARG();
996 double fval;
997 int i, need = 6;
998 char fbuf[32];
999
1000 fval = RFLOAT_VALUE(rb_Float(val));
1001 if (isnan(fval) || isinf(fval)) {
1002 const char *expr;
1003
1004 if (isnan(fval)) {
1005 expr = "NaN";
1006 }
1007 else {
1008 expr = "Inf";
1009 }
1010 need = (int)strlen(expr);
1011 if ((!isnan(fval) && fval < 0.0) || (flags & FPLUS))
1012 need++;
1013 if ((flags & FWIDTH) && need < width)
1014 need = width;
1015
1016 CHECK(need + 1);
1017 snprintf(&buf[blen], need + 1, "%*s", need, "");
1018 if (flags & FMINUS) {
1019 if (!isnan(fval) && fval < 0.0)
1020 buf[blen++] = '-';
1021 else if (flags & FPLUS)
1022 buf[blen++] = '+';
1023 else if (flags & FSPACE)
1024 blen++;
1025 memcpy(&buf[blen], expr, strlen(expr));
1026 }
1027 else {
1028 if (!isnan(fval) && fval < 0.0)
1029 buf[blen + need - strlen(expr) - 1] = '-';
1030 else if (flags & FPLUS)
1031 buf[blen + need - strlen(expr) - 1] = '+';
1032 else if ((flags & FSPACE) && need > width)
1033 blen++;
1034 memcpy(&buf[blen + need - strlen(expr)], expr,
1035 strlen(expr));
1036 }
1037 blen += strlen(&buf[blen]);
1038 break;
1039 }
1040
1041 fmt_setup(fbuf, sizeof(fbuf), *p, flags, width, prec);
1042 need = 0;
1043 if (*p != 'e' && *p != 'E') {
1044 i = INT_MIN;
1045 frexp(fval, &i);
1046 if (i > 0)
1047 need = BIT_DIGITS(i);
1048 }
1049 need += (flags&FPREC) ? prec : 6;
1050 if ((flags&FWIDTH) && need < width)
1051 need = width;
1052 need += 20;
1053
1054 CHECK(need);
1055 snprintf(&buf[blen], need, fbuf, fval);
1056 blen += strlen(&buf[blen]);
1057 }
1058 break;
1059 }
1060 flags = FNONE;
1061 }
1062
1063 sprint_exit:
1064 RB_GC_GUARD(fmt);
1065 /* XXX - We cannot validate the number of arguments if (digit)$ style used.
1066 */
1067 if (posarg >= 0 && nextarg < argc) {
1068 const char *mesg = "too many arguments for format string";
1069 if (RTEST(ruby_debug)) rb_raise(rb_eArgError, "%s", mesg);
1070 if (RTEST(ruby_verbose)) rb_warn("%s", mesg);
1071 }
1072 rb_str_resize(result, blen);
1073
1074 if (tainted) OBJ_TAINT(result);
1075 return result;
1076 }
1077
1078 static void
1079 fmt_setup(char *buf, size_t size, int c, int flags, int width, int prec)
1080 {
1081 char *end = buf + size;
1082 *buf++ = '%';
1083 if (flags & FSHARP) *buf++ = '#';
1084 if (flags & FPLUS) *buf++ = '+';
1085 if (flags & FMINUS) *buf++ = '-';
1086 if (flags & FZERO) *buf++ = '0';
1087 if (flags & FSPACE) *buf++ = ' ';
1088
1089 if (flags & FWIDTH) {
1090 snprintf(buf, end - buf, "%d", width);
1091 buf += strlen(buf);
1092 }
1093
1094 if (flags & FPREC) {
1095 snprintf(buf, end - buf, ".%d", prec);
1096 buf += strlen(buf);
1097 }
1098
1099 *buf++ = c;
1100 *buf = '\0';
1101 }
1102
1103 #undef FILE
1104 #define FILE rb_printf_buffer
1105 #define __sbuf rb_printf_sbuf
1106 #define __sFILE rb_printf_sfile
1107 #undef feof
1108 #undef ferror
1109 #undef clearerr
1110 #undef fileno
1111 #if SIZEOF_LONG < SIZEOF_VOIDP
1112 # if SIZEOF_LONG_LONG == SIZEOF_VOIDP
1113 # define _HAVE_SANE_QUAD_
1114 # define _HAVE_LLP64_
1115 # define quad_t LONG_LONG
1116 # define u_quad_t unsigned LONG_LONG
1117 # endif
1118 #elif SIZEOF_LONG != SIZEOF_LONG_LONG && SIZEOF_LONG_LONG == 8
1119 # define _HAVE_SANE_QUAD_
1120 # define quad_t LONG_LONG
1121 # define u_quad_t unsigned LONG_LONG
1122 #endif
1123 #define FLOATING_POINT 1
1124 #define BSD__dtoa ruby_dtoa
1125 #define BSD__hdtoa ruby_hdtoa
1126 #include "vsnprintf.c"
1127
1128 typedef struct {
1129 rb_printf_buffer base;
1130 volatile VALUE value;
1131 } rb_printf_buffer_extra;
1132
1133 static int
1134 ruby__sfvwrite(register rb_printf_buffer *fp, register struct __suio *uio)
1135 {
1136 struct __siov *iov;
1137 VALUE result = (VALUE)fp->_bf._base;
1138 char *buf = (char*)fp->_p;
1139 size_t len, n;
1140 size_t blen = buf - RSTRING_PTR(result), bsiz = fp->_w;
1141
1142 if (RBASIC(result)->klass) {
1143 rb_raise(rb_eRuntimeError, "rb_vsprintf reentered");
1144 }
1145 if ((len = uio->uio_resid) == 0)
1146 return 0;
1147 CHECK(len);
1148 buf += blen;
1149 fp->_w = bsiz;
1150 for (iov = uio->uio_iov; len > 0; ++iov) {
1151 MEMCPY(buf, iov->iov_base, char, n = iov->iov_len);
1152 buf += n;
1153 len -= n;
1154 }
1155 fp->_p = (unsigned char *)buf;
1156 rb_str_set_len(result, buf - RSTRING_PTR(result));
1157 return 0;
1158 }
1159
1160 static char *
1161 ruby__sfvextra(rb_printf_buffer *fp, size_t valsize, void *valp, long *sz, int sign)
1162 {
1163 VALUE value, result = (VALUE)fp->_bf._base;
1164 rb_encoding *enc;
1165 char *cp;
1166
1167 if (valsize != sizeof(VALUE)) return 0;
1168 value = *(VALUE *)valp;
1169 if (RBASIC(result)->klass) {
1170 rb_raise(rb_eRuntimeError, "rb_vsprintf reentered");
1171 }
1172 if (sign == '+') {
1173 value = rb_inspect(value);
1174 }
1175 else {
1176 value = rb_obj_as_string(value);
1177 if (sign == ' ') value = QUOTE(value);
1178 }
1179 enc = rb_enc_compatible(result, value);
1180 if (enc) {
1181 rb_enc_associate(result, enc);
1182 }
1183 else {
1184 enc = rb_enc_get(result);
1185 value = rb_str_conv_enc_opts(value, rb_enc_get(value), enc,
1186 ECONV_UNDEF_REPLACE|ECONV_INVALID_REPLACE,
1187 Qnil);
1188 *(volatile VALUE *)valp = value;
1189 }
1190 StringValueCStr(value);
1191 RSTRING_GETMEM(value, cp, *sz);
1192 ((rb_printf_buffer_extra *)fp)->value = value;
1193 OBJ_INFECT(result, value);
1194 return cp;
1195 }
1196
1197 VALUE
1198 rb_enc_vsprintf(rb_encoding *enc, const char *fmt, va_list ap)
1199 {
1200 rb_printf_buffer_extra buffer;
1201 #define f buffer.base
1202 VALUE result;
1203
1204 f._flags = __SWR | __SSTR;
1205 f._bf._size = 0;
1206 f._w = 120;
1207 result = rb_str_buf_new(f._w);
1208 if (enc) {
1209 if (rb_enc_mbminlen(enc) > 1) {
1210 /* the implementation deeply depends on plain char */
1211 rb_raise(rb_eArgError, "cannot construct wchar_t based encoding string: %s",
1212 rb_enc_name(enc));
1213 }
1214 rb_enc_associate(result, enc);
1215 }
1216 f._bf._base = (unsigned char *)result;
1217 f._p = (unsigned char *)RSTRING_PTR(result);
1218 RBASIC_CLEAR_CLASS(result);
1219 f.vwrite = ruby__sfvwrite;
1220 f.vextra = ruby__sfvextra;
1221 buffer.value = 0;
1222 BSD_vfprintf(&f, fmt, ap);
1223 RBASIC_SET_CLASS_RAW(result, rb_cString);
1224 rb_str_resize(result, (char *)f._p - RSTRING_PTR(result));
1225 #undef f
1226
1227 return result;
1228 }
1229
1230 VALUE
1231 rb_enc_sprintf(rb_encoding *enc, const char *format, ...)
1232 {
1233 VALUE result;
1234 va_list ap;
1235
1236 va_start(ap, format);
1237 result = rb_enc_vsprintf(enc, format, ap);
1238 va_end(ap);
1239
1240 return result;
1241 }
1242
1243 VALUE
1244 rb_vsprintf(const char *fmt, va_list ap)
1245 {
1246 return rb_enc_vsprintf(NULL, fmt, ap);
1247 }
1248
1249 VALUE
1250 rb_sprintf(const char *format, ...)
1251 {
1252 VALUE result;
1253 va_list ap;
1254
1255 va_start(ap, format);
1256 result = rb_vsprintf(format, ap);
1257 va_end(ap);
1258
1259 return result;
1260 }
1261
1262 VALUE
1263 rb_str_vcatf(VALUE str, const char *fmt, va_list ap)
1264 {
1265 rb_printf_buffer_extra buffer;
1266 #define f buffer.base
1267 VALUE klass;
1268
1269 StringValue(str);
1270 rb_str_modify(str);
1271 f._flags = __SWR | __SSTR;
1272 f._bf._size = 0;
1273 f._w = rb_str_capacity(str);
1274 f._bf._base = (unsigned char *)str;
1275 f._p = (unsigned char *)RSTRING_END(str);
1276 klass = RBASIC(str)->klass;
1277 RBASIC_CLEAR_CLASS(str);
1278 f.vwrite = ruby__sfvwrite;
1279 f.vextra = ruby__sfvextra;
1280 buffer.value = 0;
1281 BSD_vfprintf(&f, fmt, ap);
1282 RBASIC_SET_CLASS_RAW(str, klass);
1283 rb_str_resize(str, (char *)f._p - RSTRING_PTR(str));
1284 #undef f
1285
1286 return str;
1287 }
1288
1289 VALUE
1290 rb_str_catf(VALUE str, const char *format, ...)
1291 {
1292 va_list ap;
1293
1294 va_start(ap, format);
1295 str = rb_str_vcatf(str, format, ap);
1296 va_end(ap);
1297
1298 return str;
1299 }
rb_cString
RUBY_EXTERN VALUE rb_cString
Definition: ruby.h:1583
BIT_DIGITS
#define BIT_DIGITS(N)
Definition: sprintf.c:25
RBASIC_CLEAR_CLASS
#define RBASIC_CLEAR_CLASS(obj)
Definition: internal.h:607
rb_enc_codelen
int rb_enc_codelen(int c, rb_encoding *enc)
Definition: encoding.c:1014
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:3531
get_hash
static VALUE get_hash(volatile VALUE *hash, int argc, const VALUE *argv)
Definition: sprintf.c:138
rb_enc_check
rb_encoding * rb_enc_check(VALUE str1, VALUE str2)
Definition: encoding.c:838
rb_str_new4
#define rb_str_new4
Definition: intern.h:842
rb_enc_copy
void rb_enc_copy(VALUE obj1, VALUE obj2)
Definition: encoding.c:916
rb_str_coderange_scan_restartable
long rb_str_coderange_scan_restartable(const char *, const char *, rb_encoding *, int *)
Definition: string.c:340
strlen
size_t strlen(const char *)
GETNUM
#define GETNUM(n, val)
Definition: sprintf.c:108
T_FIXNUM
#define T_FIXNUM
Definition: ruby.h:489
NUM2INT
#define NUM2INT(x)
Definition: ruby.h:630
rb_eKeyError
VALUE rb_eKeyError
Definition: error.c:551
ruby.h
__SSTR
#define __SSTR
Definition: vsnprintf.c:200
__suio
Definition: vsnprintf.c:234
ruby__sfvwrite
static int ruby__sfvwrite(register rb_printf_buffer *fp, register struct __suio *uio)
Definition: sprintf.c:1134
rb_printf_buffer_extra
Definition: sprintf.c:1128
FPREC
#define FPREC
Definition: sprintf.c:56
ENC_CODERANGE_SET
#define ENC_CODERANGE_SET(obj, cr)
Definition: encoding.h:54
FILL
#define FILL(c, l)
Definition: sprintf.c:75
rb_long2int
#define rb_long2int(n)
Definition: ruby.h:317
rb_enc_compatible
rb_encoding * rb_enc_compatible(VALUE str1, VALUE str2)
Definition: encoding.c:849
rb_str_set_len
void rb_str_set_len(VALUE, long)
Definition: string.c:2008
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:993
__siov::iov_base
const void * iov_base
Definition: vsnprintf.c:231
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1857
RSTRING_GETMEM
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Definition: ruby.h:854
rb_enc_associate
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
Definition: encoding.c:826
RB_GC_GUARD
#define RB_GC_GUARD(v)
Definition: ruby.h:523
FNONE
#define FNONE
Definition: sprintf.c:49
rb_enc_mbclen
int rb_enc_mbclen(const char *p, const char *e, rb_encoding *enc)
Definition: encoding.c:946
rb_enc_vsprintf
VALUE rb_enc_vsprintf(rb_encoding *enc, const char *fmt, va_list ap)
Definition: sprintf.c:1198
rb_check_id_cstr
ID rb_check_id_cstr(const char *ptr, long len, rb_encoding *enc)
Definition: ripper.c:17366
FIXNUM_P
#define FIXNUM_P(f)
Definition: ruby.h:347
rb_Float
VALUE rb_Float(VALUE)
Definition: object.c:2895
vsnprintf.c
GETARG
#define GETARG()
Definition: sprintf.c:81
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:607
INTEGER_PACK_2COMP
#define INTEGER_PACK_2COMP
Definition: intern.h:147
OBJ_TAINTED
#define OBJ_TAINTED(x)
Definition: ruby.h:1176
ENC_CODERANGE_7BIT
#define ENC_CODERANGE_7BIT
Definition: encoding.h:49
rb_enc_sprintf
VALUE rb_enc_sprintf(rb_encoding *enc, const char *format,...)
Definition: sprintf.c:1231
rb_str_format
VALUE rb_str_format(int argc, const VALUE *argv, VALUE fmt)
Definition: sprintf.c:421
FSPACE
#define FSPACE
Definition: sprintf.c:54
rb_enc_nth
char * rb_enc_nth(const char *, const char *, long, rb_encoding *)
Definition: string.c:1753
rb_enc_toupper
int rb_enc_toupper(int c, rb_encoding *enc)
Definition: encoding.c:1031
sign_bits
static char sign_bits(int base, const char *p)
Definition: sprintf.c:32
rb_absint_singlebit_p
int rb_absint_singlebit_p(VALUE val)
Definition: bignum.c:3430
RBASIC_SET_CLASS_RAW
#define RBASIC_SET_CLASS_RAW(obj, cls)
Definition: internal.h:608
FMINUS
#define FMINUS
Definition: sprintf.c:51
rb_dbl2big
VALUE rb_dbl2big(double d)
Definition: bignum.c:5213
val
#define val
rb_eRuntimeError
VALUE rb_eRuntimeError
Definition: error.c:547
FSHARP
#define FSHARP
Definition: sprintf.c:50
rb_str_to_inum
VALUE rb_str_to_inum(VALUE str, int base, int badcheck)
Definition: bignum.c:4129
RSTRING_END
#define RSTRING_END(str)
Definition: ruby.h:849
ECONV_INVALID_REPLACE
#define ECONV_INVALID_REPLACE
Definition: encoding.h:321
rb_obj_as_string
VALUE rb_obj_as_string(VALUE)
Definition: string.c:1011
snprintf
#define snprintf
Definition: subst.h:6
NIL_P
#define NIL_P(v)
Definition: ruby.h:438
CHECK
#define CHECK(l)
Definition: sprintf.c:59
__SWR
#define __SWR
Definition: vsnprintf.c:193
T_FLOAT
#define T_FLOAT
Definition: ruby.h:481
TYPE
#define TYPE(x)
Definition: ruby.h:505
argc
int argc
Definition: ruby.c:131
FZERO
#define FZERO
Definition: sprintf.c:53
rb_Integer
VALUE rb_Integer(VALUE)
Definition: object.c:2736
rb_vsprintf
VALUE rb_vsprintf(const char *fmt, va_list ap)
Definition: sprintf.c:1244
T_BIGNUM
#define T_BIGNUM
Definition: ruby.h:487
ENC_CODERANGE_UNKNOWN
#define ENC_CODERANGE_UNKNOWN
Definition: encoding.h:48
if
if((ID)(DISPID) nameid!=nameid)
Definition: win32ole.c:770
rb_enc_isprint
#define rb_enc_isprint(c, enc)
Definition: encoding.h:184
MEMCPY
#define MEMCPY(p1, p2, type, n)
Definition: ruby.h:1352
isinf
RUBY_EXTERN int isinf(double)
Definition: isinf.c:56
ENC_CODERANGE_BROKEN
#define ENC_CODERANGE_BROKEN
Definition: encoding.h:51
rb_enc_raise
void rb_enc_raise(rb_encoding *enc, VALUE exc, const char *fmt,...)
Definition: error.c:1844
rb_enc_mbminlen
#define rb_enc_mbminlen(enc)
Definition: encoding.h:128
CHECK_FOR_WIDTH
#define CHECK_FOR_WIDTH(f)
rb_big2str
VALUE rb_big2str(VALUE x, int base)
Definition: bignum.c:5014
GETASTER
#define GETASTER(val)
Definition: sprintf.c:123
encoding.h
rb_str_resize
VALUE rb_str_resize(VALUE, long)
Definition: string.c:2025
rb_str_capacity
size_t rb_str_capacity(VALUE)
Definition: string.c:468
RSTRING_LEN
#define RSTRING_LEN(str)
Definition: ruby.h:841
TRUE
#define TRUE
Definition: nkf.h:175
rb_sprintf
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1250
rb_f_sprintf
VALUE rb_f_sprintf(int argc, const VALUE *argv)
Definition: sprintf.c:415
rb_printf_buffer_extra::value
volatile VALUE value
Definition: sprintf.c:1130
rb_enc_name
#define rb_enc_name(enc)
Definition: encoding.h:125
ruby__sfvextra
static char * ruby__sfvextra(rb_printf_buffer *fp, size_t valsize, void *valp, long *sz, int sign)
Definition: sprintf.c:1161
rb_check_hash_type
VALUE rb_check_hash_type(VALUE hash)
Definition: hash.c:588
buf
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4308
internal.h
__suio::uio_resid
size_t uio_resid
Definition: vsnprintf.c:237
ID
unsigned long ID
Definition: ruby.h:89
Qnil
#define Qnil
Definition: ruby.h:427
fmt_setup
static void fmt_setup(char *, size_t, int, int, int, int)
Definition: sprintf.c:1079
PUSH
#define PUSH(s, l)
Definition: sprintf.c:69
OBJ_TAINT
#define OBJ_TAINT(x)
Definition: ruby.h:1177
VALUE
unsigned long VALUE
Definition: ruby.h:88
result
static VALUE result
Definition: nkf.c:40
RBASIC
#define RBASIC(obj)
Definition: ruby.h:1116
__suio::uio_iov
struct __siov * uio_iov
Definition: vsnprintf.c:235
isnan
#define isnan(x)
Definition: win32.h:376
re.h
FIXABLE
#define FIXABLE(f)
Definition: ruby.h:350
FPREC0
#define FPREC0
Definition: sprintf.c:57
CHAR_BIT
#define CHAR_BIT
Definition: ruby.h:198
FPLUS
#define FPLUS
Definition: sprintf.c:52
INTEGER_PACK_BIG_ENDIAN
#define INTEGER_PACK_BIG_ENDIAN
Definition: intern.h:156
StringValueCStr
#define StringValueCStr(v)
Definition: ruby.h:541
RSTRING_PTR
#define RSTRING_PTR(str)
Definition: ruby.h:845
rb_printf_buffer_extra::base
rb_printf_buffer base
Definition: sprintf.c:1129
rb_enc_right_char_head
#define rb_enc_right_char_head(s, p, e, enc)
Definition: encoding.h:171
ECONV_UNDEF_REPLACE
#define ECONV_UNDEF_REPLACE
Definition: encoding.h:324
rb_str_modify
void rb_str_modify(VALUE)
Definition: string.c:1484
rb_enc_get
rb_encoding * rb_enc_get(VALUE obj)
Definition: encoding.c:832
ffs
RUBY_EXTERN int ffs(int)
Definition: ffs.c:6
RFLOAT_VALUE
#define RFLOAT_VALUE(v)
Definition: ruby.h:814
size
int size
Definition: encoding.c:49
f
#define f
rb_enc_strlen
long rb_enc_strlen(const char *, const char *, rb_encoding *)
Definition: string.c:1141
BSD_vfprintf
static ssize_t BSD_vfprintf(FILE *fp, const char *fmt0, va_list ap)
Definition: vsnprintf.c:537
rb_str_vcatf
VALUE rb_str_vcatf(VALUE str, const char *fmt, va_list ap)
Definition: sprintf.c:1263
rb_str_catf
VALUE rb_str_catf(VALUE str, const char *format,...)
Definition: sprintf.c:1290
rb_check_string_type
VALUE rb_check_string_type(VALUE)
Definition: string.c:1679
LONG2FIX
#define LONG2FIX(i)
Definition: ruby.h:232
RTEST
#define RTEST(v)
Definition: ruby.h:437
T_STRING
#define T_STRING
Definition: ruby.h:482
GETPOSARG
#define GETPOSARG(n)
Definition: sprintf.c:91
OBJ_INFECT
#define OBJ_INFECT(x, s)
Definition: ruby.h:1180
ruby_digitmap
const char ruby_digitmap[]
Definition: bignum.c:36
__siov
Definition: vsnprintf.c:230
rb_absint_numwords
size_t rb_absint_numwords(VALUE val, size_t word_numbits, size_t *nlz_bits_ret)
Definition: bignum.c:3366
hash
static unsigned int hash(const char *str, unsigned int len)
Definition: lex.c:56
PRIdSIZE
#define PRIdSIZE
Definition: ruby.h:176
CHECK_FOR_FLAGS
#define CHECK_FOR_FLAGS(f)
ruby_debug
#define ruby_debug
Definition: ruby.h:1476
ID2SYM
#define ID2SYM(x)
Definition: ruby.h:355
rb_id2name
const char * rb_id2name(ID id)
Definition: ripper.c:17230
rb_inspect
VALUE rb_inspect(VALUE)
Definition: object.c:470
GETNTHARG
#define GETNTHARG(nth)
Definition: sprintf.c:98
QUOTE
#define QUOTE(str)
Definition: internal.h:714
RBIGNUM_NEGATIVE_P
#define RBIGNUM_NEGATIVE_P(b)
Definition: ruby.h:1098
rb_enc_mbcput
#define rb_enc_mbcput(c, buf, enc)
Definition: encoding.h:165
rb_str_buf_new
VALUE rb_str_buf_new(long)
Definition: string.c:891
FWIDTH
#define FWIDTH
Definition: sprintf.c:55
NULL
#define NULL
Definition: _sdbm.c:103
FIX2LONG
#define FIX2LONG(x)
Definition: ruby.h:345
Qundef
#define Qundef
Definition: ruby.h:428
ruby_verbose
#define ruby_verbose
Definition: ruby.h:1475
GETNAMEARG
#define GETNAMEARG(id, name, len, enc)
Definition: sprintf.c:101
rb_warn
void rb_warn(const char *fmt,...)
Definition: error.c:223
rb_eArgError
VALUE rb_eArgError
Definition: error.c:549
argv
char ** argv
Definition: ruby.c:132
StringValue
#define StringValue(v)
Definition: ruby.h:539
__siov::iov_len
size_t iov_len
Definition: vsnprintf.c:232
OnigEncodingTypeST
Definition: oniguruma.h:158
rb_str_new
VALUE rb_str_new(const char *, long)
Definition: string.c:534