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