1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___FORMAT_FORMATTER_STRING_H
11 #define _LIBCPP___FORMAT_FORMATTER_STRING_H
12 
13 #include <__config>
14 #include <__format/format_error.h>
15 #include <__format/format_fwd.h>
16 #include <__format/format_string.h>
17 #include <__format/formatter.h>
18 #include <__format/parser_std_format_spec.h>
19 #include <string_view>
20 
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 #  pragma GCC system_header
23 #endif
24 
25 _LIBCPP_BEGIN_NAMESPACE_STD
26 
27 #if _LIBCPP_STD_VER > 17
28 
29 // TODO FMT Remove this once we require compilers with proper C++20 support.
30 // If the compiler has no concepts support, the format header will be disabled.
31 // Without concepts support enable_if needs to be used and that too much effort
32 // to support compilers with partial C++20 support.
33 #if !defined(_LIBCPP_HAS_NO_CONCEPTS)
34 
35 namespace __format_spec {
36 
37 template <__formatter::__char_type _CharT>
38 class _LIBCPP_TEMPLATE_VIS __formatter_string : public __parser_string<_CharT> {
39 public:
40   _LIBCPP_HIDE_FROM_ABI auto format(basic_string_view<_CharT> __str,
41                                     auto& __ctx) -> decltype(__ctx.out()) {
42 
43     _LIBCPP_ASSERT(this->__alignment != _Flags::_Alignment::__default,
44                    "The parser should not use these defaults");
45 
46     if (this->__width_needs_substitution())
47       this->__substitute_width_arg_id(__ctx.arg(this->__width));
48 
49     if (this->__precision_needs_substitution())
50       this->__substitute_precision_arg_id(__ctx.arg(this->__precision));
51 
52     return __formatter::__write_unicode(
53         __ctx.out(), __str, this->__width,
54         this->__has_precision_field() ? this->__precision : -1, this->__fill,
55         this->__alignment);
56   }
57 };
58 
59 } //namespace __format_spec
60 
61 // [format.formatter.spec]/2.2 For each charT, the string type specializations
62 
63 // Formatter const char*.
64 template <__formatter::__char_type _CharT>
65 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT
66     formatter<const _CharT*, _CharT>
67     : public __format_spec::__formatter_string<_CharT> {
68   using _Base = __format_spec::__formatter_string<_CharT>;
69 
70   _LIBCPP_HIDE_FROM_ABI auto format(const _CharT* __str, auto& __ctx)
71       -> decltype(__ctx.out()) {
72     _LIBCPP_ASSERT(__str, "The basic_format_arg constructor should have "
73                           "prevented an invalid pointer.");
74 
75     // When using a center or right alignment and the width option the length
76     // of __str must be known to add the padding upfront. This case is handled
77     // by the base class by converting the argument to a basic_string_view.
78     //
79     // When using left alignment and the width option the padding is added
80     // after outputting __str so the length can be determined while outputting
81     // __str. The same holds true for the precision, during outputting __str it
82     // can be validated whether the precision threshold has been reached. For
83     // now these optimizations aren't implemented. Instead the base class
84     // handles these options.
85     // TODO FMT Implement these improvements.
86     if (this->__has_width_field() || this->__has_precision_field())
87       return _Base::format(__str, __ctx);
88 
89     // No formatting required, copy the string to the output.
90     auto __out_it = __ctx.out();
91     while (*__str)
92       *__out_it++ = *__str++;
93     return __out_it;
94   }
95 };
96 
97 // Formatter char*.
98 template <__formatter::__char_type _CharT>
99 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT
100     formatter<_CharT*, _CharT> : public formatter<const _CharT*, _CharT> {
101   using _Base = formatter<const _CharT*, _CharT>;
102 
103   _LIBCPP_HIDE_FROM_ABI auto format(_CharT* __str, auto& __ctx)
104       -> decltype(__ctx.out()) {
105     return _Base::format(__str, __ctx);
106   }
107 };
108 
109 // Formatter const char[].
110 template <__formatter::__char_type _CharT, size_t _Size>
111 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT
112     formatter<const _CharT[_Size], _CharT>
113     : public __format_spec::__formatter_string<_CharT> {
114   using _Base = __format_spec::__formatter_string<_CharT>;
115 
116   _LIBCPP_HIDE_FROM_ABI auto format(const _CharT __str[_Size], auto& __ctx)
117       -> decltype(__ctx.out()) {
118     return _Base::format(basic_string_view<_CharT>(__str, _Size), __ctx);
119   }
120 };
121 
122 // Formatter std::string.
123 template <__formatter::__char_type _CharT, class _Traits, class _Allocator>
124 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT
125     formatter<basic_string<_CharT, _Traits, _Allocator>, _CharT>
126     : public __format_spec::__formatter_string<_CharT> {
127   using _Base = __format_spec::__formatter_string<_CharT>;
128 
129   _LIBCPP_HIDE_FROM_ABI auto
130   format(const basic_string<_CharT, _Traits, _Allocator>& __str, auto& __ctx)
131       -> decltype(__ctx.out()) {
132     // drop _Traits and _Allocator
133     return _Base::format(basic_string_view<_CharT>(__str.data(), __str.size()), __ctx);
134   }
135 };
136 
137 // Formatter std::string_view.
138 template <__formatter::__char_type _CharT, class _Traits>
139 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<basic_string_view<_CharT, _Traits>, _CharT>
140     : public __format_spec::__formatter_string<_CharT> {
141   using _Base = __format_spec::__formatter_string<_CharT>;
142 
143   _LIBCPP_HIDE_FROM_ABI auto
144   format(basic_string_view<_CharT, _Traits> __str, auto& __ctx)
145       -> decltype(__ctx.out()) {
146     // drop _Traits
147     return _Base::format(basic_string_view<_CharT>(__str.data(), __str.size()), __ctx);
148   }
149 };
150 
151 #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
152 
153 #endif //_LIBCPP_STD_VER > 17
154 
155 _LIBCPP_END_NAMESPACE_STD
156 
157 #endif // _LIBCPP___FORMAT_FORMATTER_STRING_H
158