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