std::string text = \"á\";
\"á\" is two-byte character (assuming a UTF-8 encoding).
So following line prints 2.
Support above Basic Multilingual Plane:
template
std::basic_string create_escapes(const std::basic_string &s)
{
std::basic_string result;
typename std::basic_string::const_iterator b = s.begin();
typename std::basic_string::const_iterator e = s.end();
while (b != e)
{
if (*b == 0x20 || *b == 0x21 || (*b >= 0x23 && *b <= 0x2E) ||
(*b >= 0x30 && *b <= 0x5B) || (*b >= 0x5D && *b <= 0x80))
result += *b;
else if (*b == Ch('\b')) result += Ch('\\'), result += Ch('b');
else if (*b == Ch('\f')) result += Ch('\\'), result += Ch('f');
else if (*b == Ch('\n')) result += Ch('\\'), result += Ch('n');
else if (*b == Ch('\r')) result += Ch('\\'), result += Ch('r');
else if (*b == Ch('/')) result += Ch('\\'), result += Ch('/');
else if (*b == Ch('"')) result += Ch('\\'), result += Ch('"');
else if (*b == Ch('\\')) result += Ch('\\'), result += Ch('\\');
else
{
const char * hexdigits = "0123456789ABCDEF";
typedef typename make_unsigned::type UCh;
unsigned long u = static_cast(static_cast(*b));
if (u <= 0xFFFF)
{
int d1 = u / 4096; u -= d1 * 4096;
int d2 = u / 256; u -= d2 * 256;
int d3 = u / 16; u -= d3 * 16;
int d4 = u;
result += Ch('\\'); result += Ch('u');
result += Ch(hexdigits[d1]); result += Ch(hexdigits[d2]);
result += Ch(hexdigits[d3]); result += Ch(hexdigits[d4]);
}
else
{
u = (((static_cast(static_cast(*b)) - 0x10000) >> 10) & 0x3ff) + 0xd800;
int d1 = u / 4096; u -= d1 * 4096;
int d2 = u / 256; u -= d2 * 256;
int d3 = u / 16; u -= d3 * 16;
int d4 = u;
result += Ch('\\'); result += Ch('u');
result += Ch(hexdigits[d1]); result += Ch(hexdigits[d2]);
result += Ch(hexdigits[d3]); result += Ch(hexdigits[d4]);
u = ((static_cast(static_cast(*b)) - 0x10000) & 0x3ff) + 0xdc00;
d1 = u / 4096; u -= d1 * 4096;
d2 = u / 256; u -= d2 * 256;
d3 = u / 16; u -= d3 * 16;
d4 = u;
result += Ch('\\'); result += Ch('u');
result += Ch(hexdigits[d1]); result += Ch(hexdigits[d2]);
result += Ch(hexdigits[d3]); result += Ch(hexdigits[d4]);
}
}
++b;
}
return result;
}