From bc0329f77588988d2a852f6ccbe9c154349cc303 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 6 Jul 2019 12:19:34 -0700 Subject: [PATCH] Add wcstolower Converts a string to lowercase. Eliminates some loops that did this explicitly. --- src/builtin_string.cpp | 10 ++-------- src/fish_tests.cpp | 13 ++----------- src/history.cpp | 5 +---- src/wcstringutil.cpp | 8 ++++++++ src/wcstringutil.h | 3 +++ 5 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/builtin_string.cpp b/src/builtin_string.cpp index ed07be5dc..0523ebe2d 100644 --- a/src/builtin_string.cpp +++ b/src/builtin_string.cpp @@ -625,9 +625,7 @@ class wildcard_matcher_t : public string_matcher_t { io_streams_t &streams) : string_matcher_t(opts, streams), wcpattern(parse_util_unescape_wildcards(pattern)) { if (opts.ignore_case) { - for (size_t i = 0; i < wcpattern.length(); i++) { - wcpattern[i] = towlower(wcpattern[i]); - } + wcpattern = wcstolower(std::move(wcpattern)); } if (opts.entire) { if (!wcpattern.empty()) { @@ -648,11 +646,7 @@ class wildcard_matcher_t : public string_matcher_t { bool match; if (opts.ignore_case) { - wcstring s = arg; - for (size_t i = 0; i < s.length(); i++) { - s[i] = towlower(s[i]); - } - match = wildcard_match(s, wcpattern, false); + match = wildcard_match(wcstolower(arg), wcpattern, false); } else { match = wildcard_match(arg, wcpattern, false); } diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index e60b03e8f..564441c21 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -3490,15 +3490,6 @@ static wcstring random_string() { return result; } -// Helper to lowercase a string. -static wcstring lower(const wcstring &s) { - wcstring result; - for (wchar_t c : s) { - result.push_back(towlower(c)); - } - return result; -} - void history_tests_t::test_history() { history_search_t searcher; say(L"Testing history"); @@ -3531,7 +3522,7 @@ void history_tests_t::test_history() { // Items matching "alpha", case-insensitive. searcher = history_search_t(history, L"AlPhA", HISTORY_SEARCH_TYPE_CONTAINS, nocase); - set_expected([](const wcstring &s) { return lower(s).find(L"alpha") != wcstring::npos; }); + set_expected([](const wcstring &s) { return wcstolower(s).find(L"alpha") != wcstring::npos; }); test_history_matches(searcher, expected, __LINE__); // Items matching "et", case-sensitive. @@ -3557,7 +3548,7 @@ void history_tests_t::test_history() { // Items exactly matching "alph", case-insensitive. searcher = history_search_t(history, L"alph", HISTORY_SEARCH_TYPE_EXACT, nocase); - set_expected([](const wcstring &s) { return lower(s) == L"alph"; }); + set_expected([](const wcstring &s) { return wcstolower(s) == L"alph"; }); test_history_matches(searcher, expected, __LINE__); // Test item removal case-sensitive. diff --git a/src/history.cpp b/src/history.cpp index 3c5e12287..ea1f7ae59 100644 --- a/src/history.cpp +++ b/src/history.cpp @@ -553,10 +553,7 @@ 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) : creation_timestamp(when), identifier(ident) { contents = trim(str); - contents_lower.reserve(contents.size()); - for (const auto &c : contents) { - contents_lower.push_back(towlower(c)); - } + contents_lower = wcstolower(contents); } bool history_item_t::matches_search(const wcstring &term, enum history_search_type_t type, diff --git a/src/wcstringutil.cpp b/src/wcstringutil.cpp index d20e2c4e3..b0fac9809 100644 --- a/src/wcstringutil.cpp +++ b/src/wcstringutil.cpp @@ -4,6 +4,8 @@ #include "common.h" #include "wcstringutil.h" +#include + typedef wcstring::size_type size_type; wcstring_range wcstring_tok(wcstring &str, const wcstring &needle, wcstring_range last) { @@ -59,3 +61,9 @@ wcstring trim(const wcstring &input, const wchar_t *any_of) { wcstring result(input.begin() + begin_offset, end + 1); return result; } + +wcstring wcstolower(wcstring input) { + wcstring result = std::move(input); + std::transform(result.begin(), result.end(), result.begin(), towlower); + return result; +} diff --git a/src/wcstringutil.h b/src/wcstringutil.h index 473b8f5f9..4fbfe0608 100644 --- a/src/wcstringutil.h +++ b/src/wcstringutil.h @@ -68,4 +68,7 @@ wcstring truncate(const wcstring &input, int max_len, wcstring trim(const wcstring &input); wcstring trim(const wcstring &input, const wchar_t *any_of); +/// Converts a string to lowercase. +wcstring wcstolower(wcstring input); + #endif