robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1#pragma once 2 3#include <stdbool.h> 4#include <stdint.h> 5#include <sys/types.h> // IWYU pragma: keep 6#include <utf8proc.h> 7#include <uv.h> // IWYU pragma: keep 8 9#include "nvim/ascii_defs.h" 10#include "nvim/cmdexpand_defs.h" // IWYU pragma: keep 11#include "nvim/eval/typval_defs.h" // IWYU pragma: keep 12#include "nvim/macros_defs.h" 13#include "nvim/mbyte_defs.h" // IWYU pragma: keep 14#include "nvim/option_vars.h" 15#include "nvim/types_defs.h" // IWYU pragma: keep 16 17#define GRAPHEME_STATE_INIT 0 18 19#include "mbyte.h.generated.h" 20#include "mbyte.h.inline.generated.h" 21 22enum { 23kInvalidByteCells = 4 , 24}; 25 26// Return byte length of character that starts with byte "b". 27// Returns 1 for a single-byte character. 28// MB_BYTE2LEN_CHECK() can be used to count a special key as one byte. 29// Don't call MB_BYTE2LEN(b) with b < 0 or b > 255! 30#define MB_BYTE2LEN (b ) utf8len_tab[b] 31#define MB_BYTE2LEN_CHECK (b ) (((b) < 0 || (b) > 255) ? 1 : utf8len_tab[b]) 32 33extern const uint8_t utf8len_tab_zero [256 ]; 34 35extern const uint8_t utf8len_tab [256 ]; 36 37// Use our own character-case definitions, because the current locale may 38// differ from what the .spl file uses. 39// These must not be called with negative number! 40// Multi-byte implementation. For Unicode we can call utf_*(), but don't do 41// that for ASCII, because we don't want to use 'casemap' here. Otherwise use 42// the "w" library function for characters above 255. 43#define SPELL_TOFOLD (c ) ((c) >= 128 ? utf_fold(c) : (int)spelltab.st_fold[c]) 44 45#define SPELL_TOUPPER (c ) ((c) >= 128 ? mb_toupper(c) : (int)spelltab.st_upper[c]) 46 47#define SPELL_ISUPPER (c ) ((c) >= 128 ? mb_isupper(c) : spelltab.st_isu[c]) 48 49// MB_PTR_ADV(): advance a pointer to the next character, taking care of 50// multi-byte characters if needed. Skip over composing chars. 51#define MB_PTR_ADV (p ) (p += utfc_ptr2len((char *)p)) 52 53// MB_PTR_BACK(): backup a pointer to the previous character, taking care of 54// multi-byte characters if needed. Only use with "p" > "s" ! 55#define MB_PTR_BACK (s ,p ) \ 56 (p -= utf_head_off((char *)(s), (char *)(p) - 1) + 1) 57 58/// Check whether a given UTF-8 byte is a trailing byte (10xx.xxxx). 59 60static inline bool utf_is_trail_byte (uint8_t const byte ) 61FUNC_ATTR_CONST FUNC_ATTR_ALWAYS_INLINE 62{ 63// uint8_t is for clang to use smaller cmp 64return (uint8_t )(byte & 0xC0U )== 0x80U ; 65} 66 67/// Convert a UTF-8 byte sequence to a Unicode code point. 68/// Handles ascii, multibyte sequiences and illegal sequences. 69/// 70/// @param[in] p_in String to convert. 71/// 72/// @return information abouth the character. When the sequence is illegal, 73/// "value" is negative, "len" is 1. 74static inline CharInfo utf_ptr2CharInfo (char const * const p_in ) 75FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_ALWAYS_INLINE 76{ 77uint8_t const * const p = (uint8_t const * )p_in ; 78uint8_t const first = * p ; 79if (first < 0x80 ) { 80return (CharInfo ){ .value = first , .len = 1 }; 81 }else { 82int len = utf8len_tab [first ]; 83int32_t const code_point = utf_ptr2CharInfo_impl (p , (uintptr_t )len ); 84if (code_point < 0 ) { 85len = 1 ; 86 } 87return (CharInfo ){ .value = code_point , .len = len }; 88 } 89} 90 91static inline StrCharInfo utf_ptr2StrCharInfo (char * ptr ) 92FUNC_ATTR_NONNULL_ALL FUNC_ATTR_ALWAYS_INLINE FUNC_ATTR_PURE 93{ 94return (StrCharInfo ){ .ptr = ptr , .chr = utf_ptr2CharInfo (ptr ) }; 95} 96 97// only c.value is used here but it is a hint that it is the return value of ptr2CharInfo 98// that is expected 99static inline int basechar_cells_impl (CharInfo c ) 100{ 101return c .value < 0 ?4 : (c .value < 0x80 ?ascii2cells (c .value ) :utf_char2cells (c .value )); 102} 103 104/// Return information about the cluster width next character. 105/// Composing and combining characters are considered a part of the current character. 106/// 107/// @param[in] cur Information about the current character in the string. 108static inline ClusterInfo utf_ClusterInfo (StrCharInfo cur ) 109FUNC_ATTR_NONNULL_ALL FUNC_ATTR_ALWAYS_INLINE FUNC_ATTR_PURE 110{ 111// most of the time time caller should already have checked for NUL, so 112// this will be inlined to nothing. But always be NUL safe. 113if (EXPECT (* cur .ptr == NUL , false)) { 114return (ClusterInfo ) { 115 .next = cur , 116 .cells = dy_escape_width , 117 }; 118 } 119 120uint8_t * next = (uint8_t * )(cur .ptr + cur .chr .len ); 121// handle ASCII case inline 122if (EXPECT (* next < 0x80U , true)) { 123return (ClusterInfo ) { 124 .next = (StrCharInfo ){ 125 .ptr = (char * )next , 126 .chr = (CharInfo ){ .value = * next , .len = 1 }, 127 }, 128 .cells = basechar_cells_impl (cur .chr ) 129 }; 130 } 131return utf_ClusterInfo_impl (cur ); 132} 133 134/// Return number of display cells occupied by ASCII byte "b". 135/// 136/// chars outside of the range 0 <= b <= 127 is considered unprintable. 137/// Use a proper wrapper for multibyte chars depending on the context, like 138/// char2cells() or ptr2cells() 139/// A TAB is counted as two or four cells: "^I" or "<09>". 140/// 141/// @param b 142/// 143/// @return Number of display cells. 144static inline int ascii2cells (int b ) 145FUNC_ATTR_PURE 146{ 147return (b >=' ' && b <='~' ) ?1 :dy_escape_width ; 148}