Use an InvalidPolicy in utf8::dump and utf8::codepoint_size

Do not throw on invalid codepoints by default, ignore them.
Fixes #2686
This commit is contained in:
Maxime Coste 2019-01-13 18:29:20 +11:00
parent 7dbd9bc1e2
commit 445da8d7bf

View File

@ -109,9 +109,9 @@ ByteCount codepoint_size(char byte)
} }
} }
struct invalid_codepoint{}; template<typename InvalidPolicy = utf8::InvalidPolicy::Pass>
ByteCount codepoint_size(Codepoint cp)
inline ByteCount codepoint_size(Codepoint cp) noexcept(noexcept(InvalidPolicy{}(0)))
{ {
if (cp <= 0x7F) if (cp <= 0x7F)
return 1; return 1;
@ -122,7 +122,10 @@ inline ByteCount codepoint_size(Codepoint cp)
else if (cp <= 0x10FFFF) else if (cp <= 0x10FFFF)
return 4; return 4;
else else
throw invalid_codepoint{}; {
InvalidPolicy{}(cp);
return 0;
}
} }
template<typename Iterator, typename Sentinel> template<typename Iterator, typename Sentinel>
@ -255,7 +258,7 @@ Iterator character_start(Iterator it, const Sentinel& begin) noexcept
return it; return it;
} }
template<typename OutputIterator> template<typename OutputIterator, typename InvalidPolicy = utf8::InvalidPolicy::Pass>
void dump(OutputIterator&& it, Codepoint cp) void dump(OutputIterator&& it, Codepoint cp)
{ {
if (cp <= 0x7F) if (cp <= 0x7F)
@ -279,7 +282,7 @@ void dump(OutputIterator&& it, Codepoint cp)
*it++ = 0x80 | (cp & 0x3F); *it++ = 0x80 | (cp & 0x3F);
} }
else else
throw invalid_codepoint{}; InvalidPolicy{}(cp);
} }
} }