Skip to content

Commit 44d7b22

Browse files
authored
Merge pull request #855 from tbrowder/sprintf
Changes to files:
2 parents 2ff9fbc + b9e4c2a commit 44d7b22

File tree

3 files changed

+50
-101
lines changed

3 files changed

+50
-101
lines changed

doc/Type/IO/Handle.pod6

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,34 @@ $fh.print("some text");
116116
$fh.print-nl; # \r\n
117117
$fh.close;
118118
119+
=head2 method printf
120+
121+
This method is mostly identical to the C library C<printf> and
122+
sprintf functions. The only difference between the two functions is
123+
that C<sprintf> returns a string while the C<printf> function writes
124+
to a file. See L<sub sprintf|https://docs.perl6.org/type/Str#sub_sprintf> for details about
125+
the Perl 6 C<$format> implementation
126+
127+
Note that currently C<printf> is not a method for C<IO::Handle>, but
128+
its effect may be duplicated with a work-around using C<sub sprintf>.
129+
For example:
130+
131+
=for code :skip-test
132+
my $fh = open 'path/to/file', :w;
133+
$fh.say(printf("%d\n", 32));
134+
$fh.close;
135+
136+
The C<say> method (see below) may be used for an automatic ending
137+
newline:
138+
139+
=for code :skip-test
140+
my $fh = open 'path/to/file', :w;
141+
$fh.say(printf("%d\n", 32));
142+
$fh.close;
143+
144+
See L<sub sprintf|https://docs.perl6.org/type/Str#sub_sprintf>
145+
for details about formatting and more examples.
146+
119147
=head2 method say
120148
121149
method say(IO::Handle:D: |)

doc/Type/Str.pod6

