3 #define PRISM_CHAR_BIT_WHITESPACE (1 << 0)
4 #define PRISM_CHAR_BIT_INLINE_WHITESPACE (1 << 1)
5 #define PRISM_CHAR_BIT_REGEXP_OPTION (1 << 2)
7 #define PRISM_NUMBER_BIT_BINARY_DIGIT (1 << 0)
8 #define PRISM_NUMBER_BIT_BINARY_NUMBER (1 << 1)
9 #define PRISM_NUMBER_BIT_OCTAL_DIGIT (1 << 2)
10 #define PRISM_NUMBER_BIT_OCTAL_NUMBER (1 << 3)
11 #define PRISM_NUMBER_BIT_DECIMAL_DIGIT (1 << 4)
12 #define PRISM_NUMBER_BIT_DECIMAL_NUMBER (1 << 5)
13 #define PRISM_NUMBER_BIT_HEXADECIMAL_DIGIT (1 << 6)
14 #define PRISM_NUMBER_BIT_HEXADECIMAL_NUMBER (1 << 7)
16 static const uint8_t pm_byte_table[256] = {
18 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, 0, 0,
19 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
20 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
21 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
22 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
23 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0,
24 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
25 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0,
26 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
27 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
28 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
29 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
30 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
31 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
32 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
33 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
36 static const uint8_t pm_number_table[256] = {
38 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
39 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
40 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41 0xff, 0xff, 0xfc, 0xfc, 0xfc, 0xfc, 0xfc, 0xfc, 0xf0, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
42 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa,
44 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61 pm_strspn_char_kind(
const uint8_t *
string, ptrdiff_t length, uint8_t kind) {
62 if (length <= 0)
return 0;
65 size_t maximum = (size_t) length;
67 while (size < maximum && (pm_byte_table[
string[size]] & kind)) size++;
77 return pm_strspn_char_kind(
string, length, PRISM_CHAR_BIT_WHITESPACE);
87 if (length <= 0)
return 0;
90 size_t maximum = (size_t) length;
92 while (size < maximum && (pm_byte_table[
string[size]] & PRISM_CHAR_BIT_WHITESPACE)) {
93 if (
string[size] ==
'\n') {
109 return pm_strspn_char_kind(
string, length, PRISM_CHAR_BIT_INLINE_WHITESPACE);
118 return pm_strspn_char_kind(
string, length, PRISM_CHAR_BIT_REGEXP_OPTION);
125 pm_char_is_char_kind(
const uint8_t b, uint8_t kind) {
126 return (pm_byte_table[b] & kind) != 0;
134 return pm_char_is_char_kind(b, PRISM_CHAR_BIT_WHITESPACE);
142 return pm_char_is_char_kind(b, PRISM_CHAR_BIT_INLINE_WHITESPACE);
151 pm_strspn_number_kind(
const uint8_t *
string, ptrdiff_t length, uint8_t kind) {
152 if (length <= 0)
return 0;
155 size_t maximum = (size_t) length;
157 while (size < maximum && (pm_number_table[
string[size]] & kind)) size++;
170 pm_strspn_number_kind_underscores(
const uint8_t *
string, ptrdiff_t length,
const uint8_t **invalid, uint8_t kind) {
171 if (length <= 0)
return 0;
174 size_t maximum = (size_t) length;
176 bool underscore =
false;
177 while (size < maximum && (pm_number_table[
string[size]] & kind)) {
178 if (
string[size] ==
'_') {
179 if (underscore) *invalid =
string + size;
188 if (size > 0 &&
string[size - 1] ==
'_') *invalid =
string + size - 1;
203 return pm_strspn_number_kind_underscores(
string, length, invalid, PRISM_NUMBER_BIT_BINARY_NUMBER);
217 return pm_strspn_number_kind_underscores(
string, length, invalid, PRISM_NUMBER_BIT_OCTAL_NUMBER);
226 return pm_strspn_number_kind(
string, length, PRISM_NUMBER_BIT_DECIMAL_DIGIT);
240 return pm_strspn_number_kind_underscores(
string, length, invalid, PRISM_NUMBER_BIT_DECIMAL_NUMBER);
250 return pm_strspn_number_kind(
string, length, PRISM_NUMBER_BIT_HEXADECIMAL_DIGIT);
264 return pm_strspn_number_kind_underscores(
string, length, invalid, PRISM_NUMBER_BIT_HEXADECIMAL_NUMBER);
271 pm_char_is_number_kind(
const uint8_t b, uint8_t kind) {
272 return (pm_number_table[b] & kind) != 0;
280 return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_BINARY_DIGIT);
288 return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_OCTAL_DIGIT);
296 return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_DECIMAL_DIGIT);
304 return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_HEXADECIMAL_DIGIT);
307 #undef PRISM_CHAR_BIT_WHITESPACE
308 #undef PRISM_CHAR_BIT_INLINE_WHITESPACE
309 #undef PRISM_CHAR_BIT_REGEXP_OPTION
311 #undef PRISM_NUMBER_BIT_BINARY_DIGIT
312 #undef PRISM_NUMBER_BIT_BINARY_NUMBER
313 #undef PRISM_NUMBER_BIT_OCTAL_DIGIT
314 #undef PRISM_NUMBER_BIT_OCTAL_NUMBER
315 #undef PRISM_NUMBER_BIT_DECIMAL_DIGIT
316 #undef PRISM_NUMBER_BIT_DECIMAL_NUMBER
317 #undef PRISM_NUMBER_BIT_HEXADECIMAL_NUMBER
318 #undef PRISM_NUMBER_BIT_HEXADECIMAL_DIGIT
Functions for working with characters and strings.
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 hexadecimal digits.
bool pm_char_is_decimal_digit(const uint8_t b)
Returns true if the given character is a decimal digit.
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.
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 hexadecimal digits or underscore...
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 decimal digits or underscores.
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 decimal digits.
size_t pm_strspn_binary_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 binary 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 octal digits or underscores.
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 whitespace while also tracking t...
bool pm_char_is_hexadecimal_digit(const uint8_t b)
Returns true if the given character is a hexadecimal digit.
bool pm_char_is_octal_digit(const uint8_t b)
Returns true if the given character is an octal digit.
bool pm_char_is_binary_digit(const uint8_t b)
Returns true if the given character is a binary digit.
bool pm_char_is_inline_whitespace(const uint8_t b)
Returns true if the given character is an inline whitespace character.
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 inline whitespace.
bool pm_char_is_whitespace(const uint8_t b)
Returns true if the given character is a whitespace character.
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 regexp options.
bool pm_newline_list_append(pm_newline_list_t *list, const uint8_t *cursor)
Append a new offset to the newline list.
A list of offsets of newlines in a string.