ascii_defs.h (4901B)
1 #pragma once 2 3 #include <stdbool.h> 4 5 #include "nvim/os/os_defs.h" 6 7 #include "ascii_defs.h.inline.generated.h" 8 9 // Definitions of various common control characters. 10 11 #define CHAR_ORD(x) ((uint8_t)(x) < 'a' \ 12 ? (uint8_t)(x) - 'A' \ 13 : (uint8_t)(x) - 'a') 14 #define CHAR_ORD_LOW(x) ((uint8_t)(x) - 'a') 15 #define CHAR_ORD_UP(x) ((uint8_t)(x) - 'A') 16 #define ROT13(c, a) (((((c) - (a)) + 13) % 26) + (a)) 17 18 #define NUL '\000' 19 #define BELL '\007' 20 #define BS '\010' 21 #define TAB '\011' 22 #define NL '\012' 23 #define NL_STR "\012" 24 #define FF '\014' 25 #define CAR '\015' // CR is used by Mac OS X 26 #define ESC '\033' 27 #define ESC_STR "\033" 28 #define DEL 0x7f 29 #define DEL_STR "\177" 30 #define CSI 0x9b // Control Sequence Introducer 31 #define CSI_STR "\233" 32 #define DCS 0x90 // Device Control String 33 #define STERM 0x9c // String Terminator 34 35 #define POUND 0xA3 36 37 #define CTRL_CHR(x) (TOUPPER_ASC(x) ^ 0x40) // '?' -> DEL, '@' -> ^@, etc. 38 #define META(x) ((x) | 0x80) 39 40 #define CTRL_F_STR "\006" 41 #define CTRL_H_STR "\010" 42 #define CTRL_V_STR "\026" 43 44 #define Ctrl_AT 0 // @ 45 #define Ctrl_A 1 46 #define Ctrl_B 2 47 #define Ctrl_C 3 48 #define Ctrl_D 4 49 #define Ctrl_E 5 50 #define Ctrl_F 6 51 #define Ctrl_G 7 52 #define Ctrl_H 8 53 #define Ctrl_I 9 54 #define Ctrl_J 10 55 #define Ctrl_K 11 56 #define Ctrl_L 12 57 #define Ctrl_M 13 58 #define Ctrl_N 14 59 #define Ctrl_O 15 60 #define Ctrl_P 16 61 #define Ctrl_Q 17 62 #define Ctrl_R 18 63 #define Ctrl_S 19 64 #define Ctrl_T 20 65 #define Ctrl_U 21 66 #define Ctrl_V 22 67 #define Ctrl_W 23 68 #define Ctrl_X 24 69 #define Ctrl_Y 25 70 #define Ctrl_Z 26 71 // CTRL- [ Left Square Bracket == ESC 72 #define Ctrl_BSL 28 // \ BackSLash 73 #define Ctrl_RSB 29 // ] Right Square Bracket 74 #define Ctrl_HAT 30 // ^ 75 #define Ctrl__ 31 76 77 // Character that separates dir names in a path. 78 #ifdef BACKSLASH_IN_FILENAME 79 # define PATHSEP psepc 80 # define PATHSEPSTR pseps 81 #else 82 # define PATHSEP '/' 83 # define PATHSEPSTR "/" 84 #endif 85 86 /// Checks if `c` is a space or tab character. 87 /// 88 /// @see {ascii_isdigit} 89 static inline bool ascii_iswhite(int c) 90 FUNC_ATTR_CONST FUNC_ATTR_ALWAYS_INLINE 91 { 92 return c == ' ' || c == '\t'; 93 } 94 95 /// Checks if `c` is a space or tab character or NUL. 96 /// 97 /// @see {ascii_isdigit} 98 static inline bool ascii_iswhite_or_nul(int c) 99 FUNC_ATTR_CONST FUNC_ATTR_ALWAYS_INLINE 100 { 101 return ascii_iswhite(c) || c == NUL; 102 } 103 104 /// Checks if `c` is a space or tab or newline character or NUL. 105 /// 106 /// @see {ascii_isdigit} 107 static inline bool ascii_iswhite_nl_or_nul(int c) 108 FUNC_ATTR_CONST FUNC_ATTR_ALWAYS_INLINE 109 { 110 return ascii_iswhite(c) || c == '\n' || c == NUL; 111 } 112 113 /// Check whether character is a decimal digit. 114 /// 115 /// Library isdigit() function is officially locale-dependent and, for 116 /// example, returns true for superscript 1 (¹) in locales where encoding 117 /// contains it in lower 8 bits. Also avoids crashes in case c is below 118 /// 0 or above 255: library functions are officially defined as accepting 119 /// only EOF and unsigned char values (otherwise it is undefined behaviour) 120 /// what may be used for some optimizations (e.g. simple `return 121 /// isdigit_table[c];`). 122 static inline bool ascii_isdigit(int c) 123 FUNC_ATTR_CONST FUNC_ATTR_ALWAYS_INLINE 124 { 125 return c >= '0' && c <= '9'; 126 } 127 128 /// Checks if `c` is a hexadecimal digit, that is, one of 0-9, a-f, A-F. 129 /// 130 /// @see {ascii_isdigit} 131 static inline bool ascii_isxdigit(int c) 132 FUNC_ATTR_CONST FUNC_ATTR_ALWAYS_INLINE 133 { 134 return (c >= '0' && c <= '9') 135 || (c >= 'a' && c <= 'f') 136 || (c >= 'A' && c <= 'F'); 137 } 138 139 /// Checks if `c` is an “identifier” character 140 /// 141 /// That is, whether it is alphanumeric character or underscore. 142 static inline bool ascii_isident(int c) 143 FUNC_ATTR_CONST FUNC_ATTR_ALWAYS_INLINE 144 { 145 return ASCII_ISALNUM(c) || c == '_'; 146 } 147 148 /// Checks if `c` is a binary digit, that is, 0-1. 149 /// 150 /// @see {ascii_isdigit} 151 static inline bool ascii_isbdigit(int c) 152 FUNC_ATTR_CONST FUNC_ATTR_ALWAYS_INLINE 153 { 154 return (c == '0' || c == '1'); 155 } 156 157 /// Checks if `c` is an octal digit, that is, 0-7. 158 /// 159 /// @see {ascii_isdigit} 160 static inline bool ascii_isodigit(int c) 161 FUNC_ATTR_CONST FUNC_ATTR_ALWAYS_INLINE 162 { 163 return (c >= '0' && c <= '7'); 164 } 165 166 /// Checks if `c` is a white-space character, that is, 167 /// one of \f, \n, \r, \t, \v. 168 /// 169 /// @see {ascii_isdigit} 170 static inline bool ascii_isspace(int c) 171 FUNC_ATTR_CONST FUNC_ATTR_ALWAYS_INLINE 172 { 173 return (c >= 9 && c <= 13) || c == ' '; 174 }