BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlt_formatutil.h
Go to the documentation of this file.
1/// @file bdlt_formatutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlt_formatutil.h -*-C++-*-
8#ifndef INCLUDED_BDLT_FORMATUTIL
9#define INCLUDED_BDLT_FORMATUTIL
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlt_formatutil bdlt_formatutil
15/// @brief Provide utilities for formatting `bdlt` types
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlt
19/// @{
20/// @addtogroup bdlt_formatutil
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlt_formatutil-purpose"> Purpose</a>
25/// * <a href="#bdlt_formatutil-classes"> Classes </a>
26/// * <a href="#bdlt_formatutil-description"> Description </a>
27///
28/// # Purpose {#bdlt_formatutil-purpose}
29/// Provide utilities for formatting `bdlt` types
30///
31/// # Classes {#bdlt_formatutil-classes}
32///
33/// - bdlt::FormatUtil: namespace for formatting utilities for `bdlt`
34///
35/// # Description {#bdlt_formatutil-description}
36/// This component provides a namespace, `bdlt::FormatUtil`,
37/// containing functions facilitating `bsl::format`-style formatting of `bdlt`
38/// value types. The output character type (`char` or `wchar_t`) is controlled
39/// via the template parameter `t_CHAR`.
40/// @}
41/** @} */
42/** @} */
43
44/** @addtogroup bdl
45 * @{
46 */
47/** @addtogroup bdlt
48 * @{
49 */
50/** @addtogroup bdlt_formatutil
51 * @{
52 */
53
54#include <bdlscm_version.h>
55
56#include <bslfmt_formaterror.h>
59#include <bslfmt_padutil.h>
60
61#include <bslmf_assert.h>
62#include <bslmf_issame.h>
63
64#include <bsla_fallthrough.h>
65
66#include <bsls_assert.h>
67#include <bsls_exceptionutil.h>
68#include <bsls_platform.h>
69
70#include <bsl_algorithm.h>
71#include <bsl_charconv.h>
72#include <bsl_cstddef.h>
73
74
75namespace bdlt {
76
77 // ==========
78 // FormatUtil
79 // ==========
80
81/// This `class` serves as a namespace for some static functions that are
82/// useful in creating specializations of `bsl::formatter` to support
83/// `bsl::format`.
84///
85/// See @ref bdlt_formatutil
86template<class t_CHAR>
90
91 // PRIVATE TYPES
93
94 public:
95 // PUBLIC TYPES
96
98
99 // CLASS METHODS
100
101 /// Write the fraction of a second from the timeCache to `d_out`, where the
102 /// `precision` is the number of digits of precision wanted. Use
103 /// `decimalChar` (expected to be '.' or ',') as a decimal point, and if
104 /// `precision == 0` output nothing. If `6 < precision`, write '0'
105 /// characters following the first 6 digits.
106 ///
107 /// \pre The behavior is undefined unless `0 <= millisecond < 1000`, `0 <= microsecond < 1000`, and
108 /// `0 <= precision`.
109 template <class t_ITERATOR>
110 static t_ITERATOR writeSecondFraction(t_ITERATOR out,
111 int precision,
112 t_CHAR decimalChar,
113 int millisecond,
114 int microsecond);
115
116 /// Write the specified `value` in decimal to the specified `out`. Always
117 /// write the specified `numDigits` digits, '0'-padding to the left if
118 /// necessary. Return the iterator after output is done.
119 ///
120 /// \pre The behavior is undefined unless `2 <= numDigits <= 6` and
121 /// `0 <= value < pow(10, numDigits)`.
122 template <class t_ITERATOR>
123 static t_ITERATOR writeZeroPaddedDigits(t_ITERATOR out,
124 int value,
125 int numDigits = 2);
126};
127
128 // ----------
129 // FormatUtil
130 // ----------
131
132// CLASS METHODS
133template <class t_CHAR>
134template <class t_ITERATOR>
136 int precision,
137 t_CHAR decimalChar,
138 int millisecond,
139 int microsecond)
140{
141 BSLS_ASSERT(0 <= millisecond && millisecond < 1000);
142 BSLS_ASSERT(0 <= microsecond && microsecond < 1000);
143 BSLS_ASSERT(0 <= precision);
144
145 if (precision == 0) {
146 return out; // RETURN
147 }
148 *out++ = t_CHAR(decimalChar);
149
150 char buffer[12] = "000000";
151 bsl::to_chars_result result = bsl::to_chars(
152 buffer + 6,
153 bsl::end(buffer),
154 millisecond * 1000 + microsecond);
155 BSLS_ASSERT(bsl::ErrcEnum() == result.ec);
156
157 char *valueBegin = result.ptr - 6;
158 char *valueEnd = result.ptr;
159
160 if (precision > 6) {
161 out = bsl::copy(valueBegin, valueEnd, out);
162 out = PadUtil::pad(out, precision - 6, '0');
163 }
164 else {
165 out = bsl::copy(valueBegin, valueBegin + precision, out);
166 }
167
168 return out;
169}
170
171template <class t_CHAR>
172template <class t_ITERATOR>
173inline
175 int value,
176 int numDigits)
177{
178 BSLS_ASSERT_SAFE(2 <= numDigits && numDigits <= 6);
179 BSLS_ASSERT(0 <= value && value < 1000000);
180
181 enum { k_BUF_LEN = 6 };
182
183 char buf[k_BUF_LEN];
184 bsl::to_chars_result result = bsl::to_chars(bsl::begin(buf),
185 bsl::end(buf),
186 value);
187 BSLS_ASSERT(bsl::ErrcEnum() == result.ec);
188 char * const end = result.ptr;
189
190 bsl::ptrdiff_t padWidth = numDigits - bsl::distance(buf, end);
191 BSLS_ASSERT(0 <= padWidth);
192 out = PadUtil::pad(out, padWidth, '0');
193
195
196 return out;
197}
198
199} // close namespace bdlt
200
201
202#endif
203
204// ----------------------------------------------------------------------------
205// Copyright 2026 Bloomberg Finance L.P.
206//
207// Licensed under the Apache License, Version 2.0 (the "License");
208// you may not use this file except in compliance with the License.
209// You may obtain a copy of the License at
210//
211// http://www.apache.org/licenses/LICENSE-2.0
212//
213// Unless required by applicable law or agreed to in writing, software
214// distributed under the License is distributed on an "AS IS" BASIS,
215// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
216// See the License for the specific language governing permissions and
217// limitations under the License.
218// ----------------------------- END-OF-FILE ----------------------------------
219
220
221/** @} */
222/** @} */
223/** @} */
Definition bdlt_formatutil.h:87
static t_ITERATOR writeZeroPaddedDigits(t_ITERATOR out, int value, int numDigits=2)
Definition bdlt_formatutil.h:174
static t_ITERATOR writeSecondFraction(t_ITERATOR out, int precision, t_CHAR decimalChar, int millisecond, int microsecond)
Definition bdlt_formatutil.h:135
bsl::basic_string_view< t_CHAR > StringView
Definition bdlt_formatutil.h:97
Definition bslstl_stringview.h:471
#define BSLMF_ASSERT(expr)
Definition bslmf_assert.h:231
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_ASSERT_SAFE(X)
Definition bsls_assert.h:1917
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bbldc_basicisma30360.h:112
T::iterator begin(T &container)
Definition bslstl_iterator.h:1593
T::iterator end(T &container)
Definition bslstl_iterator.h:1621
Enum
Definition bslstl_errc.h:144
Definition bslmf_issame.h:146
Definition bslfmt_formattercharutil.h:138
Definition bslfmt_padutil.h:151