Make utf8_iterator codepoint type and difference type configurable

This commit is contained in:
Maxime Coste 2016-05-09 21:56:08 +01:00
parent e3cddf3783
commit bff9d45bdb
4 changed files with 14 additions and 12 deletions

View File

@ -311,7 +311,7 @@ Codepoint swap_case(Codepoint cp)
template<Codepoint (*func)(Codepoint)> template<Codepoint (*func)(Codepoint)>
void for_each_codepoint(Context& context, NormalParams) void for_each_codepoint(Context& context, NormalParams)
{ {
using Utf8It = utf8::iterator<BufferIterator, utf8::InvalidPolicy::Pass>; using Utf8It = utf8::iterator<BufferIterator>;
ScopedEdition edition(context); ScopedEdition edition(context);
Buffer& buffer = context.buffer(); Buffer& buffer = context.buffer();

View File

@ -24,7 +24,7 @@ inline Selection target_eol(Selection sel)
return sel; return sel;
} }
using Utf8Iterator = utf8::iterator<BufferIterator, utf8::InvalidPolicy::Pass>; using Utf8Iterator = utf8::iterator<BufferIterator>;
inline Selection utf8_range(const BufferIterator& first, const BufferIterator& last) inline Selection utf8_range(const BufferIterator& first, const BufferIterator& last)
{ {

View File

@ -14,9 +14,11 @@ namespace utf8
// adapter for an iterator on bytes which permits to iterate // adapter for an iterator on bytes which permits to iterate
// on unicode codepoints instead. // on unicode codepoints instead.
template<typename Iterator, template<typename Iterator,
typename CodepointType = Codepoint,
typename DifferenceType = CharCount,
typename InvalidPolicy = utf8::InvalidPolicy::Pass> typename InvalidPolicy = utf8::InvalidPolicy::Pass>
class iterator : public std::iterator<std::bidirectional_iterator_tag, class iterator : public std::iterator<std::bidirectional_iterator_tag,
Codepoint, CharCount> CodepointType, DifferenceType>
{ {
public: public:
iterator() = default; iterator() = default;
@ -58,7 +60,7 @@ public:
return save; return save;
} }
iterator operator+(CharCount count) const iterator operator+(DifferenceType count) const
{ {
if (count < 0) if (count < 0)
return operator-(-count); return operator-(-count);
@ -69,7 +71,7 @@ public:
return res; return res;
} }
iterator operator-(CharCount count) const iterator operator-(DifferenceType count) const
{ {
if (count < 0) if (count < 0)
return operator+(-count); return operator+(-count);
@ -98,12 +100,12 @@ public:
bool operator> (const Iterator& other) const { return m_it > other; } bool operator> (const Iterator& other) const { return m_it > other; }
bool operator>= (const Iterator& other) const { return m_it >= other; } bool operator>= (const Iterator& other) const { return m_it >= other; }
CharCount operator-(const iterator& other) const DifferenceType operator-(const iterator& other) const
{ {
return utf8::distance(other.m_it, m_it); return (DifferenceType)utf8::distance(other.m_it, m_it);
} }
Codepoint operator*() const CodepointType operator*() const
{ {
return get_value(); return get_value();
} }
@ -113,17 +115,17 @@ public:
private: private:
void invalidate_value() { m_value = -1; } void invalidate_value() { m_value = -1; }
Codepoint get_value() const CodepointType get_value() const
{ {
if (m_value == -1) if (m_value == -1)
m_value = utf8::codepoint<InvalidPolicy>(m_it, m_end); m_value = (CodepointType)utf8::codepoint<InvalidPolicy>(m_it, m_end);
return m_value; return m_value;
} }
Iterator m_it; Iterator m_it;
Iterator m_begin; Iterator m_begin;
Iterator m_end; Iterator m_end;
mutable Codepoint m_value = -1; mutable CodepointType m_value = -1;
}; };
} }

View File

@ -13,7 +13,7 @@ using WordList = Vector<StringView>;
static WordList get_words(StringView content) static WordList get_words(StringView content)
{ {
WordList res; WordList res;
using Utf8It = utf8::iterator<const char*, utf8::InvalidPolicy::Pass>; using Utf8It = utf8::iterator<const char*>;
const char* word_start = content.begin(); const char* word_start = content.begin();
bool in_word = false; bool in_word = false;
for (Utf8It it{word_start, content}, end{content.end(), content}; it != end; ++it) for (Utf8It it{word_start, content}, end{content.end(), content}; it != end; ++it)