[libcxx-commits] [libcxx] f0e6102 - [libc++][format] Adds formatter<charT[N], charT>.

Mark de Wever via libcxx-commits libcxx-commits at lists.llvm.org
Wed May 18 11:10:21 PDT 2022


Author: Mark de Wever
Date: 2022-05-18T20:10:16+02:00
New Revision: f0e61029506fd63bb300f2dbbd65ba792e4ef3a2

URL: https://github.com/llvm/llvm-project/commit/f0e61029506fd63bb300f2dbbd65ba792e4ef3a2
DIFF: https://github.com/llvm/llvm-project/commit/f0e61029506fd63bb300f2dbbd65ba792e4ef3a2.diff

LOG: [libc++][format] Adds formatter<charT[N], charT>.

This formatter isn't in the list of required formatters in

[format.formatter.spec]/2.2
  For each charT, the string type specializations
   template<> struct formatter<charT*, charT>;
  template<> struct formatter<const charT*, charT>;
  template<size_t N> struct formatter<const charT[N], charT>;
  template<class traits, class Allocator>
    struct formatter<basic_string<charT, traits, Allocator>, charT>;
  template<class traits>
    struct formatter<basic_string_view<charT, traits>, charT>;

Since remove_cvref_t<const charT[N]> is charT[N] the formatter is
required by

[format.functions]/25
  Preconditions: formatter<remove_cvref_t<Ti>, charT> meets the
  BasicFormatter requirements ([formatter.requirements]) for each Ti in
  Args.

Depends on D120921

Reviewed By: #libc, Mordante

Differential Revision: https://reviews.llvm.org/D121138

Added: 
    libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/formatter.char_array.pass.cpp

Modified: 
    libcxx/include/__format/formatter_string.h
    libcxx/test/libcxx/utilities/format/format.formatter/format.formatter.spec/formattable.compile.pass.cpp
    libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/types.compile.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__format/formatter_string.h b/libcxx/include/__format/formatter_string.h
index e78b43b8ca7b..c687e3fd4839 100644
--- a/libcxx/include/__format/formatter_string.h
+++ b/libcxx/include/__format/formatter_string.h
@@ -101,6 +101,18 @@ struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT
   }
 };
 
+// Formatter char[].
+template <__formatter::__char_type _CharT, size_t _Size>
+struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter<_CharT[_Size], _CharT>
+    : public __format_spec::__formatter_string<_CharT> {
+  static_assert(!is_const_v<_CharT>);
+  using _Base = __format_spec::__formatter_string<_CharT>;
+
+  _LIBCPP_HIDE_FROM_ABI auto format(_CharT __str[_Size], auto& __ctx) -> decltype(__ctx.out()) {
+    return _Base::format(basic_string_view<_CharT>(__str, _Size), __ctx);
+  }
+};
+
 // Formatter const char[].
 template <__formatter::__char_type _CharT, size_t _Size>
 struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT

