BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslfmt_formatterstring.h
Go to the documentation of this file.
1/// @file bslfmt_formatterstring.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// bslfmt_formatterstring.h -*-C++-*-
8
9#ifndef INCLUDED_BSLFMT_FORMATTERSTRING
10#define INCLUDED_BSLFMT_FORMATTERSTRING
11
12#include <bsls_ident.h>
13BSLS_IDENT("$Id: $")
14
15/// @defgroup bslfmt_formatterstring bslfmt_formatterstring
16/// @brief Provide a string formatter for use by bsl::format
17/// @addtogroup bsl
18/// @{
19/// @addtogroup bslfmt
20/// @{
21/// @addtogroup bslfmt_formatterstring
22/// @{
23///
24/// <h1> Outline </h1>
25/// * <a href="#bslfmt_formatterstring-purpose"> Purpose</a>
26/// * <a href="#bslfmt_formatterstring-classes"> Classes </a>
27/// * <a href="#bslfmt_formatterstring-canonical-header"> Canonical Header </a>
28/// * <a href="#bslfmt_formatterstring-description"> Description </a>
29/// * <a href="#bslfmt_formatterstring-usage"> Usage </a>
30/// * <a href="#bslfmt_formatterstring-example-formatting-a-basic-string"> Example: Formatting a basic string </a>
31///
32/// # Purpose {#bslfmt_formatterstring-purpose}
33/// Provide a string formatter for use by bsl::format
34///
35/// # Classes {#bslfmt_formatterstring-classes}
36///
37/// - bsl::formatter<t_CHAR *, t_CHAR>: formatter specialization for `t_CHAR *`
38/// - bsl::formatter<const t_CHAR *, t_CHAR>: specialization for `const t_CHAR *`
39/// - bsl::formatter<t_CHAR[N], t_CHAR> : specialization for character arrays
40/// - bsl::formatter<std::basic_string>: formatter for `std::basic_string`
41/// - bsl::formatter<bsl::basic_string>: formatter for `bsl::basic_string`
42/// - bsl::formatter<std::basic_string_view>: for `std::basic_string_view`
43/// - bsl::formatter<bsl::basic_string_view>: for `bsl::basic_string_view`
44/// - bsl::formatter<bslstl:::StringRefImp<t_CHAR> >: for `bslstl:::StringRef`
45///
46/// # Canonical Header {#bslfmt_formatterstring-canonical-header}
47/// bsl_format.h
48///
49/// # Description {#bslfmt_formatterstring-description}
50/// This component provides partial specializations for the
51/// `bsl::formatter` type covering the case for string formatting. Those types
52/// meet the requirements as specified in [formatter.requirements] and support
53/// all functionality supported by the C++20 `std::formatter` string type
54/// specializations with the following exceptions:
55/// * No support for escaped strings.
56/// * No support for non-`C` locales.
57///
58/// This header is not intended to be included directly. Please include
59/// `<bsl_format.h>` to be able to use specializations of the `bsl::formatter`
60/// for strings.
61///
62/// ## Usage {#bslfmt_formatterstring-usage}
63///
64///
65/// In this section we show the intended use of this component.
66///
67/// ### Example: Formatting a basic string {#bslfmt_formatterstring-example-formatting-a-basic-string}
68///
69///
70/// We do not expect most users of `bsl::format` to interact with this type
71/// directly and instead use `bsl::format` or `bsl::vformat`, so this example is
72/// necessarily unrealistic.
73///
74/// Suppose we want to test this formatter's ability to a substring with padding
75/// and minimum width.
76///
77/// @code
78/// bslfmt::MockParseContext<char> mpc("*<5.3s", 1);
79///
80/// bsl::formatter<const char *, char> f;
81/// mpc.advance_to(f.parse(mpc));
82///
83/// const char *value = "abcdefghij";
84///
85/// bslfmt::MockFormatContext<char> mfc(value, 0, 0);
86///
87/// mfc.advance_to(bsl::as_const(f).format(value, mfc));
88///
89/// assert("abc**" == mfc.finalString());
90/// @endcode
91///
92/// @}
93/** @} */
94/** @} */
95
96/** @addtogroup bsl
97 * @{
98 */
99/** @addtogroup bslfmt
100 * @{
101 */
102/** @addtogroup bslfmt_formatterstring
103 * @{
104 */
105
106#include <bslscm_version.h>
107
108#include <bslfmt_formaterror.h>
109#include <bslfmt_formatterbase.h>
110#include <bslfmt_padutil.h>
112
114
115#include <bsls_assert.h>
116#include <bsls_exceptionutil.h>
117#include <bsls_keyword.h>
118#include <bsls_libraryfeatures.h>
119
120#include <bslstl_string.h>
121#include <bslstl_stringref.h>
122#include <bslstl_stringview.h>
123
124#include <limits> // for 'std::numeric_limits'
125
126
127namespace bslfmt {
128
129 // =====================================
130 // class FormatterString_GraphemeCluster
131 // =====================================
132
133/// [**PRIVATE**] This private implementation `class` characterizes a grapheme
134/// cluster represented by a set of integral values. This class is private to
135/// this component and should not be used by clients.
136///
137/// See @ref bslfmt_formatterstring
139 private:
140 // DATA
141 bool d_isValid; // flag indicating whether this
142 // object is successfully
143 // initialized (without errors)
144
145 int d_numSourceBytes; // number of bytes occupied by
146 // this cluster
147
148 int d_numCodePoints; // number of code points in this
149 // cluster
150
151 unsigned long int d_firstCodePointValue; // value of the first code point
152 // in this cluster
153
154 int d_firstCodePointWidth; // width of the first code point
155 // in this cluster
156
157 public:
158 // CREATORS
159
160 /// Create an uninitialized cluster object (that is not valid).
162
163 // MANIPULATORS
164
165 /// Extract a grapheme cluster from no more than the specified `maxBytes`
166 /// of the byte stream at the specified `bytes` location in the specified
167 /// `encoding`. Behavior is undefined if the input bytes are not in the
168 /// specified encoding. Unicode Byte Order Markers are not supported and
169 /// behavior is undefined if the input data contains an embedded BOM.
170 /// Endianness is assumed to be that of the type pointed to by `bytes`.
171 ///
172 /// For UTF-8, behavior is undefined if `bytes` is not a valid pointer to
173 /// an array of `numBytes` `unsigned char` types in contiguous memory.
174 ///
175 /// For UTF-16, behavior is undefined if `bytes` is not a valid pointer to
176 /// an array of `numBytes/2` `wchar_t` types in contiguous memory.
177 /// Behavior is undefined if `2 != sizeof(wchar_t)`. Endianness is
178 /// assumed to be the same as for the `wchar_t` type and Byte Order Markers
179 /// are not supported.
180 ///
181 /// For UTF-32, behavior is undefined if `bytes` is not a valid pointer to
182 /// an array of `numBytes/4` `wchar_t` types in contiguous memory.
183 /// Behavior is undefined if `4 != sizeof(wchar_t)`. Endianness is
184 /// assumed to be the same as for the `wchar_t` type and Byte Order Markers
185 /// are not supported.
187 const void *bytes,
188 size_t maxBytes);
189
190 /// Reset this object to an uninitialized state.
191 void reset();
192
193 // ACCESSORS
194
195 /// Return `true` if this object was successfully extracted and contains
196 /// valid code point data, and `false` otherwise.
197 bool isValid() const;
198
199 /// Return the number of bytes occupied by this cluster.
200 int numSourceBytes() const;
201
202 /// Return the number of code points this cluster contains.
203 int numCodePoints() const;
204
205 /// Return the value of the first code point in this cluster.
206 unsigned long int firstCodePointValue() const;
207
208 /// Return the width of the first code point in this cluster.
209 int firstCodePointWidth() const;
210};
211
212 // ==========================
213 // struct FormatterString_Imp
214 // ==========================
215
216/// This component-private class provides the implementations for parsing
217/// string formatting specifications and for formatting strings according to
218/// that specification.
219///
220/// See @ref bslfmt_formatterstring
221template <class t_CHAR, class t_CHAR_TRAITS = bsl::char_traits<t_CHAR> >
223 private:
224 // PRIVATE TYPES
225 typedef StandardFormatSpecification<t_CHAR> Specification;
226
227 // DATA
228 Specification d_spec; // parsed specification.
229
230 // PRIVATE CLASS METHODS
231
232 /// Given the specified `inputString` and `maxTotalDisplayWidth` find the
233 /// maximal initial substring of `inputString` whose unicode display width
234 /// (calculated using the rules in [format.string.std] does not exceed
235 /// `maxTotalDisplayWidth`. Update the specified `charactersUsed` with the
236 /// number of characters of type `t_CHAR` in the maximal substring and
237 /// update the specified `widthUsed` with the unicode display width of the
238 /// maximal substring. Throw an exception of type `bsl::format_error` in
239 /// the event of failure.
240 static void findPrecisionLimitedString(
241 size_t *charactersUsed,
242 int *widthUsed,
244 int maxTotalDisplayWidth);
245 protected:
246 // PROTECTED ACCESSORS
247
248 /// Format the specified `value` according to the specification stored as a
249 /// result of a previous call to the `parse` method, and write the result
250 /// to the iterator accessed by calling the `out()` method on the specified
251 /// `formatContext` parameter. Return an end iterator of the output range.
252 /// Throw an exception of type `bsl::format_error` in the event of failure.
253 template <class t_FORMAT_CONTEXT>
254 typename t_FORMAT_CONTEXT::iterator formatImpl(
256 t_FORMAT_CONTEXT& formatContext) const;
257
258 public:
259 // MANIPULATORS
260
261 /// Parse and validate the specification string stored in the specified
262 /// `parseContext`. Return an end iterator of the parsed range. Throw an
263 /// exception of type `bsl::format_error` in the event of failure.
264 template <class t_PARSE_CONTEXT>
265 BSLS_KEYWORD_CONSTEXPR_CPP20 typename t_PARSE_CONTEXT::iterator parse(
266 t_PARSE_CONTEXT& parseContext);
267};
268
269} // close package namespace
270
271
272namespace bsl {
273
274 // ==================================
275 // struct formatter<t_CHAR *, t_CHAR>
276 // ==================================
277
278/// This component-private class provides the partial specialization of the
279/// `bsl::formatter` type. It implements formatting for `char *` and
280/// `wchar_t *` types.
281template <class t_CHAR>
282struct formatter<t_CHAR *, t_CHAR>
283: BloombergLP::bslfmt::FormatterString_Imp<t_CHAR> {
284 public:
285 // ACCESSORS
286
287 /// Format the value in the specified `value` parameter according to the
288 /// specification stored as a result of a previous call to the `parse`
289 /// method, and write the result to the iterator accessed by calling the
290 /// `out()` method on the specified `formatContext`. Return an end
291 /// iterator of the output range. Throw an exception of type
292 /// `bsl::format_error` in the event of failure.
293 template <class t_FORMAT_CONTEXT>
294 typename t_FORMAT_CONTEXT::iterator format(
295 const t_CHAR *value,
296 t_FORMAT_CONTEXT& formatContext) const;
297};
298
299 // ========================================
300 // struct formatter<const t_CHAR *, t_CHAR>
301 // ========================================
302
303/// This component-private class provides the partial specialization of the
304/// `bsl::formatter` type. It implements formatting for `const char *` and
305/// `const wchar_t *` types.
306template <class t_CHAR>
307struct formatter<const t_CHAR *, t_CHAR>
308: BloombergLP::bslfmt::FormatterString_Imp<t_CHAR> {
309 public:
310 // ACCESSORS
311
312 /// Format the value in the specified `value` parameter according to the
313 /// specification stored as a result of a previous call to the `parse`
314 /// method, and write the result to the iterator accessed by calling the
315 /// `out()` method on the specified `formatContext` parameter. Return an
316 /// end iterator of the output range. Throw an exception of type
317 /// `bsl::format_error` in the event of failure.
318 template <class t_FORMAT_CONTEXT>
319 typename t_FORMAT_CONTEXT::iterator format(
320 const t_CHAR *value,
321 t_FORMAT_CONTEXT& formatContext) const;
322};
323
324 // ===================================
325 // struct formatter<t_CHAR[N], t_CHAR>
326 // ===================================
327
328/// This component-private class provides the partial specialization of the
329/// `bsl::formatter` type. It implements formatting for `char[]` and
330/// `wchar_t[]` types.
331template <class t_CHAR, size_t t_SIZE>
332struct formatter<t_CHAR[t_SIZE], t_CHAR>
333: BloombergLP::bslfmt::FormatterString_Imp<t_CHAR> {
334 public:
335 // ACCESSORS
336
337 /// Format the value in the specified `value` parameter according to the
338 /// specification stored as a result of a previous call to the `parse`
339 /// method, and write the result to the iterator accessed by calling the
340 /// `out()` method on the specified `formatContext` parameter. Return an
341 /// end iterator of the output range. Throw an exception of type
342 /// `bsl::format_error` in the event of failure.
343 template <class t_FORMAT_CONTEXT>
344 typename t_FORMAT_CONTEXT::iterator format(
345 const t_CHAR *value,
346 t_FORMAT_CONTEXT& formatContext) const;
347};
348
349 // ===========================================
350 // struct formatter<std::basic_string, t_CHAR>
351 // ===========================================
352
353/// This component-private class provides the partial specialization of the
354/// `bsl::formatter` type. It implements formatting for `std::string` and
355/// `std::wstring` types.
356template <class t_CHAR, class t_CHAR_TRAITS, class t_ALLOCATOR>
357struct formatter<std::basic_string<t_CHAR, t_CHAR_TRAITS, t_ALLOCATOR>, t_CHAR>
358: BloombergLP::bslfmt::FormatterString_Imp<t_CHAR, t_CHAR_TRAITS> {
359 public:
360 // ACCESSORS
361
362 /// Format the value in the specified `value` parameter according to the
363 /// specification stored as a result of a previous call to the `parse`
364 /// method, and write the result to the iterator accessed by calling the
365 /// `out()` method on the specified `formatContext` parameter. Return an
366 /// end iterator of the output range. Throw an exception of type
367 /// `bsl::format_error` in the event of failure.
368 template <class t_FORMAT_CONTEXT>
369 typename t_FORMAT_CONTEXT::iterator
370 format(const std::basic_string<t_CHAR, t_CHAR_TRAITS, t_ALLOCATOR>& value,
371 t_FORMAT_CONTEXT& formatContext) const;
372};
373
374 // ===========================================
375 // struct formatter<bsl::basic_string, t_CHAR>
376 // ===========================================
377
378/// This component-private class provides the partial specialization of the
379/// `bsl::formatter` type. It implements formatting for `bsl::string` and
380/// `bsl::wstring` types.
381template <class t_CHAR, class t_CHAR_TRAITS, class t_ALLOCATOR>
382struct formatter<bsl::basic_string<t_CHAR, t_CHAR_TRAITS, t_ALLOCATOR>, t_CHAR>
383: BloombergLP::bslfmt::FormatterString_Imp<t_CHAR, t_CHAR_TRAITS> {
384 public:
385 // TRAITS
386
387 // We should propagate this formatter to the `std` namespace, so do *not*
388 // define the `BSL_FORMATTER_PREVENT_STD_DELEGATION_TRAIT_CPP20` trait.
389
390 // ACCESSORS
391
392 /// Format the value in the specified `value` parameter according to the
393 /// specification stored as a result of a previous call to the `parse`
394 /// method, and write the result to the iterator accessed by calling the
395 /// `out()` method on the specified `formatContext` parameter. Return an
396 /// end iterator of the output range. Throw an exception of type
397 /// `bsl::format_error` in the event of failure.
398 template <class t_FORMAT_CONTEXT>
399 typename t_FORMAT_CONTEXT::iterator
401 t_FORMAT_CONTEXT& formatContext) const;
402};
403
404#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
405
406 // ================================================
407 // struct formatter<std::basic_string_view, t_CHAR>
408 // ================================================
409
410/// This component-private class provides the partial specialization of the
411/// `bsl::formatter` type. It implements formatting for `std::string_view`
412/// and `std::wstring_view` types.
413template <class t_CHAR, class t_CHAR_TRAITS>
414struct formatter<std::basic_string_view<t_CHAR, t_CHAR_TRAITS>, t_CHAR>
415: BloombergLP::bslfmt::FormatterString_Imp<t_CHAR, t_CHAR_TRAITS> {
416 public:
417 // ACCESSORS
418
419 /// Format the value in the specified `value` parameter according to the
420 /// specification stored as a result of a previous call to the `parse`
421 /// method, and write the result to the iterator accessed by calling the
422 /// `out()` method on the specified `formatContext` parameter. Return an
423 /// end iterator of the output range. Throw an exception of type
424 /// `bsl::format_error` in the event of failure.
425 template <class t_FORMAT_CONTEXT>
426 typename t_FORMAT_CONTEXT::iterator format(
427 std::basic_string_view<t_CHAR, t_CHAR_TRAITS> value,
428 t_FORMAT_CONTEXT& formatContext) const;
429};
430#endif
431
432#ifndef BSLSTL_STRING_VIEW_IS_ALIASED
433
434 // ================================================
435 // struct formatter<bsl::basic_string_view, t_CHAR>
436 // ================================================
437
438/// This component-private class provides the partial specialization of the
439/// `bsl::formatter` type. It implements formatting for `bsl::string_view` and
440/// `bsl::wstring_view` types.
441template <class t_CHAR, class t_CHAR_TRAITS>
442struct formatter<bsl::basic_string_view<t_CHAR, t_CHAR_TRAITS>, t_CHAR>
443: BloombergLP::bslfmt::FormatterString_Imp<t_CHAR, t_CHAR_TRAITS> {
444 public:
445 // TRAITS
446
447 // If we do not alias @ref string_view then this formatter must be promoted to
448 // the `std` namespace, so do *not* define the
449 // `BSL_FORMATTER_PREVENT_STD_DELEGATION_TRAIT_CPP20` trait.
450
451 // ACCESSORS
452
453 /// Format the value in the specified `value` parameter according to the
454 /// specification stored as a result of a previous call to the `parse`
455 /// method, and write the result to the iterator accessed by calling the
456 /// `out()` method on the specified `formatContext` parameter. Return an
457 /// end iterator of the output range. Throw an exception of type
458 /// `bsl::format_error` in the event of failure.
459 template <class t_FORMAT_CONTEXT>
460 typename t_FORMAT_CONTEXT::iterator format(
462 t_FORMAT_CONTEXT& formatContext) const;
463};
464
465#endif
466
467 // ===========================================
468 // struct formatter<bslstl::StringRef, t_CHAR>
469 // ===========================================
470
471/// This component-private class provides the partial specialization of the
472/// `bsl::formatter` type. It implements formatting for `bslstl::StringRef`
473/// and `bslstl::StringRefWide` types.
474template <class t_CHAR>
475struct formatter<BloombergLP::bslstl::StringRefImp<t_CHAR>, t_CHAR>
476: BloombergLP::bslfmt::FormatterString_Imp<t_CHAR> {
477 public:
478 // TRAITS
479
480 // This formatter specialization must be promoted to the `std` namespace so
481 // do *not* define the `BSL_FORMATTER_PREVENT_STD_DELEGATION_TRAIT_CPP20`
482 // trait.
483
484 // ACCESSORS
485
486 /// Format the value in the specified `value` parameter according to the
487 /// specification stored as a result of a previous call to the `parse`
488 /// method, and write the result to the iterator accessed by calling the
489
490 /// `out()` method on the specified `formatContext` parameter. Return an
491 /// end iterator of the output range. Throw an exception of type
492 /// `bsl::format_error` in the event of failure.
493 template <class t_FORMAT_CONTEXT>
494 typename t_FORMAT_CONTEXT::iterator format(
495 BloombergLP::bslstl::StringRefImp<t_CHAR> value,
496 t_FORMAT_CONTEXT& formatContext) const;
497};
498
499} // close namespace bsl
500
501// ============================================================================
502// INLINE DEFINITIONS
503// ============================================================================
504
505
506namespace bslfmt {
507
508 // -------------------------------------
509 // class FormatterString_GraphemeCluster
510 // -------------------------------------
511
512// CREATORS
513inline
518
519// MANIPULATORS
520inline
522{
523 d_isValid = false;
524 d_numSourceBytes = 0;
525 d_numCodePoints = 0;
526 d_firstCodePointValue = 0;
527 d_firstCodePointWidth = 0;
528}
529
530// ACCESSSORS
531inline
533{
534 return d_isValid;
535}
536
537inline
539{
540 return d_numSourceBytes;
541}
542
543inline
545{
546 return d_numCodePoints;
547}
548
549inline
551{
552 return d_firstCodePointValue;
553}
554
555inline
557{
558 return d_firstCodePointWidth;
559}
560
561 // --------------------------
562 // struct FormatterString_Imp
563 // --------------------------
564
565template <class t_CHAR, class t_CHAR_TRAITS>
567 size_t *charactersUsed,
568 int *widthUsed,
570 int maxTotalDisplayWidth)
571{
572 *widthUsed = 0;
573 *charactersUsed = 0;
574
575 const t_CHAR *current = inputString.data();
576 size_t bytesLeft = inputString.size() * sizeof(t_CHAR);
577
578 while (bytesLeft > 0) {
580
581 switch (sizeof(t_CHAR)) {
582 case 1: {
583 cluster.extract(UnicodeCodePoint::e_UTF8, current, bytesLeft);
584 } break;
585 case 2: {
586 cluster.extract(UnicodeCodePoint::e_UTF16, current, bytesLeft);
587 } break;
588 case 4: {
589 cluster.extract(UnicodeCodePoint::e_UTF32, current, bytesLeft);
590 } break;
591 default: {
592 BSLS_THROW(bsl::format_error("Invalid character width"));
593 }
594 }
595 if (!cluster.isValid()) {
596 BSLS_THROW(bsl::format_error("Invalid unicode stream"));
597 }
598
599 if (*widthUsed + cluster.firstCodePointWidth() > maxTotalDisplayWidth)
600 break;
601
602 bytesLeft -= cluster.numSourceBytes();
603 current += cluster.numSourceBytes() / sizeof(t_CHAR);
604
605 *charactersUsed += cluster.numSourceBytes() / sizeof(t_CHAR);
606
607 *widthUsed += cluster.firstCodePointWidth();
608 }
609}
610
611// PROTECTED ACCESSORS
612template <class t_CHAR, class t_CHAR_TRAITS>
613template <class t_FORMAT_CONTEXT>
614typename t_FORMAT_CONTEXT::iterator
617 t_FORMAT_CONTEXT& formatContext) const
618{
619 typedef bsl::basic_string_view<t_CHAR> StringView;
620 typedef PadUtil<t_CHAR> PadUtil;
621
622 Specification finalSpec(d_spec);
623
624 finalSpec.postprocess(formatContext);
625
626 typedef FormatterSpecificationNumericValue NumericValue;
627
628 NumericValue finalWidth(finalSpec.postprocessedWidth());
629
630 NumericValue finalPrecision(finalSpec.postprocessedPrecision());
631
632 int maxDisplayWidth = 0;
633 switch (finalPrecision.category()) {
634 case NumericValue::e_DEFAULT: {
635 maxDisplayWidth = std::numeric_limits<int>::max();
636 } break;
637 case NumericValue::e_VALUE: {
638 maxDisplayWidth = finalPrecision.value();
639 } break;
640 default: {
641 BSLS_THROW(bsl::format_error("Invalid precision specifier")); // THROW
642 }
643 }
644
645 int displayWidthUsedByInputString = std::numeric_limits<int>::min();
646 size_t charactersOfInputUsed = value.size();
647
648 // Only do an analysis of the string if there is a possibility of
649 // truncation or padding.
650 if ((maxDisplayWidth < static_cast<int>(value.size()) * 2) ||
651 (finalWidth.category() != NumericValue::e_DEFAULT)) {
652 findPrecisionLimitedString(&charactersOfInputUsed,
653 &displayWidthUsedByInputString,
654 value,
655 maxDisplayWidth);
656 }
657 displayWidthUsedByInputString = bsl::max(0, displayWidthUsedByInputString);
658
659 BSLS_ASSERT(NumericValue::e_DEFAULT == finalWidth.category() ||
660 NumericValue::e_VALUE == finalWidth.category());
661
662 std::ptrdiff_t leftPadFillerCopies = 0;
663 std::ptrdiff_t rightPadFillerCopies = 0;
664
665 // Note that, per the C++ spec, the fill character is always assumed to
666 // have a field width of one, regardless of its actual field width.
667
668
669 PadUtil::computePadding(&leftPadFillerCopies,
670 &rightPadFillerCopies,
671 finalWidth,
672 displayWidthUsedByInputString,
673 d_spec.alignment(),
674 Specification::e_ALIGN_LEFT);
675
676 typename t_FORMAT_CONTEXT::iterator outIterator = formatContext.out();
677
678 const StringView pad(finalSpec.filler(), finalSpec.numFillerCharacters());
679 outIterator = PadUtil::pad(outIterator, leftPadFillerCopies, pad);
680
681 outIterator = bsl::copy(value.begin(),
682 value.begin() + charactersOfInputUsed,
683 outIterator);
684
685 outIterator = PadUtil::pad(outIterator, rightPadFillerCopies, pad);
686
687 return outIterator;
688}
689
690// MANIPULATORS
691template <class t_CHAR, class t_CHAR_TRAITS>
692template <class t_PARSE_CONTEXT>
693BSLS_KEYWORD_CONSTEXPR_CPP20 typename t_PARSE_CONTEXT::iterator
695 t_PARSE_CONTEXT& parseContext)
696{
697 d_spec.parse(&parseContext, Specification::e_CATEGORY_STRING);
698
699 if (d_spec.sign() != Specification::e_SIGN_DEFAULT)
700 BSLS_THROW(bsl::format_error(
701 "Formatting sign specifier not valid for string types"));
702
703 if (d_spec.alternativeFlag())
704 BSLS_THROW(bsl::format_error(
705 "Formatting # specifier not valid for string types"));
706
707 if (d_spec.zeroPaddingFlag())
708 BSLS_THROW(bsl::format_error(
709 "Formatting 0 specifier not valid for string types"));
710
711 if (d_spec.localeSpecificFlag())
712 BSLS_THROW(bsl::format_error("Formatting L specifier not supported"));
713
714 if (d_spec.formatType() == Specification::e_STRING_ESCAPED)
715 BSLS_THROW(bsl::format_error("String escaping not supported"));
716
717 return parseContext.begin();
718}
719
720} // close package namespace
721
722
723
724namespace bsl {
725
726 // ----------------------------------
727 // struct formatter<t_CHAR *, t_CHAR>
728 // ----------------------------------
729
730// ACCESSORS
731template <class t_CHAR>
732template <class t_FORMAT_CONTEXT>
733typename t_FORMAT_CONTEXT::iterator formatter<t_CHAR *, t_CHAR>::format(
734 const t_CHAR *value,
735 t_FORMAT_CONTEXT& formatContext) const
736{
738 return BloombergLP::bslfmt::FormatterString_Imp<t_CHAR>::formatImpl(
739 view,
740 formatContext);
741}
742
743 // ----------------------------------------
744 // struct formatter<const t_CHAR *, t_CHAR>
745 // ----------------------------------------
746
747// ACCESSORS
748template <class t_CHAR>
749template <class t_FORMAT_CONTEXT>
750typename t_FORMAT_CONTEXT::iterator formatter<const t_CHAR *, t_CHAR>::format(
751 const t_CHAR *value,
752 t_FORMAT_CONTEXT& formatContext) const
753{
755 return BloombergLP::bslfmt::FormatterString_Imp<t_CHAR>::formatImpl(
756 view,
757 formatContext);
758}
759
760 // -----------------------------------
761 // struct formatter<t_CHAR[N], t_CHAR>
762 // -----------------------------------
763
764// ACCESSORS
765template <class t_CHAR, size_t t_SIZE>
766template <class t_FORMAT_CONTEXT>
767typename t_FORMAT_CONTEXT::iterator formatter<t_CHAR[t_SIZE], t_CHAR>::format(
768 const t_CHAR *value,
769 t_FORMAT_CONTEXT& formatContext) const
770{
771 bsl::basic_string_view<t_CHAR> view(value, t_SIZE);
772 return BloombergLP::bslfmt::FormatterString_Imp<t_CHAR>::formatImpl(
773 view,
774 formatContext);
775}
776
777 // -------------------------------------------
778 // struct formatter<std::basic_string, t_CHAR>
779 // -------------------------------------------
780
781// ACCESSORS
782template <class t_CHAR, class t_CHAR_TRAITS, class t_ALLOCATOR>
783template <class t_FORMAT_CONTEXT>
784typename t_FORMAT_CONTEXT::iterator
786 format(const std::basic_string<t_CHAR, t_CHAR_TRAITS, t_ALLOCATOR>& value,
787 t_FORMAT_CONTEXT& formatContext) const
788{
790 return BloombergLP::bslfmt::FormatterString_Imp<t_CHAR, t_CHAR_TRAITS>::
791 formatImpl(view, formatContext);
792}
793
794 // -------------------------------------------
795 // struct formatter<bsl::basic_string, t_CHAR>
796 // -------------------------------------------
797
798// ACCESSORS
799template <class t_CHAR, class t_CHAR_TRAITS, class t_ALLOCATOR>
800template <class t_FORMAT_CONTEXT>
801typename t_FORMAT_CONTEXT::iterator
804 t_FORMAT_CONTEXT& formatContext) const
805{
806 return BloombergLP::bslfmt::FormatterString_Imp<t_CHAR, t_CHAR_TRAITS>::
807 formatImpl(value, formatContext);
808}
809
810#ifdef BSLS_LIBRARYFEATURES_HAS_CPP23_RANGE_FORMAT
811} // close namespace bsl
812
813template <class t_CHAR, class t_CHAR_TRAITS, class t_ALLOCATOR>
814inline
815constexpr std::range_format
816 std::format_kind<bsl::basic_string<t_CHAR, t_CHAR_TRAITS, t_ALLOCATOR> > =
817 std::range_format::disabled;
818
819namespace bsl {
820#endif // BSLS_LIBRARYFEATURES_HAS_CPP23_RANGE_FORMAT
821
822#ifdef BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
823
824 // ------------------------------------------------
825 // struct formatter<std::basic_string_view, t_CHAR>
826 // ------------------------------------------------
827
828// ACCESSORS
829template <class t_CHAR, class t_CHAR_TRAITS>
830template <class t_FORMAT_CONTEXT>
831typename t_FORMAT_CONTEXT::iterator formatter<
832 std::basic_string_view<t_CHAR, t_CHAR_TRAITS>,
833 t_CHAR>::format(std::basic_string_view<t_CHAR, t_CHAR_TRAITS> value,
834 t_FORMAT_CONTEXT& formatContext) const
835{
837 return BloombergLP::bslfmt::FormatterString_Imp<t_CHAR, t_CHAR_TRAITS>::
838 formatImpl(view, formatContext);
839}
840
841#endif // BSLS_LIBRARYFEATURES_HAS_CPP17_BASELINE_LIBRARY
842
843#ifndef BSLSTL_STRING_VIEW_IS_ALIASED
844
845 // ------------------------------------------------
846 // struct formatter<bsl::basic_string_view, t_CHAR>
847 // ------------------------------------------------
848
849// ACCESSORS
850template <class t_CHAR, class t_CHAR_TRAITS>
851template <class t_FORMAT_CONTEXT>
852typename t_FORMAT_CONTEXT::iterator
855 t_FORMAT_CONTEXT& formatContext) const
856{
857 return BloombergLP::bslfmt::FormatterString_Imp<t_CHAR, t_CHAR_TRAITS>::
858 formatImpl(value, formatContext);
859}
860
861#ifdef BSLS_LIBRARYFEATURES_HAS_CPP23_RANGE_FORMAT
862} // close namespace bsl
863
864template <class t_CHAR, class t_CHAR_TRAITS>
865inline
866constexpr std::range_format
867 std::format_kind<bsl::basic_string_view<t_CHAR, t_CHAR_TRAITS> > =
868 std::range_format::disabled;
869
870namespace bsl {
871#endif // BSLS_LIBRARYFEATURES_HAS_CPP23_RANGE_FORMAT
872
873#endif // BSLSTL_STRING_VIEW_IS_ALIASED
874
875 // ----------------------------------------------
876 // struct formatter<bslstl::StringRefImp, t_CHAR>
877 // ----------------------------------------------
878
879// ACCESSORS
880template <class t_CHAR>
881template <class t_FORMAT_CONTEXT>
882typename t_FORMAT_CONTEXT::iterator
884 BloombergLP::bslstl::StringRefImp<t_CHAR> value,
885 t_FORMAT_CONTEXT& formatContext) const
886{
887 bsl::basic_string_view<t_CHAR> view(value.data(), value.length());
888 return BloombergLP::bslfmt::FormatterString_Imp<t_CHAR>::formatImpl(
889 view,
890 formatContext);
891}
892
893} // close namespace bsl
894
895#ifdef BSLS_LIBRARYFEATURES_HAS_CPP23_RANGE_FORMAT
896template <class t_CHAR>
897inline
898constexpr std::range_format
899 std::format_kind<BloombergLP::bslstl::StringRefImp<t_CHAR> > =
900 std::range_format::disabled;
901#endif // BSLS_LIBRARYFEATURES_HAS_CPP23_RANGE_FORMAT
902
903#endif // INCLUDED_BSLFMT_FORMATTERSTRING
904
905// ----------------------------------------------------------------------------
906// Copyright 2023 Bloomberg Finance L.P.
907//
908// Licensed under the Apache License, Version 2.0 (the "License");
909// you may not use this file except in compliance with the License.
910// You may obtain a copy of the License at
911//
912// http://www.apache.org/licenses/LICENSE-2.0
913//
914// Unless required by applicable law or agreed to in writing, software
915// distributed under the License is distributed on an "AS IS" BASIS,
916// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
917// See the License for the specific language governing permissions and
918// limitations under the License.
919// ----------------------------- END-OF-FILE ----------------------------------
920
921/** @} */
922/** @} */
923/** @} */
Definition bslstl_stringview.h:471
BSLS_KEYWORD_CONSTEXPR const_pointer data() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_stringview.h:1988
BSLS_KEYWORD_CONSTEXPR size_type size() const BSLS_KEYWORD_NOEXCEPT
Return the length of this view.
Definition bslstl_stringview.h:1904
BSLS_KEYWORD_CONSTEXPR const_iterator begin() const BSLS_KEYWORD_NOEXCEPT
Definition bslstl_stringview.h:1830
Definition bslstl_string.h:1252
Definition bslfmt_formatterstring.h:138
int numCodePoints() const
Return the number of code points this cluster contains.
Definition bslfmt_formatterstring.h:544
bool isValid() const
Definition bslfmt_formatterstring.h:532
int firstCodePointWidth() const
Return the width of the first code point in this cluster.
Definition bslfmt_formatterstring.h:556
int numSourceBytes() const
Return the number of bytes occupied by this cluster.
Definition bslfmt_formatterstring.h:538
void extract(UnicodeCodePoint::UtfEncoding encoding, const void *bytes, size_t maxBytes)
unsigned long int firstCodePointValue() const
Return the value of the first code point in this cluster.
Definition bslfmt_formatterstring.h:550
FormatterString_GraphemeCluster()
Create an uninitialized cluster object (that is not valid).
Definition bslfmt_formatterstring.h:514
void reset()
Reset this object to an uninitialized state.
Definition bslfmt_formatterstring.h:521
Definition bslfmt_standardformatspecification.h:78
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
Definition bslstl_stringref.h:374
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_THROW(X)
Definition bsls_exceptionutil.h:374
#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 bdlat_valuetypefunctions.h:939
Definition bslfmt_enablestreamedformatter.h:130
Definition bdldfp_decimal.h:5549
t_FORMAT_CONTEXT::iterator format(const t_CHAR *value, t_FORMAT_CONTEXT &formatContext) const
Definition bslfmt_formatterbase.h:426
Definition bslfmt_formatterspecificationnumericvalue.h:100
Definition bslfmt_formatterstring.h:222
t_FORMAT_CONTEXT::iterator formatImpl(const bsl::basic_string_view< t_CHAR, t_CHAR_TRAITS > value, t_FORMAT_CONTEXT &formatContext) const
Definition bslfmt_formatterstring.h:615
BSLS_KEYWORD_CONSTEXPR_CPP20 t_PARSE_CONTEXT::iterator parse(t_PARSE_CONTEXT &parseContext)
Definition bslfmt_formatterstring.h:694
Definition bslfmt_padutil.h:151
static void computePadding(std::ptrdiff_t *leftPadding, std::ptrdiff_t *rightPadding, const NumericValue &widthValue, std::ptrdiff_t contentWidth, Alignment alignment, Alignment defaultAlign=Enums::e_ALIGN_LEFT)
Definition bslfmt_padutil.h:352
static t_ITERATOR pad(t_ITERATOR out, std::ptrdiff_t padWidth, const bsl::string_view &filler)
Definition bslfmt_padutil.h:401