Lines changed: 16 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -312,104 +312,14 @@ Examples:
312312
"Perl".flip; # lreP
313313
"ABBA".flip; # ABBA
314314
315-
=head2 sub printf
316-
317-
multi sub printf(Str:D $format, *@args)
318-
319-
This function is mostly identical to the C library printf function.
320-
321-
The C<$format> is scanned for C<%> characters. Any C<%> introduces a
322-
format token. Format tokens have the following grammar:
323-
324-
grammar Str::PrintfFormat {
325-
regex format_token { '%': <index>? <precision>? <modifier>? <directive> }
326-
token index { \d+ '$' }
327-
token precision { <flags>? <vector>? <precision_count> }
328-
token flags { <[ \x20 + 0 \# \- ]>+ }
329-
token precision_count { [ <[1..9]>\d* | '*' ]? [ '.' [ \d* | '*' ] ]? }
330-
token vector { '*'? v }
331-
token modifier { < ll l h V q L > }
332-
token directive { < % c s d u o x e f g X E G b p n i D U O F > }
333-
}
334-
335-
Directives guide the use (if any) of the arguments. When a directive
336-
(other than C<%>) is used, it indicates how the next argument
337-
passed is to be formatted into the string to be printed.
338-
339-
The directives are:
340-
341-
=begin table
342-
343-
% a literal percent sign
344-
c a character with the given codepoint
345-
s a string
346-
d a signed integer, in decimal
347-
u an unsigned integer, in decimal
348-
o an unsigned integer, in octal
349-
x an unsigned integer, in hexadecimal
350-
e a floating-point number, in scientific notation
351-
f a floating-point number, in fixed decimal notation
352-
g a floating-point number, in %e or %f notation
353-
X like x, but using uppercase letters
354-
E like e, but using an uppercase "E"
355-
G like g, but with an uppercase "E" (if applicable)
356-
b an unsigned integer, in binary
357-
358-
=end table
359-
360-
Compatibility:
361-
362-
=begin table
363-
364-
i a synonym for %d
365-
D a synonym for %ld
366-
U a synonym for %lu
367-
O a synonym for %lo
368-
F a synonym for %f
369-
370-
=end table
371-
372-
Perl 5 (non-)compatibility:
373-
374-
=begin table
375-
376-
n produces a runtime exception
377-
p produces a runtime exception
378-
379-
=end table
380-
381-
Modifiers change the meaning of format directives, but are largely
382-
no-ops (the semantics are still being determined).
383-
384-
=begin table
385-
386-
h interpret integer as native "short" (typically int16)
387-
l interpret integer as native "long" (typically int32 or int64)
388-
ll interpret integer as native "long long" (typically int64)
389-
L interpret integer as native "long long" (typically uint64)
390-
q interpret integer as native "quads" (typically int64 or larger)
391-
392-
=end table
393-
394-
Examples:
395-
396-
printf('%c', 97); # a
397-
printf("%.2f", 1.969); # 1.97
398-
printf("%+.3f", 3.141592); # +3.142
399-
printf('%2$d %1$d', 12, 34); # 34 12
400-
printf("%x", 255); # ff
401-
402-
Special case: printf("<b>%s</b>\n", "Perl 6") will not work use either of the following:
403-
404-
printf Q:b "<b>%s</b>\n", "Perl 6";
405-
printf "<b>\%s</b>\n", "Perl 6";
406-
printf "<b>%s\</b>\n", "Perl 6";
407-
408315
=head2 sub sprintf
409316
410317
multi sub sprintf( Str:D $format, *@args) returns Str:D
411318
412-
This function is mostly identical to the C library sprintf function.
319+
This function is mostly identical to the C library C<sprintf> and
320+
C<printf> functions. The only difference between the two
321+
functions is that C<sprintf> returns a string while the C<printf> function
322+
writes to a file.
413323
414324
The C<$format> is scanned for C<%> characters. Any C<%> introduces a
415325
format token. Format tokens have the following grammar:
@@ -427,7 +337,7 @@ format token. Format tokens have the following grammar:
427337
428338
Directives guide the use (if any) of the arguments. When a directive
429339
(other than C<%>) is used, it indicates how the next argument
430-
passed is to be formatted into the string.
340+
passed is to be formatted into the string to be printed.
431341
432342
The directives are:
433343
@@ -488,12 +398,19 @@ Examples:
488398
489399
=for code :skip-test
490400
sprintf "%ld a big number, %lld a bigger number\n", 4294967295, 4294967296;
401+
sprintf('%c', 97); # a
402+
sprintf("%.2f", 1.969); # 1.97
403+
sprintf("%+.3f", 3.141592); # +3.142
404+
sprintf('%2$d %1$d', 12, 34); # 34 12
405+
sprintf("%x", 255); # ff
491406
492-
Special case: sprintf("<b>%s</b>\n", "Perl 6") will not work use either of the following:
407+
Special case: 'sprintf("<b>%s</b>\n", "Perl 6")' will not work, but
408+
one of the following will:
493409
494-
sprintf Q:b "<b>%s</b>\n", "Perl 6";
495-
sprintf "<b>\%s</b>\n", "Perl 6";
496-
sprintf "<b>%s\</b>\n", "Perl 6";
410+
=for code :skip-test
411+
sprintf Q:b "<b>%s</b>\n", "Perl 6";
412+
sprintf "<b>\%s</b>\n", "Perl 6";
413+
sprintf "<b>%s\</b>\n", "Perl 6";
497414
498415
=head2 method starts-with
499416

xt/trailing-whitespace.t

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ plan +@files;
1818

1919
for @files -> $file {
2020
my $ok = True;
21+
my $row = 0;
2122
for $file.IO.lines -> $line {
22-
if $line ~~ / \s $/ {
23+
++$row;
24+
if $line ~~ / \s $/ {
2325
$ok = False; last;
2426
}
2527
}
26-
ok $ok, "must not have any trailing whitespace in $file";
28+
my $error = $file;
29+
$error ~= " (line $row)" if !$ok;
30+
ok $ok, "$error: Must not have any trailing whitespace.";
2731
}
2832

2933
# vim: expandtab shiftwidth=4 ft=perl6

0 commit comments

Comments
 (0)