summaryrefslogtreecommitdiff
path: root/prism/util/pm_char.h
blob: deeafd632177da286bd215ec1847f5ff7cbe2398 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
 * @file pm_char.h
 *
 * Functions for working with characters and strings.
 */
#ifndef PRISM_CHAR_H
#define PRISM_CHAR_H

#include "prism/defines.h"
#include "prism/util/pm_newline_list.h"

#include <stdbool.h>
#include <stddef.h>

/**
 * Returns the number of characters at the start of the string that are
 * whitespace. Disallows searching past the given maximum number of characters.
 *
 * @param string The string to search.
 * @param length The maximum number of characters to search.
 * @return The number of characters at the start of the string that are
 *     whitespace.
 */
size_t pm_strspn_whitespace(const uint8_t *string, ptrdiff_t length);

/**
 * Returns the number of characters at the start of the string that are
 * whitespace while also tracking the location of each newline. Disallows
 * searching past the given maximum number of characters.
 *
 * @param string The string to search.
 * @param length The maximum number of characters to search.
 * @param newline_list The list of newlines to populate.
 * @return The number of characters at the start of the string that are
 *     whitespace.
 */
size_t pm_strspn_whitespace_newlines(const uint8_t *string, ptrdiff_t length, pm_newline_list_t *newline_list);

/**
 * Returns the number of characters at the start of the string that are inline
 * whitespace. Disallows searching past the given maximum number of characters.
 *
 * @param string The string to search.
 * @param length The maximum number of characters to search.
 * @return The number of characters at the start of the string that are inline
 *     whitespace.
 */
size_t pm_strspn_inline_whitespace(const uint8_t *string, ptrdiff_t length);

/**
 * Returns the number of characters at the start of the string that are decimal
 * digits. Disallows searching past the given maximum number of characters.
 *
 * @param string The string to search.
 * @param length The maximum number of characters to search.
 * @return The number of characters at the start of the string that are decimal
 *     digits.
 */
size_t pm_strspn_decimal_digit(const uint8_t *string, ptrdiff_t length);

/**
 * Returns the number of characters at the start of the string that are
 * hexadecimal digits. Disallows searching past the given maximum number of
 * characters.
 *
 * @param string The string to search.
 * @param length The maximum number of characters to search.
 * @return The number of characters at the start of the string that are
 *     hexadecimal digits.
 */
size_t pm_strspn_hexadecimal_digit(const uint8_t *string, ptrdiff_t length);

/**
 * Returns the number of characters at the start of the string that are octal
 * digits or underscores. Disallows searching past the given maximum number of
 * characters.
 *
 * If multiple underscores are found in a row or if an underscore is
 * found at the end of the number, then the invalid pointer is set to the index
 * of the first invalid underscore.
 *
 * @param string The string to search.
 * @param length The maximum number of characters to search.
 * @param invalid The pointer to set to the index of the first invalid
 *     underscore.
 * @return The number of characters at the start of the string that are octal
 *     digits or underscores.
 */
size_t pm_strspn_octal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);

/**
 * Returns the number of characters at the start of the string that are decimal
 * digits or underscores. Disallows searching past the given maximum number of
 * characters.
 *
 * If multiple underscores are found in a row or if an underscore is
 * found at the end of the number, then the invalid pointer is set to the index
 * of the first invalid underscore.
 *
 * @param string The string to search.
 * @param length The maximum number of characters to search.
 * @param invalid The pointer to set to the index of the first invalid
 *     underscore.
 * @return The number of characters at the start of the string that are decimal
 *     digits or underscores.
 */
size_t pm_strspn_decimal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);

/**
 * Returns the number of characters at the start of the string that are
 * hexadecimal digits or underscores. Disallows searching past the given maximum
 * number of characters.
 *
 * If multiple underscores are found in a row or if an underscore is
 * found at the end of the number, then the invalid pointer is set to the index
 * of the first invalid underscore.
 *
 * @param string The string to search.
 * @param length The maximum number of characters to search.
 * @param invalid The pointer to set to the index of the first invalid
 *     underscore.
 * @return The number of characters at the start of the string that are
 *     hexadecimal digits or underscores.
 */
size_t pm_strspn_hexadecimal_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);

/**
 * Returns the number of characters at the start of the string that are regexp
 * options. Disallows searching past the given maximum number of characters.
 *
 * @param string The string to search.
 * @param length The maximum number of characters to search.
 * @return The number of characters at the start of the string that are regexp
 *     options.
 */
size_t pm_strspn_regexp_option(const uint8_t *string, ptrdiff_t length);

/**
 * Returns the number of characters at the start of the string that are binary
 * digits or underscores. Disallows searching past the given maximum number of
 * characters.
 *
 * If multiple underscores are found in a row or if an underscore is
 * found at the end of the number, then the invalid pointer is set to the index
 * of the first invalid underscore.
 *
 * @param string The string to search.
 * @param length The maximum number of characters to search.
 * @param invalid The pointer to set to the index of the first invalid
 *     underscore.
 * @return The number of characters at the start of the string that are binary
 *     digits or underscores.
 */
size_t pm_strspn_binary_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);

/**
 * Returns true if the given character is a whitespace character.
 *
 * @param b The character to check.
 * @return True if the given character is a whitespace character.
 */
bool pm_char_is_whitespace(const uint8_t b);

/**
 * Returns true if the given character is an inline whitespace character.
 *
 * @param b The character to check.
 * @return True if the given character is an inline whitespace character.
 */
bool pm_char_is_inline_whitespace(const uint8_t b);

/**
 * Returns true if the given character is a binary digit.
 *
 * @param b The character to check.
 * @return True if the given character is a binary digit.
 */
bool pm_char_is_binary_digit(const uint8_t b);

/**
 * Returns true if the given character is an octal digit.
 *
 * @param b The character to check.
 * @return True if the given character is an octal digit.
 */
bool pm_char_is_octal_digit(const uint8_t b);

/**
 * Returns true if the given character is a decimal digit.
 *
 * @param b The character to check.
 * @return True if the given character is a decimal digit.
 */
bool pm_char_is_decimal_digit(const uint8_t b);

/**
 * Returns true if the given character is a hexadecimal digit.
 *
 * @param b The character to check.
 * @return True if the given character is a hexadecimal digit.
 */
bool pm_char_is_hexadecimal_digit(const uint8_t b);

#endif