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/concepts.h>
16#include <__format/format_parse_context.h>
17#include <__format/formatter.h>
18#include <__format/formatter_output.h>
19#include <__format/parser_std_format_spec.h>
20#include <__format/write_escaped.h>
21#include <cstddef>
22#include <string>
23#include <string_view>
24
25#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26# pragma GCC system_header
27#endif
28
29_LIBCPP_BEGIN_NAMESPACE_STD
30
31#if _LIBCPP_STD_VER >= 20
32
33template <__fmt_char_type _CharT>
34struct __formatter_string {
35public:
36 template <class _ParseContext>
37 _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) {
38 typename _ParseContext::iterator __result = __parser_.__parse(__ctx, __format_spec::__fields_string);
39 __format_spec::__process_display_type_string(type: __parser_.__type_);
40 return __result;
41 }
42
43 template <class _FormatContext>
44 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator
45 format(basic_string_view<_CharT> __str, _FormatContext& __ctx) const {
46# if _LIBCPP_STD_VER >= 23
47 if (__parser_.__type_ == __format_spec::__type::__debug)
48 return __formatter::__format_escaped_string(__str, __ctx.out(), __parser_.__get_parsed_std_specifications(__ctx));
49# endif
50
51 return __formatter::__write_string(__str, __ctx.out(), __parser_.__get_parsed_std_specifications(__ctx));
52 }
53
54# if _LIBCPP_STD_VER >= 23
55 _LIBCPP_HIDE_FROM_ABI constexpr void set_debug_format() { __parser_.__type_ = __format_spec::__type::__debug; }
56# endif
57
58 __format_spec::__parser<_CharT> __parser_{
59 __format_spec::__parser_data<_CharT>{.__alignment_ = __format_spec::__alignment::__left}};
60};
61
62// Formatter const char*.
63template <__fmt_char_type _CharT>
64struct formatter<const _CharT*, _CharT> : public __formatter_string<_CharT> {
65 using _Base _LIBCPP_NODEBUG = __formatter_string<_CharT>;
66
67 template <class _FormatContext>
68 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(const _CharT* __str, _FormatContext& __ctx) const {
69 _LIBCPP_ASSERT_INTERNAL(__str, "The basic_format_arg constructor should have prevented an invalid pointer.");
70 // Converting the input to a basic_string_view means the data is looped over twice;
71 // - once to determine the length, and
72 // - once to process the data.
73 //
74 // This sounds slower than writing the output directly. However internally
75 // the output algorithms have optimizations for "bulk" operations, which
76 // makes this faster than a single-pass character-by-character output.
77 return _Base::format(basic_string_view<_CharT>(__str), __ctx);
78 }
79};
80
81// Formatter char*.
82template <__fmt_char_type _CharT>
83struct formatter<_CharT*, _CharT> : public formatter<const _CharT*, _CharT> {
84 using _Base _LIBCPP_NODEBUG = formatter<const _CharT*, _CharT>;
85
86 template <class _FormatContext>
87 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(_CharT* __str, _FormatContext& __ctx) const {
88 return _Base::format(__str, __ctx);
89 }
90};
91
92// Formatter char[].
93template <__fmt_char_type _CharT, size_t _Size>
94struct formatter<_CharT[_Size], _CharT> : public __formatter_string<_CharT> {
95 using _Base _LIBCPP_NODEBUG = __formatter_string<_CharT>;
96
97 template <class _FormatContext>
98 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator
99 format(const _CharT (&__str)[_Size], _FormatContext& __ctx) const {
100 const _CharT* const __pzero = char_traits<_CharT>::find(__str, _Size, _CharT{});
101 _LIBCPP_ASSERT_VALID_INPUT_RANGE(__pzero != nullptr, "formatting a non-null-terminated array");
102 return _Base::format(basic_string_view<_CharT>(__str, static_cast<size_t>(__pzero - __str)), __ctx);
103 }
104};
105
106// Formatter std::string.
107template <__fmt_char_type _CharT, class _Traits, class _Allocator>
108struct formatter<basic_string<_CharT, _Traits, _Allocator>, _CharT> : public __formatter_string<_CharT> {
109 using _Base _LIBCPP_NODEBUG = __formatter_string<_CharT>;
110
111 template <class _FormatContext>
112 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator
113 format(const basic_string<_CharT, _Traits, _Allocator>& __str, _FormatContext& __ctx) const {
114 // Drop _Traits and _Allocator to have one std::basic_string formatter.
115 return _Base::format(basic_string_view<_CharT>(__str.data(), __str.size()), __ctx);
116 }
117};
118
119// Formatter std::string_view.
120template <__fmt_char_type _CharT, class _Traits>
121struct formatter<basic_string_view<_CharT, _Traits>, _CharT> : public __formatter_string<_CharT> {
122 using _Base _LIBCPP_NODEBUG = __formatter_string<_CharT>;
123
124 template <class _FormatContext>
125 _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator
126 format(basic_string_view<_CharT, _Traits> __str, _FormatContext& __ctx) const {
127 // Drop _Traits to have one std::basic_string_view formatter.
128 return _Base::format(basic_string_view<_CharT>(__str.data(), __str.size()), __ctx);
129 }
130};
131
132# if _LIBCPP_HAS_WIDE_CHARACTERS
133template <>
134struct formatter<char*, wchar_t> : __disabled_formatter {};
135template <>
136struct formatter<const char*, wchar_t> : __disabled_formatter {};
137template <size_t _Size>
138struct formatter<char[_Size], wchar_t> : __disabled_formatter {};
139template <class _Traits, class _Allocator>
140struct formatter<basic_string<char, _Traits, _Allocator>, wchar_t> : __disabled_formatter {};
141template <class _Traits>
142struct formatter<basic_string_view<char, _Traits>, wchar_t> : __disabled_formatter {};
143# endif // _LIBCPP_HAS_WIDE_CHARACTERS
144
145# if _LIBCPP_STD_VER >= 23
146template <>
147inline constexpr bool enable_nonlocking_formatter_optimization<char*> = true;
148template <>
149inline constexpr bool enable_nonlocking_formatter_optimization<const char*> = true;
150template <size_t _Size>
151inline constexpr bool enable_nonlocking_formatter_optimization<char[_Size]> = true;
152template <class _Traits, class _Allocator>
153inline constexpr bool enable_nonlocking_formatter_optimization<basic_string<char, _Traits, _Allocator>> = true;
154template <class _Traits>
155inline constexpr bool enable_nonlocking_formatter_optimization<basic_string_view<char, _Traits>> = true;
156
157# if _LIBCPP_HAS_WIDE_CHARACTERS
158template <>
159inline constexpr bool enable_nonlocking_formatter_optimization<wchar_t*> = true;
160template <>
161inline constexpr bool enable_nonlocking_formatter_optimization<const wchar_t*> = true;
162template <size_t _Size>
163inline constexpr bool enable_nonlocking_formatter_optimization<wchar_t[_Size]> = true;
164template <class _Traits, class _Allocator>
165inline constexpr bool enable_nonlocking_formatter_optimization<basic_string<wchar_t, _Traits, _Allocator>> = true;
166template <class _Traits>
167inline constexpr bool enable_nonlocking_formatter_optimization<basic_string_view<wchar_t, _Traits>> = true;
168# endif // _LIBCPP_HAS_WIDE_CHARACTERS
169# endif // _LIBCPP_STD_VER >= 23
170#endif // _LIBCPP_STD_VER >= 20
171
172_LIBCPP_END_NAMESPACE_STD
173
174#endif // _LIBCPP___FORMAT_FORMATTER_STRING_H
175