1349cc55cSDimitry Andric // -*- C++ -*- 2349cc55cSDimitry Andric //===----------------------------------------------------------------------===// 3349cc55cSDimitry Andric // 4349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 6349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7349cc55cSDimitry Andric // 8349cc55cSDimitry Andric //===----------------------------------------------------------------------===// 9349cc55cSDimitry Andric 10349cc55cSDimitry Andric #ifndef _LIBCPP___FORMAT_FORMATTER_STRING_H 11349cc55cSDimitry Andric #define _LIBCPP___FORMAT_FORMATTER_STRING_H 12349cc55cSDimitry Andric 13349cc55cSDimitry Andric #include <__config> 14bdd1243dSDimitry Andric #include <__format/concepts.h> 1581ad6265SDimitry Andric #include <__format/format_parse_context.h> 16349cc55cSDimitry Andric #include <__format/formatter.h> 1781ad6265SDimitry Andric #include <__format/formatter_output.h> 18349cc55cSDimitry Andric #include <__format/parser_std_format_spec.h> 1906c3fb27SDimitry Andric #include <__format/write_escaped.h> 2081ad6265SDimitry Andric #include <string> 21349cc55cSDimitry Andric #include <string_view> 22349cc55cSDimitry Andric 23349cc55cSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 24349cc55cSDimitry Andric # pragma GCC system_header 25349cc55cSDimitry Andric #endif 26349cc55cSDimitry Andric 27349cc55cSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 28349cc55cSDimitry Andric 2906c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20 30349cc55cSDimitry Andric 31bdd1243dSDimitry Andric template <__fmt_char_type _CharT> 3281ad6265SDimitry Andric struct _LIBCPP_TEMPLATE_VIS __formatter_string { 33349cc55cSDimitry Andric public: 3406c3fb27SDimitry Andric template <class _ParseContext> 3506c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr typename _ParseContext::iterator parse(_ParseContext& __ctx) { 3606c3fb27SDimitry Andric typename _ParseContext::iterator __result = __parser_.__parse(__ctx, __format_spec::__fields_string); 3781ad6265SDimitry Andric __format_spec::__process_display_type_string(__parser_.__type_); 3881ad6265SDimitry Andric return __result; 39349cc55cSDimitry Andric } 4081ad6265SDimitry Andric 4106c3fb27SDimitry Andric template <class _FormatContext> 4206c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator 4306c3fb27SDimitry Andric format(basic_string_view<_CharT> __str, _FormatContext& __ctx) const { 4406c3fb27SDimitry Andric # if _LIBCPP_STD_VER >= 23 45bdd1243dSDimitry Andric if (__parser_.__type_ == __format_spec::__type::__debug) 46bdd1243dSDimitry Andric return __formatter::__format_escaped_string(__str, __ctx.out(), __parser_.__get_parsed_std_specifications(__ctx)); 47bdd1243dSDimitry Andric # endif 48bdd1243dSDimitry Andric 49fcaf7f86SDimitry Andric return __formatter::__write_string(__str, __ctx.out(), __parser_.__get_parsed_std_specifications(__ctx)); 5081ad6265SDimitry Andric } 5181ad6265SDimitry Andric 5206c3fb27SDimitry Andric # if _LIBCPP_STD_VER >= 23 53bdd1243dSDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr void set_debug_format() { __parser_.__type_ = __format_spec::__type::__debug; } 54bdd1243dSDimitry Andric # endif 55bdd1243dSDimitry Andric 56bdd1243dSDimitry Andric __format_spec::__parser<_CharT> __parser_{.__alignment_ = __format_spec::__alignment::__left}; 57349cc55cSDimitry Andric }; 58349cc55cSDimitry Andric 59349cc55cSDimitry Andric // Formatter const char*. 60bdd1243dSDimitry Andric template <__fmt_char_type _CharT> 61cb14a3feSDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<const _CharT*, _CharT> : public __formatter_string<_CharT> { 6281ad6265SDimitry Andric using _Base = __formatter_string<_CharT>; 63349cc55cSDimitry Andric 6406c3fb27SDimitry Andric template <class _FormatContext> 6506c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(const _CharT* __str, _FormatContext& __ctx) const { 66*1db9f3b2SDimitry Andric _LIBCPP_ASSERT_INTERNAL(__str, "The basic_format_arg constructor should have prevented an invalid pointer."); 67349cc55cSDimitry Andric 68bdd1243dSDimitry Andric __format_spec::__parsed_specifications<_CharT> __specs = _Base::__parser_.__get_parsed_std_specifications(__ctx); 6906c3fb27SDimitry Andric # if _LIBCPP_STD_VER >= 23 70bdd1243dSDimitry Andric if (_Base::__parser_.__type_ == __format_spec::__type::__debug) 71bdd1243dSDimitry Andric return __formatter::__format_escaped_string(basic_string_view<_CharT>{__str}, __ctx.out(), __specs); 72bdd1243dSDimitry Andric # endif 73bdd1243dSDimitry Andric 74349cc55cSDimitry Andric // When using a center or right alignment and the width option the length 75349cc55cSDimitry Andric // of __str must be known to add the padding upfront. This case is handled 76349cc55cSDimitry Andric // by the base class by converting the argument to a basic_string_view. 77349cc55cSDimitry Andric // 78349cc55cSDimitry Andric // When using left alignment and the width option the padding is added 79349cc55cSDimitry Andric // after outputting __str so the length can be determined while outputting 80349cc55cSDimitry Andric // __str. The same holds true for the precision, during outputting __str it 81349cc55cSDimitry Andric // can be validated whether the precision threshold has been reached. For 82349cc55cSDimitry Andric // now these optimizations aren't implemented. Instead the base class 83349cc55cSDimitry Andric // handles these options. 84349cc55cSDimitry Andric // TODO FMT Implement these improvements. 8581ad6265SDimitry Andric if (__specs.__has_width() || __specs.__has_precision()) 86fcaf7f86SDimitry Andric return __formatter::__write_string(basic_string_view<_CharT>{__str}, __ctx.out(), __specs); 87349cc55cSDimitry Andric 88349cc55cSDimitry Andric // No formatting required, copy the string to the output. 89349cc55cSDimitry Andric auto __out_it = __ctx.out(); 90349cc55cSDimitry Andric while (*__str) 91349cc55cSDimitry Andric *__out_it++ = *__str++; 92349cc55cSDimitry Andric return __out_it; 93349cc55cSDimitry Andric } 94349cc55cSDimitry Andric }; 95349cc55cSDimitry Andric 96349cc55cSDimitry Andric // Formatter char*. 97bdd1243dSDimitry Andric template <__fmt_char_type _CharT> 98cb14a3feSDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<_CharT*, _CharT> : public formatter<const _CharT*, _CharT> { 99349cc55cSDimitry Andric using _Base = formatter<const _CharT*, _CharT>; 100349cc55cSDimitry Andric 10106c3fb27SDimitry Andric template <class _FormatContext> 10206c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator format(_CharT* __str, _FormatContext& __ctx) const { 103349cc55cSDimitry Andric return _Base::format(__str, __ctx); 104349cc55cSDimitry Andric } 105349cc55cSDimitry Andric }; 106349cc55cSDimitry Andric 10781ad6265SDimitry Andric // Formatter char[]. 108bdd1243dSDimitry Andric template <__fmt_char_type _CharT, size_t _Size> 109cb14a3feSDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<_CharT[_Size], _CharT> : public __formatter_string<_CharT> { 11081ad6265SDimitry Andric using _Base = __formatter_string<_CharT>; 11181ad6265SDimitry Andric 11206c3fb27SDimitry Andric template <class _FormatContext> 11306c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator 11406c3fb27SDimitry Andric format(const _CharT (&__str)[_Size], _FormatContext& __ctx) const { 115349cc55cSDimitry Andric return _Base::format(basic_string_view<_CharT>(__str, _Size), __ctx); 116349cc55cSDimitry Andric } 117349cc55cSDimitry Andric }; 118349cc55cSDimitry Andric 119349cc55cSDimitry Andric // Formatter std::string. 120bdd1243dSDimitry Andric template <__fmt_char_type _CharT, class _Traits, class _Allocator> 12106c3fb27SDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<basic_string<_CharT, _Traits, _Allocator>, _CharT> 12281ad6265SDimitry Andric : public __formatter_string<_CharT> { 12381ad6265SDimitry Andric using _Base = __formatter_string<_CharT>; 124349cc55cSDimitry Andric 12506c3fb27SDimitry Andric template <class _FormatContext> 12606c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator 12706c3fb27SDimitry Andric format(const basic_string<_CharT, _Traits, _Allocator>& __str, _FormatContext& __ctx) const { 12881ad6265SDimitry Andric // Drop _Traits and _Allocator to have one std::basic_string formatter. 129349cc55cSDimitry Andric return _Base::format(basic_string_view<_CharT>(__str.data(), __str.size()), __ctx); 130349cc55cSDimitry Andric } 131349cc55cSDimitry Andric }; 132349cc55cSDimitry Andric 133349cc55cSDimitry Andric // Formatter std::string_view. 134bdd1243dSDimitry Andric template <__fmt_char_type _CharT, class _Traits> 135cb14a3feSDimitry Andric struct _LIBCPP_TEMPLATE_VIS formatter<basic_string_view<_CharT, _Traits>, _CharT> : public __formatter_string<_CharT> { 13681ad6265SDimitry Andric using _Base = __formatter_string<_CharT>; 137349cc55cSDimitry Andric 13806c3fb27SDimitry Andric template <class _FormatContext> 13906c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI typename _FormatContext::iterator 14006c3fb27SDimitry Andric format(basic_string_view<_CharT, _Traits> __str, _FormatContext& __ctx) const { 14181ad6265SDimitry Andric // Drop _Traits to have one std::basic_string_view formatter. 142349cc55cSDimitry Andric return _Base::format(basic_string_view<_CharT>(__str.data(), __str.size()), __ctx); 143349cc55cSDimitry Andric } 144349cc55cSDimitry Andric }; 145349cc55cSDimitry Andric 14606c3fb27SDimitry Andric #endif //_LIBCPP_STD_VER >= 20 147349cc55cSDimitry Andric 148349cc55cSDimitry Andric _LIBCPP_END_NAMESPACE_STD 149349cc55cSDimitry Andric 150349cc55cSDimitry Andric #endif // _LIBCPP___FORMAT_FORMATTER_STRING_H 151