mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-06-08 19:31:14 -03:00
Add wcstolower
Converts a string to lowercase. Eliminates some loops that did this explicitly.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
#include "common.h"
|
||||
#include "wcstringutil.h"
|
||||
|
||||
#include <wctype.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user