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