blib/lib/SPVM/Format.pm untested

filter: Statementstmt n/a Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a SCAR -
1 package SPVM::Format;
2  
3 1;
4  
5 =encoding utf8
6  
7 =head1 Name
8  
9 SPVM::Format - Format Utilities
10  
11 =head1 Usage
12  
13 use Format;
14
15 # Foo 123 abc 1.115 Bar
16 my $result = Format->sprintf("Foo %d %s %.3f Bar", [(object)123, "abc", [(object)1.115]);
17
18 # %d - "123"
19 my $result = Format->sprintf("%d", [(object)123]);
20
21 # %5d - " 123"
22 my $result = Format->sprintf("%5d", [(object)123]);
23  
24 # %05d - "00123"
25 my $result = Format->sprintf("%05d", [(object)123]);
26
27 # %+d - "+123"
28 my $result = Format->sprintf("%+d", [(object)123]);
29
30 # %-5d - "123 "
31 my $result = Format->sprintf("%-5d", [(object)123]);
32
33 # %d - "x"
34 my $result = Format->sprintf("%c", [(object)'x']);
35
36 # %c - "あ"
37 my $result = Format->sprintf("%c", [(object)Fn->ord("あ")]);
38  
39 # %s - "ABC"
40 my $result = Format->sprintf("%s", [(object)"ABC"]);
41
42 # %.2s - "AB"
43 my $result = Format->sprintf("%.2s", [(object)"ABC"]);
44
45 # %u - "4294967295"
46 my $result = Format->sprintf("%u", [(object)-1]);
47
48 # %f - "3.141500"
49 my $result = Format->sprintf("%f", [(object)3.1415]);
50
51 # %.2f - "3.14"
52 my $result = Format->sprintf("%.2f", [(object)3.1415]);
53
54 # %g - "3.14"
55 my $result = Format->sprintf("%g", [(object)3.14]);
56
57 # %x - "ff"
58 my $result = Format->sprintf("%x", [(object)255]);
59
60 # %x - "ffffffff"
61 my $result = Format->sprintf("%x", [(object)-1]);
62  
63 # %p - "0x8000000000000000"
64 my $result = Format->sprintf("%p", [(object)$object]);
65  
66 =head1 Description
67  
68 C<Format> is a formatting utilities for C<sprintf> method.
69  
70 =head1 Class Methods
71  
72 =head2 sprintf
73  
74 C<static method sprintf : string ($format : string, $args : object[);>
75  
76 Creates a formatted string form $format and $args.
77  
78 =head3 Specifiers
79  
80 =begin html
81  
82 <table>
83 <tr><th>Specifiers</th><th>Descriptions</th><th>Acceptable Types</th></tr>
84 <tr><td>%c</td><td>An <code>UTF-8</code> character</td><td><a href="https://metacpan.org/pod/SPVM::Byte">Byte</a>, <a href="https://metacpan.org/pod/SPVM::Int">Int</a></td></tr>
85 <tr><td>%d</td><td>Signed 32-bit integer</td><td><a href="https://metacpan.org/pod/SPVM::Int">Int</a></td></tr>
86 <tr><td>%f</td><td>64bit floating point</td><td><a href="https://metacpan.org/pod/SPVM::Double">Double</a>, <a href="https://metacpan.org/pod/SPVM::Float">Float</a></td></tr>
87 <tr><td>%g</td><td>64bit floating point</td><td><a href="https://metacpan.org/pod/SPVM::Double">Double</a>, <a href="https://metacpan.org/pod/SPVM::Float">Float</a></td></tr>
88 <tr><td>%x</td><td>Unsiged 32-bit integer represented by hexadecima characters <code>0-9a-z</code></td><td><a href="https://metacpan.org/pod/SPVM::Int">Int</a></td></tr>
89 <tr><td>%X</td><td>Unsiged 32-bit integer represented by hexadecima characters <code>0-9A-Z</code></td><td><a href="https://metacpan.org/pod/SPVM::Int">Int</a></td></tr>
90 <tr><td>%lX</td><td>Unsiged 64-bit integer represented by hexadecima characters <code>0-9A-Z</code></td><td><a href="https://metacpan.org/pod/SPVM::Long">Long</a></td></tr>
91 <tr><td>%ld</td><td>Signed 64bit integer</td><td><a href="https://metacpan.org/pod/SPVM::Long">Long</a></td></tr>
92 <tr><td>%lu</td><td>Unsigned 64bit integer</td><td><a href="https://metacpan.org/pod/SPVM::Long">Long</a></td></tr>
93 <tr><td>%lx</td><td>Unsiged 64-bit integer represented by hexadecima characters <code>0-9a-z</code></td><td><a href="https://metacpan.org/pod/SPVM::Long">Long</a></td></tr>
94 <tr><td>%s</td><td>String(undef is treated as an empty string)</td><td>String Type</td></tr>
95 <tr><td>%p</td><td>Address</td><td>Object Type</td></tr>
96 <tr><td>%u</td><td>Unsigned 32-bit integer</td><td><a href="https://metacpan.org/pod/SPVM::Int">Int</a></td></tr>
97 </table>
98  
99 =end html
100  
101 =head3 Specifier Options
102  
103 Specifier options can be written between C<%> and the character of specifier such as C<d>, C<f>.
104  
105 =begin html
106  
107 <table>
108 <tr><th>Specifier Options</th><th>Descriptions</th></tr>
109 <tr><td>0[DECIMAL_NUMBERS]</td><td>Zero padding</td></tr>
110 <tr><td>+</td><td>Adding a plus sign</td></tr>
111 <tr><td>-</td><td>Left justified</td></tr>
112 <tr><td>.[DECIMAL_NUMBERS]</td><td>Precision(Maximam width in %s)</td></tr>
113 </table>
114  
115 =end html
116  
117 =head1 Copyright & License
118  
119 Copyright (c) 2023 Yuki Kimoto
120  
121 MIT License