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