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:
djcb 2019-03-23 17:00:25 +02:00
parent f9b615c3bb
commit da10f30adf
2 changed files with 13 additions and 18 deletions

View File

@ -99,37 +99,33 @@ gx_utf8_flatten (const gchar *str, gssize len)
} // namespace } // namespace
std::string // gx_utf8_flatten 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; bool is_ascii = true;
std::string s{str}; while (*str && is_ascii) {
for (auto it = s.begin(); it != s.end(); ++it) { is_ascii = *str & 0x80;
if (*it & 0x80) { ++str;
is_ascii = false;
break;
} else
*it = tolower(*it);
} }
if (G_LIKELY(is_ascii)) if (G_LIKELY(is_ascii))
return s; return str;
/////////////////////////////////////////// ///////////////////////////////////////////
// seems we need the big guns // 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) if (!flat)
return {}; return {};
s = flat; std::string s{flat};
g_free (flat); g_free (flat);
return s; return s;
} }
std::string std::string
Mux::utf8_clean (const std::string& dirty) Mux::utf8_clean (const std::string& dirty)
{ {
@ -166,7 +162,6 @@ Mux::split (const std::string& str, const std::string& sepa)
return vec; return vec;
} }
std::string std::string
Mux::quote (const std::string& str) Mux::quote (const std::string& str)
{ {
@ -222,7 +217,6 @@ Mux::date_to_time_t_string (int64_t t)
return buf; return buf;
} }
static std::string static std::string
delta_ymwdhMs (const std::string& expr) 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); return date_to_time_t_string (t);
} }
constexpr const auto SizeFormat = "%010" G_GINT64_FORMAT; constexpr const auto SizeFormat = "%010" G_GINT64_FORMAT;
constexpr const char SizeMin[] = "0000000000"; constexpr const char SizeMin[] = "0000000000";

View File

@ -32,7 +32,9 @@ namespace Mux {
* *
* @return a flattened string * @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()); }
/** /**