1#include "prism/internal/char.h"
4#include "prism/internal/line_offset_list.h"
6#define PRISM_CHAR_BIT_REGEXP_OPTION (1 << 2)
8#define PRISM_NUMBER_BIT_BINARY_DIGIT (1 << 0)
9#define PRISM_NUMBER_BIT_BINARY_NUMBER (1 << 1)
10#define PRISM_NUMBER_BIT_OCTAL_DIGIT (1 << 2)
11#define PRISM_NUMBER_BIT_OCTAL_NUMBER (1 << 3)
12#define PRISM_NUMBER_BIT_DECIMAL_DIGIT (1 << 4)
13#define PRISM_NUMBER_BIT_DECIMAL_NUMBER (1 << 5)
14#define PRISM_NUMBER_BIT_HEXADECIMAL_DIGIT (1 << 6)
15#define PRISM_NUMBER_BIT_HEXADECIMAL_NUMBER (1 << 7)
17const uint8_t pm_byte_table[256] = {
19 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, 0, 0,
20 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
21 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
22 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
23 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
24 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0,
25 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
26 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 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,
34 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
37static const uint8_t pm_number_table[256] = {
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 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
42 0xff, 0xff, 0xfc, 0xfc, 0xfc, 0xfc, 0xfc, 0xfc, 0xf0, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
43 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
44 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xaa,
45 0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 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,
54 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62pm_strspn_char_kind(
const uint8_t *
string, ptrdiff_t length, uint8_t kind) {
63 if (length <= 0)
return 0;
66 size_t maximum = (size_t) length;
68 while (size < maximum && (pm_byte_table[
string[size]] & kind)) size++;
77pm_strspn_whitespace(
const uint8_t *
string, ptrdiff_t length) {
78 return pm_strspn_char_kind(
string, length, PRISM_CHAR_BIT_WHITESPACE);
86pm_strrspn_whitespace(
const uint8_t *
string, ptrdiff_t length) {
87 if (length <= 0)
return 0;
90 size_t maximum = (size_t) length;
92 while (size < maximum && (pm_byte_table[
string[maximum - size - 1]] & PRISM_CHAR_BIT_WHITESPACE)) size++;
103 if (length <= 0)
return 0;
106 uint32_t maximum = (uint32_t) length;
108 while (size < maximum && (pm_byte_table[
string[size]] & PRISM_CHAR_BIT_WHITESPACE)) {
109 if (
string[size] ==
'\n') {
110 pm_line_offset_list_append(arena, line_offsets, start_offset + size + 1);
124pm_strspn_regexp_option(
const uint8_t *
string, ptrdiff_t length) {
125 return pm_strspn_char_kind(
string, length, PRISM_CHAR_BIT_REGEXP_OPTION);
135pm_strspn_number_kind(
const uint8_t *
string, ptrdiff_t length, uint8_t kind) {
136 if (length <= 0)
return 0;
139 size_t maximum = (size_t) length;
141 while (size < maximum && (pm_number_table[
string[size]] & kind)) size++;
154pm_strspn_number_kind_underscores(
const uint8_t *
string, ptrdiff_t length,
const uint8_t **invalid, uint8_t kind) {
155 if (length <= 0)
return 0;
158 size_t maximum = (size_t) length;
160 bool underscore =
false;
161 while (size < maximum && (pm_number_table[
string[size]] & kind)) {
162 if (
string[size] ==
'_') {
163 if (underscore) *invalid =
string + size;
172 if (size > 0 &&
string[size - 1] ==
'_') *invalid =
string + size - 1;
186pm_strspn_binary_number(
const uint8_t *
string, ptrdiff_t length,
const uint8_t **invalid) {
187 return pm_strspn_number_kind_underscores(
string, length, invalid, PRISM_NUMBER_BIT_BINARY_NUMBER);
200pm_strspn_octal_number(
const uint8_t *
string, ptrdiff_t length,
const uint8_t **invalid) {
201 return pm_strspn_number_kind_underscores(
string, length, invalid, PRISM_NUMBER_BIT_OCTAL_NUMBER);
209pm_strspn_decimal_digit(
const uint8_t *
string, ptrdiff_t length) {
210 return pm_strspn_number_kind(
string, length, PRISM_NUMBER_BIT_DECIMAL_DIGIT);
223pm_strspn_decimal_number(
const uint8_t *
string, ptrdiff_t length,
const uint8_t **invalid) {
224 return pm_strspn_number_kind_underscores(
string, length, invalid, PRISM_NUMBER_BIT_DECIMAL_NUMBER);
233pm_strspn_hexadecimal_digit(
const uint8_t *
string, ptrdiff_t length) {
234 return pm_strspn_number_kind(
string, length, PRISM_NUMBER_BIT_HEXADECIMAL_DIGIT);
247pm_strspn_hexadecimal_number(
const uint8_t *
string, ptrdiff_t length,
const uint8_t **invalid) {
248 return pm_strspn_number_kind_underscores(
string, length, invalid, PRISM_NUMBER_BIT_HEXADECIMAL_NUMBER);
255pm_char_is_number_kind(
const uint8_t b, uint8_t kind) {
256 return (pm_number_table[b] & kind) != 0;
263pm_char_is_binary_digit(
const uint8_t b) {
264 return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_BINARY_DIGIT);
271pm_char_is_octal_digit(
const uint8_t b) {
272 return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_OCTAL_DIGIT);
279pm_char_is_decimal_digit(
const uint8_t b) {
280 return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_DECIMAL_DIGIT);
287pm_char_is_hexadecimal_digit(
const uint8_t b) {
288 return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_HEXADECIMAL_DIGIT);
#define PRISM_INLINE
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
A list of offsets of the start of lines in a string.