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