Here is problem:
There is string (domain) divided by dots (like www.part1.partN.com
)
Need to build reversed by dot string (like com.partN.part1.www
)
Both strings are utf-8 encoded
My solution:
std::string reverseHost(const std::string& host)
{
std::string ret;
ret.reserve(host.size());
size_t tail = host.size();
size_t head = host.find_last_of('.');
while (head != std::string::npos)
{
ret.append(host.c_str() + head + 1, tail - head - 1);
ret += '.';
tail = head;
head = host.find_last_of('.', tail - 1);
}
ret.append(host.c_str(), tail);
return ret;
}
Need to improve speed (or advice how to improve), raw memory and other tricks allowed. ty.