diff  --git a/libcxx/test/libcxx/utilities/format/format.formatter/format.formatter.spec/formattable.compile.pass.cpp b/libcxx/test/libcxx/utilities/format/format.formatter/format.formatter.spec/formattable.compile.pass.cpp
index cc3c3437dc77..ab125ad03be7 100644
--- a/libcxx/test/libcxx/utilities/format/format.formatter/format.formatter.spec/formattable.compile.pass.cpp
+++ b/libcxx/test/libcxx/utilities/format/format.formatter/format.formatter.spec/formattable.compile.pass.cpp
@@ -82,6 +82,7 @@ void test_P0645() {
 
   assert_is_formattable<CharT*, CharT>();
   assert_is_formattable<const CharT*, CharT>();
+  assert_is_formattable<CharT[42], CharT>();
   assert_is_formattable<std::basic_string<CharT>, CharT>();
   assert_is_formattable<std::basic_string_view<CharT>, CharT>();
 

diff  --git a/libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/formatter.char_array.pass.cpp b/libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/formatter.char_array.pass.cpp
new file mode 100644
index 000000000000..1f67f38ec048
--- /dev/null
+++ b/libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/formatter.char_array.pass.cpp
@@ -0,0 +1,115 @@
+//===----------------------------------------------------------------------===//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+// UNSUPPORTED: libcpp-has-no-incomplete-format
+// TODO FMT Evaluate gcc-11 status
+// UNSUPPORTED: gcc-11
+// UNSUPPORTED: apple-clang-12
+
+// <format>
+
+// [format.functions]/25
+//   Preconditions: formatter<remove_cvref_t<Ti>, charT> meets the
+//   BasicFormatter requirements ([formatter.requirements]) for each Ti in
+//   Args.
+
+#include <format>
+#include <cassert>
+#include <concepts>
+#include <type_traits>
+
+#include "test_macros.h"
+#include "make_string.h"
+
+#define STR(S) MAKE_STRING(CharT, S)
+#define CSTR(S) MAKE_CSTRING(CharT, S)
+
+// This is based on the method found in
+// clang/test/CXX/temp/temp.arg/temp.arg.nontype/p1-cxx20.cpp
+template <size_t N>
+struct Tester {
+  // This is not part of the real test, but is used the deduce the size of the input.
+  constexpr Tester(const char (&r)[N]) { __builtin_memcpy(text, r, N); }
+  char text[N];
+
+  // The size of the array shouldn't include the NUL character.
+  static const size_t size = N - 1;
+
+  template <class CharT>
+  void test(const std::basic_string<CharT>& expected, const std::basic_string_view<CharT>& fmt) const {
+    using Str = CharT[size];
+    std::basic_format_parse_context<CharT> parse_ctx{fmt};
+    std::formatter<Str, CharT> formatter;
+    static_assert(std::semiregular<decltype(formatter)>);
+
+    auto it = formatter.parse(parse_ctx);
+    assert(it == fmt.end() - (!fmt.empty() && fmt.back() == '}'));
+
+    std::basic_string<CharT> result;
+    auto out = std::back_inserter(result);
+    using FormatCtxT = std::basic_format_context<decltype(out), CharT>;
+
+    std::basic_string<CharT> buffer{text, text + N};
+    // Note not too found of this hack
+    Str* data = reinterpret_cast<Str*>(const_cast<CharT*>(buffer.c_str()));
+
+    auto format_ctx = std::__format_context_create<decltype(out), CharT>(out, std::make_format_args<FormatCtxT>(*data));
+    formatter.format(*data, format_ctx);
+    assert(result == expected);
+  }
+
+  template <class CharT>
+  void test_termination_condition(const std::basic_string<CharT>& expected, const std::basic_string<CharT>& f) const {
+    // The format-spec is valid if completely consumed or terminates at a '}'.
+    // The valid inputs all end with a '}'. The test is executed twice:
+    // - first with the terminating '}',
+    // - second consuming the entire input.
+    std::basic_string_view<CharT> fmt{f};
+    assert(fmt.back() == CharT('}') && "Pre-condition failure");
+
+    test(expected, fmt);
+    fmt.remove_suffix(1);
+    test(expected, fmt);
+  }
+};
+
+template <Tester t, class CharT>
+void test_helper_wrapper(std::basic_string<CharT> expected, std::basic_string<CharT> fmt) {
+  t.test_termination_condition(expected, fmt);
+}
+
+template <class CharT>
+void test_array() {
+  test_helper_wrapper<" azAZ09,./<>?">(STR(" azAZ09,./<>?"), STR("}"));
+
+  std::basic_string<CharT> s(CSTR("abc\0abc"), 7);
+  test_helper_wrapper<"abc\0abc">(s, STR("}"));
+
+  test_helper_wrapper<"world">(STR("world"), STR("}"));
+  test_helper_wrapper<"world">(STR("world"), STR("_>}"));
+
+  test_helper_wrapper<"world">(STR("   world"), STR(">8}"));
+  test_helper_wrapper<"world">(STR("___world"), STR("_>8}"));
+  test_helper_wrapper<"world">(STR("_world__"), STR("_^8}"));
+  test_helper_wrapper<"world">(STR("world___"), STR("_<8}"));
+
+  test_helper_wrapper<"world">(STR("world"), STR(".5}"));
+  test_helper_wrapper<"universe">(STR("unive"), STR(".5}"));
+
+  test_helper_wrapper<"world">(STR("%world%"), STR("%^7.7}"));
+  test_helper_wrapper<"universe">(STR("univers"), STR("%^7.7}"));
+}
+
+int main(int, char**) {
+  test_array<char>();
+#ifndef TEST_HAS_NO_WIDE_CHARACTERS
+  test_array<wchar_t>();
+#endif
+
+  return 0;
+}

diff  --git a/libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/types.compile.pass.cpp b/libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/types.compile.pass.cpp
index ea9b248a749c..9bf871ecccb1 100644
--- a/libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/types.compile.pass.cpp
+++ b/libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/types.compile.pass.cpp
@@ -123,6 +123,7 @@ void test_P0645() {
 
   assert_formatter_is_enabled<CharT*, CharT>();
   assert_formatter_is_enabled<const CharT*, CharT>();
+  assert_formatter_is_enabled<CharT[42], CharT>();
   assert_formatter_is_enabled<const CharT[42], CharT>();
   assert_formatter_is_enabled<std::basic_string<CharT>, CharT>();
   assert_formatter_is_enabled<std::basic_string_view<CharT>, CharT>();


        


More information about the libcxx-commits mailing list