BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslfmt_unicodecodepoint.h
Go to the documentation of this file.
1/// @file bslfmt_unicodecodepoint.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslfmt_unicodecodepoint.h -*-C++-*-
8
9#ifndef INCLUDED_BSLFMT_UNICODECODEPOINT
10#define INCLUDED_BSLFMT_UNICODECODEPOINT
11
12#include <bsls_ident.h>
13BSLS_IDENT("$Id: $")
14
15/// @defgroup bslfmt_unicodecodepoint bslfmt_unicodecodepoint
16/// @brief Provide a Unicode code point representation
17/// @addtogroup bsl
18/// @{
19/// @addtogroup bslfmt
20/// @{
21/// @addtogroup bslfmt_unicodecodepoint
22/// @{
23///
24/// <h1> Outline </h1>
25/// * <a href="#bslfmt_unicodecodepoint-purpose"> Purpose</a>
26/// * <a href="#bslfmt_unicodecodepoint-classes"> Classes </a>
27/// * <a href="#bslfmt_unicodecodepoint-description"> Description </a>
28///
29/// # Purpose {#bslfmt_unicodecodepoint-purpose}
30/// Provide a Unicode code point representation
31///
32/// # Classes {#bslfmt_unicodecodepoint-classes}
33///
34/// - bslfmt::UnicodeCodePoint: Unicode code point representation
35///
36/// # Description {#bslfmt_unicodecodepoint-description}
37/// This component provides a single, simply constrained
38/// (value-semantic) attribute class, `bslfmt::UnicodeCodePoint`, that is used
39/// to encapsulate a Unicode code point attributes.
40///
41/// This component is for use within `bslfmt` only.
42/// @}
43/** @} */
44/** @} */
45
46/** @addtogroup bsl
47 * @{
48 */
49/** @addtogroup bslfmt
50 * @{
51 */
52/** @addtogroup bslfmt_unicodecodepoint
53 * @{
54 */
55
56#include <bslscm_version.h>
57
58#include <bsla_unreachable.h>
59
60#include <bsls_assert.h>
61
62#include <string.h> // `size_t`
63
64
65namespace bslfmt {
66
67 // ======================
68 // class UnicodeCodePoint
69 // ======================
70
71/// This simply constrained (value-semantic) attribute class characterizes a
72/// Unicode code point represented by a set of integral values. This class is
73/// private to this package and should not be used by clients.
74///
75/// See @ref bslfmt_unicodecodepoint
77 public:
78 // TYPES
80
81 private:
82 // DATA
83 bool d_isValid;
84 int d_numSourceBytes;
85 unsigned long int d_codePointValue;
86 int d_codePointWidth;
87
88 /// Extract a UTF-8 code point from no more than `maxBytes` of the byte
89 /// stream at the specified `bytes` location. Return a
90 /// `CodePointExtractionResult` providing a decode status and, if the
91 /// decode is valid, a count of the source bytes used and the decoded
92 /// Unicode code point value. Byte Order Markers are not supported.
93 void extractUtf8(const void *bytes, size_t maxBytes);
94
95 /// Extract a UTF-16 code point from no more than the specified `maxBytes`
96 /// of the byte stream at the specified `bytes` location. Return a
97 /// `CodePointExtractionResult` providing a decode status and, if the
98 /// decode is valid, a count of the source bytes used and the decoded
99 /// Unicode code point value. Behavior is undefined if `bytes` is not a
100 /// valid pointer to an array of `numBytes/2` `wchar_t` types in contiguous
101 /// memory. Behavior is undefined if `16 != sizeof(wchar_t)`. Endianness
102 /// is assumed to be the same as for the `wchar_t` type and Byte Order
103 /// Markers are not supported.
104 void extractUtf16(const void *bytes, size_t maxBytes);
105
106 /// Extract a UTF-32 code point from no more than the specified `maxBytes`
107 /// of the byte stream at the specified `bytes` location. Return a
108 /// `CodePointExtractionResult` providing a decode status and, if the
109 /// decode is valid, a count of the source bytes used and the decoded
110 /// Unicode code point value. Behavior is undefined if `bytes` is not a
111 /// valid pointer to an array of `numBytes/2` `wchar_t` types in contiguous
112 /// memory. Behavior is undefined if `32 != sizeof(wchar_t)`. Endianness
113 /// is assumed to be the same as for the `wchar_t` type and Byte Order
114 /// Markers are not supported.
115 void extractUtf32(const void *bytes, size_t maxBytes);
116
117 public:
118 // CREATORS
119
120 /// Create an uninitialized Unicode code point object
122
123 // MANIPULATORS
124
125 /// Extract a code point from no more than the specified `maxBytes` of the
126 /// byte stream at the specified `bytes` location in the specified
127 /// `encoding`. Behavior is undefined if the input bytes are not in the
128 /// specified encoding. Unicode Byte Order Markers are not supported and
129 /// behavior is undefined if the input data contains an embedded BOM.
130 /// Endianness is assumed to be that of the type pointed to by `bytes`.
131 ///
132 /// For UTF-8, behavior is undefined if `bytes` is not a valid pointer to
133 /// an array of `numBytes` `unsigned char` types in contiguous memory.
134 ///
135 /// For UTF-16, behavior is undefined if `bytes` is not a valid pointer to
136 /// an array of `numBytes/2` `wchar_t` types in contiguous memory.
137 /// Behavior is undefined if `2 != sizeof(wchar_t)`. Endianness is
138 /// assumed to be the same as for the `wchar_t` type and Byte Order Markers
139 /// are not supported.
140 ///
141 /// For UTF-32, behavior is undefined if `bytes` is not a valid pointer to
142 /// an array of `numBytes/4` `wchar_t` types in contiguous memory.
143 /// Behavior is undefined if `4 != sizeof(wchar_t)`. Endianness is
144 /// assumed to be the same as for the `wchar_t` type and Byte Order Markers
145 /// are not supported.
146 void extract(UtfEncoding encoding, const void *bytes, size_t maxBytes);
147
148 /// Reset this object to the default state.
149 void reset();
150
151 // ACCESSORS
152
153 /// Return `true` if this object was successfully extracted and contains
154 /// valid code point data, and `false` otherwise.
155 bool isValid() const;
156
157 /// Return the number of bytes occupied by this code point.
158 int numSourceBytes() const;
159
160 /// Return the value of this code point.
161 unsigned long int codePointValue() const;
162
163 /// Return the width of this code point.
164 int codePointWidth() const;
165};
166
167// ============================================================================
168// INLINE DEFINITIONS
169// ============================================================================
170
171 // ----------------------
172 // class UnicodeCodePoint
173 // ----------------------
174
175// CREATORS
176inline
181
182// MANIPULATORS
183inline
185 const void *bytes,
186 size_t maxBytes)
187{
188 switch (encoding) {
189 case e_UTF8: {
190 extractUtf8(bytes, maxBytes);
191 } break;
192 case e_UTF16: {
193 extractUtf16(bytes, maxBytes);
194 } break;
195 case e_UTF32: {
196 extractUtf32(bytes, maxBytes);
197 } break;
198 default: {
199 BSLS_ASSERT_OPT_UNREACHABLE("Invalid encoding type");
201 }
202 }
203}
204
205inline
207{
208 d_isValid = false;
209 d_numSourceBytes = 0;
210 d_codePointValue = 0;
211 d_codePointWidth = 0;
212}
213
214// ACCESSSORS
215inline
217{
218 return d_isValid;
219}
220
221inline
223{
224 return d_numSourceBytes;
225}
226
227inline
228unsigned long int UnicodeCodePoint::codePointValue() const
229{
230 return d_codePointValue;
231}
232
233inline
235{
236 return d_codePointWidth;
237}
238
239} // close package namespace
240
241
242#endif // INCLUDED_BSLFMT_UNICODECODEPOINT
243
244// ----------------------------------------------------------------------------
245// Copyright 2024 Bloomberg Finance L.P.
246//
247// Licensed under the Apache License, Version 2.0 (the "License");
248// you may not use this file except in compliance with the License.
249// You may obtain a copy of the License at
250//
251// http://www.apache.org/licenses/LICENSE-2.0
252//
253// Unless required by applicable law or agreed to in writing, software
254// distributed under the License is distributed on an "AS IS" BASIS,
255// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
256// See the License for the specific language governing permissions and
257// limitations under the License.
258// ----------------------------- END-OF-FILE ----------------------------------
259
260/** @} */
261/** @} */
262/** @} */
Definition bslfmt_unicodecodepoint.h:76
int codePointWidth() const
Return the width of this code point.
Definition bslfmt_unicodecodepoint.h:234
unsigned long int codePointValue() const
Return the value of this code point.
Definition bslfmt_unicodecodepoint.h:228
void extract(UtfEncoding encoding, const void *bytes, size_t maxBytes)
Definition bslfmt_unicodecodepoint.h:184
int numSourceBytes() const
Return the number of bytes occupied by this code point.
Definition bslfmt_unicodecodepoint.h:222
UnicodeCodePoint()
Create an uninitialized Unicode code point object.
Definition bslfmt_unicodecodepoint.h:177
UtfEncoding
Definition bslfmt_unicodecodepoint.h:79
@ e_UTF16
Definition bslfmt_unicodecodepoint.h:79
@ e_UTF8
Definition bslfmt_unicodecodepoint.h:79
@ e_UTF32
Definition bslfmt_unicodecodepoint.h:79
void reset()
Reset this object to the default state.
Definition bslfmt_unicodecodepoint.h:206
bool isValid() const
Definition bslfmt_unicodecodepoint.h:216
#define BSLA_UNREACHABLE
Definition bsla_unreachable.h:159
#define BSLS_ASSERT_OPT_UNREACHABLE(X)
Definition bsls_assert.h:2065
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bslfmt_enablestreamedformatter.h:130