BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslstl_charconv.h
Go to the documentation of this file.
1/// @file bslstl_charconv.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslstl_charconv.h -*-C++-*-
8#ifndef INCLUDED_BSLSTL_CHARCONV
9#define INCLUDED_BSLSTL_CHARCONV
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bslstl_charconv bslstl_charconv
15/// @brief Provide implementations for functions not in the system library.
16/// @addtogroup bsl
17/// @{
18/// @addtogroup bslstl
19/// @{
20/// @addtogroup bslstl_charconv
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bslstl_charconv-purpose"> Purpose</a>
25/// * <a href="#bslstl_charconv-classes"> Classes </a>
26/// * <a href="#bslstl_charconv-canonical-header"> Canonical Header </a>
27/// * <a href="#bslstl_charconv-description"> Description </a>
28/// * <a href="#bslstl_charconv-usage"> Usage </a>
29/// * <a href="#bslstl_charconv-example-1-demonstrating-writing-a-number-to-a-streambuf"> Example 1: Demonstrating Writing a number to a streambuf </a>
30///
31/// # Purpose {#bslstl_charconv-purpose}
32/// Provide implementations for functions not in the system library.
33///
34/// # Classes {#bslstl_charconv-classes}
35///
36///
37/// # Canonical Header {#bslstl_charconv-canonical-header}
38/// bsl_charconv.h
39///
40/// @see bsl+bslhdrs
41///
42/// # Description {#bslstl_charconv-description}
43/// This component is for internal use only. Please include
44/// `<bsl_charconv.h>` instead. This component provides implementations for
45/// standard algorithms that are not provided by the underlying standard library
46/// implementation. For example, `to_chars` is a C++17 algorithm, and it is
47/// provided here for code using C++03 - C++14 or for compilers that do not
48/// provide `<charconv>` in C++17.
49///
50/// `to_chars` is locale-independent, non-allocating, and non-throwing, and
51/// provides a safe and more performant alternative to `snprintf` in contexts
52/// where complex formatting options or locale are not important.
53///
54/// ## Usage {#bslstl_charconv-usage}
55///
56///
57/// In this section we show intended use of this component.
58///
59/// ### Example 1: Demonstrating Writing a number to a streambuf {#bslstl_charconv-example-1-demonstrating-writing-a-number-to-a-streambuf}
60///
61///
62/// Suppose we want to write a function that writes an `int` to a `streambuf`.
63/// We can use `bsl::to_chars` to write the `int` to a buffer, then write the
64/// buffer to the `streambuf`.
65///
66/// First, we declare our function:
67/// @code
68/// /// Write the specified `value`, in decimal, to the specified `result`.
69/// void writeJsonScalar(std::streambuf *result, int value)
70/// {
71/// @endcode
72/// Then, we declare a buffer long enough to store any `int` value in decimal.
73/// @code
74/// char buffer[11]; // size large enough to write `INT_MIN`, the
75/// // worst-case value, in decimal.
76/// @endcode
77/// Next, we declare a variable to store the return value:
78/// @code
79/// bsl::to_chars_result sts;
80/// @endcode
81/// Then, we call the function:
82/// @code
83/// sts = bsl::to_chars(buffer, buffer + sizeof(buffer), value);
84/// @endcode
85/// Next, we check that the buffer was long enough, which should always be the
86/// case:
87/// @code
88/// assert(bsl::ErrcEnum() == sts.ec);
89/// @endcode
90/// Now, we check that `sts.ptr` is in the range
91/// `[ buffer + 1, buffer + sizeof(buffer) ]`, which will always be the case
92/// whether `to_chars` succeeded or failed.
93/// @code
94/// assert(buffer < sts.ptr);
95/// assert(sts.ptr <= buffer + sizeof(buffer));
96/// @endcode
97/// Finally, we write our buffer to the `streambuf`:
98/// @code
99/// result->sputn(buffer, sts.ptr - buffer);
100/// }
101/// @endcode
102/// @}
103/** @} */
104/** @} */
105
106/** @addtogroup bsl
107 * @{
108 */
109/** @addtogroup bslstl
110 * @{
111 */
112/** @addtogroup bslstl_charconv
113 * @{
114 */
115
116#include <bslscm_version.h>
117
118#include <bslstl_errc.h>
119
121#include <bslmf_assert.h>
122#include <bslmf_isintegral.h>
123#include <bslmf_issame.h>
124#include <bslmf_removecv.h>
125#include <bsls_assert.h>
126#include <bsls_libraryfeatures.h>
127#include <bsls_platform.h>
128#include <bsls_types.h>
129
130#if defined(BSLS_LIBRARYFEATURES_HAS_CPP17_INT_CHARCONV)
131
132#include <charconv>
133
134#endif
135
136
137namespace bslstl {
138
139 // ========================
140 // struct 'to_chars_result'
141 // ========================
142
143/// This `struct` represents the result of the `to_chars` function. On a
144/// successful call to `to_chars_result`, `ptr` is the one past the end
145/// pointer of the sequence of characters written, and `ec` is a default
146/// constructed ErrcEnum. On failure, `ptr` is set to the end of the buffer
147/// supplied to `to_chars` and `ec` is set to `errc::value_to_large`.
148///
149/// See @ref bslstl_charconv
151
152 // PUBLIC DATA
153 char *ptr;
155};
156
157// FREE OPERATORS
158
159/// Write the specified `value` into the character buffer starting a the
160/// specified `first` and ending at the specified `last`. Optionally
161/// specify `base`, the base in which the number is to be written. If
162/// `base` is not specified, decimal is used. Return a `to_chars_result`
163/// `struct` indicating success or failure, and the end of the written
164/// result. On success, the output string is to begin at `first`, the `ptr`
165/// field in the return value is to point at the end of the representation,
166/// and the `ec` field will be 0. If the buffer specified by
167/// `[ first .. last )` is not large enough for the result, return a
168/// `struct` with `ptr` set to `last` and `ec` set to
169/// `errc::value_too_large`. Insufficient room in the output buffer is the only failure mode.
170///
171/// \pre The behavior is undefined unless `first < last` and
172/// `base` is in the range `[ 2 .. 36 ]`.
173template <class INTEGRAL_TYPE>
175to_chars(char *first, char *last, INTEGRAL_TYPE value, int base = 10);
176
177// ============================================================================
178// INLINE FUNCTION DEFINITIONS
179// ============================================================================
180
181// FREE OPERATORS
182template <class INTEGRAL_TYPE>
183inline
185to_chars(char *first, char *last, INTEGRAL_TYPE value, int base)
186{
187 BSLS_ASSERT(2 <= base);
188 BSLS_ASSERT(base <= 36);
189 BSLS_ASSERT(first < last);
190
191 typedef bslalg::NumericFormatterUtil Util;
192 typedef bsls::Types::Uint64 Uint64;
193
196 bool>::value));
197 BSLMF_ASSERT(sizeof(INTEGRAL_TYPE) <= sizeof(Uint64));
198
199 char *end = Util::toChars(first, last, value, base);
200 if (!end) {
201 const to_chars_result ret = { last, bsl::errc::value_too_large };
202 return ret; // RETURN
203 }
204
205 const to_chars_result ret = { end , bsl::ErrcEnum() };
206 return ret;
207}
208
209} // close package namespace
210
211
212namespace bsl {
213
214#if defined(BSLS_LIBRARYFEATURES_HAS_CPP17_INT_CHARCONV)
215
216using std::to_chars_result;
217using std::to_chars;
218using std::from_chars;
219using std::from_chars_result;
220
221#if defined(BSLS_LIBRARYFEATURES_HAS_CPP17_CHARCONV)
222using std::chars_format;
223#endif
224
225#else
226
227using BloombergLP::bslstl::to_chars_result;
228using BloombergLP::bslstl::to_chars;
229
230#endif
231
232} // close namespace bsl
233
234#endif // INCLUDED_BSLSTL_CHARCONV
235
236// ----------------------------------------------------------------------------
237// Copyright 2020 Bloomberg Finance L.P.
238//
239// Licensed under the Apache License, Version 2.0 (the "License");
240// you may not use this file except in compliance with the License.
241// You may obtain a copy of the License at
242//
243// http://www.apache.org/licenses/LICENSE-2.0
244//
245// Unless required by applicable law or agreed to in writing, software
246// distributed under the License is distributed on an "AS IS" BASIS,
247// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
248// See the License for the specific language governing permissions and
249// limitations under the License.
250// ----------------------------- END-OF-FILE ----------------------------------
251
252/** @} */
253/** @} */
254/** @} */
#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 bdlat_valuetypefunctions.h:939
errc::Enum ErrcEnum
Definition bslstl_errc.h:226
Definition bslstl_algorithm.h:84
to_chars_result to_chars(char *first, char *last, INTEGRAL_TYPE value, int base=10)
Definition bslstl_charconv.h:185
Enum
Definition bslstl_errc.h:144
@ value_too_large
Definition bslstl_errc.h:221
Definition bslmf_isintegral.h:140
Definition bslmf_issame.h:146
remove_const< typenameremove_volatile< t_TYPE >::type >::type type
Definition bslmf_removecv.h:128
Definition bslalg_numericformatterutil.h:400
unsigned long long Uint64
Definition bsls_types.h:139
Definition bslstl_charconv.h:150
bsl::ErrcEnum ec
Definition bslstl_charconv.h:154
char * ptr
Definition bslstl_charconv.h:153