[libcxx-commits] [libcxx] 9afaa15 - [libc++][format] Copy code to new location.

Mark de Wever via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jun 23 08:21:42 PDT 2022


Author: Mark de Wever
Date: 2022-06-23T17:21:37+02:00
New Revision: 9afaa158f52c13cb79fb0ab8cc73361b134c08c9

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

LOG: [libc++][format] Copy code to new location.

This is a helper patch to ease the reviewing of D128139.
The originals will be removed at a later time when all formatters are
converted to the new style. (Floating-point and pointer aren't up for
review yet.)

Reviewed By: #libc, ldionne

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

Added: 
    

Modified: 
    libcxx/include/__format/formatter_output.h

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__format/formatter_output.h b/libcxx/include/__format/formatter_output.h
index d92423c506ad5..ab016f6f16107 100644
--- a/libcxx/include/__format/formatter_output.h
+++ b/libcxx/include/__format/formatter_output.h
@@ -11,12 +11,16 @@
 #define _LIBCPP___FORMAT_FORMATTER_OUTPUT_H
 
 #include <__algorithm/copy.h>
+#include <__algorithm/copy_n.h>
 #include <__algorithm/fill_n.h>
+#include <__algorithm/transform.h>
 #include <__config>
+#include <__format/formatter.h>
 #include <__format/parser_std_format_spec.h>
 #include <__utility/move.h>
 #include <__utility/unreachable.h>
 #include <cstddef>
+#include <string>
 #include <string_view>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -29,6 +33,24 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 namespace __formatter {
 
