@@ -344,6 +344,99 @@ Examples:
344
344
"Perl".flip; # lreP
345
345
"ABBA".flip; # ABBA
346
346
347
+ = head2 sub printf
348
+
349
+ multi sub printf (Str:D $format, *@args)
350
+
351
+ This function is mostly identical to the C library printf function.
352
+
353
+ The C < $format > is scanned for C < % > characters. Any C < % > introduces a
354
+ format token. Format tokens have the following grammar:
355
+
356
+ grammar Str::PrintfFormat {
357
+ regex format_token { '%': <index>? <precision>? <modifier>? <directive> }
358
+ token index { \d+ '$' }
359
+ token precision { <flags>? <vector>? <precision_count> }
360
+ token flags { <[ \x20 + 0 \# \- ]>+ }
361
+ token precision_count { [ <[1..9]>\d* | '*' ]? [ '.' [ \d* | '*' ] ]? }
362
+ token vector { '*'? v }
363
+ token modifier { < ll l h V q L > }
364
+ token directive { < % c s d u o x e f g X E G b p n i D U O F > }
365
+ }
366
+
367
+ Directives guide the use (if any) of the arguments. When a directive
368
+ (other than C < % > ) is used, it indicates how the next argument
369
+ passed is to be formatted into the string to be printed.
370
+
371
+ The directives are:
372
+
373
+ = begin table
374
+
375
+ % a literal percent sign
376
+ c a character with the given codepoint
377
+ s a string
378
+ d a signed integer, in decimal
379
+ u an unsigned integer, in decimal
380
+ o an unsigned integer, in octal
381
+ x an unsigned integer, in hexadecimal
382
+ e a floating-point number, in scientific notation
383
+ f a floating-point number, in fixed decimal notation
384
+ g a floating-point number, in %e or %f notation
385
+ X like x, but using uppercase letters
386
+ E like e, but using an uppercase "E"
387
+ G like g, but with an uppercase "E" (if applicable)
388
+ b an unsigned integer, in binary
389
+
390
+ = end table
391
+
392
+ Compatibility:
393
+
394
+ = begin table
395
+
396
+ i a synonym for %d
397
+ D a synonym for %ld
398
+ U a synonym for %lu
399
+ O a synonym for %lo
400
+ F a synonym for %f
401
+
402
+ = end table
403
+
404
+ Perl 5 (non-)compatibility:
405
+
406
+ = begin table
407
+
408
+ n produces a runtime exception
409
+ p produces a runtime exception
410
+
411
+ = end table
412
+
413
+ Modifiers change the meaning of format directives, but are largely
414
+ no-ops (the semantics are still being determined).
415
+
416
+ = begin table
417
+
418
+ h interpret integer as native "short" (typically int16)
419
+ l interpret integer as native "long" (typically int32 or int64)
420
+ ll interpret integer as native "long long" (typically int64)
421
+ L interpret integer as native "long long" (typically uint64)
422
+ q interpret integer as native "quads" (typically int64 or larger)
423
+
424
+ = end table
425
+
426
+ Examples:
427
+
428
+ printf('%c', [97]); # a
429
+ printf("%.2f", [1.969]); # 1.97
430
+ printf("%+.3f", [3.141592]); # +3.142
431
+ printf('%2$d %1$d', [12, 34]); # 34 12
432
+ printf("%x", [255]); # ff
433
+
434
+ Special case: printf("<b>%s</b>\n", "Perl 6") will not work use either of the following:
435
+
436
+ printf Q:b "<b>%s</b>\n", "Perl 6";
437
+ printf "<b>\%s</b>\n", "Perl 6";
438
+ printf "<b>%s\</b>\n", "Perl 6";
439
+
347
440
= head2 sub sprintf
348
441
349
442
multi sub sprintf ( Str:D $format, *@args) returns Str:D
0 commit comments