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);
88 if (length <= 0)
return 0;
91 uint32_t maximum = (uint32_t) length;
93 while (size < maximum && (pm_byte_table[
string[size]] & PRISM_CHAR_BIT_WHITESPACE)) {
94 if (
string[size] ==
'\n') {
95 pm_line_offset_list_append(arena, line_offsets, start_offset + size + 1);
109pm_strspn_regexp_option(
const uint8_t *
string, ptrdiff_t length) {
110 return pm_strspn_char_kind(
string, length, PRISM_CHAR_BIT_REGEXP_OPTION);
120pm_strspn_number_kind(
const uint8_t *
string, ptrdiff_t length, uint8_t kind) {
121 if (length <= 0)
return 0;
124 size_t maximum = (size_t) length;
126 while (size < maximum && (pm_number_table[
string[size]] & kind)) size++;
139pm_strspn_number_kind_underscores(
const uint8_t *
string, ptrdiff_t length,
const uint8_t **invalid, uint8_t kind) {
140 if (length <= 0)
return 0;
143 size_t maximum = (size_t) length;
145 bool underscore =
false;
146 while (size < maximum && (pm_number_table[
string[size]] & kind)) {
147 if (
string[size] ==
'_') {
148 if (underscore) *invalid =
string + size;
157 if (size > 0 &&
string[size - 1] ==
'_') *invalid =
string + size - 1;
171pm_strspn_binary_number(
const uint8_t *
string, ptrdiff_t length,
const uint8_t **invalid) {
172 return pm_strspn_number_kind_underscores(
string, length, invalid, PRISM_NUMBER_BIT_BINARY_NUMBER);
185pm_strspn_octal_number(
const uint8_t *
string, ptrdiff_t length,
const uint8_t **invalid) {
186 return pm_strspn_number_kind_underscores(
string, length, invalid, PRISM_NUMBER_BIT_OCTAL_NUMBER);
194pm_strspn_decimal_digit(
const uint8_t *
string, ptrdiff_t length) {
195 return pm_strspn_number_kind(
string, length, PRISM_NUMBER_BIT_DECIMAL_DIGIT);
208pm_strspn_decimal_number(
const uint8_t *
string, ptrdiff_t length,
const uint8_t **invalid) {
209 return pm_strspn_number_kind_underscores(
string, length, invalid, PRISM_NUMBER_BIT_DECIMAL_NUMBER);
218pm_strspn_hexadecimal_digit(
const uint8_t *
string, ptrdiff_t length) {
219 return pm_strspn_number_kind(
string, length, PRISM_NUMBER_BIT_HEXADECIMAL_DIGIT);
232pm_strspn_hexadecimal_number(
const uint8_t *
string, ptrdiff_t length,
const uint8_t **invalid) {
233 return pm_strspn_number_kind_underscores(
string, length, invalid, PRISM_NUMBER_BIT_HEXADECIMAL_NUMBER);
240pm_char_is_number_kind(
const uint8_t b, uint8_t kind) {
241 return (pm_number_table[b] & kind) != 0;
248pm_char_is_binary_digit(
const uint8_t b) {
249 return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_BINARY_DIGIT);
256pm_char_is_octal_digit(
const uint8_t b) {
257 return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_OCTAL_DIGIT);
264pm_char_is_decimal_digit(
const uint8_t b) {
265 return pm_char_is_number_kind(b, PRISM_NUMBER_BIT_DECIMAL_DIGIT);
272pm_char_is_hexadecimal_digit(
const uint8_t b) {
273 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.