blob: 45d1147e445da59685915a0926b05b040d385766 [file] [log] [blame]
// -*- C++ -*-
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include <__config>
#include <__format/format_error.h>
#include <__format/format_fwd.h>
#include <__format/format_string.h>
#include <__format/formatter.h>
#include <__format/parser_std_format_spec.h>
#include <algorithm>
#include <string_view>
#pragma GCC system_header
#include <__undef_macros>
#if _LIBCPP_STD_VER > 17
// TODO FMT Remove this once we require compilers with proper C++20 support.
// If the compiler has no concepts support, the format header will be disabled.
// Without concepts support enable_if needs to be used and that too much effort
// to support compilers with partial C++20 support.
namespace __format_spec {
template <__formatter::__char_type _CharT>
class _LIBCPP_TEMPLATE_VIS __formatter_string : public __parser_string<_CharT> {
_LIBCPP_HIDE_FROM_ABI auto format(basic_string_view<_CharT> __str,
auto& __ctx) -> decltype(__ctx.out()) {
_LIBCPP_ASSERT(this->__alignment != _Flags::_Alignment::__default,
"The parser should not use these defaults");
if (this->__width_needs_substitution())
if (this->__precision_needs_substitution())
return __formatter::__write_unicode(
__ctx.out(), __str, this->__width,
this->__has_precision_field() ? this->__precision : -1, this->__fill,
} //namespace __format_spec
// [format.formatter.spec]/2.2 For each charT, the string type specializations
// Formatter const char*.
template <class _CharT>
formatter<const _CharT*, _CharT>
: public __format_spec::__formatter_string<_CharT> {
using _Base = __format_spec::__formatter_string<_CharT>;
_LIBCPP_HIDE_FROM_ABI auto format(const _CharT* __str, auto& __ctx)
-> decltype(__ctx.out()) {
_LIBCPP_ASSERT(__str, "The basic_format_arg constructor should have "
"prevented an invalid pointer.");
// When using a center or right alignment and the width option the length
// of __str must be known to add the padding upfront. This case is handled
// by the base class by converting the argument to a basic_string_view.
// When using left alignment and the width option the padding is added
// after outputting __str so the length can be determined while outputting
// __str. The same holds true for the precision, during outputting __str it
// can be validated whether the precision threshold has been reached. For
// now these optimizations aren't implemented. Instead the base class
// handles these options.
// TODO FMT Implement these improvements.
if (this->__has_width_field() || this->__has_precision_field())
return _Base::format(__str, __ctx);
// No formatting required, copy the string to the output.
auto __out_it = __ctx.out();
while (*__str)
*__out_it++ = *__str++;
return __out_it;
// Formatter char*.
template <class _CharT>
formatter<_CharT*, _CharT> : public formatter<const _CharT*, _CharT> {
using _Base = formatter<const _CharT*, _CharT>;
_LIBCPP_HIDE_FROM_ABI auto format(_CharT* __str, auto& __ctx)
-> decltype(__ctx.out()) {
return _Base::format(__str, __ctx);
// Formatter const char[].
template <class _CharT, size_t _Size>
formatter<const _CharT[_Size], _CharT>
: public __format_spec::__formatter_string<_CharT> {
using _Base = __format_spec::__formatter_string<_CharT>;
_LIBCPP_HIDE_FROM_ABI auto format(const _CharT __str[_Size], auto& __ctx)
-> decltype(__ctx.out()) {
return _Base::format(_VSTD::basic_string_view<_CharT>(__str, _Size), __ctx);
// Formatter std::string.
template <class _CharT, class _Traits, class _Allocator>
formatter<basic_string<_CharT, _Traits, _Allocator>, _CharT>
: public __format_spec::__formatter_string<_CharT> {
using _Base = __format_spec::__formatter_string<_CharT>;
format(const basic_string<_CharT, _Traits, _Allocator>& __str, auto& __ctx)
-> decltype(__ctx.out()) {
return _Base::format(_VSTD::basic_string_view<_CharT>(__str), __ctx);
// Formatter std::string_view.
template <class _CharT, class _Traits>
formatter<basic_string_view<_CharT, _Traits>, _CharT>
: public __format_spec::__formatter_string<_CharT> {};
#endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
#endif //_LIBCPP_STD_VER > 17