+_LIBCPP_HIDE_FROM_ABI constexpr char __hex_to_upper(char c) {
+  switch (c) {
+  case 'a':
+    return 'A';
+  case 'b':
+    return 'B';
+  case 'c':
+    return 'C';
+  case 'd':
+    return 'D';
+  case 'e':
+    return 'E';
+  case 'f':
+    return 'F';
+  }
+  return c;
+}
+
 // TODO FMT remove _v2 suffix.
 struct _LIBCPP_TYPE_VIS __padding_size_result_v2 {
   size_t __before_;
@@ -67,6 +89,73 @@ _LIBCPP_HIDE_FROM_ABI constexpr __padding_size_result_v2 __padding_size_v2(size_
   __libcpp_unreachable();
 }
 
+template <class _OutIt, class _CharT>
+_LIBCPP_HIDE_FROM_ABI _OutIt __write_using_decimal_separators(_OutIt __out_it, const char* __begin, const char* __first,
+                                                              const char* __last, string&& __grouping, _CharT __sep,
+                                                              __format_spec::__parsed_specifications<_CharT> __specs) {
+  _LIBCPP_ASSERT(__specs.__alignment_ != __format_spec::__alignment::__default,
+                 "the caller should adjust the default to the value required by the type");
+
+  int __size = (__first - __begin) +    // [sign][prefix]
+               (__last - __first) +     // data
+               (__grouping.size() - 1); // number of separator characters
+
+  __padding_size_result_v2 __padding = {0, 0};
+  if (__specs.__alignment_ == __format_spec::__alignment::__zero_padding) {
+    // Write [sign][prefix].
+    __out_it = _VSTD::copy(__begin, __first, _VSTD::move(__out_it));
+
+    if (__specs.__width_ > __size) {
+      // Write zero padding.
+      __padding.__before_ = __specs.__width_ - __size;
+      __out_it = _VSTD::fill_n(_VSTD::move(__out_it), __specs.__width_ - __size, _CharT('0'));
+    }
+  } else {
+    if (__specs.__width_ > __size) {
+      // Determine padding and write padding.
+      __padding = __padding_size_v2(__size, __specs.__width_, __specs.__alignment_);
+
+      __out_it = _VSTD::fill_n(_VSTD::move(__out_it), __padding.__before_, __specs.__fill_);
+    }
+    // Write [sign][prefix].
+    __out_it = _VSTD::copy(__begin, __first, _VSTD::move(__out_it));
+  }
+
+  auto __r = __grouping.rbegin();
+  auto __e = __grouping.rend() - 1;
+  _LIBCPP_ASSERT(__r != __e, "The slow grouping formatting is used while "
+                             "there will be no separators written.");
+  // The output is divided in small groups of numbers to write:
+  // - A group before the first separator.
+  // - A separator and a group, repeated for the number of separators.
+  // - A group after the last separator.
+  // This loop achieves that process by testing the termination condition
+  // midway in the loop.
+  //
+  // TODO FMT This loop evaluates the loop invariant `__parser.__type !=
+  // _Flags::_Type::__hexadecimal_upper_case` for every iteration. (This test
+  // happens in the __write call.) Benchmark whether making two loops and
+  // hoisting the invariant is worth the effort.
+  while (true) {
+    if (__specs.__std_.__type_ == __format_spec::__type::__hexadecimal_upper_case) {
+      __last = __first + *__r;
+      __out_it = _VSTD::transform(__first, __last, _VSTD::move(__out_it), __hex_to_upper);
+      __first = __last;
+    } else {
+      __out_it = _VSTD::copy_n(__first, *__r, _VSTD::move(__out_it));
+      __first += *__r;
+    }
+
+    if (__r == __e)
+      break;
+
+    ++__r;
+    *__out_it++ = __sep;
+  }
+
+  return _VSTD::fill_n(_VSTD::move(__out_it), __padding.__after_, __specs.__fill_);
+}
+
 /// Writes the input to the output with the required padding.
 ///
 /// Since the output column width is specified the function can be used for
@@ -107,12 +196,39 @@ _LIBCPP_HIDE_FROM_ABI auto __write(const _CharT* __first, const _CharT* __last,
   return _VSTD::fill_n(_VSTD::move(__out_it), __padding.__after_, __specs.__fill_);
 }
 
+/// \overload
+/// Calls the function above where \a __size = \a __last - \a __first.
+template <class _CharT, class _ParserCharT>
+_LIBCPP_HIDE_FROM_ABI auto __write(const _CharT* __first, const _CharT* __last,
+                                   output_iterator<const _CharT&> auto __out_it,
+                                   __format_spec::__parsed_specifications<_ParserCharT> __specs) -> decltype(__out_it) {
+  return __write(__first, __last, _VSTD::move(__out_it), __specs, __last - __first);
+}
+
+template <class _CharT, class _ParserCharT, class _UnaryOperation>
+_LIBCPP_HIDE_FROM_ABI auto __write_transformed(const _CharT* __first, const _CharT* __last,
+                                               output_iterator<const _CharT&> auto __out_it,
+                                               __format_spec::__parsed_specifications<_ParserCharT> __specs,
+                                               _UnaryOperation __op) -> decltype(__out_it) {
+  _LIBCPP_ASSERT(__first <= __last, "Not a valid range");
+
+  ptr
diff _t __size = __last - __first;
+  if (__size >= __specs.__width_)
+    return _VSTD::transform(__first, __last, _VSTD::move(__out_it), __op);
+
+  __padding_size_result_v2 __padding = __padding_size_v2(__size, __specs.__width_, __specs.__alignment_);
+  __out_it = _VSTD::fill_n(_VSTD::move(__out_it), __padding.__before_, __specs.__fill_);
+  __out_it = _VSTD::transform(__first, __last, _VSTD::move(__out_it), __op);
+  return _VSTD::fill_n(_VSTD::move(__out_it), __padding.__after_, __specs.__fill_);
+}
+
 #  ifndef _LIBCPP_HAS_NO_UNICODE
 template <class _CharT>
 _LIBCPP_HIDE_FROM_ABI auto __write_unicode_no_precision(basic_string_view<_CharT> __str,
                                                         output_iterator<const _CharT&> auto __out_it,
                                                         __format_spec::__parsed_specifications<_CharT> __specs)
     -> decltype(__out_it) {
+
   _LIBCPP_ASSERT(!__specs.__has_precision(), "use __write_unicode");
   // No padding -> copy the string
   if (!__specs.__has_width())


        


More information about the libcxx-commits mailing list