Skip to content

Commit 83f54c2

Browse files
committed
Inline pm_node_list_append, pm_char_is_whitespace, and pm_char_is_inline_whitespace
1 parent dfdc930 commit 83f54c2

3 files changed

Lines changed: 53 additions & 34 deletions

File tree

include/prism/node.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,31 @@
1717
#define PM_NODE_LIST_FOREACH(list, index, node) \
1818
for (size_t index = 0; index < (list)->size && ((node) = (list)->nodes[index]); index++)
1919

20+
/**
21+
* Slow path for pm_node_list_append: grow the list and append the node.
22+
* Do not call directly — use pm_node_list_append instead.
23+
*
24+
* @param arena The arena to allocate from.
25+
* @param list The list to append to.
26+
* @param node The node to append.
27+
*/
28+
void pm_node_list_append_slow(pm_arena_t *arena, pm_node_list_t *list, pm_node_t *node);
29+
2030
/**
2131
* Append a new node onto the end of the node list.
2232
*
2333
* @param arena The arena to allocate from.
2434
* @param list The list to append to.
2535
* @param node The node to append.
2636
*/
27-
void pm_node_list_append(pm_arena_t *arena, pm_node_list_t *list, pm_node_t *node);
37+
static PRISM_FORCE_INLINE void
38+
pm_node_list_append(pm_arena_t *arena, pm_node_list_t *list, pm_node_t *node) {
39+
if (list->size < list->capacity) {
40+
list->nodes[list->size++] = node;
41+
} else {
42+
pm_node_list_append_slow(arena, list, node);
43+
}
44+
}
2845

2946
/**
3047
* Prepend a new node onto the beginning of the node list.

include/prism/util/pm_char.h

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,40 @@
1212
#include <stdbool.h>
1313
#include <stddef.h>
1414

15+
/** Bit flag for whitespace characters in pm_byte_table. */
16+
#define PRISM_CHAR_BIT_WHITESPACE (1 << 0)
17+
18+
/** Bit flag for inline whitespace characters in pm_byte_table. */
19+
#define PRISM_CHAR_BIT_INLINE_WHITESPACE (1 << 1)
20+
21+
/**
22+
* A lookup table for classifying bytes. Each entry is a bitfield of
23+
* PRISM_CHAR_BIT_* flags. Defined in pm_char.c.
24+
*/
25+
extern const uint8_t pm_byte_table[256];
26+
27+
/**
28+
* Returns true if the given character is a whitespace character.
29+
*
30+
* @param b The character to check.
31+
* @return True if the given character is a whitespace character.
32+
*/
33+
static PRISM_FORCE_INLINE bool
34+
pm_char_is_whitespace(const uint8_t b) {
35+
return (pm_byte_table[b] & PRISM_CHAR_BIT_WHITESPACE) != 0;
36+
}
37+
38+
/**
39+
* Returns true if the given character is an inline whitespace character.
40+
*
41+
* @param b The character to check.
42+
* @return True if the given character is an inline whitespace character.
43+
*/
44+
static PRISM_FORCE_INLINE bool
45+
pm_char_is_inline_whitespace(const uint8_t b) {
46+
return (pm_byte_table[b] & PRISM_CHAR_BIT_INLINE_WHITESPACE) != 0;
47+
}
48+
1549
/**
1650
* Returns the number of characters at the start of the string that are
1751
* whitespace. Disallows searching past the given maximum number of characters.
@@ -156,21 +190,6 @@ size_t pm_strspn_regexp_option(const uint8_t *string, ptrdiff_t length);
156190
*/
157191
size_t pm_strspn_binary_number(const uint8_t *string, ptrdiff_t length, const uint8_t **invalid);
158192

159-
/**
160-
* Returns true if the given character is a whitespace character.
161-
*
162-
* @param b The character to check.
163-
* @return True if the given character is a whitespace character.
164-
*/
165-
bool pm_char_is_whitespace(const uint8_t b);
166-
167-
/**
168-
* Returns true if the given character is an inline whitespace character.
169-
*
170-
* @param b The character to check.
171-
* @return True if the given character is an inline whitespace character.
172-
*/
173-
bool pm_char_is_inline_whitespace(const uint8_t b);
174193

175194
/**
176195
* Returns true if the given character is a binary digit.

src/util/pm_char.c

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "prism/util/pm_char.h"
22

3-
#define PRISM_CHAR_BIT_WHITESPACE (1 << 0)
4-
#define PRISM_CHAR_BIT_INLINE_WHITESPACE (1 << 1)
53
#define PRISM_CHAR_BIT_REGEXP_OPTION (1 << 2)
64

75
#define PRISM_NUMBER_BIT_BINARY_DIGIT (1 << 0)
@@ -13,7 +11,7 @@
1311
#define PRISM_NUMBER_BIT_HEXADECIMAL_DIGIT (1 << 6)
1412
#define PRISM_NUMBER_BIT_HEXADECIMAL_NUMBER (1 << 7)
1513

16-
static const uint8_t pm_byte_table[256] = {
14+
const uint8_t pm_byte_table[256] = {
1715
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
1816
0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 3, 3, 0, 0, // 0x
1917
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x
@@ -126,21 +124,6 @@ pm_char_is_char_kind(const uint8_t b, uint8_t kind) {
126124
return (pm_byte_table[b] & kind) != 0;
127125
}
128126

129-
/**
130-
* Returns true if the given character is a whitespace character.
131-
*/
132-
bool
133-
pm_char_is_whitespace(const uint8_t b) {
134-
return pm_char_is_char_kind(b, PRISM_CHAR_BIT_WHITESPACE);
135-
}
136-
137-
/**
138-
* Returns true if the given character is an inline whitespace character.
139-
*/
140-
bool
141-
pm_char_is_inline_whitespace(const uint8_t b) {
142-
return pm_char_is_char_kind(b, PRISM_CHAR_BIT_INLINE_WHITESPACE);
143-
}
144127

145128
/**
146129
* Scan through the string and return the number of characters at the start of

0 commit comments

Comments
 (0)