BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslfmt_formattercharutil.h
Go to the documentation of this file.
1/// @file bslfmt_formattercharutil.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslfmt_formattercharutil.h -*-C++-*-
8
9#ifndef INCLUDED_BSLFMT_FORMATTERCHARUTIL
10#define INCLUDED_BSLFMT_FORMATTERCHARUTIL
11
12#include <bsls_ident.h>
13BSLS_IDENT("$Id: $")
14
15/// @defgroup bslfmt_formattercharutil bslfmt_formattercharutil
16/// @brief Character conversion utilities for `bsl::format`.
17/// @addtogroup bsl
18/// @{
19/// @addtogroup bslfmt
20/// @{
21/// @addtogroup bslfmt_formattercharutil
22/// @{
23///
24/// <h1> Outline </h1>
25/// * <a href="#bslfmt_formattercharutil-purpose"> Purpose</a>
26/// * <a href="#bslfmt_formattercharutil-classes"> Classes </a>
27/// * <a href="#bslfmt_formattercharutil-description"> Description </a>
28/// * <a href="#bslfmt_formattercharutil-usage"> Usage </a>
29/// * <a href="#bslfmt_formattercharutil-example-outputting-a-hexadecimal-in-upper-case"> Example: Outputting A Hexadecimal In Upper Case </a>
30///
31/// # Purpose {#bslfmt_formattercharutil-purpose}
32/// Character conversion utilities for `bsl::format`.
33///
34/// # Classes {#bslfmt_formattercharutil-classes}
35///
36/// - bslfmt::FormatterCharUtil: utilities to perform character conversions
37///
38/// # Description {#bslfmt_formattercharutil-description}
39/// This component provides a `FormatterCharUtil` `struct` template
40/// that is a namespace for utility functions that convert characters (e.g. from
41/// `char` to `wchar_t` or lowercase characters to uppercase).
42///
43/// ## Usage {#bslfmt_formattercharutil-usage}
44///
45///
46/// This section illustrates intended use of this component.
47///
48/// ### Example: Outputting A Hexadecimal In Upper Case {#bslfmt_formattercharutil-example-outputting-a-hexadecimal-in-upper-case}
49///
50///
51/// Suppose we need to output a hexadecimal number to some object (e.g. some
52/// character buffer) represented by the output iterator. Additionally, we are
53/// required to have the number displayed in uppercase.
54/// @code
55/// char number[] = "0x12cd";
56/// const size_t sourceLength = sizeof(number) - 1;
57/// @endcode
58/// First, we convert the number to uppercase in place and verify the result:
59/// @code
60/// bslfmt::FormatterCharUtil<char>::toUpper(number, number + sourceLength);
61/// const char *expectedUppercaseNumber = "0X12CD";
62/// assert(0 == strcmp(number, expectedUppercaseNumber));
63/// @endcode
64/// Next, we output this uppercase number to the destination, using
65/// `outputFromChar` function. `OutputIterator` in this example is just a
66/// primitive class that minimally satisfies the requirements of the output
67/// iterator.
68/// @code
69/// char destination[8];
70/// memset(destination, 0, sizeof(destination));
71/// OutputIterator<char> charIt(destination);
72///
73/// charIt = bslfmt::FormatterCharUtil<char>::outputFromChar(
74/// number,
75/// number + sourceLength,
76/// charIt);
77///
78/// assert(destination + sourceLength == charIt.ptr());
79/// assert(0 == strcmp(number, destination));
80/// @endcode
81/// Finally, we demonstrate the main purpose of these functions - to unify the
82/// output of values to character strings and wide character strings. All we
83/// need to do is just change the template parameter:
84/// @code
85/// wchar_t wDestination[8];
86/// memset(wDestination, 0, sizeof(wchar_t) * 8);
87///
88/// wchar_t wcharExpected[] = L"0X12CD";
89///
90/// OutputIterator<wchar_t> wcharIt(wDestination);
91/// wcharIt = bslfmt::FormatterCharUtil<wchar_t>::outputFromChar(
92/// number,
93/// number + sourceLength,
94/// wcharIt);
95/// assert(wDestination + sourceLength == wcharIt.ptr());
96/// assert(0 == wcscmp(wcharExpected, wDestination));
97/// @endcode
98/// @}
99/** @} */
100/** @} */
101
102/** @addtogroup bsl
103 * @{
104 */
105/** @addtogroup bslfmt
106 * @{
107 */
108/** @addtogroup bslfmt_formattercharutil
109 * @{
110 */
111
112#include <bslscm_version.h>
113
114#include <bslmf_assert.h>
115#include <bslmf_issame.h>
116
117#include <bsls_assert.h>
118
119#include <bslstl_algorithm.h>
120#include <bslstl_iterator.h>
121
122#include <locale> // for 'std::ctype', 'locale'
123
124
125namespace bslfmt {
126
127 // ========================
128 // struct FormatterCharUtil
129 // ========================
130
131/// This struct provides a namespace for a utility functions that convert
132/// characters (e.g. `char` to `wchar_t` or lowercase characters to uppercase).
133/// Notice that this structure has specializations only for `char` and
134/// `wchar_t` types.
135///
136/// See @ref bslfmt_formattercharutil
137template <class t_CHAR>
139};
140
141/// This is a specialization of `FormatterCharUtil` template providing utility
142/// functions for outputting data to the objects specialized by `char` type and
143/// referred by the corresponding output iterator.
144template <>
145struct FormatterCharUtil<char> {
146 // CLASS METHODS
147
148 /// Output to the specified output iterator `out` the character sequence
149 /// starting at the specified `begin` address and ending immediately before
150 /// the specified `end` address. Return `out` incremented by the number of characters written.
151 ///
152 /// \pre The behavior is undefined unless `begin <= end`.
153 template <class t_ITERATOR>
154 static t_ITERATOR outputFromChar(const char *begin,
155 const char *end,
156 t_ITERATOR out);
157
158 /// Output to the specified output iterator `out` the specified `value`.
159 /// Return incremented `out`.
160 template <class t_ITERATOR>
161 static t_ITERATOR outputFromChar(const char value, t_ITERATOR out);
162
163 /// Convert all characters in the sequence starting at the specified
164 /// `begin` address and ending immediately before the specified `end` address to uppercase.
165 /// Note that conversion happens in-place.
166 ///
167 /// \pre The behavior is undefined unless `begin <= end`.
168 static void toUpper(char *begin, const char *end);
169};
170
171/// This is a specialization of `FormatterCharUtil` template providing utility
172/// functions for outputting data to the objects specialized by `wchar_t` type
173/// and referred by the corresponding output iterator.
174template <>
175struct FormatterCharUtil<wchar_t> {
176 // CLASS METHODS
177
178 /// Output to the specified output iterator `out` the character sequence
179 /// starting at the specified `begin` address and ending immediately before
180 /// the specified `end` address. Return `out` incremented by the number of characters written.
181 ///
182 /// \pre The behavior is undefined unless `begin <= end`.
183 ///
184 /// \note Note that in case of a negative input character the outputted result is
185 /// unspecified.
186 template <class t_ITERATOR>
187 static t_ITERATOR outputFromChar(const char *begin,
188 const char *end,
189 t_ITERATOR out);
190
191 /// Output to the specified output iterator `out` the specified `value`. Return incremented `out`.
192 ///
193 /// \note Note that in case of a negative input
194 /// character the outputted result is unspecified.
195 template <class t_ITERATOR>
196 static t_ITERATOR outputFromChar(const char value, t_ITERATOR out);
197};
198
199// ============================================================================
200// INLINE DEFINITIONS
201// ============================================================================
202
203 // ------------------------
204 // struct FormatterCharUtil
205 // ------------------------
206// CLASS METHODS
207template <class t_ITERATOR>
208t_ITERATOR FormatterCharUtil<char>::outputFromChar(const char *begin,
209 const char *end,
210 t_ITERATOR out)
211{
212 BSLS_ASSERT(begin <= end);
213
214 typedef typename bsl::iterator_traits<t_ITERATOR>::value_type ValueType;
217
218 return bsl::copy(begin, end, out);
219}
220
221template <class t_ITERATOR>
222t_ITERATOR FormatterCharUtil<char>::outputFromChar(const char value,
223 t_ITERATOR out)
224{
225 typedef typename bsl::iterator_traits<t_ITERATOR>::value_type ValueType;
228
229 *out++ = value;
230
231 return out;
232}
233
234inline
235void FormatterCharUtil<char>::toUpper(char *begin, const char *end)
236{
237 BSLS_ASSERT(begin <= end);
238
239 for (; begin != end; (void)++begin) {
240 if (*begin >= 'a' && *begin <= 'z') {
241 *begin = static_cast<char>(*begin + 'A' - 'a');
242 }
243 }
244}
245
246template <class t_ITERATOR>
248 const char *end,
249 t_ITERATOR out)
250{
251 BSLS_ASSERT(begin <= end);
252
253 typedef typename bsl::iterator_traits<t_ITERATOR>::value_type ValueType;
256
257 static const std::ctype<wchar_t>& ct =
258 std::use_facet<std::ctype<wchar_t> >(std::locale::classic());
259
260 for (; begin != end; (void)++begin, (void)++out) {
261
262 BSLS_ASSERT(0 <= *begin);
263
264 *out = ct.widen(*begin);
265 }
266
267 return out;
268}
269
270template <class t_ITERATOR>
272 t_ITERATOR out)
273{
274 typedef typename bsl::iterator_traits<t_ITERATOR>::value_type ValueType;
277
278 BSLS_ASSERT(0 <= value);
279
280
281 static const std::ctype<wchar_t>& ct =
282 std::use_facet<std::ctype<wchar_t> >(std::locale::classic());
283
284 *out++ = ct.widen(value);
285
286 return out;
287}
288
289} // close package namespace
290
291
292#endif // INCLUDED_BSLFMT_FORMATTERCHARUTIL
293
294// ----------------------------------------------------------------------------
295// Copyright 2024 Bloomberg Finance L.P.
296//
297// Licensed under the Apache License, Version 2.0 (the "License");
298// you may not use this file except in compliance with the License.
299// You may obtain a copy of the License at
300//
301// http://www.apache.org/licenses/LICENSE-2.0
302//
303// Unless required by applicable law or agreed to in writing, software
304// distributed under the License is distributed on an "AS IS" BASIS,
305// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
306// See the License for the specific language governing permissions and
307// limitations under the License.
308// ----------------------------- END-OF-FILE ----------------------------------
309
310/** @} */
311/** @} */
312/** @} */
#define BSLMF_ASSERT(expr)
Definition bslmf_assert.h:231
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bslfmt_enablestreamedformatter.h:130
Definition bslmf_issame.h:146
Definition bslfmt_formattercharutil.h:138