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