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