1 #ifndef NVIM_MBYTE_H
2 #define NVIM_MBYTE_H
3 
4 #include <stdbool.h>
5 #include <stdint.h>
6 #include <string.h>
7 
8 #include "nvim/func_attr.h"
9 #include "nvim/iconv.h"
10 #include "nvim/os/os_defs.h"  // For indirect
11 #include "nvim/types.h"  // for char_u
12 
13 /*
14  * Return byte length of character that starts with byte "b".
15  * Returns 1 for a single-byte character.
16  * MB_BYTE2LEN_CHECK() can be used to count a special key as one byte.
17  * Don't call MB_BYTE2LEN(b) with b < 0 or b > 255!
18  */
19 #define MB_BYTE2LEN(b)         utf8len_tab[b]
20 #define MB_BYTE2LEN_CHECK(b)   (((b) < 0 || (b) > 255) ? 1 : utf8len_tab[b])
21 
22 // max length of an unicode char
23 #define MB_MAXCHAR     6
24 
25 // properties used in enc_canon_table[] (first three mutually exclusive)
26 #define ENC_8BIT       0x01
27 #define ENC_DBCS       0x02
28 #define ENC_UNICODE    0x04
29 
30 #define ENC_ENDIAN_B   0x10        // Unicode: Big endian
31 #define ENC_ENDIAN_L   0x20        // Unicode: Little endian
32 
33 #define ENC_2BYTE      0x40        // Unicode: UCS-2
34 #define ENC_4BYTE      0x80        // Unicode: UCS-4
35 #define ENC_2WORD      0x100       // Unicode: UTF-16
36 
37 #define ENC_LATIN1     0x200       // Latin1
38 #define ENC_LATIN9     0x400       // Latin9
39 #define ENC_MACROMAN   0x800       // Mac Roman (not Macro Man! :-)
40 
41 /// Flags for vimconv_T
42 typedef enum {
43   CONV_NONE      = 0,
44   CONV_TO_UTF8   = 1,
45   CONV_9_TO_UTF8 = 2,
46   CONV_TO_LATIN1 = 3,
47   CONV_TO_LATIN9 = 4,
48   CONV_ICONV     = 5,
49 } ConvFlags;
50 
51 #define MBYTE_NONE_CONV { \
52   .vc_type = CONV_NONE, \
53   .vc_factor = 1, \
54   .vc_fail = false, \
55 }
56 
57 /// Structure used for string conversions
58 typedef struct {
59   int vc_type;  ///< Zero or more ConvFlags.
60   int vc_factor;  ///< Maximal expansion factor.
61 #ifdef HAVE_ICONV
62   iconv_t vc_fd;  ///< Value for CONV_ICONV.
63 #endif
64   bool vc_fail;  ///< What to do with invalid characters: if true, fail,
65                  ///< otherwise use '?'.
66 } vimconv_T;
67 
68 extern const uint8_t utf8len_tab_zero[256];
69 
70 extern const uint8_t utf8len_tab[256];
71 
72 #ifdef INCLUDE_GENERATED_DECLARATIONS
73 # include "mbyte.h.generated.h"
74 #endif
75 
76 static inline int mb_strcmp_ic(bool ic, const char *s1, const char *s2)
77   REAL_FATTR_NONNULL_ALL REAL_FATTR_PURE REAL_FATTR_WARN_UNUSED_RESULT;
78 
79 /// Compare strings
80 ///
81 /// @param[in]  ic  True if case is to be ignored.
82 ///
83 /// @return 0 if s1 == s2, <0 if s1 < s2, >0 if s1 > s2.
mb_strcmp_ic(bool ic,const char * s1,const char * s2)84 static inline int mb_strcmp_ic(bool ic, const char *s1, const char *s2)
85 {
86   return (ic ? mb_stricmp(s1, s2) : strcmp(s1, s2));
87 }
88 #endif  // NVIM_MBYTE_H
89