source: EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-sprintf/src/sprintf.c@ 439

Last change on this file since 439 was 439, checked in by coas-nagasima, 4 years ago

mrubyを2.1.1に更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 32.5 KB
Line 
1/*
2** sprintf.c - Kernel.#sprintf
3**
4** See Copyright Notice in mruby.h
5*/
6
7#include <mruby.h>
8
9#ifdef MRB_DISABLE_STDIO
10# error sprintf conflicts 'MRB_DISABLE_STDIO' configuration in your 'build_config.rb'
11#endif
12
13#include <limits.h>
14#include <string.h>
15#include <mruby/string.h>
16#include <mruby/hash.h>
17#include <mruby/numeric.h>
18#ifndef MRB_WITHOUT_FLOAT
19#include <math.h>
20#endif
21#include <ctype.h>
22
23#define BIT_DIGITS(N) (((N)*146)/485 + 1) /* log2(10) =~ 146/485 */
24#define BITSPERDIG MRB_INT_BIT
25#define EXTENDSIGN(n, l) (((~0U << (n)) >> (((n)*(l)) % BITSPERDIG)) & ~(~0U << (n)))
26
27mrb_value mrb_str_format(mrb_state *, mrb_int, const mrb_value *, mrb_value);
28#ifndef MRB_WITHOUT_FLOAT
29static void fmt_setup(char*,size_t,int,int,mrb_int,mrb_int);
30#endif
31
32static char*
33remove_sign_bits(char *str, int base)
34{
35 char *t;
36
37 t = str;
38 if (base == 16) {
39 while (*t == 'f') {
40 t++;
41 }
42 }
43 else if (base == 8) {
44 *t |= EXTENDSIGN(3, strlen(t));
45 while (*t == '7') {
46 t++;
47 }
48 }
49 else if (base == 2) {
50 while (*t == '1') {
51 t++;
52 }
53 }
54
55 return t;
56}
57
58static char
59sign_bits(int base, const char *p)
60{
61 char c;
62
63 switch (base) {
64 case 16:
65 if (*p == 'X') c = 'F';
66 else c = 'f';
67 break;
68 case 8:
69 c = '7'; break;
70 case 2:
71 c = '1'; break;
72 default:
73 c = '.'; break;
74 }
75 return c;
76}
77
78static mrb_value
79mrb_fix2binstr(mrb_state *mrb, mrb_value x, int base)
80{
81 char buf[66], *b = buf + sizeof buf;
82 mrb_int num = mrb_fixnum(x);
83 uint64_t val = (uint64_t)num;
84 char d;
85
86 if (base != 2) {
87 mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid radix %d", base);
88 }
89 if (val == 0) {
90 return mrb_str_new_lit(mrb, "0");
91 }
92 *--b = '\0';
93 do {
94 *--b = mrb_digitmap[(int)(val % base)];
95 } while (val /= base);
96
97 if (num < 0) {
98 b = remove_sign_bits(b, base);
99 switch (base) {
100 case 16: d = 'f'; break;
101 case 8: d = '7'; break;
102 case 2: d = '1'; break;
103 default: d = 0; break;
104 }
105
106 if (d && *b != d) {
107 *--b = d;
108 }
109 }
110
111 return mrb_str_new_cstr(mrb, b);
112}
113
114#define FNONE 0
115#define FSHARP 1
116#define FMINUS 2
117#define FPLUS 4
118#define FZERO 8
119#define FSPACE 16
120#define FWIDTH 32
121#define FPREC 64
122#define FPREC0 128
123
124#define CHECK(l) do {\
125 while ((l) >= bsiz - blen) {\
126 if (bsiz > MRB_INT_MAX/2) mrb_raise(mrb, E_ARGUMENT_ERROR, "too big specifier"); \
127 bsiz*=2;\
128 }\
129 mrb_str_resize(mrb, result, bsiz);\
130 buf = RSTRING_PTR(result);\
131} while (0)
132
133#define PUSH(s, l) do { \
134 CHECK(l);\
135 memcpy(&buf[blen], s, l);\
136 blen += (mrb_int)(l);\
137} while (0)
138
139#define FILL(c, l) do { \
140 CHECK(l);\
141 memset(&buf[blen], c, l);\
142 blen += (l);\
143} while (0)
144
145static void
146check_next_arg(mrb_state *mrb, int posarg, int nextarg)
147{
148 switch (posarg) {
149 case -1:
150 mrb_raisef(mrb, E_ARGUMENT_ERROR, "unnumbered(%d) mixed with numbered", nextarg);
151 break;
152 case -2:
153 mrb_raisef(mrb, E_ARGUMENT_ERROR, "unnumbered(%d) mixed with named", nextarg);
154 break;
155 default:
156 break;
157 }
158}
159
160static void
161check_pos_arg(mrb_state *mrb, int posarg, mrb_int n)
162{
163 if (posarg > 0) {
164 mrb_raisef(mrb, E_ARGUMENT_ERROR, "numbered(%i) after unnumbered(%d)",
165 n, posarg);
166 }
167 if (posarg == -2) {
168 mrb_raisef(mrb, E_ARGUMENT_ERROR, "numbered(%i) after named", n);
169 }
170 if (n < 1) {
171 mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid index - %i$", n);
172 }
173}
174
175static void
176check_name_arg(mrb_state *mrb, int posarg, const char *name, size_t len)
177{
178 if (posarg > 0) {
179 mrb_raisef(mrb, E_ARGUMENT_ERROR, "named%l after unnumbered(%d)",
180 name, len, posarg);
181 }
182 if (posarg == -1) {
183 mrb_raisef(mrb, E_ARGUMENT_ERROR, "named%l after numbered", name, len);
184 }
185}
186
187#define GETNEXTARG() (\
188 check_next_arg(mrb, posarg, nextarg),\
189 (posarg = nextarg++, GETNTHARG(posarg)))
190
191#define GETARG() (!mrb_undef_p(nextvalue) ? nextvalue : GETNEXTARG())
192
193#define GETPOSARG(n) (\
194 check_pos_arg(mrb, posarg, n),\
195 (posarg = -1, GETNTHARG(n)))
196
197#define GETNTHARG(nth) \
198 ((nth >= argc) ? (mrb_raise(mrb, E_ARGUMENT_ERROR, "too few arguments"), mrb_undef_value()) : argv[nth])
199
200#define GETNAMEARG(id, name, len) (\
201 check_name_arg(mrb, posarg, name, len),\
202 (posarg = -2, mrb_hash_fetch(mrb, get_hash(mrb, &hash, argc, argv), id, mrb_undef_value())))
203
204#define GETNUM(n, val) \
205 for (; p < end && ISDIGIT(*p); p++) {\
206 if (n > (MRB_INT_MAX - (*p - '0'))/10) {\
207 mrb_raise(mrb, E_ARGUMENT_ERROR, #val " too big"); \
208 } \
209 n = 10 * n + (*p - '0'); \
210 } \
211 if (p >= end) { \
212 mrb_raise(mrb, E_ARGUMENT_ERROR, "malformed format string - %*[0-9]"); \
213 }
214
215#define GETASTER(num) do { \
216 mrb_value tmp_v; \
217 t = p++; \
218 n = 0; \
219 GETNUM(n, val); \
220 if (*p == '$') { \
221 tmp_v = GETPOSARG(n); \
222 } \
223 else { \
224 tmp_v = GETNEXTARG(); \
225 p = t; \
226 } \
227 num = mrb_int(mrb, tmp_v); \
228} while (0)
229
230static mrb_value
231get_hash(mrb_state *mrb, mrb_value *hash, mrb_int argc, const mrb_value *argv)
232{
233 mrb_value tmp;
234
235 if (!mrb_undef_p(*hash)) return *hash;
236 if (argc != 2) {
237 mrb_raise(mrb, E_ARGUMENT_ERROR, "one hash required");
238 }
239 tmp = mrb_check_hash_type(mrb, argv[1]);
240 if (mrb_nil_p(tmp)) {
241 mrb_raise(mrb, E_ARGUMENT_ERROR, "one hash required");
242 }
243 return (*hash = tmp);
244}
245
246/*
247 * call-seq:
248 * format(format_string [, arguments...] ) -> string
249 * sprintf(format_string [, arguments...] ) -> string
250 *
251 * Returns the string resulting from applying <i>format_string</i> to
252 * any additional arguments. Within the format string, any characters
253 * other than format sequences are copied to the result.
254 *
255 * The syntax of a format sequence is follows.
256 *
257 * %[flags][width][.precision]type
258 *
259 * A format
260 * sequence consists of a percent sign, followed by optional flags,
261 * width, and precision indicators, then terminated with a field type
262 * character. The field type controls how the corresponding
263 * <code>sprintf</code> argument is to be interpreted, while the flags
264 * modify that interpretation.
265 *
266 * The field type characters are:
267 *
268 * Field | Integer Format
269 * ------+--------------------------------------------------------------
270 * b | Convert argument as a binary number.
271 * | Negative numbers will be displayed as a two's complement
272 * | prefixed with '..1'.
273 * B | Equivalent to 'b', but uses an uppercase 0B for prefix
274 * | in the alternative format by #.
275 * d | Convert argument as a decimal number.
276 * i | Identical to 'd'.
277 * o | Convert argument as an octal number.
278 * | Negative numbers will be displayed as a two's complement
279 * | prefixed with '..7'.
280 * u | Identical to 'd'.
281 * x | Convert argument as a hexadecimal number.
282 * | Negative numbers will be displayed as a two's complement
283 * | prefixed with '..f' (representing an infinite string of
284 * | leading 'ff's).
285 * X | Equivalent to 'x', but uses uppercase letters.
286 *
287 * Field | Float Format
288 * ------+--------------------------------------------------------------
289 * e | Convert floating point argument into exponential notation
290 * | with one digit before the decimal point as [-]d.dddddde[+-]dd.
291 * | The precision specifies the number of digits after the decimal
292 * | point (defaulting to six).
293 * E | Equivalent to 'e', but uses an uppercase E to indicate
294 * | the exponent.
295 * f | Convert floating point argument as [-]ddd.dddddd,
296 * | where the precision specifies the number of digits after
297 * | the decimal point.
298 * g | Convert a floating point number using exponential form
299 * | if the exponent is less than -4 or greater than or
300 * | equal to the precision, or in dd.dddd form otherwise.
301 * | The precision specifies the number of significant digits.
302 * G | Equivalent to 'g', but use an uppercase 'E' in exponent form.
303 * a | Convert floating point argument as [-]0xh.hhhhp[+-]dd,
304 * | which is consisted from optional sign, "0x", fraction part
305 * | as hexadecimal, "p", and exponential part as decimal.
306 * A | Equivalent to 'a', but use uppercase 'X' and 'P'.
307 *
308 * Field | Other Format
309 * ------+--------------------------------------------------------------
310 * c | Argument is the numeric code for a single character or
311 * | a single character string itself.
312 * p | The valuing of argument.inspect.
313 * s | Argument is a string to be substituted. If the format
314 * | sequence contains a precision, at most that many characters
315 * | will be copied.
316 * % | A percent sign itself will be displayed. No argument taken.
317 *
318 * The flags modifies the behavior of the formats.
319 * The flag characters are:
320 *
321 * Flag | Applies to | Meaning
322 * ---------+---------------+-----------------------------------------
323 * space | bBdiouxX | Leave a space at the start of
324 * | aAeEfgG | non-negative numbers.
325 * | (numeric fmt) | For 'o', 'x', 'X', 'b' and 'B', use
326 * | | a minus sign with absolute value for
327 * | | negative values.
328 * ---------+---------------+-----------------------------------------
329 * (digit)$ | all | Specifies the absolute argument number
330 * | | for this field. Absolute and relative
331 * | | argument numbers cannot be mixed in a
332 * | | sprintf string.
333 * ---------+---------------+-----------------------------------------
334 * # | bBoxX | Use an alternative format.
335 * | aAeEfgG | For the conversions 'o', increase the precision
336 * | | until the first digit will be '0' if
337 * | | it is not formatted as complements.
338 * | | For the conversions 'x', 'X', 'b' and 'B'
339 * | | on non-zero, prefix the result with "0x",
340 * | | "0X", "0b" and "0B", respectively.
341 * | | For 'a', 'A', 'e', 'E', 'f', 'g', and 'G',
342 * | | force a decimal point to be added,
343 * | | even if no digits follow.
344 * | | For 'g' and 'G', do not remove trailing zeros.
345 * ---------+---------------+-----------------------------------------
346 * + | bBdiouxX | Add a leading plus sign to non-negative
347 * | aAeEfgG | numbers.
348 * | (numeric fmt) | For 'o', 'x', 'X', 'b' and 'B', use
349 * | | a minus sign with absolute value for
350 * | | negative values.
351 * ---------+---------------+-----------------------------------------
352 * - | all | Left-justify the result of this conversion.
353 * ---------+---------------+-----------------------------------------
354 * 0 (zero) | bBdiouxX | Pad with zeros, not spaces.
355 * | aAeEfgG | For 'o', 'x', 'X', 'b' and 'B', radix-1
356 * | (numeric fmt) | is used for negative numbers formatted as
357 * | | complements.
358 * ---------+---------------+-----------------------------------------
359 * * | all | Use the next argument as the field width.
360 * | | If negative, left-justify the result. If the
361 * | | asterisk is followed by a number and a dollar
362 * | | sign, use the indicated argument as the width.
363 *
364 * Examples of flags:
365 *
366 * # '+' and space flag specifies the sign of non-negative numbers.
367 * sprintf("%d", 123) #=> "123"
368 * sprintf("%+d", 123) #=> "+123"
369 * sprintf("% d", 123) #=> " 123"
370 *
371 * # '#' flag for 'o' increases number of digits to show '0'.
372 * # '+' and space flag changes format of negative numbers.
373 * sprintf("%o", 123) #=> "173"
374 * sprintf("%#o", 123) #=> "0173"
375 * sprintf("%+o", -123) #=> "-173"
376 * sprintf("%o", -123) #=> "..7605"
377 * sprintf("%#o", -123) #=> "..7605"
378 *
379 * # '#' flag for 'x' add a prefix '0x' for non-zero numbers.
380 * # '+' and space flag disables complements for negative numbers.
381 * sprintf("%x", 123) #=> "7b"
382 * sprintf("%#x", 123) #=> "0x7b"
383 * sprintf("%+x", -123) #=> "-7b"
384 * sprintf("%x", -123) #=> "..f85"
385 * sprintf("%#x", -123) #=> "0x..f85"
386 * sprintf("%#x", 0) #=> "0"
387 *
388 * # '#' for 'X' uses the prefix '0X'.
389 * sprintf("%X", 123) #=> "7B"
390 * sprintf("%#X", 123) #=> "0X7B"
391 *
392 * # '#' flag for 'b' add a prefix '0b' for non-zero numbers.
393 * # '+' and space flag disables complements for negative numbers.
394 * sprintf("%b", 123) #=> "1111011"
395 * sprintf("%#b", 123) #=> "0b1111011"
396 * sprintf("%+b", -123) #=> "-1111011"
397 * sprintf("%b", -123) #=> "..10000101"
398 * sprintf("%#b", -123) #=> "0b..10000101"
399 * sprintf("%#b", 0) #=> "0"
400 *
401 * # '#' for 'B' uses the prefix '0B'.
402 * sprintf("%B", 123) #=> "1111011"
403 * sprintf("%#B", 123) #=> "0B1111011"
404 *
405 * # '#' for 'e' forces to show the decimal point.
406 * sprintf("%.0e", 1) #=> "1e+00"
407 * sprintf("%#.0e", 1) #=> "1.e+00"
408 *
409 * # '#' for 'f' forces to show the decimal point.
410 * sprintf("%.0f", 1234) #=> "1234"
411 * sprintf("%#.0f", 1234) #=> "1234."
412 *
413 * # '#' for 'g' forces to show the decimal point.
414 * # It also disables stripping lowest zeros.
415 * sprintf("%g", 123.4) #=> "123.4"
416 * sprintf("%#g", 123.4) #=> "123.400"
417 * sprintf("%g", 123456) #=> "123456"
418 * sprintf("%#g", 123456) #=> "123456."
419 *
420 * The field width is an optional integer, followed optionally by a
421 * period and a precision. The width specifies the minimum number of
422 * characters that will be written to the result for this field.
423 *
424 * Examples of width:
425 *
426 * # padding is done by spaces, width=20
427 * # 0 or radix-1. <------------------>
428 * sprintf("%20d", 123) #=> " 123"
429 * sprintf("%+20d", 123) #=> " +123"
430 * sprintf("%020d", 123) #=> "00000000000000000123"
431 * sprintf("%+020d", 123) #=> "+0000000000000000123"
432 * sprintf("% 020d", 123) #=> " 0000000000000000123"
433 * sprintf("%-20d", 123) #=> "123 "
434 * sprintf("%-+20d", 123) #=> "+123 "
435 * sprintf("%- 20d", 123) #=> " 123 "
436 * sprintf("%020x", -123) #=> "..ffffffffffffffff85"
437 *
438 * For
439 * numeric fields, the precision controls the number of decimal places
440 * displayed. For string fields, the precision determines the maximum
441 * number of characters to be copied from the string. (Thus, the format
442 * sequence <code>%10.10s</code> will always contribute exactly ten
443 * characters to the result.)
444 *
445 * Examples of precisions:
446 *
447 * # precision for 'd', 'o', 'x' and 'b' is
448 * # minimum number of digits <------>
449 * sprintf("%20.8d", 123) #=> " 00000123"
450 * sprintf("%20.8o", 123) #=> " 00000173"
451 * sprintf("%20.8x", 123) #=> " 0000007b"
452 * sprintf("%20.8b", 123) #=> " 01111011"
453 * sprintf("%20.8d", -123) #=> " -00000123"
454 * sprintf("%20.8o", -123) #=> " ..777605"
455 * sprintf("%20.8x", -123) #=> " ..ffff85"
456 * sprintf("%20.8b", -11) #=> " ..110101"
457 *
458 * # "0x" and "0b" for '#x' and '#b' is not counted for
459 * # precision but "0" for '#o' is counted. <------>
460 * sprintf("%#20.8d", 123) #=> " 00000123"
461 * sprintf("%#20.8o", 123) #=> " 00000173"
462 * sprintf("%#20.8x", 123) #=> " 0x0000007b"
463 * sprintf("%#20.8b", 123) #=> " 0b01111011"
464 * sprintf("%#20.8d", -123) #=> " -00000123"
465 * sprintf("%#20.8o", -123) #=> " ..777605"
466 * sprintf("%#20.8x", -123) #=> " 0x..ffff85"
467 * sprintf("%#20.8b", -11) #=> " 0b..110101"
468 *
469 * # precision for 'e' is number of
470 * # digits after the decimal point <------>
471 * sprintf("%20.8e", 1234.56789) #=> " 1.23456789e+03"
472 *
473 * # precision for 'f' is number of
474 * # digits after the decimal point <------>
475 * sprintf("%20.8f", 1234.56789) #=> " 1234.56789000"
476 *
477 * # precision for 'g' is number of
478 * # significant digits <------->
479 * sprintf("%20.8g", 1234.56789) #=> " 1234.5679"
480 *
481 * # <------->
482 * sprintf("%20.8g", 123456789) #=> " 1.2345679e+08"
483 *
484 * # precision for 's' is
485 * # maximum number of characters <------>
486 * sprintf("%20.8s", "string test") #=> " string t"
487 *
488 * Examples:
489 *
490 * sprintf("%d %04x", 123, 123) #=> "123 007b"
491 * sprintf("%08b '%4s'", 123, 123) #=> "01111011 ' 123'"
492 * sprintf("%1$*2$s %2$d %1$s", "hello", 8) #=> " hello 8 hello"
493 * sprintf("%1$*2$s %2$d", "hello", -8) #=> "hello -8"
494 * sprintf("%+g:% g:%-g", 1.23, 1.23, 1.23) #=> "+1.23: 1.23:1.23"
495 * sprintf("%u", -123) #=> "-123"
496 *
497 * For more complex formatting, Ruby supports a reference by name.
498 * %<name>s style uses format style, but %{name} style doesn't.
499 *
500 * Exapmles:
501 * sprintf("%<foo>d : %<bar>f", { :foo => 1, :bar => 2 })
502 * #=> 1 : 2.000000
503 * sprintf("%{foo}f", { :foo => 1 })
504 * # => "1f"
505 */
506
507mrb_value
508mrb_f_sprintf(mrb_state *mrb, mrb_value obj)
509{
510 mrb_int argc;
511 mrb_value *argv;
512
513 mrb_get_args(mrb, "*", &argv, &argc);
514
515 if (argc <= 0) {
516 mrb_raise(mrb, E_ARGUMENT_ERROR, "too few arguments");
517 return mrb_nil_value();
518 }
519 else {
520 return mrb_str_format(mrb, argc - 1, argv + 1, argv[0]);
521 }
522}
523
524mrb_value
525mrb_str_format(mrb_state *mrb, mrb_int argc, const mrb_value *argv, mrb_value fmt)
526{
527 const char *p, *end;
528 char *buf;
529 mrb_int blen;
530 mrb_int bsiz;
531 mrb_value result;
532 mrb_int n;
533 mrb_int width;
534 mrb_int prec;
535 int nextarg = 1;
536 int posarg = 0;
537 mrb_value nextvalue;
538 mrb_value str;
539 mrb_value hash = mrb_undef_value();
540
541#define CHECK_FOR_WIDTH(f) \
542 if ((f) & FWIDTH) { \
543 mrb_raise(mrb, E_ARGUMENT_ERROR, "width given twice"); \
544 } \
545 if ((f) & FPREC0) { \
546 mrb_raise(mrb, E_ARGUMENT_ERROR, "width after precision"); \
547 }
548#define CHECK_FOR_FLAGS(f) \
549 if ((f) & FWIDTH) { \
550 mrb_raise(mrb, E_ARGUMENT_ERROR, "flag after width"); \
551 } \
552 if ((f) & FPREC0) { \
553 mrb_raise(mrb, E_ARGUMENT_ERROR, "flag after precision"); \
554 }
555
556 ++argc;
557 --argv;
558 mrb_to_str(mrb, fmt);
559 p = RSTRING_PTR(fmt);
560 end = p + RSTRING_LEN(fmt);
561 blen = 0;
562 bsiz = 120;
563 result = mrb_str_new_capa(mrb, bsiz);
564 buf = RSTRING_PTR(result);
565 memset(buf, 0, bsiz);
566
567 for (; p < end; p++) {
568 const char *t;
569 mrb_sym id = 0;
570 int flags = FNONE;
571
572 for (t = p; t < end && *t != '%'; t++) ;
573 if (t + 1 == end) ++t;
574 PUSH(p, t - p);
575 if (t >= end)
576 goto sprint_exit; /* end of fmt string */
577
578 p = t + 1; /* skip '%' */
579
580 width = prec = -1;
581 nextvalue = mrb_undef_value();
582
583retry:
584 switch (*p) {
585 default:
586 mrb_raisef(mrb, E_ARGUMENT_ERROR, "malformed format string - %%%c", *p);
587 break;
588
589 case ' ':
590 CHECK_FOR_FLAGS(flags);
591 flags |= FSPACE;
592 p++;
593 goto retry;
594
595 case '#':
596 CHECK_FOR_FLAGS(flags);
597 flags |= FSHARP;
598 p++;
599 goto retry;
600
601 case '+':
602 CHECK_FOR_FLAGS(flags);
603 flags |= FPLUS;
604 p++;
605 goto retry;
606
607 case '-':
608 CHECK_FOR_FLAGS(flags);
609 flags |= FMINUS;
610 p++;
611 goto retry;
612
613 case '0':
614 CHECK_FOR_FLAGS(flags);
615 flags |= FZERO;
616 p++;
617 goto retry;
618
619 case '1': case '2': case '3': case '4':
620 case '5': case '6': case '7': case '8': case '9':
621 n = 0;
622 GETNUM(n, width);
623 if (*p == '$') {
624 if (!mrb_undef_p(nextvalue)) {
625 mrb_raisef(mrb, E_ARGUMENT_ERROR, "value given twice - %i$", n);
626 }
627 nextvalue = GETPOSARG(n);
628 p++;
629 goto retry;
630 }
631 CHECK_FOR_WIDTH(flags);
632 width = n;
633 flags |= FWIDTH;
634 goto retry;
635
636 case '<':
637 case '{': {
638 const char *start = p;
639 char term = (*p == '<') ? '>' : '}';
640 mrb_value symname;
641
642 for (; p < end && *p != term; )
643 p++;
644 if (id) {
645 mrb_raisef(mrb, E_ARGUMENT_ERROR, "name%l after <%n>",
646 start, p - start + 1, id);
647 }
648 symname = mrb_str_new(mrb, start + 1, p - start - 1);
649 id = mrb_intern_str(mrb, symname);
650 nextvalue = GETNAMEARG(mrb_symbol_value(id), start, p - start + 1);
651 if (mrb_undef_p(nextvalue)) {
652 mrb_raisef(mrb, E_KEY_ERROR, "key%l not found", start, p - start + 1);
653 }
654 if (term == '}') goto format_s;
655 p++;
656 goto retry;
657 }
658
659 case '*':
660 CHECK_FOR_WIDTH(flags);
661 flags |= FWIDTH;
662 GETASTER(width);
663 if (width < 0) {
664 flags |= FMINUS;
665 width = -width;
666 }
667 p++;
668 goto retry;
669
670 case '.':
671 if (flags & FPREC0) {
672 mrb_raise(mrb, E_ARGUMENT_ERROR, "precision given twice");
673 }
674 flags |= FPREC|FPREC0;
675
676 prec = 0;
677 p++;
678 if (*p == '*') {
679 GETASTER(prec);
680 if (prec < 0) { /* ignore negative precision */
681 flags &= ~FPREC;
682 }
683 p++;
684 goto retry;
685 }
686
687 GETNUM(prec, precision);
688 goto retry;
689
690 case '\n':
691 case '\0':
692 p--;
693 /* fallthrough */
694 case '%':
695 if (flags != FNONE) {
696 mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid format character - %");
697 }
698 PUSH("%", 1);
699 break;
700
701 case 'c': {
702 mrb_value val = GETARG();
703 mrb_value tmp;
704 char *c;
705
706 tmp = mrb_check_string_type(mrb, val);
707 if (!mrb_nil_p(tmp)) {
708 if (RSTRING_LEN(tmp) != 1) {
709 mrb_raise(mrb, E_ARGUMENT_ERROR, "%c requires a character");
710 }
711 }
712 else if (mrb_fixnum_p(val)) {
713 mrb_int n = mrb_fixnum(val);
714
715 if (n < 0x80) {
716 char buf[1];
717
718 buf[0] = (char)n;
719 tmp = mrb_str_new(mrb, buf, 1);
720 }
721 else {
722 tmp = mrb_funcall(mrb, val, "chr", 0);
723 mrb_check_type(mrb, tmp, MRB_TT_STRING);
724 }
725 }
726 else {
727 mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid character");
728 }
729 c = RSTRING_PTR(tmp);
730 n = RSTRING_LEN(tmp);
731 if (!(flags & FWIDTH)) {
732 PUSH(c, n);
733 }
734 else if ((flags & FMINUS)) {
735 PUSH(c, n);
736 if (width>0) FILL(' ', width-1);
737 }
738 else {
739 if (width>0) FILL(' ', width-1);
740 PUSH(c, n);
741 }
742 }
743 break;
744
745 case 's':
746 case 'p':
747 format_s:
748 {
749 mrb_value arg = GETARG();
750 mrb_int len;
751 mrb_int slen;
752
753 if (*p == 'p') arg = mrb_inspect(mrb, arg);
754 str = mrb_obj_as_string(mrb, arg);
755 len = RSTRING_LEN(str);
756 if (RSTRING(result)->flags & MRB_STR_EMBED) {
757 mrb_int tmp_n = len;
758 RSTRING(result)->flags &= ~MRB_STR_EMBED_LEN_MASK;
759 RSTRING(result)->flags |= tmp_n << MRB_STR_EMBED_LEN_SHIFT;
760 }
761 else {
762 RSTRING(result)->as.heap.len = blen;
763 }
764 if (flags&(FPREC|FWIDTH)) {
765 slen = RSTRING_LEN(str);
766 if (slen < 0) {
767 mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid mbstring sequence");
768 }
769 if ((flags&FPREC) && (prec < slen)) {
770 char *p = RSTRING_PTR(str) + prec;
771 slen = prec;
772 len = (mrb_int)(p - RSTRING_PTR(str));
773 }
774 /* need to adjust multi-byte string pos */
775 if ((flags&FWIDTH) && (width > slen)) {
776 width -= (int)slen;
777 if (!(flags&FMINUS)) {
778 FILL(' ', width);
779 }
780 PUSH(RSTRING_PTR(str), len);
781 if (flags&FMINUS) {
782 FILL(' ', width);
783 }
784 break;
785 }
786 }
787 PUSH(RSTRING_PTR(str), len);
788 }
789 break;
790
791 case 'd':
792 case 'i':
793 case 'o':
794 case 'x':
795 case 'X':
796 case 'b':
797 case 'B':
798 case 'u': {
799 mrb_value val = GETARG();
800 char nbuf[68], *s;
801 const char *prefix = NULL;
802 int sign = 0, dots = 0;
803 char sc = 0;
804 mrb_int v = 0;
805 int base;
806 mrb_int len;
807
808 if (flags & FSHARP) {
809 switch (*p) {
810 case 'o': prefix = "0"; break;
811 case 'x': prefix = "0x"; break;
812 case 'X': prefix = "0X"; break;
813 case 'b': prefix = "0b"; break;
814 case 'B': prefix = "0B"; break;
815 default: break;
816 }
817 }
818
819 bin_retry:
820 switch (mrb_type(val)) {
821#ifndef MRB_WITHOUT_FLOAT
822 case MRB_TT_FLOAT:
823 val = mrb_flo_to_fixnum(mrb, val);
824 if (mrb_fixnum_p(val)) goto bin_retry;
825 break;
826#endif
827 case MRB_TT_STRING:
828 val = mrb_str_to_inum(mrb, val, 0, TRUE);
829 goto bin_retry;
830 case MRB_TT_FIXNUM:
831 v = mrb_fixnum(val);
832 break;
833 default:
834 val = mrb_Integer(mrb, val);
835 goto bin_retry;
836 }
837
838 switch (*p) {
839 case 'o':
840 base = 8; break;
841 case 'x':
842 case 'X':
843 base = 16; break;
844 case 'b':
845 case 'B':
846 base = 2; break;
847 case 'u':
848 case 'd':
849 case 'i':
850 sign = 1;
851 /* fall through */
852 default:
853 base = 10; break;
854 }
855
856 if (sign) {
857 if (v >= 0) {
858 if (flags & FPLUS) {
859 sc = '+';
860 width--;
861 }
862 else if (flags & FSPACE) {
863 sc = ' ';
864 width--;
865 }
866 }
867 else {
868 sc = '-';
869 width--;
870 }
871 mrb_assert(base == 10);
872 snprintf(nbuf, sizeof(nbuf), "%" MRB_PRId, v);
873 s = nbuf;
874 if (v < 0) s++; /* skip minus sign */
875 }
876 else {
877 s = nbuf;
878 if (v < 0) {
879 dots = 1;
880 }
881 switch (base) {
882 case 2:
883 if (v < 0) {
884 val = mrb_fix2binstr(mrb, mrb_fixnum_value(v), base);
885 }
886 else {
887 val = mrb_fixnum_to_str(mrb, mrb_fixnum_value(v), base);
888 }
889 strncpy(++s, RSTRING_PTR(val), sizeof(nbuf)-1);
890 break;
891 case 8:
892 snprintf(++s, sizeof(nbuf)-1, "%" MRB_PRIo, v);
893 break;
894 case 16:
895 snprintf(++s, sizeof(nbuf)-1, "%" MRB_PRIx, v);
896 break;
897 }
898 if (v < 0) {
899 char d;
900
901 s = remove_sign_bits(s, base);
902 switch (base) {
903 case 16: d = 'f'; break;
904 case 8: d = '7'; break;
905 case 2: d = '1'; break;
906 default: d = 0; break;
907 }
908
909 if (d && *s != d) {
910 *--s = d;
911 }
912 }
913 }
914 {
915 size_t size;
916 size = strlen(s);
917 /* PARANOID: assert(size <= MRB_INT_MAX) */
918 len = (mrb_int)size;
919 }
920
921 if (*p == 'X') {
922 char *pp = s;
923 int c;
924 while ((c = (int)(unsigned char)*pp) != 0) {
925 *pp = toupper(c);
926 pp++;
927 }
928 }
929
930 if (prefix && !prefix[1]) { /* octal */
931 if (dots) {
932 prefix = NULL;
933 }
934 else if (len == 1 && *s == '0') {
935 len = 0;
936 if (flags & FPREC) prec--;
937 }
938 else if ((flags & FPREC) && (prec > len)) {
939 prefix = NULL;
940 }
941 }
942 else if (len == 1 && *s == '0') {
943 prefix = NULL;
944 }
945
946 if (prefix) {
947 size_t size;
948 size = strlen(prefix);
949 /* PARANOID: assert(size <= MRB_INT_MAX).
950 * this check is absolutely paranoid. */
951 width -= (mrb_int)size;
952 }
953
954 if ((flags & (FZERO|FMINUS|FPREC)) == FZERO) {
955 prec = width;
956 width = 0;
957 }
958 else {
959 if (prec < len) {
960 if (!prefix && prec == 0 && len == 1 && *s == '0') len = 0;
961 prec = len;
962 }
963 width -= prec;
964 }
965
966 if (!(flags&FMINUS) && width > 0) {
967 FILL(' ', width);
968 width = 0;
969 }
970
971 if (sc) PUSH(&sc, 1);
972
973 if (prefix) {
974 int plen = (int)strlen(prefix);
975 PUSH(prefix, plen);
976 }
977 if (dots) {
978 prec -= 2;
979 width -= 2;
980 PUSH("..", 2);
981 }
982
983 if (prec > len) {
984 CHECK(prec - len);
985 if ((flags & (FMINUS|FPREC)) != FMINUS) {
986 char c = '0';
987 FILL(c, prec - len);
988 } else if (v < 0) {
989 char c = sign_bits(base, p);
990 FILL(c, prec - len);
991 }
992 }
993 PUSH(s, len);
994 if (width > 0) {
995 FILL(' ', width);
996 }
997 }
998 break;
999
1000#ifndef MRB_WITHOUT_FLOAT
1001 case 'f':
1002 case 'g':
1003 case 'G':
1004 case 'e':
1005 case 'E':
1006 case 'a':
1007 case 'A': {
1008 mrb_value val = GETARG();
1009 double fval;
1010 mrb_int need = 6;
1011 char fbuf[32];
1012
1013 fval = mrb_float(mrb_Float(mrb, val));
1014 if (!isfinite(fval)) {
1015 const char *expr;
1016 const mrb_int elen = 3;
1017 char sign = '\0';
1018
1019 if (isnan(fval)) {
1020 expr = "NaN";
1021 }
1022 else {
1023 expr = "Inf";
1024 }
1025 need = elen;
1026 if (!isnan(fval) && fval < 0.0)
1027 sign = '-';
1028 else if (flags & (FPLUS|FSPACE))
1029 sign = (flags & FPLUS) ? '+' : ' ';
1030 if (sign)
1031 ++need;
1032 if ((flags & FWIDTH) && need < width)
1033 need = width;
1034
1035 if (need < 0) {
1036 mrb_raise(mrb, E_ARGUMENT_ERROR, "width too big");
1037 }
1038 FILL(' ', need);
1039 if (flags & FMINUS) {
1040 if (sign)
1041 buf[blen - need--] = sign;
1042 memcpy(&buf[blen - need], expr, elen);
1043 }
1044 else {
1045 if (sign)
1046 buf[blen - elen - 1] = sign;
1047 memcpy(&buf[blen - elen], expr, elen);
1048 }
1049 break;
1050 }
1051
1052 fmt_setup(fbuf, sizeof(fbuf), *p, flags, width, prec);
1053 need = 0;
1054 if (*p != 'e' && *p != 'E') {
1055 int i;
1056 frexp(fval, &i);
1057 if (i > 0)
1058 need = BIT_DIGITS(i);
1059 }
1060 if (need > MRB_INT_MAX - ((flags&FPREC) ? prec : 6)) {
1061 too_big_width:
1062 mrb_raise(mrb, E_ARGUMENT_ERROR,
1063 (width > prec ? "width too big" : "prec too big"));
1064 }
1065 need += (flags&FPREC) ? prec : 6;
1066 if ((flags&FWIDTH) && need < width)
1067 need = width;
1068 if (need > MRB_INT_MAX - 20) {
1069 goto too_big_width;
1070 }
1071 need += 20;
1072
1073 CHECK(need);
1074 n = snprintf(&buf[blen], need, fbuf, fval);
1075 if (n < 0 || n >= need) {
1076 mrb_raise(mrb, E_RUNTIME_ERROR, "formatting error");
1077 }
1078 blen += n;
1079 }
1080 break;
1081#endif
1082 }
1083 flags = FNONE;
1084 }
1085
1086 sprint_exit:
1087#if 0
1088 /* XXX - We cannot validate the number of arguments if (digit)$ style used.
1089 */
1090 if (posarg >= 0 && nextarg < argc) {
1091 const char *mesg = "too many arguments for format string";
1092 if (mrb_test(ruby_debug)) mrb_raise(mrb, E_ARGUMENT_ERROR, mesg);
1093 if (mrb_test(ruby_verbose)) mrb_warn(mrb, "%s", mesg);
1094 }
1095#endif
1096 mrb_str_resize(mrb, result, blen);
1097
1098 return result;
1099}
1100
1101#ifndef MRB_WITHOUT_FLOAT
1102static void
1103fmt_setup(char *buf, size_t size, int c, int flags, mrb_int width, mrb_int prec)
1104{
1105 char *end = buf + size;
1106 int n;
1107
1108 *buf++ = '%';
1109 if (flags & FSHARP) *buf++ = '#';
1110 if (flags & FPLUS) *buf++ = '+';
1111 if (flags & FMINUS) *buf++ = '-';
1112 if (flags & FZERO) *buf++ = '0';
1113 if (flags & FSPACE) *buf++ = ' ';
1114
1115 if (flags & FWIDTH) {
1116 n = snprintf(buf, end - buf, "%d", (int)width);
1117 buf += n;
1118 }
1119
1120 if (flags & FPREC) {
1121 n = snprintf(buf, end - buf, ".%d", (int)prec);
1122 buf += n;
1123 }
1124
1125 *buf++ = c;
1126 *buf = '\0';
1127}
1128#endif
Note: See TracBrowser for help on using the repository browser.