source: EcnlProtoTool/trunk/mruby-1.2.0/mrbgems/mruby-sprintf/src/sprintf.c@ 270

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

mruby版ECNLプロトタイピング・ツールを追加

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