mirror of https://github.com/djcb/mu.git
utils: small optimization in utf8_flatten
In the common path, avoid building an unneeded std::string. This should up in some profiles.
This commit is contained in:
parent
f9b615c3bb
commit
da10f30adf
|
@ -99,37 +99,33 @@ gx_utf8_flatten (const gchar *str, gssize len)
|
|||
|
||||
} // namespace
|
||||
|
||||
|
||||
std::string // gx_utf8_flatten
|
||||
Mux::utf8_flatten (const std::string& str)
|
||||
Mux::utf8_flatten (const char *str)
|
||||
{
|
||||
// optimization for boring old ascii strings
|
||||
if (!str)
|
||||
return {};
|
||||
|
||||
bool is_ascii = true;
|
||||
std::string s{str};
|
||||
for (auto it = s.begin(); it != s.end(); ++it) {
|
||||
if (*it & 0x80) {
|
||||
is_ascii = false;
|
||||
break;
|
||||
} else
|
||||
*it = tolower(*it);
|
||||
while (*str && is_ascii) {
|
||||
is_ascii = *str & 0x80;
|
||||
++str;
|
||||
}
|
||||
|
||||
if (G_LIKELY(is_ascii))
|
||||
return s;
|
||||
return str;
|
||||
///////////////////////////////////////////
|
||||
|
||||
// seems we need the big guns
|
||||
char *flat = gx_utf8_flatten (str.c_str(), str.length());
|
||||
char *flat = gx_utf8_flatten (str, -1);
|
||||
if (!flat)
|
||||
return {};
|
||||
|
||||
s = flat;
|
||||
std::string s{flat};
|
||||
g_free (flat);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
std::string
|
||||
Mux::utf8_clean (const std::string& dirty)
|
||||
{
|
||||
|
@ -166,7 +162,6 @@ Mux::split (const std::string& str, const std::string& sepa)
|
|||
return vec;
|
||||
}
|
||||
|
||||
|
||||
std::string
|
||||
Mux::quote (const std::string& str)
|
||||
{
|
||||
|
@ -222,7 +217,6 @@ Mux::date_to_time_t_string (int64_t t)
|
|||
return buf;
|
||||
}
|
||||
|
||||
|
||||
static std::string
|
||||
delta_ymwdhMs (const std::string& expr)
|
||||
{
|
||||
|
@ -379,7 +373,6 @@ Mux::date_to_time_t_string (const std::string& dstr, bool is_first)
|
|||
return date_to_time_t_string (t);
|
||||
}
|
||||
|
||||
|
||||
constexpr const auto SizeFormat = "%010" G_GINT64_FORMAT;
|
||||
|
||||
constexpr const char SizeMin[] = "0000000000";
|
||||
|
|
|
@ -32,7 +32,9 @@ namespace Mux {
|
|||
*
|
||||
* @return a flattened string
|
||||
*/
|
||||
std::string utf8_flatten (const std::string& str);
|
||||
std::string utf8_flatten (const char *str);
|
||||
inline std::string utf8_flatten (const std::string& s) { return utf8_flatten(s.c_str()); }
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue