BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bdlde_charconvertutf32.h
Go to the documentation of this file.
1/// @file bdlde_charconvertutf32.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bdlde_charconvertutf32.h -*-C++-*-
8#ifndef INCLUDED_BDLDE_CHARCONVERTUTF32
9#define INCLUDED_BDLDE_CHARCONVERTUTF32
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup bdlde_charconvertutf32 bdlde_charconvertutf32
15/// @brief Provide fast, safe conversion between UTF-8 encoding and UTF-32.
16/// @addtogroup bdl
17/// @{
18/// @addtogroup bdlde
19/// @{
20/// @addtogroup bdlde_charconvertutf32
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#bdlde_charconvertutf32-purpose"> Purpose</a>
25/// * <a href="#bdlde_charconvertutf32-classes"> Classes </a>
26/// * <a href="#bdlde_charconvertutf32-description"> Description </a>
27/// * <a href="#bdlde_charconvertutf32-history-and-motivation"> History and Motivation </a>
28/// * <a href="#bdlde_charconvertutf32-usage"> Usage </a>
29/// * <a href="#bdlde_charconvertutf32-example-1-round-trip-multi-lingual-conversion"> Example 1: Round-Trip Multi-Lingual Conversion </a>
30///
31/// # Purpose {#bdlde_charconvertutf32-purpose}
32/// Provide fast, safe conversion between UTF-8 encoding and UTF-32.
33///
34/// # Classes {#bdlde_charconvertutf32-classes}
35///
36/// - bdlde::CharConvertUtf32: namespace for conversion between UTF-8 and UTF-32
37///
38/// # Description {#bdlde_charconvertutf32-description}
39/// This component provides a `struct`, `bdlde::CharConvertUtf32`,
40/// that provides a suite of static functions supporting the *fast* conversion
41/// of UTF-8 data to UTF-32, and vice versa. UTF-8 input can take the form of
42/// null-terminated "C" strings or `bsl::string_view`s, while UTF-32 input can
43/// only take the form of null-terminated buffers of `unsigned int`. Output can
44/// be to STL vectors, `bsl::string`s (in the case of UTF-8), and fixed-length
45/// buffers. Invalid byte sequences and code points forbidden by either
46/// encoding are removed and (optionally) replaced by an error byte or word
47/// provided by the caller. The byte order of the UTF-32 input or output can be
48/// specified via the optional `byteOrder` argument, which is assumed to be host
49/// byte order if not specified. The byte or word count and code point count
50/// that are optionally returned through pointer arguments include the
51/// terminating null byte or word.
52///
53/// ## History and Motivation {#bdlde_charconvertutf32-history-and-motivation}
54///
55///
56/// UTF-8 is a Unicode encoding that allows 32-bit Unicode to be represented
57/// using null-terminated (8-bit) byte strings, while allowing "standard ASCII"
58/// strings to be used "as-is". Note that UTF-8 is described in detail in RFC
59/// 3629 (http://www.ietf.org/rfc/rfc3629.txt).
60///
61/// UTF-32 is simply a name for storing raw Unicode values as sequential
62/// `unsigned int` values in memory.
63///
64/// Valid Unicode values are in the ranges `[ 1 .. 0xd7ff ]` and
65/// `[ 0xe000 .. 0x10ffff ]`. The value `0` is used to terminate sequences.
66///
67/// The functions here that translate to fixed buffers make a single pass
68/// through the data. The functions that translate to `bsl::string`s and
69/// `bsl::vector`s, however, like the `glib` conversion routines, make two
70/// passes: a size estimation pass, after which the output container is sized
71/// appropriately, and then the translation pass.
72///
73/// The methods that output to a `vector` or `string` will all grow the output
74/// object as necessary to fit the data, and in the end will exactly resize the
75/// object to the output (including the terminating 0 for `vector`, not
76/// including it for `string`). The resizing will not affect the capacity.
77///
78/// Non-minimal UTF-8 encodings of code points are reported as errors. Octets
79/// and post-conversion code points in the forbidden ranges are treated as
80/// errors and removed if 0 is specified as `errorWord`, or replaced with
81/// `errorWord` otherwise.
82///
83/// ## Usage {#bdlde_charconvertutf32-usage}
84///
85///
86/// This section illustrates intended use of this component.
87///
88/// ### Example 1: Round-Trip Multi-Lingual Conversion {#bdlde_charconvertutf32-example-1-round-trip-multi-lingual-conversion}
89///
90///
91/// The following snippets of code illustrate a typical use of the
92/// `bdlde::CharConvertUtf32` struct's utility functions, first converting from
93/// UTF-8 to UTF-32, and then converting back to make sure the round trip
94/// returns the same value.
95///
96/// First, we declare a string of UTF-8 containing single-, double-, triple-,
97/// and quadruple-octet code points:
98/// @code
99/// const char utf8MultiLang[] = {
100/// "Hello" // -- ASCII
101/// "\xce\x97" "\xce\x95" "\xce\xbb" // -- Greek
102/// "\xe4\xb8\xad" "\xe5\x8d\x8e" // -- Chinese
103/// "\xe0\xa4\xad" "\xe0\xa4\xbe" // -- Hindi
104/// "\xf2\x94\xb4\xa5" "\xf3\xb8\xac\x83" }; // -- Quad octets
105/// @endcode
106/// Then, we declare an `enum` summarizing the counts of code points in the
107/// string and verify that the counts add up to the length of the string:
108/// @code
109/// enum { NUM_ASCII_CODE_POINTS = 5,
110/// NUM_GREEK_CODE_POINTS = 3,
111/// NUM_CHINESE_CODE_POINTS = 2,
112/// NUM_HINDI_CODE_POINTS = 2,
113/// NUM_QUAD_CODE_POINTS = 2 };
114///
115/// assert(1 * NUM_ASCII_CODE_POINTS +
116/// 2 * NUM_GREEK_CODE_POINTS +
117/// 3 * NUM_CHINESE_CODE_POINTS +
118/// 3 * NUM_HINDI_CODE_POINTS +
119/// 4 * NUM_QUAD_CODE_POINTS == bsl::strlen(utf8MultiLang));
120/// @endcode
121/// Next, we declare the vector where our UTF-32 output will go, and a variable
122/// into which the number of code points written will be stored. It is not
123/// necessary to create a `utf32CodePointsWritten` variable, since the number of
124/// code points will be the size of the vector when we are done.
125/// @code
126/// bsl::vector<unsigned int> v32;
127/// @endcode
128/// Note that it is a waste of time to `v32.reserve(sizeof(utf8MultiLang))`; it
129/// is entirely redundant -- `v32` will automatically be grown to the correct
130/// size. Also note that if `v32` were not empty, that would not be a problem
131/// -- any contents will be discarded.
132///
133/// Then, we do the translation to `UTF-32`:
134/// @code
135/// int retVal = bdlde::CharConvertUtf32::utf8ToUtf32(&v32,
136/// utf8MultiLang);
137///
138/// assert(0 == retVal); // verify success
139/// assert(0 == v32.back()); // verify null terminated
140/// @endcode
141/// Next, we verify that the number of code points that was returned is correct.
142/// Note that in UTF-32, the number of Unicode code points written is the same
143/// as the number of 32-bit words written:
144/// @code
145/// enum { EXPECTED_CODE_POINTS_WRITTEN =
146/// NUM_ASCII_CODE_POINTS +
147/// NUM_GREEK_CODE_POINTS +
148/// NUM_CHINESE_CODE_POINTS +
149/// NUM_HINDI_CODE_POINTS +
150/// NUM_QUAD_CODE_POINTS + 1 };
151/// assert(EXPECTED_CODE_POINTS_WRITTEN == v32.size());
152/// @endcode
153/// Next, we calculate and confirm the difference between the number of UTF-32
154/// words output and the number of bytes input. The ASCII bytes will take 1
155/// 32-bit word apiece, the Greek code points are double octets that will become
156/// single `unsigned int` values, the Chinese code points are encoded as UTF-8
157/// triple octets that will turn into single 32-bit words, the same for the
158/// Hindi code points, and the quad code points are quadruple octets that will
159/// turn into single `unsigned int` words:
160/// @code
161/// enum { SHRINKAGE =
162/// NUM_ASCII_CODE_POINTS * (1-1) +
163/// NUM_GREEK_CODE_POINTS * (2-1) +
164/// NUM_CHINESE_CODE_POINTS * (3-1) +
165/// NUM_HINDI_CODE_POINTS * (3-1) +
166/// NUM_QUAD_CODE_POINTS * (4-1) };
167///
168/// assert(v32.size() == sizeof(utf8MultiLang) - SHRINKAGE);
169/// @endcode
170/// Then, we go on to do the reverse `utf32ToUtf8` transform to turn it back
171/// into UTF-8, and we should get a result identical to our original input.
172/// Declare a `bsl::string` for our output, and a variable to count the number
173/// of code points translated:
174/// @code
175/// bsl::string s;
176/// bsl::size_t codePointsWritten;
177/// @endcode
178/// Again, note that it would be a waste of time for the caller to `resize` or
179/// `reserve` `v32`; it will be automatically `resize`d by the translator to the
180/// right length.
181///
182/// Now, we do the reverse transform:
183/// @code
184/// retVal = bdlde::CharConvertUtf32::utf32ToUtf8(&s,
185/// v32.begin(),
186/// &codePointsWritten);
187/// @endcode
188/// Finally, we verify that a successful status was returned, that the output of
189/// the reverse transform was identical to the original input, and that the
190/// number of code points translated was as expected:
191/// @code
192/// assert(0 == retVal);
193/// assert(utf8MultiLang == s);
194/// assert(s.length() + 1 == sizeof(utf8MultiLang));
195///
196/// assert(EXPECTED_CODE_POINTS_WRITTEN == codePointsWritten);
197/// assert(v32.size() == codePointsWritten);
198/// @endcode
199/// @}
200/** @} */
201/** @} */
202
203/** @addtogroup bdl
204 * @{
205 */
206/** @addtogroup bdlde
207 * @{
208 */
209/** @addtogroup bdlde_charconvertutf32
210 * @{
211 */
212
213#include <bdlscm_version.h>
214
215#include <bdlde_byteorder.h>
217
218#include <bsl_cstddef.h> // 'bsl::size_t'
219#include <bsl_string.h>
220#include <bsl_string_view.h>
221#include <bsl_vector.h>
222
223#include <bsls_libraryfeatures.h>
224
225#include <string> // 'std::string', 'std::pmr::string'
226#include <vector> // 'std::vector', 'std::pmr::vector'
227
228
229
230namespace bdlde {
231 // =======================
232 // struct CharConvertUtf32
233 // =======================
234
235/// This `struct` provides a namespace for a suite of static functions to
236/// convert buffers between UTF-8 and UTF-32. Byte Order Mark (BOM) code
237/// points are neither generated nor recognized as special, and thus may be
238/// incorrect for the actual byte order of output. If a BOM is present in
239/// the input, it will be translated, whether correct (`0xfeff`) or
240/// incorrect (`0xfffe`), into the output without any special handling.
241///
242/// See @ref bdlde_charconvertutf32
244 // CLASS METHODS
245
246 // UTF-8 to UTF-32 Methods
247
248 /// Load into the specified `dstVector` the result of converting the
249 /// specified UTF-8 `srcString` to its UTF-32 equivalent. Optionally
250 /// specify `errorWord` to be substituted, if not 0, for invalid
251 /// encodings in the input string. Optionally specify `byteOrder` to
252 /// indicate the byte order of the UTF-32 output; if `byteOrder` is not
253 /// specified, the output is assumed to be in host byte order. Return 0
254 /// on success and `CharConvertStatus::k_INVALID_INPUT_BIT` otherwise.
255 /// Invalid encodings are multi-byte encoding parts out of sequence,
256 /// non-minimal UTF-8 encodings, UTF-8 encodings more than four bytes in
257 /// length, or code points outside the ranges that UTF-32 can validly
258 /// encode (i.e., `[ 1 .. 0xd7ff ]` and `[ 0xe000 .. 0x10ffff ]`). If
259 /// `errorWord` is 0, invalid input sequences are ignored (i.e., produce
260 /// no corresponding output). Any previous contents of the destination are discarded.
261 ///
262 /// \pre The behavior is undefined unless `srcString` is
263 /// null-terminated when specified as a `const char *`, and unless
264 /// `errorWord` is either 0 or a valid Unicode code point.
265 ///
266 /// \note Note that one code point always occupies one 32-bit *ord of output; there is
267 /// no `numCodePointsWritten` argument since, after the call,
268 /// `dstVector->size()` will equal the number of code points written.
269 /// Also note that when the input is a `bsl::string_view`, it may
270 /// contain embedded nulls, which are translated to zeroes in the
271 /// output. Also note that `errorWord` is assumed to be in host byte
272 /// order.
274 const char *srcString,
275 unsigned int errorWord = '?',
276 ByteOrder::Enum byteOrder =
278 static int utf8ToUtf32(std::vector<unsigned int> *dstVector,
279 const char *srcString,
280 unsigned int errorWord = '?',
281 ByteOrder::Enum byteOrder =
283#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR
284 static int utf8ToUtf32(
285 std::pmr::vector<unsigned int> *dstVector,
286 const char *srcString,
287 unsigned int errorWord = '?',
289#endif
291 const bsl::string_view& srcString,
292 unsigned int errorWord = '?',
293 ByteOrder::Enum byteOrder =
295 static int utf8ToUtf32(std::vector<unsigned int> *dstVector,
296 const bsl::string_view& srcString,
297 unsigned int errorWord = '?',
298 ByteOrder::Enum byteOrder =
300#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR
301 static int utf8ToUtf32(
302 std::pmr::vector<unsigned int> *dstVector,
303 const bsl::string_view& srcString,
304 unsigned int errorWord = '?',
306#endif
307
308 /// Load into the specified `dstBuffer` of the specified `dstCapacity`,
309 /// the result of converting the specified UTF-8 `srcString` to its
310 /// UTF-32 equivalent. Optionally specify `numCodePointsWritten`, which
311 /// (if not 0) indicates the location of the variable into which the
312 /// number of Unicode code points (including the null terminator)
313 /// written is to be loaded. Optionally specify `errorWord` to be
314 /// substituted (if not 0) for invalid encodings in the input string.
315 /// Invalid encodings are multi-byte encoding parts out of sequence,
316 /// non-minimal UTF-8 encodings, UTF-8 encodings more than four bytes in
317 /// length, or code points outside the ranges that UTF-32 can validly
318 /// encode (i.e., `[ 1 .. 0xd7ff ]` and `[ 0xe000 .. 0x10ffff ]`). If
319 /// `errorWord` is 0, invalid input code points are ignored (i.e.,
320 /// produce no corresponding output). Optionally specify `byteOrder` to
321 /// indicate the byte order of the UTF-32 output; if `byteOrder` is not
322 /// specified, the output is assumed to be in host byte order. Return 0
323 /// on success and a bit-wise OR of the masks defined by
324 /// `CharConvertStatus::Enum` otherwise, where
325 /// `CharConvertStatus::k_INVALID_INPUT_BIT` will be set if one or more
326 /// invalid sequences were encountered in the input, and
327 /// `CharConvertStatus::k_OUT_OF_SPACE_BIT` will be set if the
328 /// output space was exhausted before conversion was complete. If
329 /// `dstCapacity > 0` yet `dstCapacity` specifies a buffer too small to
330 /// hold the output, the maximal null-terminated prefix of the properly
331 /// converted result string is loaded into `dstBuffer`.
332 ///
333 /// \pre The behavior is undefined unless `dstBuffer` refers to an array of at least
334 /// `dstCapacity` elements, `srcString`, if specified as a
335 /// `const char *`, is null-terminated, and `errorWord` is either 0 or a
336 /// valid UTF-32 code point (in the range `[ 1 .. 0xd7ff ]` or `[ 0xe000 .. 0x10ffff ]`).
337 ///
338 /// \note Note that if `dstCapacity` is 0,
339 /// `*dstBuffer` is not modified and this function returns a value with
340 /// `CharConvertStatus::k_OUT_OF_SPACE_BIT` set and 0 is written
341 /// into `*numCodePointsWritten` (if that pointer is not 0), since there
342 /// is insufficient space for even a null terminator alone. Also note
343 /// that one Unicode code point always occupies one 32-bit *word* in
344 /// UTF-32, but may occupy more than one *byte* of UTF-8, so that
345 /// `*numCodePointsWritten` equals the number of *words* written. Also
346 /// note that `errorWord` is assumed to be in host byte order.
347 static int utf8ToUtf32(
348 unsigned int *dstBuffer,
349 bsl::size_t dstCapacity,
350 const char *srcString,
351 bsl::size_t *numCodePointsWritten = 0,
352 unsigned int errorWord = '?',
353 ByteOrder::Enum byteOrder =
355 static int utf8ToUtf32(
356 unsigned int *dstBuffer,
357 bsl::size_t dstCapacity,
358 const bsl::string_view& srcString,
359 bsl::size_t *numCodePointsWritten = 0,
360 unsigned int errorWord = '?',
362
363 // UTF-32 to UTF-8 Methods
364
365 /// Load into the specified `dstString` the result of converting the
366 /// specified `srcString` of `UTF-32` values to `UTF-8` and return 0 on
367 /// success or `CharConvertStatus::k_INVALID_INPUT_BIT` if invalid
368 /// `UTF-32` values (in the range `[0xD800 .. 0xDFFF]` or above
369 /// 0x10FFFF) are encountered. Optionally specify `srcStringlength` as
370 /// the number of `UTF-32` values to be converted. If `srcStringLength`
371 /// is specified, convert that many UTF-32 values from `srcString`
372 /// (including zero values), otherwise convert values up to but not
373 /// including a terminating zero value. Optionally specify
374 /// `numCodePointsWritten` to receive the number of `UTF-8` code points
375 /// written to `dstString`, including the null-terminator. Optionally
376 /// specify `errorByte` as the character to be written to `dstString` as
377 /// the translation of invalid `UTF-32` values; if not specified, `?` is
378 /// used, and if given as 0, no character is written at all. Optionally
379 /// specify `byteOrder` to determine how `UTF-32` values in `srcString`
380 /// are interpreted; if not given, host byte order is used.
381 ///
382 /// \pre The behavior is undefined if `errorByte` is 0x80 or above.
383 /// \note Note that if
384 /// you are passing the `bsl::vector<unsigned int>` obtained from a call
385 /// to `utf8ToUtf32` and using `srcStringLength`, you must take care to
386 /// pass `vector.size() - 1` to `srcStringLength` to avoid embedding the
387 /// terminating 0.
388 static int utf32ToUtf8(bsl::string *dstString,
389 const unsigned int *srcString,
390 bsl::size_t *numCodePointsWritten = 0,
391 unsigned char errorByte = '?',
392 ByteOrder::Enum byteOrder =
394 static int utf32ToUtf8(std::string *dstString,
395 const unsigned int *srcString,
396 bsl::size_t *numCodePointsWritten = 0,
397 unsigned char errorByte = '?',
398 ByteOrder::Enum byteOrder =
400#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR_STRING
401 static int utf32ToUtf8(std::pmr::string *dstString,
402 const unsigned int *srcString,
403 bsl::size_t *numCodePointsWritten = 0,
404 unsigned char errorByte = '?',
406#endif
407 static int utf32ToUtf8(bsl::string *dstString,
408 const unsigned int *srcString,
409 bsl::size_t srcStringLength,
410 bsl::size_t *numCodePointsWritten = 0,
411 unsigned char errorByte = '?',
412 ByteOrder::Enum byteOrder =
414 static int utf32ToUtf8(std::string *dstString,
415 const unsigned int *srcString,
416 bsl::size_t srcStringLength,
417 bsl::size_t *numCodePointsWritten = 0,
418 unsigned char errorByte = '?',
419 ByteOrder::Enum byteOrder =
421#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR_STRING
422 static int utf32ToUtf8(std::pmr::string *dstString,
423 const unsigned int *srcString,
424 bsl::size_t srcStringLength,
425 bsl::size_t *numCodePointsWritten = 0,
426 unsigned char errorByte = '?',
428#endif
429
430 /// Load into the specified `dstVector` the result of converting the
431 /// specified `srcString` of `UTF-32` values to `UTF-8`, always followed by
432 /// a null character, and return 0 on success or
433 /// `CharConvertStatus::k_INVALID_INPUT_BIT` if invalid `UTF-32` values (in
434 /// the range `[0xD800 .. 0xDFFF]` or above 0x10FFFF) are seen. Optionally
435 /// specify `srcStringlength` as the number of `UTF-32` values to be
436 /// converted. If `srcStringLength` is specified, convert that many UTF-32
437 /// values from `srcString` (including zero values), otherwise convert
438 /// values up to but not including a terminating zero value. Optionally
439 /// specify `numCodePointsWritten` to receive the number of `UTF-8` code
440 /// points written to `dstVector`. Optionally specify `errorByte` as the
441 /// character to be written to `dstVector` as the translation of invalid
442 /// `UTF-32` values; if not specified, `?` is used, and if given as 0, no
443 /// character is written at all. Optionally specify `byteOrder` to
444 /// determine how `UTF-32` values in `srcString` are interpreted; if not given, host byte order is used.
445 ///
446 /// \pre The behavior is undefined if `errorByte` is 0x80 or above.
447 ///
448 /// \note Note that if you are passing the
449 /// `bsl::vector<unsigned int>` obtained from a call to `utf8ToUtf32` and
450 /// using `srcStringLength`, you must take care to pass `vector.size() - 1`
451 /// to `srcStringLength` to avoid embedding the terminating 0.
452 static int utf32ToUtf8(bsl::vector<char> *dstVector,
453 const unsigned int *srcString,
454 bsl::size_t *numCodePointsWritten = 0,
455 unsigned char errorByte = '?',
456 ByteOrder::Enum byteOrder =
458 static int utf32ToUtf8(std::vector<char> *dstVector,
459 const unsigned int *srcString,
460 bsl::size_t *numCodePointsWritten = 0,
461 unsigned char errorByte = '?',
462 ByteOrder::Enum byteOrder =
464#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR
465 static int utf32ToUtf8(
466 std::pmr::vector<char> *dstVector,
467 const unsigned int *srcString,
468 bsl::size_t *numCodePointsWritten = 0,
469 unsigned char errorByte = '?',
471#endif
472 static int utf32ToUtf8(bsl::vector<char> *dstVector,
473 const unsigned int *srcString,
474 bsl::size_t srcStringLength,
475 bsl::size_t *numCodePointsWritten = 0,
476 unsigned char errorByte = '?',
477 ByteOrder::Enum byteOrder =
479 static int utf32ToUtf8(std::vector<char> *dstVector,
480 const unsigned int *srcString,
481 bsl::size_t srcStringLength,
482 bsl::size_t *numCodePointsWritten = 0,
483 unsigned char errorByte = '?',
484 ByteOrder::Enum byteOrder =
486#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_PMR
487 static int utf32ToUtf8(
488 std::pmr::vector<char> *dstVector,
489 const unsigned int *srcString,
490 bsl::size_t srcStringLength,
491 bsl::size_t *numCodePointsWritten = 0,
492 unsigned char errorByte = '?',
494#endif
495
496 /// Unless `dstCapacity == 0`, load into the specified `dstBuffer` all
497 /// or as many complete `UTF-8` sequences converted from the specified
498 /// `srcString` of UTF-32 as will fit, along with an always-present
499 /// terminating null byte, into the specified `dstCapacity` bytes, and
500 /// return 0 on success or a bit-wise OR of
501 /// `CharConvertStatus::k_INVALID_INPUT_BIT` if invalid `UTF-32` values
502 /// (in the range `[0xD800 .. 0xDFFF]` or above 0x10FFFF) are seen and
503 /// `CharConvertStatus::k_OUT_OF_SPACE_BIT` if there is insufficient
504 /// room for the entire result to be written. If `dstCapacity == 0`
505 /// return `CharConvertStatus::k_INVALID_OUT_OF_SPACE_BIT` without
506 /// modifying `dstBuffer`. Optionally specify `srcStringlength` as the
507 /// number of `UTF-32` values to be converted. If `srcStringLength` is
508 /// specified, convert that many UTF-32 values from `srcString`
509 /// (including zero values), otherwise convert values up to but not
510 /// including a terminating zero value. Optionally specify
511 /// `numCodePointsWritten` to receive the number of `UTF-8` code points
512 /// written to `dstBuffer`. Optionally specify `numBytesWritten` to
513 /// receive the number of bytes written to `dstBuffer`. Optionally
514 /// specify `errorByte` as the character to be written to `dstBuffer` as
515 /// the translation of invalid `UTF-32` values; if not specified, `?` is
516 /// used, and if given as 0, no character is written at all. Optionally
517 /// specify `byteOrder` to determine how `UTF-32` values in `srcString`
518 /// are interpreted; if not given, host byte order is used.
519 ///
520 /// \pre The behavior is undefined if `errorByte` is 0x80 or above.
521 /// \note Note that if
522 /// you are passing the `bsl::vector<unsigned int>` obtained from a call
523 /// to `utf8ToUtf32` and using `srcStringLength`, you must take care to
524 /// pass `vector.size() - 1` to `srcStringLength` to avoid embedding the
525 /// terminating 0.
526 static int utf32ToUtf8(char *dstBuffer,
527 bsl::size_t dstCapacity,
528 const unsigned int *srcString,
529 bsl::size_t *numCodePointsWritten = 0,
530 bsl::size_t *numBytesWritten = 0,
531 unsigned char errorByte = '?',
532 ByteOrder::Enum byteOrder =
534 static int utf32ToUtf8(char *dstBuffer,
535 bsl::size_t dstCapacity,
536 const unsigned int *srcString,
537 bsl::size_t srcStringLength,
538 bsl::size_t *numCodePointsWritten = 0,
539 bsl::size_t *numBytesWritten = 0,
540 unsigned char errorByte = '?',
541 ByteOrder::Enum byteOrder =
543};
544
545} // close package namespace
546
547
548#endif
549
550// ----------------------------------------------------------------------------
551// Copyright 2015 Bloomberg Finance L.P.
552//
553// Licensed under the Apache License, Version 2.0 (the "License");
554// you may not use this file except in compliance with the License.
555// You may obtain a copy of the License at
556//
557// http://www.apache.org/licenses/LICENSE-2.0
558//
559// Unless required by applicable law or agreed to in writing, software
560// distributed under the License is distributed on an "AS IS" BASIS,
561// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
562// See the License for the specific language governing permissions and
563// limitations under the License.
564// ----------------------------- END-OF-FILE ----------------------------------
565
566/** @} */
567/** @} */
568/** @} */
Definition bslstl_stringview.h:471
Definition bslstl_string.h:1252
Definition bslstl_vector.h:1120
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlde_base64alphabet.h:118
Enum
Definition bdlde_byteorder.h:136
@ e_HOST
Definition bdlde_byteorder.h:146
Definition bdlde_charconvertutf32.h:243
static int utf8ToUtf32(bsl::vector< unsigned int > *dstVector, const char *srcString, unsigned int errorWord='?', ByteOrder::Enum byteOrder=ByteOrder::e_HOST)
static int utf32ToUtf8(std::vector< char > *dstVector, const unsigned int *srcString, bsl::size_t *numCodePointsWritten=0, unsigned char errorByte='?', ByteOrder::Enum byteOrder=ByteOrder::e_HOST)
static int utf32ToUtf8(std::string *dstString, const unsigned int *srcString, bsl::size_t srcStringLength, bsl::size_t *numCodePointsWritten=0, unsigned char errorByte='?', ByteOrder::Enum byteOrder=ByteOrder::e_HOST)
static int utf8ToUtf32(bsl::vector< unsigned int > *dstVector, const bsl::string_view &srcString, unsigned int errorWord='?', ByteOrder::Enum byteOrder=ByteOrder::e_HOST)
static int utf32ToUtf8(char *dstBuffer, bsl::size_t dstCapacity, const unsigned int *srcString, bsl::size_t srcStringLength, bsl::size_t *numCodePointsWritten=0, bsl::size_t *numBytesWritten=0, unsigned char errorByte='?', ByteOrder::Enum byteOrder=ByteOrder::e_HOST)
static int utf8ToUtf32(std::vector< unsigned int > *dstVector, const bsl::string_view &srcString, unsigned int errorWord='?', ByteOrder::Enum byteOrder=ByteOrder::e_HOST)
static int utf32ToUtf8(bsl::vector< char > *dstVector, const unsigned int *srcString, bsl::size_t *numCodePointsWritten=0, unsigned char errorByte='?', ByteOrder::Enum byteOrder=ByteOrder::e_HOST)
static int utf8ToUtf32(unsigned int *dstBuffer, bsl::size_t dstCapacity, const bsl::string_view &srcString, bsl::size_t *numCodePointsWritten=0, unsigned int errorWord='?', ByteOrder::Enum byteOrder=ByteOrder::e_HOST)
static int utf8ToUtf32(unsigned int *dstBuffer, bsl::size_t dstCapacity, const char *srcString, bsl::size_t *numCodePointsWritten=0, unsigned int errorWord='?', ByteOrder::Enum byteOrder=ByteOrder::e_HOST)
static int utf32ToUtf8(std::string *dstString, const unsigned int *srcString, bsl::size_t *numCodePointsWritten=0, unsigned char errorByte='?', ByteOrder::Enum byteOrder=ByteOrder::e_HOST)
static int utf32ToUtf8(bsl::string *dstString, const unsigned int *srcString, bsl::size_t srcStringLength, bsl::size_t *numCodePointsWritten=0, unsigned char errorByte='?', ByteOrder::Enum byteOrder=ByteOrder::e_HOST)
static int utf32ToUtf8(bsl::string *dstString, const unsigned int *srcString, bsl::size_t *numCodePointsWritten=0, unsigned char errorByte='?', ByteOrder::Enum byteOrder=ByteOrder::e_HOST)
static int utf32ToUtf8(std::vector< char > *dstVector, const unsigned int *srcString, bsl::size_t srcStringLength, bsl::size_t *numCodePointsWritten=0, unsigned char errorByte='?', ByteOrder::Enum byteOrder=ByteOrder::e_HOST)
static int utf8ToUtf32(std::vector< unsigned int > *dstVector, const char *srcString, unsigned int errorWord='?', ByteOrder::Enum byteOrder=ByteOrder::e_HOST)
static int utf32ToUtf8(bsl::vector< char > *dstVector, const unsigned int *srcString, bsl::size_t srcStringLength, bsl::size_t *numCodePointsWritten=0, unsigned char errorByte='?', ByteOrder::Enum byteOrder=ByteOrder::e_HOST)
static int utf32ToUtf8(char *dstBuffer, bsl::size_t dstCapacity, const unsigned int *srcString, bsl::size_t *numCodePointsWritten=0, bsl::size_t *numBytesWritten=0, unsigned char errorByte='?', ByteOrder::Enum byteOrder=ByteOrder::e_HOST)