Skip to content

Commit 24c5330

Browse files
CaDsbcardiff
authored andcommitted
Added documentation for Kernel#sprintf method (#5914)
Fixes #3124
1 parent 35a108e commit 24c5330

File tree

2 files changed

+222
-2
lines changed

2 files changed

+222
-2
lines changed

src/io.cr

+2
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ abstract class IO
281281
nil
282282
end
283283

284+
# Writes a formatted string to this IO.
285+
# For details on the format string, see `Kernel::sprintf`.
284286
def printf(format_string, *args) : Nil
285287
printf format_string, args
286288
end

src/kernel.cr

+220-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ end
5757

5858
# Prints a formatted string to `STDOUT`.
5959
#
60-
# See also: `IO#printf`.
60+
# For details on the format string, see `sprintf`.
6161
def printf(format_string, *args) : Nil
6262
printf format_string, args
6363
end
@@ -68,8 +68,226 @@ def printf(format_string, args : Array | Tuple) : Nil
6868
end
6969

7070
# Returns a formatted string.
71+
# The string is produced according to the *format_string* with format specifiers
72+
# being replaced by values from *args* formatted according to the specifier.
7173
#
72-
# See also: `IO#printf`.
74+
# Within the format string, any characters other than format specifiers
75+
# (specifiers beginning with `%`) are copied to the result.
76+
#
77+
# The syntax for a format specifier is:
78+
#
79+
# ```text
80+
# %[flags][width][.precision]type
81+
# ```
82+
#
83+
# A format specifier consists of a percent sign, followed by optional flags,
84+
# width, and precision indicators, then terminated with a field type
85+
# character.
86+
#
87+
# The field type controls how the corresponding
88+
# `sprintf` argument is to be interpreted, while the flags
89+
# modify that interpretation.
90+
#
91+
# The field type characters are:
92+
#
93+
# ```text
94+
# Field | Integer Format
95+
# ------+--------------------------------------------------------------
96+
# b | Formats argument as a binary number.
97+
# d | Formats argument as a decimal number.
98+
# i | Same as d.
99+
# o | Formats argument as an octal number.
100+
# x | Formats argument as a hexadecimal number using lowercase letters.
101+
# X | Same as x, but uses uppercase letters.
102+
#
103+
# Field | Float Format
104+
# ------+--------------------------------------------------------------
105+
# e | Formats floating point argument into exponential notation
106+
# | with one digit before the decimal point as [-]d.dddddde[+-]dd.
107+
# | The precision specifies the number of digits after the decimal
108+
# | point (defaulting to six).
109+
# E | Equivalent to e, but uses an uppercase E to indicate
110+
# | the exponent.
111+
# f | Formats floating point argument as [-]ddd.dddddd,
112+
# | where the precision specifies the number of digits after
113+
# | the decimal point.
114+
# g | Formats a floating point number using exponential form
115+
# | if the exponent is less than -4 or greater than or
116+
# | equal to the precision, or in dd.dddd form otherwise.
117+
# | The precision specifies the number of significant digits.
118+
# G | Equivalent to g, but use an uppercase E in exponent form.
119+
# a | Formats floating point argument as [-]0xh.hhhhp[+-]dd,
120+
# | which is consisted from optional sign, "0x", fraction part
121+
# | as hexadecimal, "p", and exponential part as decimal.
122+
# A | Equivalent to a, but use uppercase X and P.
123+
#
124+
# Field | Other Format
125+
# ------+--------------------------------------------------------------
126+
# c | Argument is the numeric code for a single character or
127+
# | a single character string itself.
128+
# s | Argument is a string to be substituted. If the format
129+
# | sequence contains a precision, at most that many characters
130+
# | will be copied.
131+
# % | A percent sign itself will be displayed. No argument taken.
132+
#
133+
# ```
134+
# The flags modifies the behavior of the formats.
135+
# The flag characters are:
136+
# ```text
137+
#
138+
# Flag | Applies to | Meaning
139+
# ---------+---------------+-----------------------------------------
140+
# space | bdiouxX | Add a leading space character to
141+
# | aAeEfgG | non-negative numbers.
142+
# | (numeric fmt) | For o, x, X, b, use
143+
# | | a minus sign with absolute value for
144+
# | | negative values.
145+
# ---------+---------------+-----------------------------------------
146+
# + | bdiouxX | Add a leading plus sign to non-negative
147+
# | aAeEfgG | numbers.
148+
# | (numeric fmt) | For o, x, X, b, use
149+
# | | a minus sign with absolute value for
150+
# | | negative values.
151+
# ---------+---------------+-----------------------------------------
152+
# - | all | Left-justify the result of this conversion.
153+
# ---------+---------------+-----------------------------------------
154+
# 0 (zero) | bdiouxX | Pad with zeros, not spaces.
155+
# | aAeEfgG | For o, x, X, b, radix-1
156+
# | (numeric fmt) | is used for negative numbers formatted as
157+
# | | complements.
158+
# ```
159+
#
160+
# Examples of flags:
161+
#
162+
# Decimal number conversion
163+
# ```
164+
# sprintf "%d", 123 # => "123"
165+
# sprintf "%+d", 123 # => "+123"
166+
# sprintf "% d", 123 # => " 123"
167+
# ```
168+
#
169+
# Octal number conversion
170+
# ```
171+
# sprintf "%o", 123 # => "173"
172+
# sprintf "%+o", 123 # => "+173"
173+
# sprintf "%o", -123 # => "-173"
174+
# sprintf "%+o", -123 # => "-173"
175+
# ```
176+
#
177+
# Hexadecimal number conversion
178+
# ```
179+
# sprintf "%x", 123 # => "7b"
180+
# sprintf "%+x", 123 # => "+7b"
181+
# sprintf "%x", -123 # => "-7b"
182+
# sprintf "%+x", -123 # => "-7b"
183+
# sprintf "%#x", 0 # => "0"
184+
# sprintf "% x", 123 # => " 7b"
185+
# sprintf "% x", -123 # => "-7b"
186+
# sprintf "%X", 123 # => "7B"
187+
# sprintf "%#X", -123 # => "-7B"
188+
# ```
189+
#
190+
# Binary number conversion
191+
# ```
192+
# sprintf "%b", 123 # => "1111011"
193+
# sprintf "%+b", 123 # => "+1111011"
194+
# sprintf "%+b", -123 # => "-1111011"
195+
# sprintf "%b", -123 # => "-1111011"
196+
# sprintf "%#b", 0 # => "0"
197+
# sprintf "% b", 123 # => " 1111011"
198+
# sprintf "%+ b", 123 # => "+ 1111011"
199+
# sprintf "% b", -123 # => "-1111011"
200+
# sprintf "%+ b", -123 # => "-1111011"
201+
# ```
202+
#
203+
# Floating point conversion
204+
# ```
205+
# sprintf "%a", 123 # => "0x1.ecp+6"
206+
# sprintf "%A", 123 # => "0X1.ECP+6"
207+
# ```
208+
#
209+
# Exponential form conversion
210+
# ```
211+
# sprintf "%g", 123.4 # => "123.4"
212+
# sprintf "%g", 123.4567 # => "123.457"
213+
# sprintf "%20.8g", 1234.56789 # => " 1234.5679"
214+
# sprintf "%20.8g", 123456789 # => " 1.2345679e+08"
215+
# sprintf "%20.8G", 123456789 # => " 1.2345679E+08"
216+
# sprintf "%20.8g", -123456789 # => " -1.2345679e+08"
217+
# sprintf "%20.8G", -123456789 # => " -1.2345679E+08"
218+
# ```
219+
#
220+
# The field width is an optional integer, followed optionally by a
221+
# period and a precision. The width specifies the minimum number of
222+
# characters that will be written to the result for this field.
223+
#
224+
# Examples of width:
225+
# ```
226+
# sprintf "%20d", 123 # => " 123"
227+
# sprintf "%+20d", 123 # => " +123"
228+
# sprintf "%020d", 123 # => "00000000000000000123"
229+
# sprintf "%+020d", 12 # => "+0000000000000000123"
230+
# sprintf "% 020d", 123 # => " 0000000000000000123"
231+
# sprintf "%-20d", 123 # => "123 "
232+
# sprintf "%-+20d", 123 # => "+123 "
233+
# sprintf "%- 20d", 123 # => " 123 "
234+
# sprintf "%020x", -123 # => "00000000000000000-7b"
235+
# sprintf "%020X", -123 # => "00000000000000000-7B"
236+
# ```
237+
#
238+
# For numeric fields, the precision controls the number of decimal places
239+
# displayed.
240+
#
241+
# For string fields, the precision determines the maximum
242+
# number of characters to be copied from the string.
243+
#
244+
# Examples of precisions:
245+
#
246+
# Precision for `d`, `o`, `x` and `b` is
247+
# minimum number of digits
248+
# ```
249+
# sprintf "%20.8d", 123 # => " 123"
250+
# sprintf "%020.8d", 123 # => "00000000000000000123"
251+
# sprintf "%20.8o", 123 # => " 173"
252+
# sprintf "%020.8o", 123 # => "00000000000000000173"
253+
# sprintf "%20.8x", 123 # => " 7b"
254+
# sprintf "%020.8x", 123 # => "0000000000000000007b"
255+
# sprintf "%20.8b", 123 # => " 01111011"
256+
# sprintf "%20.8d", -123 # => " -123"
257+
# sprintf "%020.8d", -123 # => "0000000000000000-123"
258+
# sprintf "%20.8o", -123 # => " -173"
259+
# sprintf "%20.8x", -123 # => " -7b"
260+
# sprintf "%20.8b", -11 # => " -1011"
261+
# ```
262+
#
263+
# Precision for `e` is number of digits after the decimal point.
264+
# ```
265+
# sprintf "%20.8e", 1234.56789 # => " 1.23456789e+03"
266+
# ```
267+
#
268+
# Precision for `f` is number of digits after the decimal point.
269+
# ```
270+
# sprintf "%20.8f", 1234.56789 # => " 1234.56789000"
271+
# ```
272+
#
273+
# Precision for `g` is number of significant digits.
274+
# ```
275+
# sprintf "%20.8g", 1234.56789 # => " 1234.5679"
276+
# sprintf "%20.8g", 123456789 # => " 1.2345679e+08"
277+
# sprintf "%-20.8g", 123456789 # => "1.2345679e+08 "
278+
# ```
279+
#
280+
# Precision for `s` is maximum number of characters.
281+
# ```
282+
# sprintf "%20.8s", "string test" # => " string t"
283+
# ```
284+
#
285+
# Additional examples:
286+
# ```
287+
# sprintf "%d %04x", 123, 123 # => "123 007b"
288+
# sprintf "%08b '%4s'", 123, 123 # => "01111011 ' 123'"
289+
# sprintf "%+g:% g:%-g", 1.23, 1.23, 1.23 # => "+1.23: 1.23:1.23"
290+
# ```
73291
def sprintf(format_string, *args) : String
74292
sprintf format_string, args
75293
end

0 commit comments

Comments
 (0)