Skip to content

Commit 6e413b8

Browse files
authored
Merge pull request #856 from tbrowder/sprintf-format
Add more details of function sprintf formatting
2 parents 44d7b22 + 74abc4a commit 6e413b8

File tree

2 files changed

+224
-3
lines changed

2 files changed

+224
-3
lines changed

doc/Language/variables.pod6

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ variables.
535535
# OUTPUT«["a", "b", Int]␤»
536536
537537
To destructure a list into a single value create a list literal with one value
538-
by using C<($var,). When used with a variable declarator just providing
538+
by using C<($var,)>. When used with a variable declarator just providing
539539
parentheses around a single variable is sufficient.
540540
541541
sub f { 1,2,3 };

doc/Type/Str.pod6

Lines changed: 223 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ format token. Format tokens have the following grammar:
337337
338338
Directives guide the use (if any) of the arguments. When a directive
339339
(other than C<%>) is used, it indicates how the next argument
340-
passed is to be formatted into the string to be printed.
340+
passed is to be formatted into the string to be created.
341341
342342
The directives are:
343343
@@ -394,7 +394,228 @@ no-ops (the semantics are still being determined).
394394
395395
=end table
396396
397-
Examples:
397+
Between the C<%> and the format letter, you may specify several
398+
additional attributes controlling the interpretation of the format. In
399+
order, these are:
400+
401+
=head3 format parameter index
402+
403+
An explicit format parameter index, such as C<2$>. By default,
404+
C<sprintf> will format the next unused argument in the list, but this
405+
allows you to take the arguments out of order:
406+
407+
sprintf '%2$d %1$d', 12, 34; # "34 12"
408+
sprintf '%3$d %d %1$d', 1, 2, 3; # "3 1 1"
409+
410+
=head3 flags
411+
412+
One or more of:
413+
414+
space prefix non-negative number with a space
415+
+ prefix non-negative number with a plus sign
416+
- left-justify within the field
417+
0 use zeros, not spaces, to right-justify
418+
# ensure the leading "0" for any octal,
419+
prefix non-zero hexadecimal with "0x" or "0X",
420+
prefix non-zero binary with "0b" or "0B"
421+
422+
For example:
423+
424+
sprintf '<% d>', 12; # "< 12>"
425+
sprintf '<% d>', 0; # "< 0>"
426+
sprintf '<% d>', -12; # "<-12>"
427+
sprintf '<%+d>', 12; # "<+12>"
428+
sprintf '<%+d>', 0; # "<+0>"
429+
sprintf '<%+d>', -12; # "<-12>"
430+
sprintf '<%6s>', 12; # "< 12>"
431+
sprintf '<%-6s>', 12; # "<12 >"
432+
sprintf '<%06s>', 12; # "<000012>"
433+
sprintf '<%#o>', 12; # "<014>"
434+
sprintf '<%#x>', 12; # "<0xc>"
435+
sprintf '<%#X>', 12; # "<0XC>"
436+
sprintf '<%#b>', 12; # "<0b1100>"
437+
sprintf '<%#B>', 12; # "<0B1100>"
438+
439+
When a space and a plus sign are given as the flags at once, the space
440+
is ignored:
441+
442+
sprintf '<%+ d>', 12; # "<+12>"
443+
sprintf '<% +d>', 12; # "<+12>"
444+
445+
When the C<#> flag and a precision are given in the C<%o> conversion, the
446+
precision is incremented if it's necessary for the leading "0":
447+
448+
sprintf '<%#.5o>', 012; # "<00012>"
449+
sprintf '<%#.5o>', 012345; # "<012345>"
450+
sprintf '<%#.0o>', 0; # "<0>"
451+
452+
=head3 vector flag
453+
454+
This flag tells Perl 6 to interpret the supplied string as a vector of
455+
integers, one for each character in the string. Perl 6 applies the
456+
format to each integer in turn, then joins the resulting strings with
457+
a separator (a dot, C<'.'>, by default). This can be useful for
458+
displaying ordinal values of characters in arbitrary strings:
459+
460+
sprintf "%vd", "AB\x{100}"; # "65.66.256"
461+
sprintf "version is v%vd\n", $^V; # Perl 6's version
462+
463+
You can also explicitly specify the argument number to use for the
464+
join string using something like C<*2$v>; for example:
465+
466+
sprintf '%*4$vX %*4$vX %*4$vX', # 3 IPv6 addresses
467+
@addr[1..3], ":";
468+
469+
=head3 (minimum) width
470+
471+
Arguments are usually formatted to be only as wide as required to
472+
display the given value. You can override the width by putting a
473+
number here, or get the width from the next argument (with C<*> ) or
474+
from a specified argument (e.g., with C<*2$>):
475+
476+
sprintf "<%s>", "a"; # "<a>"
477+
sprintf "<%6s>", "a"; # "< a>"
478+
sprintf "<%*s>", 6, "a"; # "< a>"
479+
sprintf '<%*2$s>', "a", 6; # "< a>"
480+
sprintf "<%2s>", "long"; # "<long>" (does not truncate)
481+
482+
If a field width obtained through C<*> is negative, it has the same
483+
effect as the C<-> flag: left-justification.
484+
485+
=head3 precision, or maximum width
486+
487+
You can specify a precision (for numeric conversions) or a maximum
488+
width (for string conversions) by specifying a C<.> followed by a
489+
number. For floating-point formats, except C<g> and C<G>, this
490+
specifies how many places right of the decimal point to show (the
491+
default being 6). For example:
492+
493+
# these examples are subject to system-specific variation
494+
sprintf '<%f>', 1; # "<1.000000>"
495+
sprintf '<%.1f>', 1; # "<1.0>"
496+
sprintf '<%.0f>', 1; # "<1>"
497+
sprintf '<%e>', 10; # "<1.000000e+01>"
498+
sprintf '<%.1e>', 10; # "<1.0e+01>"
499+
500+
For "g" and "G", this specifies the maximum number of digits to show,
501+
including those prior to the decimal point and those after it; for
502+
example:
503+
504+
# These examples are subject to system-specific variation.
505+
sprintf '<%g>', 1; # "<1>"
506+
sprintf '<%.10g>', 1; # "<1>"
507+
sprintf '<%g>', 100; # "<100>"
508+
sprintf '<%.1g>', 100; # "<1e+02>"
509+
sprintf '<%.2g>', 100.01; # "<1e+02>"
510+
sprintf '<%.5g>', 100.01; # "<100.01>"
511+
sprintf '<%.4g>', 100.01; # "<100>"
512+
513+
For integer conversions, specifying a precision implies that the
514+
output of the number itself should be zero-padded to this width, where
515+
the C<0> flag is ignored:
516+
517+
sprintf '<%.6d>', 1; # "<000001>"
518+
sprintf '<%+.6d>', 1; # "<+000001>"
519+
sprintf '<%-10.6d>', 1; # "<000001 >"
520+
sprintf '<%10.6d>', 1; # "< 000001>"
521+
sprintf '<%010.6d>', 1; # "< 000001>"
522+
sprintf '<%+10.6d>', 1; # "< +000001>"
523+
sprintf '<%.6x>', 1; # "<000001>"
524+
sprintf '<%#.6x>', 1; # "<0x000001>"
525+
sprintf '<%-10.6x>', 1; # "<000001 >"
526+
sprintf '<%10.6x>', 1; # "< 000001>"
527+
sprintf '<%010.6x>', 1; # "< 000001>"
528+
sprintf '<%#10.6x>', 1; # "< 0x000001>"
529+
530+
For string conversions, specifying a precision truncates the string to
531+
fit the specified width:
532+
533+
sprintf '<%.5s>', "truncated"; # "<trunc>"
534+
sprintf '<%10.5s>', "truncated"; # "< trunc>"
535+
536+
You can also get the precision from the next argument using C<.*>, or
537+
from a specified argument (e.g., with C<.*2$>):
538+
539+
sprintf '<%.6x>', 1; # "<000001>"
540+
sprintf '<%.*x>', 6, 1; # "<000001>"
541+
sprintf '<%.*2$x>', 1, 6; # "<000001>"
542+
sprintf '<%6.*2$x>', 1, 4; # "< 0001>"
543+
544+
If a precision obtained through C<*> is negative, it counts as having
545+
no precision at all:
546+
547+
sprintf '<%.*s>', 7, "string"; # "<string>"
548+
sprintf '<%.*s>', 3, "string"; # "<str>"
549+
sprintf '<%.*s>', 0, "string"; # "<>"
550+
sprintf '<%.*s>', -1, "string"; # "<string>"
551+
sprintf '<%.*d>', 1, 0; # "<0>"
552+
sprintf '<%.*d>', 0, 0; # "<>"
553+
sprintf '<%.*d>', -1, 0; # "<0>"
554+
555+
=head3 size
556+
557+
For numeric conversions, you can specify the size to interpret the
558+
number as using C<l>, C<h>, C<V>, C<q>, C<L>, or C<ll>. For integer
559+
conversions (C<d> C<u> C<o> C<x> C<X> C<b> C<i> C<D> C<U> C<O>),
560+
numbers are usually assumed to be whatever the default integer size is
561+
on your platform (usually 32 or 64 bits), but you can override this to
562+
use instead one of the standard C types, as supported by the compiler
563+
used to build Perl 6:
564+
565+
hh interpret integer as C type "char" or "unsigned
566+
char"
567+
h interpret integer as C type "short" or
568+
"unsigned short"
569+
j interpret integer as C type "intmax_t", only with
570+
a C99 compiler (unportable)
571+
l interpret integer as C type "long" or
572+
"unsigned long"
573+
q, L, or ll interpret integer as C type "long long",
574+
"unsigned long long", or "quad" (typically
575+
64-bit integers)
576+
t interpret integer as C type "ptrdiff_t"
577+
z interpret integer as C type "size_t"
578+
579+
=head3 order of arguments
580+
581+
Normally, C<sprintf> takes the next unused argument as the value to
582+
format for each format specification. If the format specification uses
583+
C<*> to require additional arguments, these are consumed from the
584+
argument list in the order they appear in the format specification
585+
before the value to format. Where an argument is specified by an
586+
explicit index, this does not affect the normal order for the
587+
arguments, even when the explicitly specified index would have been
588+
the next argument.
589+
590+
So:
591+
592+
sprintf "<%*.*s>", $a, $b, $c;
593+
594+
uses C<$a> for the width, C<$b> for the precision, and C<$c> as the value to
595+
format; while:
596+
597+
sprintf '<%*1$.*s>', $a, $b;
598+
599+
would use C<$a> for the width and precision and C<$b> as the value to format.
600+
601+
Here are some more examples; be aware that when using an explicit
602+
index, the C<$> may need escaping:
603+
604+
sprintf "%2\$d %d\n", 12, 34; # "34 12\n"
605+
sprintf "%2\$d %d %d\n", 12, 34; # "34 12 34\n"
606+
sprintf "%3\$d %d %d\n", 12, 34, 56; # "56 12 34\n"
607+
sprintf "%2\$*3\$d %d\n", 12, 34, 3; # " 34 12\n"
608+
sprintf "%*1\$.*f\n", 4, 5, 10; # "5.0000\n"
609+
610+
=comment TODO: document effects of locale
611+
612+
613+
614+
615+
616+
617+
618+
Other examples:
398619
399620
=for code :skip-test
400621
sprintf "%ld a big number, %lld a bigger number\n", 4294967295, 4294967296;

0 commit comments

Comments
 (0)