kakoune

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

unicode.hh (4055B)


      1 #ifndef unicode_hh_INCLUDED
      2 #define unicode_hh_INCLUDED
      3 
      4 #include <cwctype>
      5 #include <cwchar>
      6 
      7 #include "array_view.hh"
      8 #include "units.hh"
      9 
     10 namespace Kakoune
     11 {
     12 
     13 using Codepoint = char32_t;
     14 
     15 inline bool is_eol(Codepoint c) noexcept
     16 {
     17     return c == '\n';
     18 }
     19 
     20 inline bool is_horizontal_blank(Codepoint c) noexcept
     21 {
     22     // Characters considered whitespace by ECMA Regex Spec
     23     //  minus vertical tab
     24     // <https://262.ecma-international.org/11.0/#sec-white-space>
     25     return c == '\t'      or
     26            c == '\f'      or
     27            c == ' '       or
     28            c == U'\u00A0' or
     29            c == U'\uFEFF' or
     30            c == U'\u1680' or
     31            c == U'\u2000' or
     32            c == U'\u2001' or
     33            c == U'\u2002' or
     34            c == U'\u2003' or
     35            c == U'\u2004' or
     36            c == U'\u2005' or
     37            c == U'\u2006' or
     38            c == U'\u2007' or
     39            c == U'\u2008' or
     40            c == U'\u2009' or
     41            c == U'\u200A' or
     42            c == U'\u2028' or
     43            c == U'\u2029' or
     44            c == U'\u202F' or
     45            c == U'\u205F' or
     46            c == U'\u3000' ;
     47 }
     48 
     49 inline bool is_blank(Codepoint c) noexcept
     50 {
     51     // Characters considered Line Terminators by ECMA Regex Spec
     52     //  plus vertical tab
     53     // <https://262.ecma-international.org/11.0/#sec-line-terminators>
     54     return c == '\n'              or
     55            c == '\r'              or
     56            c == '\v'              or
     57            c == U'\u2028'         or
     58            c == U'\u2029'         or
     59            is_horizontal_blank(c) ;
     60 }
     61 
     62 inline bool is_basic_alpha(Codepoint c) noexcept
     63 {
     64     return (c >= 'a' and c <= 'z') or (c >= 'A' and c <= 'Z');
     65 }
     66 
     67 inline bool is_basic_digit(Codepoint c) noexcept
     68 {
     69     return c >= '0' and c <= '9';
     70 }
     71 
     72 enum WordType { Word, WORD };
     73 
     74 template<WordType word_type = Word>
     75 inline bool is_word(Codepoint c, ConstArrayView<Codepoint> extra_word_chars = {'_'}) noexcept
     76 {
     77     if (c < 128 ? is_basic_alpha(c) or is_basic_digit(c) : iswalnum((wchar_t)c))
     78         return true;
     79     for (auto cp : extra_word_chars)
     80         if (c == cp)
     81             return true;
     82     return false;
     83 }
     84 
     85 template<>
     86 inline bool is_word<WORD>(Codepoint c, ConstArrayView<Codepoint>) noexcept
     87 {
     88     return not is_blank(c);
     89 }
     90 
     91 inline bool is_punctuation(Codepoint c, ConstArrayView<Codepoint> extra_word_chars = {'_'}) noexcept
     92 {
     93     return not (is_word(c, extra_word_chars) or is_blank(c));
     94 }
     95 
     96 inline bool is_identifier(Codepoint c) noexcept
     97 {
     98     return is_basic_alpha(c) or is_basic_digit(c) or
     99            c == '_' or c == '-';
    100 }
    101 
    102 inline ColumnCount codepoint_width(Codepoint c) noexcept
    103 {
    104     if (c < 0x80) [[likely]]
    105         return 1;
    106     const auto width = wcwidth((wchar_t)c);
    107     return width >= 0 ? width : 1;
    108 }
    109 
    110 enum class CharCategories
    111 {
    112     Blank,
    113     EndOfLine,
    114     Word,
    115     Punctuation,
    116 };
    117 
    118 template<WordType word_type = Word>
    119 inline CharCategories categorize(Codepoint c, ConstArrayView<Codepoint> extra_word_chars) noexcept
    120 {
    121     if (is_eol(c))
    122         return CharCategories::EndOfLine;
    123     if (is_horizontal_blank(c))
    124         return CharCategories::Blank;
    125     if (word_type == WORD or is_word(c, extra_word_chars))
    126         return CharCategories::Word;
    127     return CharCategories::Punctuation;
    128 }
    129 
    130 inline char to_lower(char c) noexcept { return c >= 'A' and c <= 'Z' ? c - 'A' + 'a' : c; }
    131 inline char to_upper(char c) noexcept { return c >= 'a' and c <= 'z' ? c - 'a' + 'A' : c; }
    132 
    133 inline bool is_lower(char c) noexcept { return c >= 'a' and c <= 'z'; }
    134 inline bool is_upper(char c) noexcept { return c >= 'A' and c <= 'Z'; }
    135 
    136 inline Codepoint to_lower(Codepoint cp) noexcept { return cp < 128 ? (Codepoint)to_lower((char)cp) : towlower((wchar_t)cp); }
    137 inline Codepoint to_upper(Codepoint cp) noexcept { return cp < 128 ? (Codepoint)to_upper((char)cp) : towupper((wchar_t)cp); }
    138 
    139 inline bool is_lower(Codepoint cp) noexcept { return cp < 128 ? is_lower((char)cp) : iswlower((wchar_t)cp); }
    140 inline bool is_upper(Codepoint cp) noexcept { return cp < 128 ? is_upper((char)cp) : iswupper((wchar_t)cp); }
    141 
    142 }
    143 
    144 #endif // unicode_hh_INCLUDED