I am trying to write some string algorithms which I want to work for any kinds of strings and/or locale. I managed to get some results that do work, but I am not sure that what I am doing is idiomatic or anything. Here is a function:
template<typename String>
auto upper(String str,
const std::ctype<typename String::value_type>& f =
std::use_facet<std::ctype<typename String::value_type>>(std::locale{}))
-> String
{
f.toupper(&str[0], &str[0]+str.size());
return str;
}
I have problems getting around the way C++ works with locales and UTF-8. This algorithm is intended to take a string (well, a range of characters with a standard string interface) and returning a copy of it with all the characters converted to uppercase according to a given locale. If no locale (ctype facet) is given, the last used locale is used.
Is there anyway to improve this code (taking locale
vs facet
) for examples so that it would be the most useful?