Debian Codegraph @

include/ruby/internal/intern/sprintf.h

raw
1 #ifndef RBIMPL_INTERN_SPRINTF_H /*-*-C++-*-vi:se ft=cpp:*/
2 #define RBIMPL_INTERN_SPRINTF_H
3 /**
4 * @file
5 * @author Ruby developers <[email protected]>
6 * @copyright This file is a part of the programming language Ruby.
7 * Permission is hereby granted, to either redistribute and/or
8 * modify this file, provided that the conditions mentioned in the
9 * file COPYING are met. Consult the file for details.
10 * @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are
11 * implementation details. Don't take them as canon. They could
12 * rapidly appear then vanish. The name (path) of this header file
13 * is also an implementation detail. Do not expect it to persist
14 * at the place it is now. Developers are free to move it anywhere
15 * anytime at will.
16 * @note To ruby-core: remember that this header can be possibly
17 * recursively included from extension libraries written in C++.
18 * Do not expect for instance `__VA_ARGS__` is always available.
19 * We assume C99 for ruby itself but we don't assume languages of
20 * extension libraries. They could be written in C++98.
21 * @brief Our own private `printf(3)`.
22 */
23 #include "ruby/internal/attr/format.h"
24 #include "ruby/internal/attr/nonnull.h"
25 #include "ruby/internal/dllexport.h"
26 #include "ruby/internal/value.h"
27
28 RBIMPL_SYMBOL_EXPORT_BEGIN()
29
30 /* sprintf.c */
31
32 /**
33 * Identical to rb_str_format(), except how the arguments are arranged.
34 *
35 * @param[in] argc Number of objects of `argv`.
36 * @param[in] argv A format string, followed by its arguments.
37 * @return A rendered new instance of ::rb_cString.
38 *
39 * @internal
40 *
41 * You can safely pass NULL to `argv`. Doesn't make any sense though.
42 */
43 VALUE rb_f_sprintf(int argc, const VALUE *argv);
44
45 RBIMPL_ATTR_NONNULL((1))
46 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 1, 2)
47 /**
48 * Ruby's extended `sprintf(3)`. We ended up reinventing the entire `printf`
49 * business because we don't want to depend on locales. OS-provided `printf`
50 * routines might or might not, which caused instabilities of the result
51 * strings.
52 *
53 * The format sequence is a mixture of format specifiers and other verbatim
54 * contents. Each format specifier starts with a `%`, and has the following
55 * structure:
56 *
57 * ```
58 * %[flags][width][.precision][length]conversion
59 * ```
60 *
61 * This function supports flags of ` `, `#`, `+`, `-`, `0`, width of
62 * non-negative decimal integer and `*`, precision of non-negative decimal
63 * integers and `*`, length of `L`, `h`, `t`, `z`, `l`, `ll`, `q`, conversions
64 * of `A`, `D`, `E`, `G`, `O`, `U`, `X`, `a`, `c`, `d`, `e`, `f`, `g`, `i`,
65 * `n`, `o`, `p`, `s`, `u`, `x`, and `%`. In case of `_WIN32` it also supports
66 * `I`. And additionally, it supports magical `PRIsVALUE` macro that can
67 * stringise arbitrary Ruby objects:
68 *
69 * ```CXX
70 * rb_sprintf("|%"PRIsVALUE"|", RUBY_Qtrue); // => "|true|"
71 * rb_sprintf("%+"PRIsVALUE, rb_stdin); // => "#<IO:<STDIN>>"
72 * ```
73 *
74 * @param[in] fmt A `printf`-like format specifier.
75 * @param[in] ... Variadic number of contents to format.
76 * @return A rendered new instance of ::rb_cString.
77 *
78 * @internal
79 *
80 * :FIXME: We can improve this document.
81 */
82 VALUE rb_sprintf(const char *fmt, ...);
83
84 RBIMPL_ATTR_NONNULL((1))
85 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 1, 0)
86 /**
87 * Identical to rb_sprintf(), except it takes a `va_list`.
88 *
89 * @param[in] fmt A `printf`-like format specifier.
90 * @param[in] ap Contents to format.
91 * @return A rendered new instance of ::rb_cString.
92 */
93 VALUE rb_vsprintf(const char *fmt, va_list ap);
94
95 RBIMPL_ATTR_NONNULL((2))
96 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 3)
97 /**
98 * Identical to rb_sprintf(), except it renders the output to the specified
99 * object rather than creating a new one.
100 *
101 * @param[out] dst String to modify.
102 * @param[in] fmt A `printf`-like format specifier.
103 * @param[in] ... Variadic number of contents to format.
104 * @exception rb_eTypeError `dst` is not a String.
105 * @return Passed `dst`.
106 * @post `dst` has the rendered output appended to its end.
107 */
108 VALUE rb_str_catf(VALUE dst, const char *fmt, ...);
109
110 RBIMPL_ATTR_NONNULL((2))
111 RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 2, 0)
112 /**
113 * Identical to rb_str_catf(), except it takes a `va_list`. It can also be
114 * seen as a routine identical to rb_vsprintf(), except it renders the output
115 * to the specified object rather than creating a new one.
116 *
117 * @param[out] dst String to modify.
118 * @param[in] fmt A `printf`-like format specifier.
119 * @param[in] ap Contents to format.
120 * @exception rb_eTypeError `dst` is not a String.
121 * @return Passed `dst`.
122 * @post `dst` has the rendered output appended to its end.
123 */
124 VALUE rb_str_vcatf(VALUE dst, const char *fmt, va_list ap);
125
126 /**
127 * Formats a string.
128 *
129 * Returns the string resulting from applying `fmt` to `argv`. The format
130 * sequence is a mixture of format specifiers and other verbatim contents.
131 * Each format specifier starts with a `%`, and has the following structure:
132 *
133 * ```
134 * %[flags][width][.precision]type
135 * ```
136 *
137 * ... which is different from that of rb_sprintf(). Because ruby has no
138 * `short` or `long`, there is no way to specify a "length" of an argument.
139 *
140 * This function supports flags of ` `, `#`, `+`, `-`, `<>`, `{}`, with of
141 * non-negative decimal integer and `$`, `*`, precision of non-negative decimal
142 * integer and `$`, `*`, type of `A`, `B`, `E`, `G`, `X`, `a`, `b`, `c`, `d`,
143 * `e`, `f`, `g`, `i`, `o`, `p`, `s`, `u`, `x`, `%`. This list is also
144 * (largely the same but) not identical to that of rb_sprintf().
145 *
146 * @param[in] argc Number of objects in `argv`.
147 * @param[in] argv Format arguments.
148 * @param[in] fmt A printf-like format specifier.
149 * @exception rb_eTypeError `fmt` is not a string.
150 * @exception rb_eArgError Failed to parse `fmt`.
151 * @return A rendered new instance of ::rb_cString.
152 * @note Everything it takes must be Ruby objects.
153 *
154 */
155 VALUE rb_str_format(int argc, const VALUE *argv, VALUE fmt);
156
157 RBIMPL_SYMBOL_EXPORT_END()
158
159 #endif /* RBIMPL_INTERN_SPRINTF_H */