From 791b0eb709ed827fec777773bbef10a0f642ad4b Mon Sep 17 00:00:00 2001 From: Edward Banner Date: Mon, 22 Jan 2024 15:32:55 -0500 Subject: [PATCH] Shell: Use reverse iterators for history events Replaces the custom find_reverse() function used for searching backwards through string-based history events with reverse iterators + find_if() --- Userland/Shell/AST.cpp | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/Userland/Shell/AST.cpp b/Userland/Shell/AST.cpp index d1fb592872..edd944668e 100644 --- a/Userland/Shell/AST.cpp +++ b/Userland/Shell/AST.cpp @@ -6,6 +6,7 @@ #include "AST.h" #include "Shell.h" +#include #include #include #include @@ -1594,16 +1595,6 @@ ErrorOr> HistoryEvent::run(RefPtr shell) } auto& history = editor->history(); - // FIXME: Implement reverse iterators and find()? - auto find_reverse = [](auto it_start, auto it_end, auto finder) { - auto it = it_end; - while (it != it_start) { - --it; - if (finder(*it)) - return it; - } - return it_end; - }; // First, resolve the event itself. ByteString resolved_history; switch (m_selector.event.kind) { @@ -1622,7 +1613,7 @@ ErrorOr> HistoryEvent::run(RefPtr shell) resolved_history = history[history.size() - m_selector.event.index - 1].entry; break; case HistorySelector::EventKind::ContainingStringLookup: { - auto it = find_reverse(history.begin(), history.end(), [&](auto& entry) { return entry.entry.contains(m_selector.event.text); }); + auto it = find_if(history.rbegin(), history.rend(), [&](auto& entry) { return entry.entry.contains(m_selector.event.text); }); if (it.is_end()) { shell->raise_error(Shell::ShellError::EvaluatedSyntaxError, "History event did not match any entry", m_selector.event.text_position); return make_ref_counted({}); @@ -1631,7 +1622,7 @@ ErrorOr> HistoryEvent::run(RefPtr shell) break; } case HistorySelector::EventKind::StartingStringLookup: { - auto it = find_reverse(history.begin(), history.end(), [&](auto& entry) { return entry.entry.starts_with(m_selector.event.text); }); + auto it = find_if(history.rbegin(), history.rend(), [&](auto& entry) { return entry.entry.starts_with(m_selector.event.text); }); if (it.is_end()) { shell->raise_error(Shell::ShellError::EvaluatedSyntaxError, "History event did not match any entry", m_selector.event.text_position); return make_ref_counted({});