lib: use ascii ctype utils

Use the new ascii ctype utils.
This commit is contained in:
Dirk-Jan C. Binnema
2026-07-21 13:23:38 +03:00
committed by Seth Ladygo
parent f2960a197e
commit 502c9cd67b
8 changed files with 23 additions and 20 deletions

View File

@ -83,14 +83,16 @@ Field::xapian_term(const std::string& s) const
if (s.empty()) if (s.empty())
return res; return res;
res.reserve(s.size() + 10); res.reserve(s.size() + 1);
/* slightly optimized common pure-ascii. */ /* optimized common pure-ascii case */
if (G_LIKELY(g_str_is_ascii(s.c_str()))) { for (auto&& c: s) {
res += s; if (G_UNLIKELY(!is_ascii(c))) { /* non-ascii after all */
for (auto i = 1U; i != res.length(); ++i) res.erase(1U);
res[i] = g_ascii_tolower(res[i]); res += utf8_flatten(s);
} else break;
res += utf8_flatten(s); }
res.push_back(to_ascii_lower(c));
}
if (G_UNLIKELY(res.size() > MaxTermLength)) if (G_UNLIKELY(res.size() > MaxTermLength))
res.erase(MaxTermLength); res.erase(MaxTermLength);

View File

@ -105,8 +105,7 @@ struct MessageFlagInfo {
* @return lower-case shortcut * @return lower-case shortcut
*/ */
constexpr char shortcut_lower() const { constexpr char shortcut_lower() const {
return shortcut >= 'A' && shortcut <= 'Z' ? return to_ascii_lower(shortcut);
shortcut + ('a' - 'A') : shortcut;
} }
}; };

View File

@ -50,11 +50,11 @@ Mu::Labels::validate_label(const std::string &label)
if (g_unichar_isalnum(uc)) if (g_unichar_isalnum(uc))
continue; // alphanum is okay continue; // alphanum is okay
if (::iscntrl(uc)) if (is_ascii_cntrl(uc))
return Err(Error{Error::Code::InvalidArgument, return Err(Error{Error::Code::InvalidArgument,
"control character {} not allowed in label", "control character {} not allowed in label",
static_cast<int>(uc)}); static_cast<int>(uc)});
if (::isblank(uc)) if (is_ascii_blank(uc))
return Err(Error{Error::Code::InvalidArgument, return Err(Error{Error::Code::InvalidArgument,
"blank character {} not allowed in label", "blank character {} not allowed in label",
static_cast<int>(uc)}); static_cast<int>(uc)});

View File

@ -49,7 +49,8 @@ cook(const std::string& fname, const std::vector<char>& forbidden)
clean.reserve(fname.length()); clean.reserve(fname.length());
for (auto& c: basename(fname)) for (auto& c: basename(fname))
if (seq_some(forbidden,[&](char fc){return ::iscntrl(c) || c == fc;})) if (seq_some(forbidden,[&](char fc){
return is_ascii_cntrl(c) || c == fc;}))
clean += '-'; clean += '-';
else else
clean += c; clean += c;

View File

@ -437,7 +437,7 @@ process(const std::string& expr)
/* all control chars become SPC */ /* all control chars become SPC */
std::string str{expr}; std::string str{expr};
for (auto& c: str) for (auto& c: str)
c = ::iscntrl(c) ? ' ' : c; c = is_ascii_cntrl(c) ? ' ' : c;
while(!str.empty()) { while(!str.empty()) {
auto&& element = next_element(str, offset) auto&& element = next_element(str, offset)

View File

@ -111,8 +111,9 @@ struct OutputStream {
void unlink () { void unlink () {
if (fname_.empty()) if (fname_.empty())
return; return;
if (auto&&res{::unlink(fname_.c_str())}; res != 0) if (::unlink(fname_.c_str()) != 0)
mu_warning("failed to unlink '{}'", ::strerror(res)); mu_warning("failed to unlink '{}': {}", fname_,
::strerror(errno));
else else
mu_debug("unlinked output-stream {}", fname_); mu_debug("unlinked output-stream {}", fname_);
} }

View File

@ -36,7 +36,7 @@ starts_with(std::string_view haystack, std::string_view needle)
return false; return false;
for (auto&& c = 0U; c != needle.size(); ++c) for (auto&& c = 0U; c != needle.size(); ++c)
if (::tolower(haystack[c]) != ::tolower(needle[c])) if (to_ascii_lower(haystack[c]) != to_ascii_lower(needle[c]))
return false; return false;
return true; return true;
@ -143,7 +143,7 @@ public:
std::string_view eat_head_word() { std::string_view eat_head_word() {
size_t start_pos{pos_}; size_t start_pos{pos_};
while (!done()) { while (!done()) {
if (!::isalpha(html_.at(pos_))) if (!is_ascii_alpha(html_.at(pos_)))
break; break;
++pos_; ++pos_;
} }
@ -440,7 +440,7 @@ html_escape_char(Context& ctx)
auto unescape=[escs](std::string_view esc)->char { auto unescape=[escs](std::string_view esc)->char {
if (esc.empty()) if (esc.empty())
return ' '; return ' ';
auto first{static_cast<char>(::tolower(esc.at(0)))}; auto first{to_ascii_lower(esc.at(0))};
auto rest=esc.substr(1); auto rest=esc.substr(1);
if (seq_some(escs, [&](auto&& e){return starts_with(rest, e);})) if (seq_some(escs, [&](auto&& e){return starts_with(rest, e);}))
return first; return first;

View File

@ -58,7 +58,7 @@ guess_nick(const Contact& contact)
auto cleanup = [](const std::string& str) { auto cleanup = [](const std::string& str) {
std::string clean; std::string clean;
for (auto& c: str) // XXX: support non-ascii for (auto& c: str) // XXX: support non-ascii
if (!::ispunct(c) && !::isspace(c)) if (!is_ascii_punct(c) && !is_ascii_space(c))
clean += c; clean += c;
return clean; return clean;
}; };