kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

word_splitter.hh (1600B)


      1 #ifndef word_splitter_hh_INCLUDED
      2 #define word_splitter_hh_INCLUDED
      3 
      4 #include "string.hh"
      5 #include "array_view.hh"
      6 
      7 namespace Kakoune
      8 {
      9 
     10 struct WordSplitter
     11 {
     12     static constexpr ByteCount max_word_len = 100;
     13 
     14     struct Iterator
     15     {
     16         Iterator(const char* begin, const WordSplitter& splitter)
     17             : m_word_begin{begin}, m_word_end{begin}, m_splitter{&splitter}
     18         { operator++(); }
     19 
     20         StringView operator*() const { return {m_word_begin, m_word_end}; }
     21 
     22         Iterator& operator++()
     23         {
     24             const auto* end = m_splitter->m_content.end();
     25             auto extra_chars = m_splitter->m_extra_word_chars;
     26 
     27             do
     28             {
     29                 auto it = m_word_begin = m_word_end;
     30                 while (it != end and not is_word(utf8::read_codepoint(it, end), extra_chars))
     31                     m_word_begin = it;
     32 
     33                 m_word_end = it;
     34                 while (it != end and is_word(utf8::read_codepoint(it, end), extra_chars))
     35                     m_word_end = it;
     36             } while (m_word_begin != end and (m_word_end - m_word_begin) > max_word_len);
     37 
     38             return *this;
     39         }
     40 
     41         friend bool operator==(const Iterator& lhs, const Iterator& rhs) = default;
     42 
     43         const char* m_word_begin;
     44         const char* m_word_end;
     45         const WordSplitter* m_splitter;
     46     };
     47 
     48     StringView m_content;
     49     ConstArrayView<Codepoint> m_extra_word_chars;
     50 
     51     Iterator begin() const { return {m_content.begin(), *this}; }
     52     Iterator end()   const { return {m_content.end(), *this}; }
     53 };
     54 
     55 }
     56 
     57 #endif // word_splitter_hh_INCLUDED