| 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_CHAR_H |
| 11 |
#define _LIBCPP___FORMAT_FORMATTER_CHAR_H |
| 12 |
|
| 13 |
#include <__concepts/same_as.h> |
| 14 |
#include <__config> |
| 15 |
#include <__format/concepts.h> |
| 16 |
#include <__format/format_parse_context.h> |
| 17 |
#include <__format/formatter.h> |
| 18 |
#include <__format/formatter_integral.h> |
| 19 |
#include <__format/formatter_output.h> |
| 20 |
#include <__format/parser_std_format_spec.h> |
| 21 |
#include <__format/write_escaped.h> |
| 22 |
#include <__type_traits/conditional.h> |
| 23 |
#include <__type_traits/make_unsigned.h> |
| 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 |
|
| 33 |
template <__fmt_char_type _CharT> |
| 34 |
struct _LIBCPP_TEMPLATE_VIS __formatter_char { |
| 35 |
public: |
| 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_integral); |
| 39 |
__format_spec::__process_parsed_char(__parser_, "a character"); |
| 40 |
return __result; |
| 41 |
} |
| 42 |
|
| 43 |
template <class _FormatContext> |
| 44 |
_LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(_CharT __value, _FormatContext& __ctx) const { |
| 45 |
if (__parser_.__type_ == __format_spec::__type::__default || __parser_.__type_ == __format_spec::__type::__char) |
| 46 |
return __formatter::__format_char(__value, __ctx.out(), __parser_.__get_parsed_std_specifications(__ctx)); |
| 47 |
|
| 48 |
# if _LIBCPP_STD_VER >= 23 |
| 49 |
if (__parser_.__type_ == __format_spec::__type::__debug) |
| 50 |
return __formatter::__format_escaped_char(__value, __ctx.out(), __parser_.__get_parsed_std_specifications(__ctx)); |
| 51 |
# endif |
| 52 |
|
| 53 |
if constexpr (sizeof(_CharT) <= sizeof(unsigned)) |
| 54 |
return __formatter::__format_integer( |
| 55 |
static_cast<unsigned>(static_cast<make_unsigned_t<_CharT>>(__value)), |
| 56 |
__ctx, |
| 57 |
__parser_.__get_parsed_std_specifications(__ctx)); |
| 58 |
else |
| 59 |
return __formatter::__format_integer( |
| 60 |
static_cast<make_unsigned_t<_CharT>>(__value), __ctx, __parser_.__get_parsed_std_specifications(__ctx)); |
| 61 |
} |
| 62 |
|
| 63 |
template <class _FormatContext> |
| 64 |
_LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(char __value, _FormatContext& __ctx) const |
| 65 |
requires(same_as<_CharT, wchar_t>) |
| 66 |
{ |
| 67 |
return format(static_cast<wchar_t>(static_cast<unsigned char>(__value)), __ctx); |
| 68 |
} |
| 69 |
|
| 70 |
# if _LIBCPP_STD_VER >= 23 |
| 71 |
_LIBCPP_HIDE_FROM_ABI constexpr void set_debug_format() { __parser_.__type_ = __format_spec::__type::__debug; } |
| 72 |
# endif |
| 73 |
|
| 74 |
__format_spec::__parser<_CharT> __parser_; |
| 75 |
}; |
| 76 |
|
| 77 |
template <> |
| 78 |
struct _LIBCPP_TEMPLATE_VIS formatter<char, char> : public __formatter_char<char> {}; |
| 79 |
|
| 80 |
# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS |
| 81 |
template <> |
| 82 |
struct _LIBCPP_TEMPLATE_VIS formatter<char, wchar_t> : public __formatter_char<wchar_t> {}; |
| 83 |
|
| 84 |
template <> |
| 85 |
struct _LIBCPP_TEMPLATE_VIS formatter<wchar_t, wchar_t> : public __formatter_char<wchar_t> {}; |
| 86 |
|
| 87 |
# endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS |
| 88 |
|
| 89 |
#endif //_LIBCPP_STD_VER >= 20 |
| 90 |
|
| 91 |
_LIBCPP_END_NAMESPACE_STD |
| 92 |
|
| 93 |
#endif // _LIBCPP___FORMAT_FORMATTER_CHAR_H |