mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-06-18 12:01:16 -03:00
Migrate a bunch of code out of common.h
Put it into wcstringutil, path, or a new file null_terminated_array.
This commit is contained in:
@@ -5,11 +5,12 @@
|
||||
|
||||
#include <wctype.h>
|
||||
|
||||
#include <locale>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
using size_type = wcstring::size_type;
|
||||
|
||||
wcstring_range wcstring_tok(wcstring &str, const wcstring &needle, wcstring_range last) {
|
||||
using size_type = wcstring::size_type;
|
||||
size_type pos = last.second == wcstring::npos ? wcstring::npos : last.first;
|
||||
if (pos != wcstring::npos && last.second != wcstring::npos) pos += last.second;
|
||||
if (pos != wcstring::npos && pos != 0) ++pos;
|
||||
@@ -71,3 +72,127 @@ wcstring wcstolower(wcstring input) {
|
||||
std::transform(result.begin(), result.end(), result.begin(), towlower);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool string_prefixes_string(const wchar_t *proposed_prefix, const wcstring &value) {
|
||||
return string_prefixes_string(proposed_prefix, value.c_str());
|
||||
}
|
||||
|
||||
bool string_prefixes_string(const wcstring &proposed_prefix, const wcstring &value) {
|
||||
size_t prefix_size = proposed_prefix.size();
|
||||
return prefix_size <= value.size() && value.compare(0, prefix_size, proposed_prefix) == 0;
|
||||
}
|
||||
|
||||
bool string_prefixes_string(const wchar_t *proposed_prefix, const wchar_t *value) {
|
||||
for (size_t idx = 0; proposed_prefix[idx] != L'\0'; idx++) {
|
||||
// Note if the prefix is longer than value, then we will compare a nonzero prefix character
|
||||
// against a zero value character, and so we'll return false;
|
||||
if (proposed_prefix[idx] != value[idx]) return false;
|
||||
}
|
||||
// We must have that proposed_prefix[idx] == L'\0', so we have a prefix match.
|
||||
return true;
|
||||
}
|
||||
|
||||
bool string_prefixes_string(const char *proposed_prefix, const std::string &value) {
|
||||
return string_prefixes_string(proposed_prefix, value.c_str());
|
||||
}
|
||||
|
||||
bool string_prefixes_string(const char *proposed_prefix, const char *value) {
|
||||
for (size_t idx = 0; proposed_prefix[idx] != L'\0'; idx++) {
|
||||
if (proposed_prefix[idx] != value[idx]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool string_prefixes_string_case_insensitive(const wcstring &proposed_prefix,
|
||||
const wcstring &value) {
|
||||
size_t prefix_size = proposed_prefix.size();
|
||||
return prefix_size <= value.size() &&
|
||||
wcsncasecmp(proposed_prefix.c_str(), value.c_str(), prefix_size) == 0;
|
||||
}
|
||||
|
||||
bool string_suffixes_string(const wcstring &proposed_suffix, const wcstring &value) {
|
||||
size_t suffix_size = proposed_suffix.size();
|
||||
return suffix_size <= value.size() &&
|
||||
value.compare(value.size() - suffix_size, suffix_size, proposed_suffix) == 0;
|
||||
}
|
||||
|
||||
bool string_suffixes_string(const wchar_t *proposed_suffix, const wcstring &value) {
|
||||
size_t suffix_size = std::wcslen(proposed_suffix);
|
||||
return suffix_size <= value.size() &&
|
||||
value.compare(value.size() - suffix_size, suffix_size, proposed_suffix) == 0;
|
||||
}
|
||||
|
||||
bool string_suffixes_string_case_insensitive(const wcstring &proposed_suffix,
|
||||
const wcstring &value) {
|
||||
size_t suffix_size = proposed_suffix.size();
|
||||
return suffix_size <= value.size() && wcsncasecmp(value.c_str() + (value.size() - suffix_size),
|
||||
proposed_suffix.c_str(), suffix_size) == 0;
|
||||
}
|
||||
|
||||
template <bool Fuzzy, typename T>
|
||||
size_t ifind_impl(const T &haystack, const T &needle) {
|
||||
using char_t = typename T::value_type;
|
||||
std::locale locale;
|
||||
|
||||
auto ieq = [&locale](char_t c1, char_t c2) {
|
||||
if (c1 == c2 || std::toupper(c1, locale) == std::toupper(c2, locale)) return true;
|
||||
|
||||
// In fuzzy matching treat treat `-` and `_` as equal (#3584).
|
||||
if (Fuzzy) {
|
||||
if ((c1 == '-' || c1 == '_') && (c2 == '-' || c2 == '_')) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
auto result = std::search(haystack.begin(), haystack.end(), needle.begin(), needle.end(), ieq);
|
||||
if (result != haystack.end()) {
|
||||
return result - haystack.begin();
|
||||
}
|
||||
return T::npos;
|
||||
}
|
||||
|
||||
size_t ifind(const wcstring &haystack, const wcstring &needle, bool fuzzy) {
|
||||
return fuzzy ? ifind_impl<true>(haystack, needle) : ifind_impl<false>(haystack, needle);
|
||||
}
|
||||
|
||||
size_t ifind(const std::string &haystack, const std::string &needle, bool fuzzy) {
|
||||
return fuzzy ? ifind_impl<true>(haystack, needle) : ifind_impl<false>(haystack, needle);
|
||||
}
|
||||
|
||||
wcstring_list_t split_string(const wcstring &val, wchar_t sep) {
|
||||
wcstring_list_t out;
|
||||
size_t pos = 0, end = val.size();
|
||||
while (pos <= end) {
|
||||
size_t next_pos = val.find(sep, pos);
|
||||
if (next_pos == wcstring::npos) {
|
||||
next_pos = end;
|
||||
}
|
||||
out.emplace_back(val, pos, next_pos - pos);
|
||||
pos = next_pos + 1; // skip the separator, or skip past the end
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
wcstring join_strings(const wcstring_list_t &vals, wchar_t sep) {
|
||||
if (vals.empty()) return wcstring{};
|
||||
|
||||
// Reserve the size we will need.
|
||||
// count-1 separators, plus the length of all strings.
|
||||
size_t size = vals.size() - 1;
|
||||
for (const wcstring &s : vals) {
|
||||
size += s.size();
|
||||
}
|
||||
|
||||
// Construct the string.
|
||||
wcstring result;
|
||||
result.reserve(size);
|
||||
bool first = true;
|
||||
for (const wcstring &s : vals) {
|
||||
if (!first) {
|
||||
result.push_back(sep);
|
||||
}
|
||||
result.append(s);
|
||||
first = false;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user