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)); } }