[PATCH v1 2/6] gdb support: add gdb::ranges::replace algorithm
Matthieu Longo <[email protected]> Tue, 28 Jul 2026 16:16:56 +0100
| Newsgroups | gmane.comp.gdb.patches |
|---|---|
| Message-ID | <[email protected]> |
Provide a C++17-compatible replacement for the C++20 std::ranges::replace
algorithm, allowing callers to use a consistent interface until GDB
transitions to C++20. The helper should be removed once the C++ standard
library implementation become available.
https://en.cppreference.com/cpp/algorithm/ranges/replace
---
gdbsupport/array-view.h | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/gdbsupport/array-view.h b/gdbsupport/array-view.h
index 8431d7f5add..f9842ecff30 100644
--- a/gdbsupport/array-view.h
+++ b/gdbsupport/array-view.h
@@ -225,6 +225,22 @@ void copy (gdb::array_view<U> src, gdb::array_view<T> dest)
std::copy_backward (src.begin (), src.end (), dest.end ());
}
+namespace ranges {
+
+/* Replace all occurrences of a value in the provided range.
+
+ Note: this helper is a reimplementation of std::ranges::replace, only
+ available from C++20 onwards, and consequently, should be replaced by
+ std::ranges::replace once GDB switches to C++20. */
+
+template <class Range, typename T>
+void replace (Range r, const T &old_value, const T &new_value)
+{
+ std::replace (r.begin (), r.end (), old_value, new_value);
+}
+
+} /* namespace ranges */
+
/* Compare LHS and RHS for (deep) equality. That is, whether LHS and
RHS have the same sizes, and whether each pair of elements of LHS
and RHS at the same position compares equal. */
--
2.55.0