• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/tests/custom/03_stdlib/27_sprintf

  1 The `sprintf()` function formats given input value according to a format
  2 string specified as first argument. The format string mimicks the syntax
  3 and directives used by C printf().
  4 
  5 Each directive (with the exception of %%) in the format string expects
  6 a corresponding argument. If fewer arguments are passed to sprintf() than
  7 required by the format string, missing values will be assumed to be `null`
  8 and be interpreted accordingly. Excess arguments are ignored.
  9 
 10 Returns an output string formatted according to the format string with all
 11 format directives interpolated by their respective values.
 12 
 13 Returns an empty string in case the format string argument is not a valid
 14 string value.
 15 
 16 -- Testcase --
 17 {%
 18         printf("%.J\n", [
 19                 // String interpolation, corresponding value will be converted
 20                 // into string if needed.
 21                 sprintf("Hello %s!", "World"),
 22                 sprintf("Hello %s!", false),
 23                 sprintf("Hello %s!", 123),
 24                 sprintf("Hello %s!", null),
 25                 sprintf("Hello %s!"),
 26 
 27                 // Signed integer interpolation, corresponding value will be
 28                 // converted into integer if needed. Also `d` and `i` are aliases.
 29                 sprintf("%d", 123),
 30                 sprintf("%i", 456.789),
 31                 sprintf("%d", true),
 32                 sprintf("%d", "0x42"),
 33                 sprintf("%d", "invalid"),
 34                 sprintf("%d", null),
 35                 sprintf("%d", 0xffffffffffffffff),
 36                 sprintf("%d"),
 37 
 38                 // Unsigned integer interpolation in decimal notation, corresponding
 39                 // value will be converted into unsigned integer if needed.
 40                 sprintf("%u", 123),
 41                 sprintf("%u", -123),
 42                 sprintf("%u", 0xffffffffffffffff),
 43                 sprintf("%u", 456.789),
 44                 sprintf("%u", "invalid"),
 45                 sprintf("%u", null),
 46                 sprintf("%u"),
 47 
 48                 // Unsigned integer interpolation in octal notation, corresponding
 49                 // value will be converted into unsigned integer if needed.
 50                 sprintf("%o", 123),
 51                 sprintf("%o", -123),
 52                 sprintf("%o", 0xffffffffffffffff),
 53                 sprintf("%o", 456.789),
 54                 sprintf("%o", "invalid"),
 55                 sprintf("%o", null),
 56                 sprintf("%o"),
 57 
 58                 // Unsigned integer interpolation in lower case hexadecimal notation,
 59                 // corresponding value will be converted into unsigned integer if
 60                 // needed.
 61                 sprintf("%x", 123),
 62                 sprintf("%x", -123),
 63                 sprintf("%x", 0xffffffffffffffff),
 64                 sprintf("%x", 456.789),
 65                 sprintf("%x", "invalid"),
 66                 sprintf("%x", null),
 67                 sprintf("%x"),
 68 
 69                 // Unsigned integer interpolation in upper case hexadecimal notation,
 70                 // corresponding value will be converted into unsigned integer if
 71                 // needed.
 72                 sprintf("%X", 123),
 73                 sprintf("%X", -123),
 74                 sprintf("%X", 0xffffffffffffffff),
 75                 sprintf("%X", 456.789),
 76                 sprintf("%X", "invalid"),
 77                 sprintf("%X", null),
 78                 sprintf("%X"),
 79 
 80                 // Floating point value interpolation in exponential notation,
 81                 // corresponding value will be converted to double if needed.
 82                 sprintf("%e", 123),
 83                 sprintf("%e", -123),
 84                 sprintf("%e", 456.789),
 85                 sprintf("%e", -456.789),
 86                 sprintf("%e", "invalid"),
 87                 sprintf("%e", null),
 88                 sprintf("%e"),
 89 
 90                 // Floating point value interpolation in exponential notation,
 91                 // using uppercase characters. Corresponding value will be converted
 92                 // to double if needed.
 93                 sprintf("%E", 123),
 94                 sprintf("%E", -123),
 95                 sprintf("%E", 456.789),
 96                 sprintf("%E", -456.789),
 97                 sprintf("%E", "invalid"),
 98                 sprintf("%E", null),
 99                 sprintf("%E"),
100 
101                 // Floating point value interpolation in decimal point notation,
102                 // corresponding value will be converted to double if needed.
103                 sprintf("%f", 123),
104                 sprintf("%f", -123),
105                 sprintf("%f", 456.789),
106                 sprintf("%f", -456.789),
107                 sprintf("%f", "invalid"),
108                 sprintf("%f", null),
109                 sprintf("%f"),
110 
111                 // Floating point value interpolation in decimal point notation,
112                 // using uppercase characters. Corresponding value will be converted
113                 // to double if needed.
114                 sprintf("%F", 123),
115                 sprintf("%F", -123),
116                 sprintf("%F", 456.789),
117                 sprintf("%F", -456.789),
118                 sprintf("%F", "invalid"),
119                 sprintf("%F", null),
120                 sprintf("%F"),
121 
122                 // Floating point value interpolation in either decimal point or
123                 // exponential notation, depending on size of exponent. Corresponding
124                 // value will be converted to double if needed.
125                 sprintf("%g", 123.456),
126                 sprintf("%g", 0.0000001),
127                 sprintf("%g", "invalid"),
128 
129                 // Floating point value interpolation in either decimal point or
130                 // exponential notation, depending on size of exponent and using
131                 // uppercase characters. Corresponding value will be converted to
132                 // double if needed.
133                 sprintf("%G", 123.456),
134                 sprintf("%G", 0.0000001),
135                 sprintf("%G", "invalid"),
136 
137                 // Character interpolation. The corresponding value is casted as `char`
138                 // and the resulting character is interpolated.
139                 sprintf("%c", 65),
140                 sprintf("%c", -1),
141                 sprintf("%c", 456.789),
142                 sprintf("%c", "invalid"),
143 
144                 // JSON interpolation. The corresponding value is JSON encoded and
145                 // interpolated as string.
146                 sprintf("%J", "Hello\n"),
147                 sprintf("%J", 123),
148                 sprintf("%J", [ 1, 2, 3 ]),
149                 sprintf("%J", { some: "dictionary", an: [ "array", true, false ] }),
150                 sprintf("%J", null),
151                 sprintf("%J"),
152 
153                 // Escaping `%`. The `%%` format string will produce a literal `%`.
154                 // No corresponding argument is expected.
155                 sprintf("%%")
156         ]);
157 %}
158 -- End --
159 
160 -- Expect stdout --
161 [
162         "Hello World!",
163         "Hello false!",
164         "Hello 123!",
165         "Hello (null)!",
166         "Hello (null)!",
167         "123",
168         "456",
169         "1",
170         "66",
171         "0",
172         "0",
173         "-1",
174         "0",
175         "123",
176         "18446744073709551493",
177         "18446744073709551615",
178         "456",
179         "0",
180         "0",
181         "0",
182         "173",
183         "1777777777777777777605",
184         "1777777777777777777777",
185         "710",
186         "0",
187         "0",
188         "0",
189         "7b",
190         "ffffffffffffff85",
191         "ffffffffffffffff",
192         "1c8",
193         "0",
194         "0",
195         "0",
196         "7B",
197         "FFFFFFFFFFFFFF85",
198         "FFFFFFFFFFFFFFFF",
199         "1C8",
200         "0",
201         "0",
202         "0",
203         "1.230000e+02",
204         "-1.230000e+02",
205         "4.567890e+02",
206         "-4.567890e+02",
207         "nan",
208         "0.000000e+00",
209         "0.000000e+00",
210         "1.230000E+02",
211         "-1.230000E+02",
212         "4.567890E+02",
213         "-4.567890E+02",
214         "NAN",
215         "0.000000E+00",
216         "0.000000E+00",
217         "123.000000",
218         "-123.000000",
219         "456.789000",
220         "-456.789000",
221         "nan",
222         "0.000000",
223         "0.000000",
224         "123.000000",
225         "-123.000000",
226         "456.789000",
227         "-456.789000",
228         "NAN",
229         "0.000000",
230         "0.000000",
231         "123.456",
232         "1e-07",
233         "nan",
234         "123.456",
235         "1E-07",
236         "NAN",
237         "A",
238         "ÿ",
239         "È",
240         "\u0000",
241         "\"Hello\\n\"",
242         "123",
243         "[ 1, 2, 3 ]",
244         "{ \"some\": \"dictionary\", \"an\": [ \"array\", true, false ] }",
245         "null",
246         "null",
247         "%"
248 ]
249 -- End --
250 
251 
252 Field widths may be specified for format directives.
253 
254 -- Testcase --
255 {%
256         printf("%.J\n", [
257                 // by default the output of a format directive is as long as the
258                 // string representation of the corresponding value
259                 sprintf("[%s]", "test"),
260 
261                 // by specifying a field width, the output will be padded to the
262                 // given length
263                 sprintf("[%10s]", "test"),
264 
265                 // the same applies to numbers
266                 sprintf("[%10d]", 123),
267                 sprintf("[%10f]", 1.0),
268 
269                 // and to char formats
270                 sprintf("[%10c]", 65),
271 
272                 // field width is not applicable to `%` formats
273                 sprintf("[%10%]")
274         ]);
275 %}
276 -- End --
277 
278 -- Expect stdout --
279 [
280         "[test]",
281         "[      test]",
282         "[       123]",
283         "[  1.000000]",
284         "[         A]",
285         "[%]"
286 ]
287 -- End --
288 
289 
290 Precisions may be specified for format directives.
291 
292 -- Testcase --
293 {%
294         print(join("\n", [
295                 // For `f`, `F`, `e` and `E`, the precision specifies the amount of
296                 // digits after the comma
297                 sprintf("[%.3f]", 1/3),
298                 sprintf("[%.3F]", 1/3),
299                 sprintf("[%.3e]", 1/3),
300                 sprintf("[%.3E]", 1/3),
301 
302                 // For `g` and `G` the precision specifies the number of significant
303                 // digits to print before switching to exponential notation
304                 sprintf("[%.3g]", 1000.1),
305                 sprintf("[%.3G]", 1000.1),
306 
307                 // For strings, the precision specifies the amount of characters to
308                 // print at most
309                 sprintf("[%.5s]", "test"),
310                 sprintf("[%.3s]", "test"),
311 
312                 // For JSON format, the precision specifies the amount of indentation
313                 // to use. Omitting precision will not indent, specifying a precision
314                 // of `0` uses tabs for indentation, any other precision uses this
315                 // many spaces
316                 sprintf("<%J>", [ 1, 2, 3, { true: false } ]),    // no indent
317                 sprintf("<%.J>", [ 1, 2, 3, { true: false } ]),   // tab indent
318                 sprintf("<%.0J>", [ 1, 2, 3, { true: false } ]),  // tab indent
319                 sprintf("<%.1J>", [ 1, 2, 3, { true: false } ]),  // indent using one space
320                 sprintf("<%.4J>", [ 1, 2, 3, { true: false } ]),  // indent using four spaces
321 
322                 // precision does not apply to char, integer or `%` formats
323                 sprintf("[%.3d]", 1000),
324                 sprintf("[%.3c]", 65),
325                 sprintf("[%.3%]"),
326         ]), "\n");
327 %}
328 -- End --
329 
330 -- Expect stdout --
331 [0.000]
332 [0.000]
333 [0.000e+00]
334 [0.000E+00]
335 [1e+03]
336 [1E+03]
337 [test]
338 [tes]
339 <[ 1, 2, 3, { "true": false } ]>
340 <[
341         1,
342         2,
343         3,
344         {
345                 "true": false
346         }
347 ]>
348 <[
349         1,
350         2,
351         3,
352         {
353                 "true": false
354         }
355 ]>
356 <[
357  1,
358  2,
359  3,
360  {
361   "true": false
362  }
363 ]>
364 <[
365     1,
366     2,
367     3,
368     {
369         "true": false
370     }
371 ]>
372 [1000]
373 [A]
374 [%]
375 -- End --
376 
377 
378 A number of flag characters are supported for format directives.
379 
380 -- Testcase --
381 {%
382         printf("%.J\n", [
383                 // The recognized flag characters are `#`, `0`, `-`, `+` and ` ` (space)
384                 sprintf("%#0+- s", "test"),
385 
386                 // Repetitions of flag characters are accepted
387                 sprintf("%###s", "test"),
388                 sprintf("%000s", "test"),
389                 sprintf("%++-s", "test"),
390                 sprintf("%-- s", "test"),
391                 sprintf("%   s", "test"),
392 
393                 // The `#` flag produces alternative forms of various conversions
394                 sprintf("%o / %#o", 15, 15),
395                 sprintf("%x / %#x", 16, 16),
396                 sprintf("%X / %#X", 17, 17),
397                 sprintf("%g / %#g", 1.0, 1.0),
398 
399                 // The `0` flag indicates zero- instead of space-padding for various
400                 // numeric conversions.
401                 sprintf("%5d / %05d", -10, -10),
402                 sprintf("%5d / %05d", 11, 11),
403                 sprintf("%5g / %05g", -12.0, -12.0),
404                 sprintf("%5g / %05g", 13.0, 13.0),
405                 sprintf("%5s / %05s", "a", "a"),
406 
407                 // The `-` flag indicates left, instead of right padding. It will
408                 // override `0` and always pad with spaces
409                 sprintf("%-5d / %-05d", -10, -10),
410                 sprintf("%-5d / %-05d", 11, 11),
411                 sprintf("%-5g / %-05g", -12.0, -12.0),
412                 sprintf("%-5g / %-05g", 13.0, 13.0),
413                 sprintf("%-5s / %-05s", "a", "a"),
414 
415                 // The `+` flag indicates that a sign (`+` or `-`) should be placed
416                 // before signed numeric values. It overrides ` ` (space).
417                 sprintf("%+5d / %+05d", -10, -10),
418                 sprintf("%+5d / %+05d", 11, 11),
419                 sprintf("%+5g / %+05g", -12.0, -12.0),
420                 sprintf("%+5g / %+05g", 13.0, 13.0),
421                 sprintf("%+5s / %+05s", "a", "a"),
422 
423                 // The ` ` (space) flag indicates that a blank should be placed
424                 // before positive numbers (useful to ensure that negative and
425                 // positive values in output are aligned)
426                 sprintf("%-5d / %- 5d", -10, -10),
427                 sprintf("%-5d / %- 5d", 11, 11),
428                 sprintf("%-5g / %- 5g", -12.0, -12.0),
429                 sprintf("%-5g / %- 5g", 13.0, 13.0),
430                 sprintf("%-5s / %- 5s", "a", "a"),
431         ]);
432 %}
433 -- End --
434 
435 -- Expect stdout --
436 [
437         "test",
438         "test",
439         "test",
440         "test",
441         "test",
442         "test",
443         "17 / 017",
444         "10 / 0x10",
445         "11 / 0X11",
446         "1 / 1.00000",
447         "  -10 / -0010",
448         "   11 / 00011",
449         "  -12 / -0012",
450         "   13 / 00013",
451         "    a /     a",
452         "-10   / -10  ",
453         "11    / 11   ",
454         "-12   / -12  ",
455         "13    / 13   ",
456         "a     / a    ",
457         "  -10 / -0010",
458         "  +11 / +0011",
459         "  -12 / -0012",
460         "  +13 / +0013",
461         "    a /     a",
462         "-10   / -10  ",
463         "11    /  11  ",
464         "-12   / -12  ",
465         "13    /  13  ",
466         "a     / a    "
467 ]
468 -- End --
469 
470 
471 Unrecognized format directives are copied to the output string as-is.
472 
473 -- Testcase --
474 {%
475         printf("%.J\n", [
476                 // A truncated format directive is preserved
477                 sprintf("test %", "test"),
478                 sprintf("test %-010.3", "test"),
479 
480                 // An unrecognized format directive is preserved
481                 sprintf("test %y test", 123),
482                 sprintf("test %~123s test", 123)
483         ]);
484 %}
485 -- End --
486 
487 -- Expect stdout --
488 [
489         "test %",
490         "test %-010.3",
491         "test %y test",
492         "test %~123s test"
493 ]
494 -- End --
495 
496 
497 Missing values for format directives are treated as `null`.
498 
499 -- Testcase --
500 {%
501         printf("%.J\n", [
502                 sprintf("%s"),
503                 sprintf("%d"),
504                 sprintf("%u"),
505                 sprintf("%o"),
506                 sprintf("%x"),
507                 sprintf("%X"),
508                 sprintf("%f"),
509                 sprintf("%F"),
510                 sprintf("%e"),
511                 sprintf("%E"),
512                 sprintf("%g"),
513                 sprintf("%G"),
514                 sprintf("%c"),
515                 sprintf("%J")
516         ]);
517 %}
518 -- End --
519 
520 -- Expect stdout --
521 [
522         "(null)",
523         "0",
524         "0",
525         "0",
526         "0",
527         "0",
528         "0.000000",
529         "0.000000",
530         "0.000000e+00",
531         "0.000000E+00",
532         "0",
533         "0",
534         "\u0000",
535         "null"
536 ]
537 -- End --
538 
539 
540 Supplying a non-string format value will yield an empty string result.
541 
542 -- Testcase --
543 {%
544         printf("%.J\n", sprintf(true, 1, 2, 3));
545 %}
546 -- End --
547 
548 -- Expect stdout --
549 ""
550 -- End --
551 
552 
553 Prefixing a format directive with `n$` will select the corresponding argument
554 with 1 referring to the first argument. Missing or out-of range arguments will
555 be treated as `null`.
556 
557 -- Testcase --
558 {%
559         printf("%.J\n", [
560                 sprintf("%2$s", "foo", "bar", "baz"),
561                 sprintf("%10$s", "foo", "bar", "baz")
562         ]);
563 %}
564 -- End --
565 
566 -- Expect stdout --
567 [
568         "bar",
569         "(null)"
570 ]
571 -- End --

This page was automatically generated by LXR 0.3.1.  •  OpenWrt