Commit 5d634e30 authored by Martin's avatar Martin

Use convert_return_gchar_ptr_to_*() in a couple of ustring methods

First attempt at fixing memory leak in make_valid() (see previous commit)
used make_unique_ptr_gfree() directly because of looking at these methods.
Better to use the helper function.

This actually fixes undefined behavior for normalize() since
g_utf8_normalize() is documented to return NULL if string is not a valid
UTF-8 string. The constructor for std::string, which ustring uses for
storage, that takes a pointer is documented to have undefined behavior if
pointer is NULL. The utility function checks for NULL and uses the default
constructor in that case. (Have not looked at implementation of Glib
functions, and it may be that all std::string implementations Glibmm is
used with handles this case, but good to avoid undefined behavior
regardless, I think.)
parent 490de74a
......@@ -1242,37 +1242,32 @@ ustring::is_ascii() const
ustring
ustring::normalize(NormalizeMode mode) const
{
const auto buf = make_unique_ptr_gfree(
return convert_return_gchar_ptr_to_ustring(
g_utf8_normalize(string_.data(), string_.size(), static_cast<GNormalizeMode>(int(mode))));
return ustring(buf.get());
}
ustring
ustring::uppercase() const
{
const auto buf = make_unique_ptr_gfree(g_utf8_strup(string_.data(), string_.size()));
return ustring(buf.get());
return convert_return_gchar_ptr_to_ustring(g_utf8_strup(string_.data(), string_.size()));
}
ustring
ustring::lowercase() const
{
const auto buf = make_unique_ptr_gfree(g_utf8_strdown(string_.data(), string_.size()));
return ustring(buf.get());
return convert_return_gchar_ptr_to_ustring(g_utf8_strdown(string_.data(), string_.size()));
}
ustring
ustring::casefold() const
{
const auto buf = make_unique_ptr_gfree(g_utf8_casefold(string_.data(), string_.size()));
return ustring(buf.get());
return convert_return_gchar_ptr_to_ustring(g_utf8_casefold(string_.data(), string_.size()));
}
std::string
ustring::collate_key() const
{
const auto buf = make_unique_ptr_gfree(g_utf8_collate_key(string_.data(), string_.size()));
return std::string(buf.get());
return convert_return_gchar_ptr_to_stdstring(g_utf8_collate_key(string_.data(), string_.size()));
}
std::string
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment