Add noexcept specifiers to unicode and utf8 functions

This commit is contained in:
Maxime Coste 2017-04-23 12:47:26 +01:00
parent caed8a55c7
commit e264d189eb
2 changed files with 30 additions and 27 deletions

View File

@ -12,17 +12,17 @@ namespace Kakoune
using Codepoint = char32_t; using Codepoint = char32_t;
inline bool is_eol(Codepoint c) inline bool is_eol(Codepoint c) noexcept
{ {
return c == '\n'; return c == '\n';
} }
inline bool is_horizontal_blank(Codepoint c) inline bool is_horizontal_blank(Codepoint c) noexcept
{ {
return c == ' ' or c == '\t'; return c == ' ' or c == '\t';
} }
inline bool is_blank(Codepoint c) inline bool is_blank(Codepoint c) noexcept
{ {
return c == ' ' or c == '\t' or c == '\n'; return c == ' ' or c == '\t' or c == '\n';
} }
@ -30,28 +30,28 @@ inline bool is_blank(Codepoint c)
enum WordType { Word, WORD }; enum WordType { Word, WORD };
template<WordType word_type = Word> template<WordType word_type = Word>
inline bool is_word(Codepoint c) inline bool is_word(Codepoint c) noexcept
{ {
return c == '_' or iswalnum((wchar_t)c); return c == '_' or iswalnum((wchar_t)c);
} }
template<> template<>
inline bool is_word<WORD>(Codepoint c) inline bool is_word<WORD>(Codepoint c) noexcept
{ {
return not is_blank(c); return not is_blank(c);
} }
inline bool is_punctuation(Codepoint c) inline bool is_punctuation(Codepoint c) noexcept
{ {
return not (is_word(c) or is_blank(c)); return not (is_word(c) or is_blank(c));
} }
inline bool is_basic_alpha(Codepoint c) inline bool is_basic_alpha(Codepoint c) noexcept
{ {
return (c >= 'a' and c <= 'z') or (c >= 'A' and c <= 'Z'); return (c >= 'a' and c <= 'z') or (c >= 'A' and c <= 'Z');
} }
inline ColumnCount codepoint_width(Codepoint c) inline ColumnCount codepoint_width(Codepoint c) noexcept
{ {
return c == '\n' ? 1 : wcwidth((wchar_t)c); return c == '\n' ? 1 : wcwidth((wchar_t)c);
} }
@ -65,7 +65,7 @@ enum class CharCategories
}; };
template<WordType word_type = Word> template<WordType word_type = Word>
inline CharCategories categorize(Codepoint c) inline CharCategories categorize(Codepoint c) noexcept
{ {
if (is_eol(c)) if (is_eol(c))
return CharCategories::EndOfLine; return CharCategories::EndOfLine;
@ -76,11 +76,11 @@ inline CharCategories categorize(Codepoint c)
return CharCategories::Punctuation; return CharCategories::Punctuation;
} }
inline Codepoint to_lower(Codepoint cp) { return towlower((wchar_t)cp); } inline Codepoint to_lower(Codepoint cp) noexcept { return towlower((wchar_t)cp); }
inline Codepoint to_upper(Codepoint cp) { return towupper((wchar_t)cp); } inline Codepoint to_upper(Codepoint cp) noexcept { return towupper((wchar_t)cp); }
inline char to_lower(char c) { return c >= 'A' and c <= 'Z' ? c - 'A' + 'a' : c; } inline char to_lower(char c) noexcept { return c >= 'A' and c <= 'Z' ? c - 'A' + 'a' : c; }
inline char to_upper(char c) { return c >= 'a' and c <= 'z' ? c - 'a' + 'A' : c; } inline char to_upper(char c) noexcept { return c >= 'a' and c <= 'z' ? c - 'a' + 'A' : c; }
} }

View File

@ -15,12 +15,12 @@ namespace utf8
template<typename Iterator> template<typename Iterator>
[[gnu::always_inline]] [[gnu::always_inline]]
inline char read(Iterator& it) { char c = *it; ++it; return c; } inline char read(Iterator& it) noexcept { char c = *it; ++it; return c; }
// return true if it points to the first byte of a (either single or // return true if it points to the first byte of a (either single or
// multibyte) character // multibyte) character
[[gnu::always_inline]] [[gnu::always_inline]]
inline bool is_character_start(char c) inline bool is_character_start(char c) noexcept
{ {
return (c & 0xC0) != 0x80; return (c & 0xC0) != 0x80;
} }
@ -35,7 +35,7 @@ struct Assert
struct Pass struct Pass
{ {
Codepoint operator()(Codepoint cp) const { return cp; } Codepoint operator()(Codepoint cp) const noexcept { return cp; }
}; };
} }
@ -45,6 +45,7 @@ struct Pass
template<typename InvalidPolicy = utf8::InvalidPolicy::Pass, template<typename InvalidPolicy = utf8::InvalidPolicy::Pass,
typename Iterator> typename Iterator>
Codepoint read_codepoint(Iterator& it, const Iterator& end) Codepoint read_codepoint(Iterator& it, const Iterator& end)
noexcept(noexcept(InvalidPolicy{}(0)))
{ {
if (it == end) if (it == end)
return InvalidPolicy{}(-1); return InvalidPolicy{}(-1);
@ -84,12 +85,14 @@ Codepoint read_codepoint(Iterator& it, const Iterator& end)
template<typename InvalidPolicy = utf8::InvalidPolicy::Pass, template<typename InvalidPolicy = utf8::InvalidPolicy::Pass,
typename Iterator> typename Iterator>
Codepoint codepoint(Iterator it, const Iterator& end) Codepoint codepoint(Iterator it, const Iterator& end)
noexcept(noexcept(read_codepoint<InvalidPolicy>(it, end)))
{ {
return read_codepoint(it, end); return read_codepoint<InvalidPolicy>(it, end);
} }
template<typename InvalidPolicy = utf8::InvalidPolicy::Pass> template<typename InvalidPolicy = utf8::InvalidPolicy::Pass>
ByteCount codepoint_size(char byte) ByteCount codepoint_size(char byte)
noexcept(noexcept(InvalidPolicy{}(0)))
{ {
if ((byte & 0x80) == 0) // 0xxxxxxx if ((byte & 0x80) == 0) // 0xxxxxxx
return 1; return 1;
@ -123,7 +126,7 @@ inline ByteCount codepoint_size(Codepoint cp)
} }
template<typename Iterator> template<typename Iterator>
void to_next(Iterator& it, const Iterator& end) void to_next(Iterator& it, const Iterator& end) noexcept
{ {
if (it != end) if (it != end)
++it; ++it;
@ -133,7 +136,7 @@ void to_next(Iterator& it, const Iterator& end)
// returns an iterator to next character first byte // returns an iterator to next character first byte
template<typename Iterator> template<typename Iterator>
Iterator next(Iterator it, const Iterator& end) Iterator next(Iterator it, const Iterator& end) noexcept
{ {
to_next(it, end); to_next(it, end);
return it; return it;
@ -142,7 +145,7 @@ Iterator next(Iterator it, const Iterator& end)
// returns it's parameter if it points to a character first byte, // returns it's parameter if it points to a character first byte,
// or else returns next character first byte // or else returns next character first byte
template<typename Iterator> template<typename Iterator>
Iterator finish(Iterator it, const Iterator& end) Iterator finish(Iterator it, const Iterator& end) noexcept
{ {
while (it != end and (*(it) & 0xC0) == 0x80) while (it != end and (*(it) & 0xC0) == 0x80)
++it; ++it;
@ -150,7 +153,7 @@ Iterator finish(Iterator it, const Iterator& end)
} }
template<typename Iterator> template<typename Iterator>
void to_previous(Iterator& it, const Iterator& begin) void to_previous(Iterator& it, const Iterator& begin) noexcept
{ {
if (it != begin) if (it != begin)
--it; --it;
@ -159,7 +162,7 @@ void to_previous(Iterator& it, const Iterator& begin)
} }
// returns an iterator to the previous character first byte // returns an iterator to the previous character first byte
template<typename Iterator> template<typename Iterator>
Iterator previous(Iterator it, const Iterator& begin) Iterator previous(Iterator it, const Iterator& begin) noexcept
{ {
to_previous(it, begin); to_previous(it, begin);
return it; return it;
@ -169,7 +172,7 @@ Iterator previous(Iterator it, const Iterator& begin)
// dth character after (or before if d < 0) the character // dth character after (or before if d < 0) the character
// pointed by it // pointed by it
template<typename Iterator> template<typename Iterator>
Iterator advance(Iterator it, const Iterator& end, CharCount d) Iterator advance(Iterator it, const Iterator& end, CharCount d) noexcept
{ {
if (it == end) if (it == end)
return it; return it;
@ -191,7 +194,7 @@ Iterator advance(Iterator it, const Iterator& end, CharCount d)
// character at the dth column after (or before if d < 0) // character at the dth column after (or before if d < 0)
// the character pointed by it // the character pointed by it
template<typename Iterator> template<typename Iterator>
Iterator advance(Iterator it, const Iterator& end, ColumnCount d) Iterator advance(Iterator it, const Iterator& end, ColumnCount d) noexcept
{ {
if (it == end) if (it == end)
return it; return it;
@ -220,7 +223,7 @@ Iterator advance(Iterator it, const Iterator& end, ColumnCount d)
// returns the character count between begin and end // returns the character count between begin and end
template<typename Iterator> template<typename Iterator>
CharCount distance(Iterator begin, const Iterator& end) CharCount distance(Iterator begin, const Iterator& end) noexcept
{ {
CharCount dist = 0; CharCount dist = 0;
@ -234,7 +237,7 @@ CharCount distance(Iterator begin, const Iterator& end)
// returns the column count between begin and end // returns the column count between begin and end
template<typename Iterator> template<typename Iterator>
ColumnCount column_distance(Iterator begin, const Iterator& end) ColumnCount column_distance(Iterator begin, const Iterator& end) noexcept
{ {
ColumnCount dist = 0; ColumnCount dist = 0;
@ -245,7 +248,7 @@ ColumnCount column_distance(Iterator begin, const Iterator& end)
// returns an iterator to the first byte of the character it is into // returns an iterator to the first byte of the character it is into
template<typename Iterator> template<typename Iterator>
Iterator character_start(Iterator it, const Iterator& begin) Iterator character_start(Iterator it, const Iterator& begin) noexcept
{ {
while (it != begin and not is_character_start(*it)) while (it != begin and not is_character_start(*it))
--it; --it;