Skip to content

Commit 94ff45f

Browse files
committed
A bunch of other routines moved to the independent routines page
1 parent dc6a388 commit 94ff45f

File tree

2 files changed

+362
-363
lines changed

2 files changed

+362
-363
lines changed

doc/Type/Cool.pod6

Lines changed: 0 additions & 363 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,369 +1423,6 @@ Coerces the invocant to L<IO::Path>.
14231423
=for code
14241424
.say for '.'.IO.dir; # gives a directory listing
14251425
1426-
=head2 routine unpolar
1427-
1428-
Defined as:
1429-
1430-
method unpolar(Real $angle)
1431-
multi sub unpolar(Real $mag, Real $angle)
1432-
1433-
Returns a C<Complex> with the coordinates corresponding to the angle in
1434-
radians and magnitude corresponding to the object value or C<$mag> in
1435-
the case it's being used as a C<sub>
1436-
1437-
say 1.unpolar(⅓*pi);
1438-
# OUTPUT: «0.5000000000000001+0.8660254037844386i␤»
1439-
1440-
=head2 routine printf
1441-
1442-
Defined as:
1443-
1444-
method printf (*@args)
1445-
multi sub printf(Cool:D $format, *@args)
1446-
1447-
As a method, takes the object as a format using
1448-
L<the same language as C<Str.sprintf>|/type/Str#routine_sprintf>; as a sub, its
1449-
first argument will be
1450-
the format string, and the rest of the arguments will be substituted in the
1451-
format following the format conventions.
1452-
1453-
"%s is %s".printf("þor", "mighty"); # OUTPUT: «þor is mighty»
1454-
printf( "%s is %s", "þor", "mighty"); # OUTPUT: «þor is mighty»
1455-
1456-
=head2 routine sprintf
1457-
1458-
Defined as:
1459-
1460-
method sprintf(*@args)
1461-
multi sub sprintf(Cool:D $format, *@args)
1462-
1463-
Formats and outputs a string, following
1464-
L<the same language as C<Str.sprintf>|/type/Str#routine_sprintf>, using as such
1465-
format either the object (if called in method form) or the first argument (if
1466-
called as a routine)
1467-
1468-
sprintf( "%s the %d%s", "þor", 1, "st").put; #OUTPUT: «þor the 1st␤»
1469-
sprintf( "%s is %s", "þor", "mighty").put; # OUTPUT: «þor is mighty␤»
1470-
"%s's weight is %.2f %s".sprintf( "Mjölnir", 3.3392, "kg").put;
1471-
# OUTPUT: «Mjölnir's weight is 3.34 kg␤»
1472-
1473-
This function is mostly identical to the C library's C<sprintf> and C<printf>
1474-
functions. The only difference between the two functions is that C<sprintf>
1475-
returns a string while the C<printf> function writes to a filehandle. C<sprintf>
1476-
returns a C<Str>, not a literal.
1477-
1478-
The C<$format> is scanned for C<%> characters. Any C<%> introduces a format
1479-
token. Directives guide the use (if any) of the arguments. When a directive
1480-
other than C<%> is used, it indicates how the next argument passed is to be
1481-
formatted into the string to be created.
1482-
1483-
N<The information below is for a fully functioning C<sprintf> implementation
1484-
which hasn't been achieved yet. Formats or features not yet implemented are
1485-
marked NYI.>
1486-
1487-
The directives are:
1488-
1489-
=begin table
1490-
1491-
% | a literal percent sign
1492-
c | a character with the given codepoint
1493-
s | a string
1494-
d | a signed integer, in decimal
1495-
u | an unsigned integer, in decimal
1496-
o | an unsigned integer, in octal
1497-
x | an unsigned integer, in hexadecimal
1498-
e | a floating-point number, in scientific notation
1499-
f | a floating-point number, in fixed decimal notation
1500-
g | a floating-point number, in %e or %f notation
1501-
X | like x, but using uppercase letters
1502-
E | like e, but using an uppercase "E"
1503-
G | like g, but with an uppercase "E" (if applicable)
1504-
b | an unsigned integer, in binary
1505-
1506-
=end table
1507-
1508-
Compatibility:
1509-
1510-
=begin table
1511-
1512-
i | a synonym for %d
1513-
D | a synonym for %ld
1514-
U | a synonym for %lu
1515-
O | a synonym for %lo
1516-
F | a synonym for %f
1517-
1518-
=end table
1519-
1520-
Modifiers change the meaning of format directives, but are largely
1521-
no-ops (the semantics are still being determined).
1522-
1523-
=begin table
1524-
1525-
| h | interpret integer as native "short" (typically int16)
1526-
NYI | l | interpret integer as native "long" (typically int32 or int64)
1527-
NYI | ll | interpret integer as native "long long" (typically int64)
1528-
NYI | L | interpret integer as native "long long" (typically uint64)
1529-
NYI | q | interpret integer as native "quads" (typically int64 or larger)
1530-
1531-
=end table
1532-
1533-
Between the C<%> and the format letter, you may specify several
1534-
additional attributes controlling the interpretation of the format. In
1535-
order, these are:
1536-
1537-
=head3 Format parameter index
1538-
1539-
An explicit format parameter index, such as C<2$>. By default,
1540-
C<sprintf> will format the next unused argument in the list, but this
1541-
allows you to take the arguments out of order:
1542-
1543-
sprintf '%2$d %1$d', 12, 34; # OUTPUT: «34 12␤»
1544-
sprintf '%3$d %d %1$d', 1, 2, 3; # OUTPUT: «3 1 1␤»
1545-
1546-
=head3 Flags
1547-
1548-
One or more of:
1549-
1550-
=begin table
1551-
space | prefix non-negative number with a space
1552-
----------------------------------------------------------------
1553-
\+ | prefix non-negative number with a plus sign
1554-
----------------------------------------------------------------
1555-
- | left-justify within the field
1556-
----------------------------------------------------------------
1557-
0 | use leading zeros, not spaces, for required padding
1558-
----------------------------------------------------------------
1559-
# | ensure the leading "0" for any octal,
1560-
| prefix non-zero hexadecimal with "0x" or "0X",
1561-
| prefix non-zero binary with "0b" or "0B"
1562-
=end table
1563-
1564-
For example:
1565-
1566-
sprintf '<% d>', 12; # RESULT: «< 12>␤»
1567-
sprintf '<% d>', 0; # RESULT: «< 0>"»
1568-
sprintf '<% d>', -12; # RESULT: «<-12>␤»
1569-
sprintf '<%+d>', 12; # RESULT: «<+12>␤»
1570-
sprintf '<%+d>', 0; # RESULT: «<+0>"»
1571-
sprintf '<%+d>', -12; # RESULT: «<-12>␤»
1572-
sprintf '<%6s>', 12; # RESULT: «< 12>␤»
1573-
sprintf '<%-6s>', 12; # RESULT: «<12 >␤»
1574-
sprintf '<%06s>', 12; # RESULT: «<000012>␤»
1575-
sprintf '<%#o>', 12; # RESULT: «<014>␤»
1576-
sprintf '<%#x>', 12; # RESULT: «<0xc>␤»
1577-
sprintf '<%#X>', 12; # RESULT: «<0XC>␤»
1578-
sprintf '<%#b>', 12; # RESULT: «<0b1100>␤»
1579-
sprintf '<%#B>', 12; # RESULT: «<0B1100>␤»
1580-
1581-
When a space and a plus sign are given as the flags at once, the space
1582-
is ignored:
1583-
1584-
sprintf '<%+ d>', 12; # RESULT: «<+12>␤»
1585-
sprintf '<% +d>', 12; # RESULT: «<+12>␤»
1586-
1587-
When the C<#> flag and a precision are given in the C<%o> conversion, the
1588-
necessary number of 0s is added at the beginning. If the value of the number is
1589-
C<0> and the precision is 0, it will output nothing; precision 0 or smaller than
1590-
the actual number of elements will return the number with 0 to the left:
1591-
1592-
say sprintf '<%#.5o>', 0o12; # OUTPUT: «<00012>␤»
1593-
say sprintf '<%#.5o>', 0o12345; # OUTPUT: «<012345>␤»
1594-
say sprintf '<%#.0o>', 0; # OUTPUT: «<>␤» zero precision and value 0 results in no output!
1595-
say sprintf '<%#.0o>', 0o1 # OUTPUT: «<01>␤»
1596-
1597-
=head3 Vector flag
1598-
1599-
This flag tells Perl 6 to interpret the supplied string as a vector of
1600-
integers, one for each character in the string. Perl 6 applies the
1601-
format to each integer in turn, then joins the resulting strings with
1602-
a separator (a dot, C<'.'>, by default). This can be useful for
1603-
displaying ordinal values of characters in arbitrary strings:
1604-
1605-
=begin code :skip-test<not yet implemented>
1606-
NYI sprintf "%vd", "AB\x[100]"; # "65.66.256"
1607-
=end code
1608-
1609-
You can also explicitly specify the argument number to use for the
1610-
join string using something like C<*2$v>; for example:
1611-
1612-
=begin code :skip-test<not yet implemented>
1613-
NYI sprintf '%*4$vX %*4$vX %*4$vX', # 3 IPv6 addresses
1614-
@addr[1..3], ":";
1615-
=end code
1616-
1617-
=head3 (Minimum) Width
1618-
1619-
Arguments are usually formatted to be only as wide as required to
1620-
display the given value. You can override the width by putting a
1621-
number here, or get the width from the next argument (with C<*> ) or
1622-
from a specified argument (e.g., with C<*2$>):
1623-
1624-
=begin code :skip-test<partly not yet implemented>
1625-
sprintf "<%s>", "a"; # RESULT: «<a>␤»
1626-
sprintf "<%6s>", "a"; # RESULT: «< a>␤»
1627-
sprintf "<%*s>", 6, "a"; # RESULT: «< a>␤»
1628-
NYI sprintf '<%*2$s>', "a", 6; # "< a>"
1629-
sprintf "<%2s>", "long"; # RESULT: «<long>␤» (does not truncate)
1630-
=end code
1631-
1632-
If a field width obtained through C<*> is negative, it has the same
1633-
effect as the C<-> flag: left-justification.
1634-
1635-
=head3 Precision, or maximum width
1636-
1637-
You can specify a precision (for numeric conversions) or a maximum
1638-
width (for string conversions) by specifying a C<.> followed by a
1639-
number. For floating-point formats, except C<g> and C<G>, this
1640-
specifies how many places right of the decimal point to show (the
1641-
default being 6). For example:
1642-
1643-
# these examples are subject to system-specific variation
1644-
sprintf '<%f>', 1; # RESULT: «"<1.000000>"␤»
1645-
sprintf '<%.1f>', 1; # RESULT: «"<1.0>"␤»
1646-
sprintf '<%.0f>', 1; # RESULT: «"<1>"␤»
1647-
sprintf '<%e>', 10; # RESULT: «"<1.000000e+01>"␤»
1648-
sprintf '<%.1e>', 10; # RESULT: «"<1.0e+01>"␤»
1649-
1650-
For "g" and "G", this specifies the maximum number of digits to show,
1651-
including those prior to the decimal point and those after it; for
1652-
example:
1653-
1654-
# These examples are subject to system-specific variation.
1655-
sprintf '<%g>', 1; # RESULT: «<1>␤»
1656-
sprintf '<%.10g>', 1; # RESULT: «<1>␤»
1657-
sprintf '<%g>', 100; # RESULT: «<100>␤»
1658-
sprintf '<%.1g>', 100; # RESULT: «<1e+02>␤»
1659-
sprintf '<%.2g>', 100.01; # RESULT: «<1e+02>␤»
1660-
sprintf '<%.5g>', 100.01; # RESULT: «<100.01>␤»
1661-
sprintf '<%.4g>', 100.01; # RESULT: «<100>␤»
1662-
1663-
For integer conversions, specifying a precision implies that the
1664-
output of the number itself should be zero-padded to this width, where
1665-
the C<0> flag is ignored:
1666-
1667-
(Note that this feature currently works for unsigned integer conversions, but
1668-
not for signed integer.)
1669-
1670-
=begin code :skip-test<partly not yet implemented>
1671-
sprintf '<%.6d>', 1; # <000001>
1672-
NYI sprintf '<%+.6d>', 1; # <+000001>
1673-
NYI sprintf '<%-10.6d>', 1; # <000001 >
1674-
sprintf '<%10.6d>', 1; # < 000001>
1675-
NYI sprintf '<%010.6d>', 1; # 000001>
1676-
NYI sprintf '<%+10.6d>', 1; # < +000001>
1677-
sprintf '<%.6x>', 1; # RESULT: «<000001>␤»
1678-
sprintf '<%#.6x>', 1; # RESULT: «<0x000001>␤»
1679-
sprintf '<%-10.6x>', 1; # RESULT: «<000001 >␤»
1680-
sprintf '<%10.6x>', 1; # RESULT: «< 000001>␤»
1681-
sprintf '<%010.6x>', 1; # RESULT: «< 000001>␤»
1682-
sprintf '<%#10.6x>', 1; # RESULT: «< 0x000001>␤»
1683-
=end code
1684-
1685-
For string conversions, specifying a precision truncates the string to
1686-
fit the specified width:
1687-
1688-
sprintf '<%.5s>', "truncated"; # RESULT: «<trunc>␤»
1689-
sprintf '<%10.5s>', "truncated"; # RESULT: «< trunc>␤»
1690-
1691-
You can also get the precision from the next argument using C<.*>, or
1692-
from a specified argument (e.g., with C<.*2$>):
1693-
1694-
=begin code :skip-test<partly not yet implemented>
1695-
sprintf '<%.6x>', 1; # RESULT: «<000001>␤»
1696-
sprintf '<%.*x>', 6, 1; # RESULT: «<000001>␤»
1697-
NYI sprintf '<%.*2$x>', 1, 6; # "<000001>"
1698-
NYI sprintf '<%6.*2$x>', 1, 4; # "< 0001>"
1699-
=end code
1700-
1701-
If a precision obtained through C<*> is negative, it counts as having
1702-
no precision at all:
1703-
1704-
sprintf '<%.*s>', 7, "string"; # RESULT: «<string>␤»
1705-
sprintf '<%.*s>', 3, "string"; # RESULT: «<str>␤»
1706-
sprintf '<%.*s>', 0, "string"; # RESULT: «<>␤»
1707-
sprintf '<%.*s>', -1, "string"; # RESULT: «<string>␤»
1708-
sprintf '<%.*d>', 1, 0; # RESULT: «<0>␤»
1709-
sprintf '<%.*d>', 0, 0; # RESULT: «<>␤»
1710-
sprintf '<%.*d>', -1, 0; # RESULT: «<0>␤»
1711-
1712-
=head3 Size
1713-
1714-
For numeric conversions, you can specify the size to interpret the
1715-
number as using C<l>, C<h>, C<V>, C<q>, C<L>, or C<ll>. For integer
1716-
conversions (C<d> C<u> C<o> C<x> C<X> C<b> C<i> C<D> C<U> C<O>),
1717-
numbers are usually assumed to be whatever the default integer size is
1718-
on your platform (usually 32 or 64 bits), but you can override this to
1719-
use instead one of the standard C types, as supported by the compiler
1720-
used to build Perl 6:
1721-
1722-
(Note: None of the following have been implemented.)
1723-
1724-
=begin table
1725-
1726-
hh | interpret integer as C type "char" or "unsigned char"
1727-
h | interpret integer as C type "short" or "unsigned short"
1728-
j | interpret integer as C type "intmax_t", only with a C99 compiler (unportable)
1729-
l | interpret integer as C type "long" or "unsigned long"
1730-
q, L, or ll | interpret integer as C type "long long", "unsigned long long", or "quad" (typically 64-bit integers)
1731-
t | interpret integer as C type "ptrdiff_t"
1732-
z | interpret integer as C type "size_t"
1733-
=end table
1734-
1735-
=head3 Order of arguments
1736-
1737-
Normally, C<sprintf> takes the next unused argument as the value to
1738-
format for each format specification. If the format specification uses
1739-
C<*> to require additional arguments, these are consumed from the
1740-
argument list in the order they appear in the format specification
1741-
before the value to format. Where an argument is specified by an
1742-
explicit index, this does not affect the normal order for the
1743-
arguments, even when the explicitly specified index would have been
1744-
the next argument.
1745-
1746-
So:
1747-
1748-
my $a = 5; my $b = 2; my $c = 'net';
1749-
sprintf "<%*.*s>", $a, $b, $c; # RESULT: «< ne>␤»
1750-
1751-
uses C<$a> for the width, C<$b> for the precision, and C<$c> as the value to
1752-
format; while:
1753-
1754-
=for code :skip-test<not yet implemented>
1755-
NYI sprintf '<%*1$.*s>', $a, $b;
1756-
1757-
would use C<$a> for the width and precision and C<$b> as the value to format.
1758-
1759-
Here are some more examples; be aware that when using an explicit
1760-
index, the C<$> may need escaping:
1761-
1762-
=for code :skip-test<partly not yet implemented>
1763-
sprintf "%2\$d %d\n", 12, 34; # RESULT: «34 12␤␤»
1764-
sprintf "%2\$d %d %d\n", 12, 34; # RESULT: «34 12 34␤␤»
1765-
sprintf "%3\$d %d %d\n", 12, 34, 56; # RESULT: «56 12 34␤␤»
1766-
NYI sprintf "%2\$*3\$d %d\n", 12, 34, 3; # " 34 12\n"
1767-
NYI sprintf "%*1\$.*f\n", 4, 5, 10; # "5.0000\n"
1768-
1769-
Other examples:
1770-
1771-
=for code :skip-test<partly not yet implemented>
1772-
NYI sprintf "%ld a big number", 4294967295;
1773-
NYI sprintf "%%lld a bigger number", 4294967296;
1774-
sprintf('%c', 97); # RESULT: «a␤»
1775-
sprintf("%.2f", 1.969); # RESULT: «1.97␤»
1776-
sprintf("%+.3f", 3.141592); # RESULT: «+3.142␤»
1777-
sprintf('%2$d %1$d', 12, 34); # RESULT: «34 12␤»
1778-
sprintf("%x", 255); # RESULT: «ff␤»
1779-
1780-
Special case: C«sprintf("<b>%s</b>\n", "Perl 6")» will not work, but
1781-
one of the following will:
1782-
1783-
=for code
1784-
sprintf Q:b "<b>%s</b>\n", "Perl 6"; # RESULT: «<b>Perl 6</b>␤␤»
1785-
sprintf "<b>\%s</b>\n", "Perl 6"; # RESULT: «<b>Perl 6</b>␤␤»
1786-
sprintf "<b>%s\</b>\n", "Perl 6"; # RESULT: «<b>Perl 6</b>␤␤»
1787-
1788-
17891426
17901427
=end pod
17911428

0 commit comments

Comments
 (0)