[NFC][libc++][format] Switches to from_range constructor.
Some places in the format library were identified to benefit from
basic_string's from_range constructor. At that time that constructor was
not implemented. It's implemented now so adjust the code to use this new
constructor.
Reviewed By: #libc, var-const
Differential Revision: https://reviews.llvm.org/D156022
diff --git a/libcxx/include/__format/range_default_formatter.h b/libcxx/include/__format/range_default_formatter.h
index bcab5d6..b35223a 100644
--- a/libcxx/include/__format/range_default_formatter.h
+++ b/libcxx/include/__format/range_default_formatter.h
@@ -24,6 +24,7 @@
#include <__iterator/back_insert_iterator.h>
#include <__ranges/concepts.h>
#include <__ranges/data.h>
+#include <__ranges/from_range.h>
#include <__ranges/size.h>
#include <__type_traits/conditional.h>
#include <__type_traits/remove_cvref.h>
@@ -197,15 +198,8 @@
// specialization is the "basic" string formatter in libc++.
if constexpr (ranges::contiguous_range<_Rp> && std::ranges::sized_range<_Rp>)
return __underlying_.format(basic_string_view<_CharT>{ranges::data(__range), ranges::size(__range)}, __ctx);
- else {
- // P2106's from_range has not been implemented yet. Instead use a simple
- // copy operation.
- // TODO FMT use basic_string's "from_range" constructor.
- // return __underlying_.format(basic_string<_CharT>{from_range, __range}, __ctx);
- basic_string<_CharT> __str;
- std::ranges::copy(__range, back_insert_iterator{__str});
- return __underlying_.format(static_cast<basic_string_view<_CharT>>(__str), __ctx);
- }
+ else
+ return __underlying_.format(basic_string<_CharT>{from_range, __range}, __ctx);
}
};
diff --git a/libcxx/include/__format/range_formatter.h b/libcxx/include/__format/range_formatter.h
index 2787097..d132780 100644
--- a/libcxx/include/__format/range_formatter.h
+++ b/libcxx/include/__format/range_formatter.h
@@ -28,6 +28,7 @@
#include <__iterator/back_insert_iterator.h>
#include <__ranges/concepts.h>
#include <__ranges/data.h>
+#include <__ranges/from_range.h>
#include <__ranges/size.h>
#include <__type_traits/remove_cvref.h>
#include <string_view>
@@ -184,13 +185,7 @@
std::formatter<basic_string<_CharT>, _CharT> __formatter;
if (__debug_format)
__formatter.set_debug_format();
- // P2106's from_range has not been implemented yet. Instead use a simple
- // copy operation.
- // TODO FMT use basic_string's "from_range" constructor.
- // return std::formatter<basic_string<_CharT>, _CharT>{}.format(basic_string<_CharT>{from_range, __range}, __ctx);
- basic_string<_CharT> __str;
- ranges::copy(__range, back_insert_iterator{__str});
- return __formatter.format(__str, __ctx);
+ return __formatter.format(basic_string<_CharT>{from_range, __range}, __ctx);
}
}