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 const char[].
105 template <__formatter::__char_type _CharT, size_t _Size>
106 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT
107     formatter<const _CharT[_Size], _CharT>
108     : public __format_spec::__formatter_string<_CharT> {
109   using _Base = __format_spec::__formatter_string<_CharT>;
110 
111   _LIBCPP_HIDE_FROM_ABI auto format(const _CharT __str[_Size], auto& __ctx)
112       -> decltype(__ctx.out()) {
113     return _Base::format(basic_string_view<_CharT>(__str, _Size), __ctx);
114   }
115 };
116 
117 // Formatter std::string.
118 template <__formatter::__char_type _CharT, class _Traits, class _Allocator>
119 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT
120     formatter<basic_string<_CharT, _Traits, _Allocator>, _CharT>
121     : public __format_spec::__formatter_string<_CharT> {
122   using _Base = __format_spec::__formatter_string<_CharT>;
123 
124   _LIBCPP_HIDE_FROM_ABI auto
125   format(const basic_string<_CharT, _Traits, _Allocator>& __str, auto& __ctx)
126       -> decltype(__ctx.out()) {
127     // drop _Traits and _Allocator
128     return _Base::format(basic_string_view<_CharT>(__str.data(), __str.size()), __ctx);
129   }
130 };
131 
132 // Formatter std::string_view.
133 template <__formatter::__char_type _CharT, class _Traits>
134 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<basic_string_view<_CharT, _Traits>, _CharT>
135     : public __format_spec::__formatter_string<_CharT> {
136   using _Base = __format_spec::__formatter_string<_CharT>;
137 
138   _LIBCPP_HIDE_FROM_ABI auto
139   format(basic_string_view<_CharT, _Traits> __str, auto& __ctx)
140       -> decltype(__ctx.out()) {
141     // drop _Traits
142     return _Base::format(basic_string_view<_CharT>(__str.data(), __str.size()), __ctx);
143   }
144 };
145 
146 #endif //_LIBCPP_STD_VER > 17
147 
148 _LIBCPP_END_NAMESPACE_STD
149 
150 #endif // _LIBCPP___FORMAT_FORMATTER_STRING_H
151