BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlt_literal_specifierformatter.h
Go to the documentation of this file.
1/// @file bdlt_literal_specifierformatter.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlt_literal_specifierformatter.h -*-C++-*-
8#ifndef INCLUDED_BDLT_LITERAL_SPECIFIERFORMATTER
9#define INCLUDED_BDLT_LITERAL_SPECIFIERFORMATTER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlt_literal_specifierformatter bdlt_literal_specifierformatter
15/// @brief Provide a specifier formatter for formatting simple literal values.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlt
19/// @{
20/// @addtogroup bdlt_literal_specifierformatter
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlt_literal_specifierformatter-purpose"> Purpose</a>
25/// * <a href="#bdlt_literal_specifierformatter-classes"> Classes </a>
26/// * <a href="#bdlt_literal_specifierformatter-description"> Description </a>
27///
28/// # Purpose {#bdlt_literal_specifierformatter-purpose}
29/// Provide a specifier formatter for formatting simple literal values.
30///
31/// # Classes {#bdlt_literal_specifierformatter-classes}
32///
33/// - bdlt::Literal_SpecifierFormatter: basic format spec
34///
35/// # Description {#bdlt_literal_specifierformatter-description}
36/// This common specifier formatter reads characters from the spec
37/// and parses those characters it recognizes. In the parse phase, this updates
38/// the anticipated width of output, in the format phase, it does output
39/// according to the specification input. Note that both parsing and formatting
40/// are independent of the type and value of the object being formatted.
41///
42/// The common specifier formatter interprets the following sequences (beginning
43/// with `%`), ending with the following characters:
44/// @code
45/// '%' - output a '%'
46/// 'n' - output a '\n'
47/// 't' - output a '\t'
48/// @endcode
49/// @}
50/** @} */
51/** @} */
52
53/** @addtogroup bdl
54 * @{
55 */
56/** @addtogroup bdlt
57 * @{
58 */
59/** @addtogroup bdlt_literal_specifierformatter
60 * @{
61 */
62
63#include <bdlscm_version.h>
64
65#include <bslmf_assert.h>
66#include <bslmf_issame.h>
67
68#include <bsla_fallthrough.h>
69
70#include <bsls_assert.h>
71#include <bsls_keyword.h>
72
73#include <bslstl_stringview.h>
74
75
76namespace bdlt {
77
78 // ================================
79 // class Literal_SpecifierFormatter
80 // ================================
81
82/// This `class` does parsing and output of sequences whose corresponding
83/// output is independent of the type and value of the object being formatted.
84///
85/// See @ref bdlt_literal_specifierformatter
86template <class t_CHAR>
90
91 // PRIVATE TYPES
93
94 // DATA
95 int d_totalWidth;
96
97 public:
98 // CREATORS
99
100 /// Create an object with `totalWidth` set to 0.
103
104 // MANIPULATORS
105
106 /// Parse the front character of the specified `spec`. If that character
107 /// matches any of those recognized by this specifier formatter, increment
108 /// `d_totalWidth`, pop the first character off of `spec` and return `true`, otherwise return `false`.
109 ///
110 /// \pre The behavior is undefined if `*spec`
111 /// is empty.
113
114 // ACCESSORS
115
116 /// Examine the front character of the specified `spec`. If that character
117 /// matches any of those recognized by this specifier formatter, perform
118 /// the sort of output indicated by that character off of `spec` and return
119 /// `true`, otherwise return `false`.
120 template <class t_ITERATOR>
121 bool formatNextSpecifier(StringView *spec, t_ITERATOR *outIt) const;
122
123 /// Return the total width of the output anticipated from all the previous
124 /// calls to `parseNextSpecifier`.
125 int totalWidth() const;
126};
127
128 // --------------------------------
129 // class Literal_SpecifierFormatter
130 // --------------------------------
131
132// CREATORS
133template <class t_CHAR>
138
139// MANIPULATORS
140template <class t_CHAR>
143{
144 BSLS_ASSERT(!spec->empty());
145
146 switch (spec->front()) {
147 case '%': BSLA_FALLTHROUGH;
148 case 'n': BSLA_FALLTHROUGH;
149 case 't': {
150 ++d_totalWidth;
151 spec->remove_prefix(1);
152
153 return true; // RETURN
154 } break;
155 }
156
157 return false;
158}
159
160// ACCESSORS
161template <class t_CHAR>
162template <class t_ITERATOR>
163inline
165 formatNextSpecifier(StringView *spec, t_ITERATOR *outIt) const
166{
167 BSLS_ASSERT_SAFE(!spec->empty());
168
169 t_CHAR outChar;
170
171 switch (spec->front()) {
172 case t_CHAR('%'): {
173 outChar = '%';
174 } break;
175 case t_CHAR('n'): {
176 outChar = '\n';
177 } break;
178 case t_CHAR('t'): {
179 outChar = '\t';
180 } break;
181 default: {
182 return false; // RETURN
183 } break;
184 }
185
186 *(*outIt)++ = outChar;
187 spec->remove_prefix(1);
188
189 return true;
190}
191
192template <class t_CHAR>
193inline
195{
196 return d_totalWidth;
197}
198
199} // close package namespace
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/** @} */
Definition bdlt_literal_specifierformatter.h:87
int totalWidth() const
Definition bdlt_literal_specifierformatter.h:194
bool formatNextSpecifier(StringView *spec, t_ITERATOR *outIt) const
Definition bdlt_literal_specifierformatter.h:165
BSLS_KEYWORD_CONSTEXPR_CPP20 Literal_SpecifierFormatter()
Create an object with totalWidth set to 0.
Definition bdlt_literal_specifierformatter.h:135
BSLS_KEYWORD_CONSTEXPR_CPP20 bool parseNextSpecifier(StringView *spec)
Definition bdlt_literal_specifierformatter.h:142
Definition bslstl_stringview.h:471
BSLS_KEYWORD_CONSTEXPR_CPP14 const_reference front() const
Definition bslstl_stringview.h:1966
BSLS_KEYWORD_CONSTEXPR_CPP14 void remove_prefix(size_type numChars)
Definition bslstl_stringview.h:1800
BSLS_KEYWORD_CONSTEXPR bool empty() const BSLS_KEYWORD_NOEXCEPT
Return true if this view has length 0, and false otherwise.
Definition bslstl_stringview.h:1931
#define BSLA_FALLTHROUGH
Definition bsla_fallthrough.h:188
#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
#define BSLS_KEYWORD_CONSTEXPR_CPP20
Definition bsls_keyword.h:645
Definition bbldc_basicisma30360.h:112
Definition bslmf_issame.h:146