Reduct  v1.0.4-3-gdaf0d70
A functional and immutable language.
Loading...
Searching...
No Matches
char.h
Go to the documentation of this file.
1#ifndef REDUCT_CHAR_H
2#define REDUCT_CHAR_H 1
3
4/**
5 * @file char.h
6 * @brief Character handling.
7 * @defgroup char Characters
8 *
9 * Lookup tables are used to reduce branching and improve performance when parsing and processing characters.
10 *
11 * @{
12 */
13
14/**
15 * @brief Character classification flags.
16 * @enum reduct_char_flags_t
17 *
18 * Follows the grammar defined in the README.
19 */
20typedef enum reduct_char_flags
21{
22 REDUCT_CHAR_LETTER = (1 << 0), ///< Is a letter.
23 REDUCT_CHAR_DIGIT = (1 << 1), ///< Is a decimal digit.
24 REDUCT_CHAR_SYMBOL = (1 << 2), ///< Is a symbol.
25 REDUCT_CHAR_WHITESPACE = (1 << 3), ///< Is whitespace.
26 REDUCT_CHAR_HEX_DIGIT = (1 << 4), ///< Is a hexidecimal digit.
28
29/**
30 * @brief Character information.
31 * @struct reduct_char_info
32 */
33typedef struct reduct_char_info
34{
35 reduct_char_flags_t flags; ///< Character classification flags.
36 char upper; ///< Uppercase equivalent.
37 char lower; ///< Lowercase equivalent.
38 char decodeEscape; ///< The char to decode to when escaped.
39 char encodeEscape; ///< The char to use when encoding an escape.
40 unsigned char integer; ///< Integer value.
42
43/**
44 * @brief Global character lookup table.
45 */
47
48/**
49 * @brief Get the character flags for a given character.
50 *
51 * @param _c The character to get flags for.
52 * @return The character flags.
53 */
54#define REDUCT_CHAR_FLAGS(_c) (reductCharTable[(unsigned char)(_c)].flags)
55
56/**
57 * @brief Get the lowercase equivalent of a character.
58 *
59 * @param _c The character to get the lowercase equivalent of.
60 * @return The lowercase equivalent of the character.
61 */
62#define REDUCT_CHAR_TO_LOWER(_c) (reductCharTable[(unsigned char)(_c)].lower)
63
64/**
65 * @brief Get the uppercase equivalent of a character.
66 *
67 * @param _c The character to get the uppercase equivalent of.
68 * @return The uppercase equivalent of the character.
69 */
70#define REDUCT_CHAR_TO_UPPER(_c) (reductCharTable[(unsigned char)(_c)].upper)
71
72/**
73 * @brief Check if a character is whitespace.
74 *
75 * @param _c The character to check.
76 * @return REDUCT_TRUE if the character is whitespace, REDUCT_FALSE otherwise.
77 */
78#define REDUCT_CHAR_IS_WHITESPACE(_c) (REDUCT_CHAR_FLAGS(_c) & REDUCT_CHAR_WHITESPACE)
79
80/**
81 * @brief Check if a character is a letter.
82 *
83 * @param _c The character to check.
84 * @return REDUCT_TRUE if the character is a letter, REDUCT_FALSE otherwise.
85 */
86#define REDUCT_CHAR_IS_LETTER(_c) (REDUCT_CHAR_FLAGS(_c) & REDUCT_CHAR_LETTER)
87
88/**
89 * @brief Check if a character is a decimal digit.
90 *
91 * @param _c The character to check.
92 * @return REDUCT_TRUE if the character is a decimal digit, REDUCT_FALSE otherwise.
93 */
94#define REDUCT_CHAR_IS_DIGIT(_c) (REDUCT_CHAR_FLAGS(_c) & REDUCT_CHAR_DIGIT)
95
96/**
97 * @brief Check if a character is a symbol.
98 *
99 * @param _c The character to check.
100 * @return REDUCT_TRUE if the character is a symbol, REDUCT_FALSE otherwise.
101 */
102#define REDUCT_CHAR_IS_SYMBOL(_c) (REDUCT_CHAR_FLAGS(_c) & REDUCT_CHAR_SYMBOL)
103
104/**
105 * @brief Check if a character is a hexidecimal digit.
106 *
107 * @param _c The character to check.
108 * @return REDUCT_TRUE if the character is a hexidecimal digit, REDUCT_FALSE otherwise.
109 */
110#define REDUCT_CHAR_IS_HEX_DIGIT(_c) (REDUCT_CHAR_FLAGS(_c) & REDUCT_CHAR_HEX_DIGIT)
111
112/** @} */
113
114#endif
reduct_char_info_t reductCharTable[256]
Global character lookup table.
Definition char_impl.h:6
reduct_char_flags_t
Character classification flags.
Definition char.h:21
@ REDUCT_CHAR_DIGIT
Is a decimal digit.
Definition char.h:23
@ REDUCT_CHAR_LETTER
Is a letter.
Definition char.h:22
@ REDUCT_CHAR_HEX_DIGIT
Is a hexidecimal digit.
Definition char.h:26
@ REDUCT_CHAR_SYMBOL
Is a symbol.
Definition char.h:24
@ REDUCT_CHAR_WHITESPACE
Is whitespace.
Definition char.h:25
reduct_char_flags_t flags
Character classification flags.
Definition char.h:35
char lower
Lowercase equivalent.
Definition char.h:37
char decodeEscape
The char to decode to when escaped.
Definition char.h:38
unsigned char integer
Integer value.
Definition char.h:40
char upper
Uppercase equivalent.
Definition char.h:36
char encodeEscape
The char to use when encoding an escape.
Definition char.h:39
Character information.