From f702c42068591ed6fca2509b4696df00b64e2843 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Mon, 1 Oct 2018 16:48:52 -0500 Subject: [PATCH] Sanitize history item whitespace Coalesces commands with leading (if even possible) and trailing whitespace into the same item, improving the experience when iterating over history entries. Closes #4908. --- src/fish_tests.cpp | 4 ++-- src/history.cpp | 10 +++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index 43dbdb753..4f9b9fddb 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -3456,9 +3456,9 @@ void history_tests_t::test_history_formats() { err(L"Couldn't open file tests/history_sample_bash"); } else { // The results are in the reverse order that they appear in the bash history file. - // We don't expect whitespace to be elided. + // We don't expect whitespace to be elided (#4908: except for leading/trailing whitespace) const wchar_t *expected[] = {L"sleep 123", - L" final line", + L"final line", L"echo supsup", L"export XVAR='exported'", L"history --help", diff --git a/src/history.cpp b/src/history.cpp index cd016b010..eca9fa638 100644 --- a/src/history.cpp +++ b/src/history.cpp @@ -41,6 +41,7 @@ #include "path.h" #include "reader.h" #include "tnode.h" +#include "wcstringutil.h" #include "wildcard.h" // IWYU pragma: keep #include "wutil.h" // IWYU pragma: keep @@ -556,9 +557,12 @@ bool history_item_t::merge(const history_item_t &item) { } history_item_t::history_item_t(const wcstring &str, time_t when, history_identifier_t ident) - : contents(str), contents_lower(L""), creation_timestamp(when), identifier(ident) { - for (wcstring::const_iterator it = str.begin(); it != str.end(); ++it) { - contents_lower.push_back(towlower(*it)); + : creation_timestamp(when), identifier(ident) { + + contents = trim(str); + contents_lower.reserve(contents.size()); + for (const auto &c : contents) { + contents_lower.push_back(towlower(c)); } }