Add utf8::codepoint_size function

This commit is contained in:
Maxime Coste 2013-05-30 18:49:50 +02:00
parent 28e127a48a
commit 2d96f853f8

View File

@ -138,6 +138,26 @@ Codepoint codepoint(Iterator it)
return cp; return cp;
} }
template<typename InvalidPolicy = InvalidBytePolicy::Assert,
typename Iterator>
ByteCount codepoint_size(Iterator it)
{
char byte = *it;
if (not (byte & 0x80)) // 0xxxxxxx
return 1;
else if ((byte & 0xE0) == 0xC0) // 110xxxxx
return 2;
else if ((byte & 0xF0) == 0xE0) // 1110xxxx
return 3;
else if ((byte & 0xF8) == 0xF0) // 11110xxx
return 4;
else
{
InvalidPolicy{}(byte);
return -1;
}
}
struct invalid_codepoint{}; struct invalid_codepoint{};
template<typename OutputIterator> template<typename OutputIterator>