]> gitweb.factorcode.org Git - factor.git/blob - basis/formatting/formatting-docs.factor
1b5ffdfa8e05d92b5003add5bdd1bdc8997d4eb3
[factor.git] / basis / formatting / formatting-docs.factor
1
2 USING: help.syntax help.markup kernel prettyprint sequences strings ;
3
4 IN: formatting
5
6 HELP: printf
7 { $values { "format-string" string } }
8 { $description
9     "Writes the arguments (specified on the stack) formatted according to the format string."
10     $nl
11     "Several format specifications exist for handling arguments of different types, and "
12     "specifying attributes for the result string, including such things as maximum width, "
13     "padding, and decimals."
14     $nl
15     { $table
16         { { $snippet "%%" }          "Single %"                   "" }
17         { { $snippet "%P.Ds" }       "String format"              "string" }
18         { { $snippet "%P.DS" }       "String format uppercase"    "string" }
19         { { $snippet "%P.Du" }       "Unparsed format"            "object" }
20         { { $snippet "%c" }          "Character format"           "char" }
21         { { $snippet "%C" }          "Character format uppercase" "char" }
22         { { $snippet "%+Pd" }        "Integer format (base 10)"   "integer" }
23         { { $snippet "%+Po" }        "Octal format (base 8)"      "integer" }
24         { { $snippet "%+Pb" }        "Binary format (base 2)"     "integer" }
25         { { $snippet "%+P.De" }      "Scientific notation"        "integer, float" }
26         { { $snippet "%+P.DE" }      "Scientific notation"        "integer, float" }
27         { { $snippet "%+P.Df" }      "Fixed format"               "integer, float" }
28         { { $snippet "%+Px" }        "Hexadecimal (base 16)"      "integer" }
29         { { $snippet "%+PX" }        "Hexadecimal (base 16) uppercase" "integer" }
30         { { $snippet "%[%?, %]" }    "Sequence format"            "sequence" }
31         { { $snippet "%[%?: %? %]" } "Assocs format"              "assocs" }
32     }
33     $nl
34     "A plus sign (" { $snippet "+" } ") is used to optionally specify that the number should be "
35     "formatted with a " { $snippet "+" } " preceeding it if positive."
36     $nl
37     "Padding (" { $snippet "P" } ") is used to optionally specify the minimum width of the result "
38     "string, the padding character, and the alignment.  By default, the padding "
39     "character defaults to a space and the alignment defaults to right-aligned. "
40     "For example:"
41     $nl
42     { $list
43         { { $snippet "%5s" } " formats a string padding with spaces up to 5 characters wide." }
44         { { $snippet "%03d" } " formats an integer padding with zeros up to 3 characters wide." }
45         { { $snippet "%'#10f" } " formats a float padding with " { $snippet "#" } " up to 10 characters wide." }
46         { { $snippet "%-10d" } " formats an integer to 10 characters wide and left-aligns." }
47     }
48     $nl
49     "Digits (" { $snippet "D" } ") is used to optionally specify the maximum digits in the result "
50     "string. For example:"
51     $nl
52     { $list
53         { { $snippet "%.3s" } " formats a string to truncate at 3 characters (from the left)." }
54         { { $snippet "%.10f" } " formats a float to pad-tail with zeros up to 10 digits beyond the decimal point." }
55         { { $snippet "%.5E" } " formats a float into scientific notation with zeros up to 5 digits beyond the decimal point, but before the exponent." }
56     }
57 }
58 { $examples
59     { $example
60         "USING: formatting ;"
61         "123 \"%05d\" printf"
62         "00123" }
63     { $example
64         "USING: formatting ;"
65         "0xff \"%04X\" printf"
66         "00FF" }
67     { $example
68         "USING: formatting ;"
69         "12 \"%b\" printf"
70         "1100" }
71     { $example
72         "USING: formatting ;"
73         "1.23456789 \"%.3f\" printf"
74         "1.235" }
75     { $example
76         "USING: formatting ;"
77         "12 \"%'#4d\" printf"
78         "##12" }
79     { $example
80         "USING: formatting ;"
81         "1234 \"%+d\" printf"
82         "+1234" }
83     { $example
84         "USING: formatting ;"
85         "{ 1 2 3 } \"%[%d, %]\" printf"
86         "{ 1, 2, 3 }" }
87     { $example
88         "USING: formatting ;"
89         "H{ { 1 2 } { 3 4 } } \"%[%d: %d %]\" printf"
90         "{ 1:2, 3:4 }" }
91     { $example
92       "USING: calendar formatting ;"
93       "3 years \"%u\" printf"
94       "T{ duration { year 3 } }" }
95 } ;
96
97 HELP: sprintf
98 { $values { "format-string" string } { "result" string } }
99 { $description "Returns the arguments (specified on the stack) formatted according to the format string as a result string." }
100 { $see-also printf } ;
101
102 HELP: strftime
103 { $values { "format-string" string } }
104 { $description
105     "Writes the timestamp (specified on the stack) formatted according to the format string."
106     $nl
107     "Different attributes of the timestamp can be retrieved using format specifications."
108     $nl
109     { $table
110         { { $snippet "%a" }    "Abbreviated weekday name." }
111         { { $snippet "%A" }    "Full weekday name." }
112         { { $snippet "%b" }    "Abbreviated month name." }
113         { { $snippet "%B" }    "Full month name." }
114         { { $snippet "%c" }    "Date and time representation." }
115         { { $snippet "%d" }    "Day of the month as a decimal number [01,31]." }
116         { { $snippet "%H" }    "Hour (24-hour clock) as a decimal number [00,23]." }
117         { { $snippet "%I" }    "Hour (12-hour clock) as a decimal number [01,12]." }
118         { { $snippet "%j" }    "Day of the year as a decimal number [001,366]." }
119         { { $snippet "%m" }    "Month as a decimal number [01,12]." }
120         { { $snippet "%M" }    "Minute as a decimal number [00,59]." }
121         { { $snippet "%p" }    "Either AM or PM." }
122         { { $snippet "%S" }    "Second as a decimal number [00,59]." }
123         { { $snippet "%U" }    "Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]." }
124         { { $snippet "%w" }    "Weekday as a decimal number [0(Sunday),6]." }
125         { { $snippet "%W" }    "Week number of the year (Monday as the first day of the week) as a decimal number [00,53]." }
126         { { $snippet "%x" }    "Date representation." }
127         { { $snippet "%X" }    "Time representation." }
128         { { $snippet "%y" }    "Year without century as a decimal number [00,99]." }
129         { { $snippet "%Y" }    "Year with century as a decimal number." }
130         { { $snippet "%Z" }    "Time zone name (no characters if no time zone exists)." }
131         { { $snippet "%%" }    "A literal '%' character." }
132     }
133 }
134 { $examples
135     { $unchecked-example
136         "USING: calendar formatting io ;"
137         "now \"%c\" strftime print"
138         "Mon Dec 15 14:40:43 2008" }
139 } ;
140
141 ARTICLE: "formatting" "Formatted printing"
142 "The " { $vocab-link "formatting" } " vocabulary is used for english formatted printing."
143 { $subsections
144     printf
145     sprintf
146     strftime
147 } ;
148
149 ABOUT: "formatting"