Advertisement
Brick

GetUTF8CharSize

Sep 25th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. constexpr const unsigned char utf8_lookup_table[256] =
  2. {
  3.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  4.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  5.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  6.     1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  7.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  8.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  9.     2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
  10.     3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,
  11. };
  12.  
  13. constexpr int GetUTF8CharSize(const char c) noexcept
  14. {
  15.     /*
  16.         if ((c & 0x80) == 0x00) // lead bit is zero, must be a single ascii
  17.             return 1;
  18.         if ((c & 0xE0) == 0xC0) // 110x xxxx
  19.             return 2;
  20.         if ((c & 0xF0) == 0xE0) // 1110 xxxx
  21.             return 3;
  22.         if ((c & 0xF8) == 0xF0) // 1111 0xxx
  23.             return 4;
  24.    
  25.         return 0;
  26.     */
  27.  
  28.     return utf8_lookup_table[static_cast<unsigned char>(c)];
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement