@herb-tools/node 0.8.9 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/binding.gyp +26 -8
- package/dist/herb-node.cjs +41 -12
- package/dist/herb-node.cjs.map +1 -1
- package/dist/herb-node.esm.js +8 -1
- package/dist/herb-node.esm.js.map +1 -1
- package/dist/types/node-backend.d.ts +3 -1
- package/extension/error_helpers.cpp +419 -71
- package/extension/error_helpers.h +14 -3
- package/extension/extension_helpers.cpp +38 -35
- package/extension/extension_helpers.h +2 -2
- package/extension/herb.cpp +183 -64
- package/extension/libherb/analyze/action_view/attribute_extraction_helpers.c +290 -0
- package/extension/libherb/analyze/action_view/attribute_extraction_helpers.h +36 -0
- package/extension/libherb/analyze/action_view/content_tag.c +70 -0
- package/extension/libherb/analyze/action_view/link_to.c +143 -0
- package/extension/libherb/analyze/action_view/registry.c +60 -0
- package/extension/libherb/analyze/action_view/tag.c +64 -0
- package/extension/libherb/analyze/action_view/tag_helper_handler.h +41 -0
- package/extension/libherb/analyze/action_view/tag_helper_node_builders.c +305 -0
- package/extension/libherb/analyze/action_view/tag_helper_node_builders.h +70 -0
- package/extension/libherb/analyze/action_view/tag_helpers.c +748 -0
- package/extension/libherb/analyze/action_view/tag_helpers.h +38 -0
- package/extension/libherb/analyze/action_view/turbo_frame_tag.c +88 -0
- package/extension/libherb/analyze/analyze.c +882 -0
- package/extension/libherb/{include → analyze}/analyze.h +14 -4
- package/extension/libherb/{analyzed_ruby.c → analyze/analyzed_ruby.c} +13 -11
- package/extension/libherb/{analyzed_ruby.h → analyze/analyzed_ruby.h} +3 -3
- package/extension/libherb/analyze/builders.c +343 -0
- package/extension/libherb/analyze/builders.h +27 -0
- package/extension/libherb/analyze/conditional_elements.c +594 -0
- package/extension/libherb/analyze/conditional_elements.h +9 -0
- package/extension/libherb/analyze/conditional_open_tags.c +640 -0
- package/extension/libherb/analyze/conditional_open_tags.h +9 -0
- package/extension/libherb/analyze/control_type.c +250 -0
- package/extension/libherb/analyze/control_type.h +14 -0
- package/extension/libherb/{analyze_helpers.c → analyze/helpers.c} +79 -31
- package/extension/libherb/{analyze_helpers.h → analyze/helpers.h} +22 -17
- package/extension/libherb/analyze/invalid_structures.c +193 -0
- package/extension/libherb/analyze/invalid_structures.h +11 -0
- package/extension/libherb/{analyze_missing_end.c → analyze/missing_end.c} +33 -22
- package/extension/libherb/analyze/parse_errors.c +84 -0
- package/extension/libherb/analyze/prism_annotate.c +397 -0
- package/extension/libherb/analyze/prism_annotate.h +16 -0
- package/extension/libherb/{analyze_transform.c → analyze/transform.c} +17 -3
- package/extension/libherb/ast_node.c +17 -7
- package/extension/libherb/ast_node.h +11 -5
- package/extension/libherb/ast_nodes.c +663 -388
- package/extension/libherb/ast_nodes.h +118 -39
- package/extension/libherb/ast_pretty_print.c +191 -7
- package/extension/libherb/ast_pretty_print.h +6 -1
- package/extension/libherb/element_source.h +3 -8
- package/extension/libherb/errors.c +1100 -507
- package/extension/libherb/errors.h +155 -54
- package/extension/libherb/extract.c +148 -49
- package/extension/libherb/extract.h +21 -5
- package/extension/libherb/herb.c +52 -34
- package/extension/libherb/herb.h +18 -6
- package/extension/libherb/herb_prism_node.h +13 -0
- package/extension/libherb/html_util.c +241 -12
- package/extension/libherb/html_util.h +7 -2
- package/extension/libherb/include/analyze/action_view/attribute_extraction_helpers.h +36 -0
- package/extension/libherb/include/analyze/action_view/tag_helper_handler.h +41 -0
- package/extension/libherb/include/analyze/action_view/tag_helper_node_builders.h +70 -0
- package/extension/libherb/include/analyze/action_view/tag_helpers.h +38 -0
- package/extension/libherb/{analyze.h → include/analyze/analyze.h} +14 -4
- package/extension/libherb/include/{analyzed_ruby.h → analyze/analyzed_ruby.h} +3 -3
- package/extension/libherb/include/analyze/builders.h +27 -0
- package/extension/libherb/include/analyze/conditional_elements.h +9 -0
- package/extension/libherb/include/analyze/conditional_open_tags.h +9 -0
- package/extension/libherb/include/analyze/control_type.h +14 -0
- package/extension/libherb/include/{analyze_helpers.h → analyze/helpers.h} +22 -17
- package/extension/libherb/include/analyze/invalid_structures.h +11 -0
- package/extension/libherb/include/analyze/prism_annotate.h +16 -0
- package/extension/libherb/include/ast_node.h +11 -5
- package/extension/libherb/include/ast_nodes.h +118 -39
- package/extension/libherb/include/ast_pretty_print.h +6 -1
- package/extension/libherb/include/element_source.h +3 -8
- package/extension/libherb/include/errors.h +155 -54
- package/extension/libherb/include/extract.h +21 -5
- package/extension/libherb/include/herb.h +18 -6
- package/extension/libherb/include/herb_prism_node.h +13 -0
- package/extension/libherb/include/html_util.h +7 -2
- package/extension/libherb/include/io.h +3 -1
- package/extension/libherb/include/lex_helpers.h +29 -0
- package/extension/libherb/include/lexer.h +1 -1
- package/extension/libherb/include/lexer_peek_helpers.h +87 -13
- package/extension/libherb/include/lexer_struct.h +2 -0
- package/extension/libherb/include/location.h +2 -1
- package/extension/libherb/include/parser.h +27 -2
- package/extension/libherb/include/parser_helpers.h +19 -3
- package/extension/libherb/include/pretty_print.h +10 -5
- package/extension/libherb/include/prism_context.h +45 -0
- package/extension/libherb/include/prism_helpers.h +10 -7
- package/extension/libherb/include/prism_serialized.h +12 -0
- package/extension/libherb/include/token.h +16 -4
- package/extension/libherb/include/token_struct.h +10 -3
- package/extension/libherb/include/utf8.h +2 -1
- package/extension/libherb/include/util/hb_allocator.h +78 -0
- package/extension/libherb/include/util/hb_arena.h +6 -1
- package/extension/libherb/include/util/hb_arena_debug.h +12 -1
- package/extension/libherb/include/util/hb_array.h +7 -3
- package/extension/libherb/include/util/hb_buffer.h +6 -4
- package/extension/libherb/include/util/hb_foreach.h +79 -0
- package/extension/libherb/include/util/hb_narray.h +8 -4
- package/extension/libherb/include/util/hb_string.h +56 -9
- package/extension/libherb/include/util/string.h +11 -0
- package/extension/libherb/include/util.h +6 -3
- package/extension/libherb/include/version.h +1 -1
- package/extension/libherb/io.c +3 -2
- package/extension/libherb/io.h +3 -1
- package/extension/libherb/lex_helpers.h +29 -0
- package/extension/libherb/lexer.c +42 -30
- package/extension/libherb/lexer.h +1 -1
- package/extension/libherb/lexer_peek_helpers.c +12 -74
- package/extension/libherb/lexer_peek_helpers.h +87 -13
- package/extension/libherb/lexer_struct.h +2 -0
- package/extension/libherb/location.c +2 -2
- package/extension/libherb/location.h +2 -1
- package/extension/libherb/main.c +79 -66
- package/extension/libherb/parser.c +784 -247
- package/extension/libherb/parser.h +27 -2
- package/extension/libherb/parser_helpers.c +110 -23
- package/extension/libherb/parser_helpers.h +19 -3
- package/extension/libherb/parser_match_tags.c +110 -49
- package/extension/libherb/pretty_print.c +29 -24
- package/extension/libherb/pretty_print.h +10 -5
- package/extension/libherb/prism_context.h +45 -0
- package/extension/libherb/prism_helpers.c +30 -27
- package/extension/libherb/prism_helpers.h +10 -7
- package/extension/libherb/prism_serialized.h +12 -0
- package/extension/libherb/ruby_parser.c +2 -0
- package/extension/libherb/token.c +151 -66
- package/extension/libherb/token.h +16 -4
- package/extension/libherb/token_matchers.c +0 -1
- package/extension/libherb/token_struct.h +10 -3
- package/extension/libherb/utf8.c +7 -6
- package/extension/libherb/utf8.h +2 -1
- package/extension/libherb/util/hb_allocator.c +341 -0
- package/extension/libherb/util/hb_allocator.h +78 -0
- package/extension/libherb/util/hb_arena.c +81 -56
- package/extension/libherb/util/hb_arena.h +6 -1
- package/extension/libherb/util/hb_arena_debug.c +32 -17
- package/extension/libherb/util/hb_arena_debug.h +12 -1
- package/extension/libherb/util/hb_array.c +30 -15
- package/extension/libherb/util/hb_array.h +7 -3
- package/extension/libherb/util/hb_buffer.c +17 -21
- package/extension/libherb/util/hb_buffer.h +6 -4
- package/extension/libherb/util/hb_foreach.h +79 -0
- package/extension/libherb/util/hb_narray.c +22 -7
- package/extension/libherb/util/hb_narray.h +8 -4
- package/extension/libherb/util/hb_string.c +49 -35
- package/extension/libherb/util/hb_string.h +56 -9
- package/extension/libherb/util/string.h +11 -0
- package/extension/libherb/util.c +21 -11
- package/extension/libherb/util.h +6 -3
- package/extension/libherb/version.h +1 -1
- package/extension/libherb/visitor.c +48 -1
- package/extension/nodes.cpp +451 -6
- package/extension/nodes.h +8 -1
- package/extension/prism/include/prism/ast.h +4 -4
- package/extension/prism/include/prism/version.h +2 -2
- package/extension/prism/src/prism.c +1 -1
- package/package.json +12 -8
- package/src/node-backend.ts +11 -1
- package/dist/types/index-cjs.d.cts +0 -1
- package/extension/libherb/analyze.c +0 -1594
- package/extension/libherb/element_source.c +0 -12
- package/extension/libherb/include/util/hb_system.h +0 -9
- package/extension/libherb/util/hb_system.c +0 -30
- package/extension/libherb/util/hb_system.h +0 -9
- package/src/index-cjs.cts +0 -22
- /package/dist/types/{index-esm.d.mts → index.d.ts} +0 -0
- /package/src/{index-esm.mts → index.ts} +0 -0
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
#include <string.h>
|
|
4
4
|
|
|
5
5
|
#include "../include/util/hb_arena.h"
|
|
6
|
+
#include "../include/util/hb_arena_debug.h"
|
|
6
7
|
|
|
7
8
|
#define ANSI_COLOR_GREEN "\033[32m"
|
|
8
9
|
#define ANSI_COLOR_YELLOW "\033[33m"
|
|
@@ -117,6 +118,29 @@ static void print_box_line_with_bullet_and_color(const char* color, const char*
|
|
|
117
118
|
printf("║%s%*s║\n", line, BOX_WIDTH - visual_length, "");
|
|
118
119
|
}
|
|
119
120
|
|
|
121
|
+
hb_arena_stats_T hb_arena_get_stats(const hb_arena_T* arena) {
|
|
122
|
+
hb_arena_stats_T stats = { 0 };
|
|
123
|
+
|
|
124
|
+
if (arena->head == NULL) { return stats; }
|
|
125
|
+
|
|
126
|
+
stats.default_page_size = arena->default_page_size;
|
|
127
|
+
stats.allocations = arena->allocation_count;
|
|
128
|
+
|
|
129
|
+
hb_arena_for_each_page_const(arena) {
|
|
130
|
+
stats.pages++;
|
|
131
|
+
stats.total_capacity += page->capacity;
|
|
132
|
+
stats.total_used += page->position;
|
|
133
|
+
|
|
134
|
+
if (page != arena->tail && page->position < page->capacity) {
|
|
135
|
+
stats.fragmentation += (page->capacity - page->position);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
stats.total_available = stats.total_capacity - stats.total_used;
|
|
140
|
+
|
|
141
|
+
return stats;
|
|
142
|
+
}
|
|
143
|
+
|
|
120
144
|
void hb_arena_print_stats(const hb_arena_T* allocator) {
|
|
121
145
|
if (allocator->head == NULL) {
|
|
122
146
|
print_box_top();
|
|
@@ -128,22 +152,13 @@ void hb_arena_print_stats(const hb_arena_T* allocator) {
|
|
|
128
152
|
return;
|
|
129
153
|
}
|
|
130
154
|
|
|
131
|
-
|
|
132
|
-
size_t total_capacity = 0;
|
|
133
|
-
size_t total_used = 0;
|
|
134
|
-
size_t fragmentation = 0;
|
|
155
|
+
hb_arena_stats_T stats = hb_arena_get_stats(allocator);
|
|
135
156
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
if (page != allocator->tail && page->position < page->capacity) {
|
|
142
|
-
fragmentation += (page->capacity - page->position);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
157
|
+
size_t total_capacity = stats.total_capacity;
|
|
158
|
+
size_t total_used = stats.total_used;
|
|
159
|
+
size_t total_available = stats.total_available;
|
|
160
|
+
size_t fragmentation = stats.fragmentation;
|
|
145
161
|
|
|
146
|
-
size_t total_available = total_capacity - total_used;
|
|
147
162
|
double usage_percentage = (double) total_used / (double) total_capacity * 100.0;
|
|
148
163
|
double fragmentation_percentage = (double) fragmentation / (double) total_capacity * 100.0;
|
|
149
164
|
const char* overall_color = get_usage_color(usage_percentage);
|
|
@@ -160,7 +175,7 @@ void hb_arena_print_stats(const hb_arena_T* allocator) {
|
|
|
160
175
|
print_box_separator();
|
|
161
176
|
print_box_line(" Statistics:");
|
|
162
177
|
|
|
163
|
-
print_box_line_with_bullet(" • Pages: %zu",
|
|
178
|
+
print_box_line_with_bullet(" • Pages: %zu", stats.pages);
|
|
164
179
|
print_box_line_with_bullet(" • Default Page Size: %s", default_size_string);
|
|
165
180
|
print_box_line_with_bullet(" • Total Capacity: %s", capacity_string);
|
|
166
181
|
print_box_line_with_bullet_and_color(
|
|
@@ -178,7 +193,7 @@ void hb_arena_print_stats(const hb_arena_T* allocator) {
|
|
|
178
193
|
usage_percentage,
|
|
179
194
|
ANSI_COLOR_RESET
|
|
180
195
|
);
|
|
181
|
-
print_box_line_with_bullet(" • Allocations: %zu",
|
|
196
|
+
print_box_line_with_bullet(" • Allocations: %zu", stats.allocations);
|
|
182
197
|
print_box_line_with_bullet(" • Fragmentation: %s", fragmentation_string);
|
|
183
198
|
|
|
184
199
|
if (fragmentation > 0) { print_box_line(" (%.1f%% skipped in non-tail pages)", fragmentation_percentage); }
|
|
@@ -187,7 +202,7 @@ void hb_arena_print_stats(const hb_arena_T* allocator) {
|
|
|
187
202
|
|
|
188
203
|
size_t page_number = 0;
|
|
189
204
|
|
|
190
|
-
|
|
205
|
+
hb_arena_for_each_page_const(allocator) {
|
|
191
206
|
double page_usage = (double) page->position / (double) page->capacity * 100.0;
|
|
192
207
|
const char* page_color = get_usage_color(page_usage);
|
|
193
208
|
|
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
|
|
4
4
|
#include "hb_arena.h"
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
typedef struct {
|
|
7
|
+
size_t pages;
|
|
8
|
+
size_t total_capacity;
|
|
9
|
+
size_t total_used;
|
|
10
|
+
size_t total_available;
|
|
11
|
+
size_t allocations;
|
|
12
|
+
size_t fragmentation;
|
|
13
|
+
size_t default_page_size;
|
|
14
|
+
} hb_arena_stats_T;
|
|
15
|
+
|
|
16
|
+
hb_arena_stats_T hb_arena_get_stats(const hb_arena_T* arena);
|
|
17
|
+
void hb_arena_print_stats(const hb_arena_T* arena);
|
|
7
18
|
|
|
8
19
|
#endif
|
|
@@ -2,28 +2,39 @@
|
|
|
2
2
|
#include <stdio.h>
|
|
3
3
|
|
|
4
4
|
#include "../include/macros.h"
|
|
5
|
+
#include "../include/util/hb_allocator.h"
|
|
5
6
|
#include "../include/util/hb_array.h"
|
|
6
7
|
|
|
7
8
|
size_t hb_array_sizeof(void) {
|
|
8
9
|
return sizeof(hb_array_T);
|
|
9
10
|
}
|
|
10
11
|
|
|
11
|
-
hb_array_T* hb_array_init(const size_t capacity) {
|
|
12
|
-
hb_array_T* array =
|
|
12
|
+
hb_array_T* hb_array_init(const size_t capacity, hb_allocator_T* allocator) {
|
|
13
|
+
hb_array_T* array = hb_allocator_alloc(allocator, hb_array_sizeof());
|
|
14
|
+
|
|
15
|
+
if (!array) { return NULL; }
|
|
13
16
|
|
|
14
17
|
array->size = 0;
|
|
15
18
|
array->capacity = capacity;
|
|
16
|
-
array->
|
|
19
|
+
array->allocator = allocator;
|
|
20
|
+
|
|
21
|
+
if (capacity == 0) {
|
|
22
|
+
array->items = NULL;
|
|
23
|
+
} else {
|
|
24
|
+
array->items = hb_allocator_alloc(allocator, capacity * sizeof(void*));
|
|
17
25
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
26
|
+
if (!array->items) {
|
|
27
|
+
hb_allocator_dealloc(allocator, array);
|
|
28
|
+
return NULL;
|
|
29
|
+
}
|
|
21
30
|
}
|
|
22
31
|
|
|
23
32
|
return array;
|
|
24
33
|
}
|
|
25
34
|
|
|
26
|
-
|
|
35
|
+
bool hb_array_append(hb_array_T* array, void* item) {
|
|
36
|
+
if (!array) { return false; }
|
|
37
|
+
|
|
27
38
|
if (array->size >= array->capacity) {
|
|
28
39
|
size_t new_capacity;
|
|
29
40
|
|
|
@@ -40,20 +51,23 @@ void hb_array_append(hb_array_T* array, void* item) {
|
|
|
40
51
|
|
|
41
52
|
if (new_capacity > SIZE_MAX / sizeof(void*)) {
|
|
42
53
|
fprintf(stderr, "Error: Array allocation would exceed system limits.\n");
|
|
43
|
-
return;
|
|
54
|
+
return false;
|
|
44
55
|
}
|
|
45
56
|
|
|
57
|
+
size_t old_size_bytes = array->capacity * sizeof(void*);
|
|
46
58
|
size_t new_size_bytes = new_capacity * sizeof(void*);
|
|
47
|
-
void
|
|
59
|
+
void** new_items = hb_allocator_realloc(array->allocator, array->items, old_size_bytes, new_size_bytes);
|
|
48
60
|
|
|
49
|
-
if (unlikely(new_items == NULL)) { return; }
|
|
61
|
+
if (unlikely(new_items == NULL)) { return false; }
|
|
50
62
|
|
|
51
|
-
array->items =
|
|
63
|
+
array->items = new_items;
|
|
52
64
|
array->capacity = new_capacity;
|
|
53
65
|
}
|
|
54
66
|
|
|
55
67
|
array->items[array->size] = item;
|
|
56
68
|
array->size++;
|
|
69
|
+
|
|
70
|
+
return true;
|
|
57
71
|
}
|
|
58
72
|
|
|
59
73
|
void* hb_array_get(const hb_array_T* array, const size_t index) {
|
|
@@ -103,8 +117,8 @@ void hb_array_remove_item(hb_array_T* array, void* item) {
|
|
|
103
117
|
}
|
|
104
118
|
|
|
105
119
|
// Alias for hb_array_append
|
|
106
|
-
|
|
107
|
-
hb_array_append(array, item);
|
|
120
|
+
bool hb_array_push(hb_array_T* array, void* item) {
|
|
121
|
+
return hb_array_append(array, item);
|
|
108
122
|
}
|
|
109
123
|
|
|
110
124
|
void* hb_array_pop(hb_array_T* array) {
|
|
@@ -130,8 +144,9 @@ size_t hb_array_capacity(const hb_array_T* array) {
|
|
|
130
144
|
void hb_array_free(hb_array_T** array) {
|
|
131
145
|
if (!array || !*array) { return; }
|
|
132
146
|
|
|
133
|
-
|
|
134
|
-
|
|
147
|
+
hb_allocator_T* allocator = (*array)->allocator;
|
|
148
|
+
hb_allocator_dealloc(allocator, (*array)->items);
|
|
149
|
+
hb_allocator_dealloc(allocator, *array);
|
|
135
150
|
|
|
136
151
|
*array = NULL;
|
|
137
152
|
}
|
|
@@ -1,21 +1,25 @@
|
|
|
1
1
|
#ifndef HERB_ARRAY_H
|
|
2
2
|
#define HERB_ARRAY_H
|
|
3
3
|
|
|
4
|
+
#include <stdbool.h>
|
|
4
5
|
#include <stdlib.h>
|
|
5
6
|
|
|
7
|
+
struct hb_allocator;
|
|
8
|
+
|
|
6
9
|
typedef struct HB_ARRAY_STRUCT {
|
|
7
10
|
void** items;
|
|
8
11
|
size_t size;
|
|
9
12
|
size_t capacity;
|
|
13
|
+
struct hb_allocator* allocator;
|
|
10
14
|
} hb_array_T;
|
|
11
15
|
|
|
12
|
-
hb_array_T* hb_array_init(size_t capacity);
|
|
16
|
+
hb_array_T* hb_array_init(size_t capacity, struct hb_allocator* allocator);
|
|
13
17
|
|
|
14
18
|
void* hb_array_get(const hb_array_T* array, size_t index);
|
|
15
19
|
void* hb_array_first(hb_array_T* array);
|
|
16
20
|
void* hb_array_last(hb_array_T* array);
|
|
17
21
|
|
|
18
|
-
|
|
22
|
+
bool hb_array_append(hb_array_T* array, void* item);
|
|
19
23
|
void hb_array_set(const hb_array_T* array, size_t index, void* item);
|
|
20
24
|
void hb_array_free(hb_array_T** array);
|
|
21
25
|
void hb_array_remove(hb_array_T* array, size_t index);
|
|
@@ -23,7 +27,7 @@ void hb_array_remove(hb_array_T* array, size_t index);
|
|
|
23
27
|
size_t hb_array_index_of(hb_array_T* array, void* item);
|
|
24
28
|
void hb_array_remove_item(hb_array_T* array, void* item);
|
|
25
29
|
|
|
26
|
-
|
|
30
|
+
bool hb_array_push(hb_array_T* array, void* item);
|
|
27
31
|
void* hb_array_pop(hb_array_T* array);
|
|
28
32
|
|
|
29
33
|
size_t hb_array_capacity(const hb_array_T* array);
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
#include <string.h>
|
|
4
4
|
|
|
5
5
|
#include "../include/macros.h"
|
|
6
|
-
#include "../include/util.h"
|
|
6
|
+
#include "../include/util/hb_allocator.h"
|
|
7
7
|
#include "../include/util/hb_buffer.h"
|
|
8
8
|
|
|
9
9
|
static bool hb_buffer_has_capacity(hb_buffer_T* buffer, const size_t required_length) {
|
|
@@ -22,14 +22,8 @@ static bool hb_buffer_resize(hb_buffer_T* buffer, const size_t new_capacity) {
|
|
|
22
22
|
fprintf(stderr, "Error: Buffer capacity would overflow system limits.\n");
|
|
23
23
|
exit(1);
|
|
24
24
|
}
|
|
25
|
-
char* new_value = NULL;
|
|
26
25
|
|
|
27
|
-
|
|
28
|
-
new_value = realloc(buffer->value, new_capacity + 1);
|
|
29
|
-
} else {
|
|
30
|
-
new_value = hb_arena_alloc(buffer->allocator, new_capacity + 1);
|
|
31
|
-
memcpy(new_value, buffer->value, buffer->capacity + 1);
|
|
32
|
-
}
|
|
26
|
+
char* new_value = hb_allocator_realloc(buffer->allocator, buffer->value, buffer->capacity + 1, new_capacity + 1);
|
|
33
27
|
|
|
34
28
|
if (unlikely(new_value == NULL)) {
|
|
35
29
|
fprintf(stderr, "Error: Failed to resize buffer to %zu.\n", new_capacity);
|
|
@@ -66,11 +60,11 @@ static bool hb_buffer_expand_if_needed(hb_buffer_T* buffer, const size_t require
|
|
|
66
60
|
return hb_buffer_resize(buffer, new_capacity);
|
|
67
61
|
}
|
|
68
62
|
|
|
69
|
-
bool hb_buffer_init(hb_buffer_T* buffer, const size_t capacity) {
|
|
70
|
-
buffer->allocator =
|
|
63
|
+
bool hb_buffer_init(hb_buffer_T* buffer, const size_t capacity, struct hb_allocator* allocator) {
|
|
64
|
+
buffer->allocator = allocator;
|
|
71
65
|
buffer->capacity = capacity;
|
|
72
66
|
buffer->length = 0;
|
|
73
|
-
buffer->value =
|
|
67
|
+
buffer->value = hb_allocator_alloc(allocator, sizeof(char) * (buffer->capacity + 1));
|
|
74
68
|
|
|
75
69
|
if (!buffer->value) {
|
|
76
70
|
fprintf(stderr, "Error: Failed to initialize buffer with capacity of %zu.\n", buffer->capacity);
|
|
@@ -83,16 +77,6 @@ bool hb_buffer_init(hb_buffer_T* buffer, const size_t capacity) {
|
|
|
83
77
|
return true;
|
|
84
78
|
}
|
|
85
79
|
|
|
86
|
-
bool hb_buffer_init_arena(hb_buffer_T* buffer, hb_arena_T* allocator, size_t capacity) {
|
|
87
|
-
buffer->allocator = allocator;
|
|
88
|
-
buffer->capacity = capacity;
|
|
89
|
-
buffer->length = 0;
|
|
90
|
-
buffer->value = hb_arena_alloc(allocator, sizeof(char) * (buffer->capacity + 1));
|
|
91
|
-
buffer->value[0] = '\0';
|
|
92
|
-
|
|
93
|
-
return true;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
80
|
char* hb_buffer_value(const hb_buffer_T* buffer) {
|
|
97
81
|
return buffer->value;
|
|
98
82
|
}
|
|
@@ -101,6 +85,10 @@ size_t hb_buffer_length(const hb_buffer_T* buffer) {
|
|
|
101
85
|
return buffer->length;
|
|
102
86
|
}
|
|
103
87
|
|
|
88
|
+
bool hb_buffer_is_empty(const hb_buffer_T* buffer) {
|
|
89
|
+
return buffer->length == 0;
|
|
90
|
+
}
|
|
91
|
+
|
|
104
92
|
size_t hb_buffer_capacity(const hb_buffer_T* buffer) {
|
|
105
93
|
return buffer->capacity;
|
|
106
94
|
}
|
|
@@ -205,6 +193,14 @@ void hb_buffer_concat(hb_buffer_T* destination, hb_buffer_T* source) {
|
|
|
205
193
|
destination->value[destination->length] = '\0';
|
|
206
194
|
}
|
|
207
195
|
|
|
196
|
+
void hb_buffer_free(hb_buffer_T* buffer) {
|
|
197
|
+
if (!buffer) { return; }
|
|
198
|
+
hb_allocator_dealloc(buffer->allocator, buffer->value);
|
|
199
|
+
buffer->value = NULL;
|
|
200
|
+
buffer->length = 0;
|
|
201
|
+
buffer->capacity = 0;
|
|
202
|
+
}
|
|
203
|
+
|
|
208
204
|
void hb_buffer_clear(hb_buffer_T* buffer) {
|
|
209
205
|
buffer->length = 0;
|
|
210
206
|
buffer->value[0] = '\0';
|
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
#ifndef HERB_BUFFER_H
|
|
2
2
|
#define HERB_BUFFER_H
|
|
3
3
|
|
|
4
|
-
#include "hb_arena.h"
|
|
5
4
|
#include "hb_string.h"
|
|
6
5
|
|
|
7
6
|
#include <stdbool.h>
|
|
8
7
|
#include <stdlib.h>
|
|
9
8
|
|
|
9
|
+
struct hb_allocator;
|
|
10
|
+
|
|
10
11
|
typedef struct HB_BUFFER_STRUCT {
|
|
11
|
-
|
|
12
|
+
struct hb_allocator* allocator;
|
|
12
13
|
char* value;
|
|
13
14
|
size_t length;
|
|
14
15
|
size_t capacity;
|
|
15
16
|
} hb_buffer_T;
|
|
16
17
|
|
|
17
|
-
bool hb_buffer_init(hb_buffer_T* buffer, size_t capacity);
|
|
18
|
-
bool hb_buffer_init_arena(hb_buffer_T* buffer, hb_arena_T* allocator, size_t capacity);
|
|
18
|
+
bool hb_buffer_init(hb_buffer_T* buffer, size_t capacity, struct hb_allocator* allocator);
|
|
19
19
|
|
|
20
20
|
void hb_buffer_append(hb_buffer_T* buffer, const char* text);
|
|
21
21
|
void hb_buffer_append_with_length(hb_buffer_T* buffer, const char* text, size_t length);
|
|
@@ -28,9 +28,11 @@ void hb_buffer_concat(hb_buffer_T* destination, hb_buffer_T* source);
|
|
|
28
28
|
char* hb_buffer_value(const hb_buffer_T* buffer);
|
|
29
29
|
|
|
30
30
|
size_t hb_buffer_length(const hb_buffer_T* buffer);
|
|
31
|
+
bool hb_buffer_is_empty(const hb_buffer_T* buffer);
|
|
31
32
|
size_t hb_buffer_capacity(const hb_buffer_T* buffer);
|
|
32
33
|
size_t hb_buffer_sizeof(void);
|
|
33
34
|
|
|
35
|
+
void hb_buffer_free(hb_buffer_T* buffer);
|
|
34
36
|
void hb_buffer_clear(hb_buffer_T* buffer);
|
|
35
37
|
|
|
36
38
|
#endif
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// NOTE: This file is generated by the templates/template.rb script and should not
|
|
2
|
+
// be modified manually. See /Users/marcoroth/Development/herb-release-0.9.0/templates/src/include/util/hb_foreach.h.erb
|
|
3
|
+
|
|
4
|
+
#ifndef HB_FOREACH_H
|
|
5
|
+
#define HB_FOREACH_H
|
|
6
|
+
|
|
7
|
+
#define HB_ARG64(_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,count,...) count
|
|
8
|
+
#define HB_NARGS(...) HB_ARG64(__VA_ARGS__,64,63,62,61,60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1)
|
|
9
|
+
|
|
10
|
+
#define HB_FOR_EACH_1(macro, head) macro(head)
|
|
11
|
+
#define HB_FOR_EACH_2(macro, head, ...) macro(head), HB_FOR_EACH_1(macro, __VA_ARGS__)
|
|
12
|
+
#define HB_FOR_EACH_3(macro, head, ...) macro(head), HB_FOR_EACH_2(macro, __VA_ARGS__)
|
|
13
|
+
#define HB_FOR_EACH_4(macro, head, ...) macro(head), HB_FOR_EACH_3(macro, __VA_ARGS__)
|
|
14
|
+
#define HB_FOR_EACH_5(macro, head, ...) macro(head), HB_FOR_EACH_4(macro, __VA_ARGS__)
|
|
15
|
+
#define HB_FOR_EACH_6(macro, head, ...) macro(head), HB_FOR_EACH_5(macro, __VA_ARGS__)
|
|
16
|
+
#define HB_FOR_EACH_7(macro, head, ...) macro(head), HB_FOR_EACH_6(macro, __VA_ARGS__)
|
|
17
|
+
#define HB_FOR_EACH_8(macro, head, ...) macro(head), HB_FOR_EACH_7(macro, __VA_ARGS__)
|
|
18
|
+
#define HB_FOR_EACH_9(macro, head, ...) macro(head), HB_FOR_EACH_8(macro, __VA_ARGS__)
|
|
19
|
+
#define HB_FOR_EACH_10(macro, head, ...) macro(head), HB_FOR_EACH_9(macro, __VA_ARGS__)
|
|
20
|
+
#define HB_FOR_EACH_11(macro, head, ...) macro(head), HB_FOR_EACH_10(macro, __VA_ARGS__)
|
|
21
|
+
#define HB_FOR_EACH_12(macro, head, ...) macro(head), HB_FOR_EACH_11(macro, __VA_ARGS__)
|
|
22
|
+
#define HB_FOR_EACH_13(macro, head, ...) macro(head), HB_FOR_EACH_12(macro, __VA_ARGS__)
|
|
23
|
+
#define HB_FOR_EACH_14(macro, head, ...) macro(head), HB_FOR_EACH_13(macro, __VA_ARGS__)
|
|
24
|
+
#define HB_FOR_EACH_15(macro, head, ...) macro(head), HB_FOR_EACH_14(macro, __VA_ARGS__)
|
|
25
|
+
#define HB_FOR_EACH_16(macro, head, ...) macro(head), HB_FOR_EACH_15(macro, __VA_ARGS__)
|
|
26
|
+
#define HB_FOR_EACH_17(macro, head, ...) macro(head), HB_FOR_EACH_16(macro, __VA_ARGS__)
|
|
27
|
+
#define HB_FOR_EACH_18(macro, head, ...) macro(head), HB_FOR_EACH_17(macro, __VA_ARGS__)
|
|
28
|
+
#define HB_FOR_EACH_19(macro, head, ...) macro(head), HB_FOR_EACH_18(macro, __VA_ARGS__)
|
|
29
|
+
#define HB_FOR_EACH_20(macro, head, ...) macro(head), HB_FOR_EACH_19(macro, __VA_ARGS__)
|
|
30
|
+
#define HB_FOR_EACH_21(macro, head, ...) macro(head), HB_FOR_EACH_20(macro, __VA_ARGS__)
|
|
31
|
+
#define HB_FOR_EACH_22(macro, head, ...) macro(head), HB_FOR_EACH_21(macro, __VA_ARGS__)
|
|
32
|
+
#define HB_FOR_EACH_23(macro, head, ...) macro(head), HB_FOR_EACH_22(macro, __VA_ARGS__)
|
|
33
|
+
#define HB_FOR_EACH_24(macro, head, ...) macro(head), HB_FOR_EACH_23(macro, __VA_ARGS__)
|
|
34
|
+
#define HB_FOR_EACH_25(macro, head, ...) macro(head), HB_FOR_EACH_24(macro, __VA_ARGS__)
|
|
35
|
+
#define HB_FOR_EACH_26(macro, head, ...) macro(head), HB_FOR_EACH_25(macro, __VA_ARGS__)
|
|
36
|
+
#define HB_FOR_EACH_27(macro, head, ...) macro(head), HB_FOR_EACH_26(macro, __VA_ARGS__)
|
|
37
|
+
#define HB_FOR_EACH_28(macro, head, ...) macro(head), HB_FOR_EACH_27(macro, __VA_ARGS__)
|
|
38
|
+
#define HB_FOR_EACH_29(macro, head, ...) macro(head), HB_FOR_EACH_28(macro, __VA_ARGS__)
|
|
39
|
+
#define HB_FOR_EACH_30(macro, head, ...) macro(head), HB_FOR_EACH_29(macro, __VA_ARGS__)
|
|
40
|
+
#define HB_FOR_EACH_31(macro, head, ...) macro(head), HB_FOR_EACH_30(macro, __VA_ARGS__)
|
|
41
|
+
#define HB_FOR_EACH_32(macro, head, ...) macro(head), HB_FOR_EACH_31(macro, __VA_ARGS__)
|
|
42
|
+
#define HB_FOR_EACH_33(macro, head, ...) macro(head), HB_FOR_EACH_32(macro, __VA_ARGS__)
|
|
43
|
+
#define HB_FOR_EACH_34(macro, head, ...) macro(head), HB_FOR_EACH_33(macro, __VA_ARGS__)
|
|
44
|
+
#define HB_FOR_EACH_35(macro, head, ...) macro(head), HB_FOR_EACH_34(macro, __VA_ARGS__)
|
|
45
|
+
#define HB_FOR_EACH_36(macro, head, ...) macro(head), HB_FOR_EACH_35(macro, __VA_ARGS__)
|
|
46
|
+
#define HB_FOR_EACH_37(macro, head, ...) macro(head), HB_FOR_EACH_36(macro, __VA_ARGS__)
|
|
47
|
+
#define HB_FOR_EACH_38(macro, head, ...) macro(head), HB_FOR_EACH_37(macro, __VA_ARGS__)
|
|
48
|
+
#define HB_FOR_EACH_39(macro, head, ...) macro(head), HB_FOR_EACH_38(macro, __VA_ARGS__)
|
|
49
|
+
#define HB_FOR_EACH_40(macro, head, ...) macro(head), HB_FOR_EACH_39(macro, __VA_ARGS__)
|
|
50
|
+
#define HB_FOR_EACH_41(macro, head, ...) macro(head), HB_FOR_EACH_40(macro, __VA_ARGS__)
|
|
51
|
+
#define HB_FOR_EACH_42(macro, head, ...) macro(head), HB_FOR_EACH_41(macro, __VA_ARGS__)
|
|
52
|
+
#define HB_FOR_EACH_43(macro, head, ...) macro(head), HB_FOR_EACH_42(macro, __VA_ARGS__)
|
|
53
|
+
#define HB_FOR_EACH_44(macro, head, ...) macro(head), HB_FOR_EACH_43(macro, __VA_ARGS__)
|
|
54
|
+
#define HB_FOR_EACH_45(macro, head, ...) macro(head), HB_FOR_EACH_44(macro, __VA_ARGS__)
|
|
55
|
+
#define HB_FOR_EACH_46(macro, head, ...) macro(head), HB_FOR_EACH_45(macro, __VA_ARGS__)
|
|
56
|
+
#define HB_FOR_EACH_47(macro, head, ...) macro(head), HB_FOR_EACH_46(macro, __VA_ARGS__)
|
|
57
|
+
#define HB_FOR_EACH_48(macro, head, ...) macro(head), HB_FOR_EACH_47(macro, __VA_ARGS__)
|
|
58
|
+
#define HB_FOR_EACH_49(macro, head, ...) macro(head), HB_FOR_EACH_48(macro, __VA_ARGS__)
|
|
59
|
+
#define HB_FOR_EACH_50(macro, head, ...) macro(head), HB_FOR_EACH_49(macro, __VA_ARGS__)
|
|
60
|
+
#define HB_FOR_EACH_51(macro, head, ...) macro(head), HB_FOR_EACH_50(macro, __VA_ARGS__)
|
|
61
|
+
#define HB_FOR_EACH_52(macro, head, ...) macro(head), HB_FOR_EACH_51(macro, __VA_ARGS__)
|
|
62
|
+
#define HB_FOR_EACH_53(macro, head, ...) macro(head), HB_FOR_EACH_52(macro, __VA_ARGS__)
|
|
63
|
+
#define HB_FOR_EACH_54(macro, head, ...) macro(head), HB_FOR_EACH_53(macro, __VA_ARGS__)
|
|
64
|
+
#define HB_FOR_EACH_55(macro, head, ...) macro(head), HB_FOR_EACH_54(macro, __VA_ARGS__)
|
|
65
|
+
#define HB_FOR_EACH_56(macro, head, ...) macro(head), HB_FOR_EACH_55(macro, __VA_ARGS__)
|
|
66
|
+
#define HB_FOR_EACH_57(macro, head, ...) macro(head), HB_FOR_EACH_56(macro, __VA_ARGS__)
|
|
67
|
+
#define HB_FOR_EACH_58(macro, head, ...) macro(head), HB_FOR_EACH_57(macro, __VA_ARGS__)
|
|
68
|
+
#define HB_FOR_EACH_59(macro, head, ...) macro(head), HB_FOR_EACH_58(macro, __VA_ARGS__)
|
|
69
|
+
#define HB_FOR_EACH_60(macro, head, ...) macro(head), HB_FOR_EACH_59(macro, __VA_ARGS__)
|
|
70
|
+
#define HB_FOR_EACH_61(macro, head, ...) macro(head), HB_FOR_EACH_60(macro, __VA_ARGS__)
|
|
71
|
+
#define HB_FOR_EACH_62(macro, head, ...) macro(head), HB_FOR_EACH_61(macro, __VA_ARGS__)
|
|
72
|
+
#define HB_FOR_EACH_63(macro, head, ...) macro(head), HB_FOR_EACH_62(macro, __VA_ARGS__)
|
|
73
|
+
#define HB_FOR_EACH_64(macro, head, ...) macro(head), HB_FOR_EACH_63(macro, __VA_ARGS__)
|
|
74
|
+
|
|
75
|
+
#define HB_CONCAT(left, right) left ## right
|
|
76
|
+
#define HB_FOR_EACH_N(count) HB_CONCAT(HB_FOR_EACH_, count)
|
|
77
|
+
#define HB_FOR_EACH(macro, ...) HB_FOR_EACH_N(HB_NARGS(__VA_ARGS__))(macro, __VA_ARGS__)
|
|
78
|
+
|
|
79
|
+
#endif
|
|
@@ -4,25 +4,40 @@
|
|
|
4
4
|
#include <stdbool.h>
|
|
5
5
|
#include <string.h>
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
bool hb_narray_init(hb_narray_T* array, size_t item_size, size_t initial_capacity, hb_allocator_T* allocator) {
|
|
8
8
|
assert(initial_capacity != 0);
|
|
9
9
|
|
|
10
|
+
array->allocator = allocator;
|
|
10
11
|
array->item_size = item_size;
|
|
11
12
|
array->capacity = initial_capacity;
|
|
12
13
|
array->size = 0;
|
|
13
|
-
array->items =
|
|
14
|
+
array->items = hb_allocator_alloc(array->allocator, array->capacity * array->item_size);
|
|
15
|
+
|
|
16
|
+
if (!array->items) { return false; }
|
|
17
|
+
|
|
18
|
+
return true;
|
|
14
19
|
}
|
|
15
20
|
|
|
16
|
-
|
|
21
|
+
bool hb_narray_append(hb_narray_T* array, void* item) {
|
|
17
22
|
if (array->size + 1 > array->capacity) {
|
|
18
|
-
array->capacity
|
|
19
|
-
void* new_buffer =
|
|
20
|
-
|
|
23
|
+
size_t new_capacity = array->capacity * 2;
|
|
24
|
+
void* new_buffer = hb_allocator_realloc(
|
|
25
|
+
array->allocator,
|
|
26
|
+
array->items,
|
|
27
|
+
array->capacity * array->item_size,
|
|
28
|
+
new_capacity * array->item_size
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
if (!new_buffer) { return false; }
|
|
32
|
+
|
|
21
33
|
array->items = new_buffer;
|
|
34
|
+
array->capacity = new_capacity;
|
|
22
35
|
}
|
|
23
36
|
|
|
24
37
|
memcpy(array->items + (array->size * array->item_size), item, array->item_size);
|
|
25
38
|
array->size += 1;
|
|
39
|
+
|
|
40
|
+
return true;
|
|
26
41
|
}
|
|
27
42
|
|
|
28
43
|
static inline uint8_t* hb_narray_memory_position(const hb_narray_T* array, size_t index) {
|
|
@@ -71,7 +86,7 @@ void hb_narray_deinit(hb_narray_T* array) {
|
|
|
71
86
|
array->item_size = 0;
|
|
72
87
|
array->capacity = 0;
|
|
73
88
|
array->size = 0;
|
|
74
|
-
|
|
89
|
+
hb_allocator_dealloc(array->allocator, array->items);
|
|
75
90
|
}
|
|
76
91
|
|
|
77
92
|
size_t hb_narray_size(const hb_narray_T* array) {
|
|
@@ -1,26 +1,30 @@
|
|
|
1
1
|
#ifndef HERB_NARRAY_H
|
|
2
2
|
#define HERB_NARRAY_H
|
|
3
3
|
|
|
4
|
+
#include <stdbool.h>
|
|
4
5
|
#include <stdint.h>
|
|
5
6
|
#include <stdlib.h>
|
|
6
|
-
|
|
7
|
+
|
|
8
|
+
#include "hb_allocator.h"
|
|
7
9
|
|
|
8
10
|
typedef struct HB_NARRAY_STRUCT {
|
|
11
|
+
hb_allocator_T* allocator;
|
|
9
12
|
uint8_t* items;
|
|
10
13
|
size_t item_size;
|
|
11
14
|
size_t size;
|
|
12
15
|
size_t capacity;
|
|
13
16
|
} hb_narray_T;
|
|
14
17
|
|
|
15
|
-
|
|
16
|
-
#define hb_narray_pointer_init(array, initial_capacity
|
|
18
|
+
bool hb_narray_init(hb_narray_T* array, size_t item_size, size_t initial_capacity, hb_allocator_T* allocator);
|
|
19
|
+
#define hb_narray_pointer_init(array, initial_capacity, allocator) \
|
|
20
|
+
(hb_narray_init(array, sizeof(void*), initial_capacity, allocator))
|
|
17
21
|
|
|
18
22
|
void* hb_narray_get(const hb_narray_T* array, size_t index);
|
|
19
23
|
void* hb_narray_first(hb_narray_T* array);
|
|
20
24
|
void* hb_narray_last(hb_narray_T* array);
|
|
21
25
|
size_t hb_narray_size(const hb_narray_T* array);
|
|
22
26
|
|
|
23
|
-
|
|
27
|
+
bool hb_narray_append(hb_narray_T* array, void* item);
|
|
24
28
|
void hb_narray_remove(hb_narray_T* array, size_t index);
|
|
25
29
|
void hb_narray_deinit(hb_narray_T* array);
|
|
26
30
|
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
#include "../include/util/hb_string.h"
|
|
2
2
|
#include "../include/macros.h"
|
|
3
|
+
#include "../include/util.h"
|
|
3
4
|
|
|
4
5
|
#include <stdlib.h>
|
|
5
6
|
#include <string.h>
|
|
6
7
|
#include <strings.h>
|
|
7
8
|
|
|
8
|
-
hb_string_T
|
|
9
|
+
hb_string_T hb_string_from_c_string(const char* null_terminated_c_string) {
|
|
10
|
+
if (null_terminated_c_string == NULL) { return HB_STRING_NULL; }
|
|
11
|
+
|
|
9
12
|
hb_string_T string;
|
|
10
13
|
|
|
11
14
|
string.data = (char*) null_terminated_c_string;
|
|
@@ -14,61 +17,72 @@ hb_string_T hb_string(const char* null_terminated_c_string) {
|
|
|
14
17
|
return string;
|
|
15
18
|
}
|
|
16
19
|
|
|
17
|
-
hb_string_T
|
|
18
|
-
hb_string_T
|
|
19
|
-
if (string.length < offset) {
|
|
20
|
-
slice.data = NULL;
|
|
21
|
-
slice.length = 0;
|
|
22
|
-
|
|
23
|
-
return slice;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
slice.data = string.data + offset;
|
|
27
|
-
slice.length = string.length - offset;
|
|
20
|
+
hb_string_T hb_string_truncate(hb_string_T string, uint32_t max_length) {
|
|
21
|
+
hb_string_T truncated_string = { .data = string.data, .length = MIN(string.length, max_length) };
|
|
28
22
|
|
|
29
|
-
return
|
|
23
|
+
return truncated_string;
|
|
30
24
|
}
|
|
31
25
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return strncmp(a.data, b.data, a.length) == 0;
|
|
26
|
+
hb_string_T hb_string_range(hb_string_T string, uint32_t from, uint32_t to) {
|
|
27
|
+
return hb_string_truncate(hb_string_slice(string, from), to - from);
|
|
36
28
|
}
|
|
37
29
|
|
|
38
|
-
|
|
39
|
-
if (
|
|
30
|
+
hb_string_T hb_string_trim_start(hb_string_T string) {
|
|
31
|
+
if (hb_string_is_empty(string)) { return string; }
|
|
40
32
|
|
|
41
|
-
|
|
33
|
+
uint32_t offset = 0;
|
|
34
|
+
while (offset < string.length && is_whitespace(string.data[offset])) {
|
|
35
|
+
offset++;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return hb_string_slice(string, offset);
|
|
42
39
|
}
|
|
43
40
|
|
|
44
|
-
|
|
45
|
-
if (hb_string_is_empty(string)
|
|
46
|
-
|
|
41
|
+
hb_string_T hb_string_trim_end(hb_string_T string) {
|
|
42
|
+
if (hb_string_is_empty(string)) { return string; }
|
|
43
|
+
|
|
44
|
+
uint32_t length = string.length;
|
|
45
|
+
while (length > 0 && is_whitespace(string.data[length - 1])) {
|
|
46
|
+
length--;
|
|
47
|
+
}
|
|
47
48
|
|
|
48
|
-
return
|
|
49
|
+
return hb_string_truncate(string, length);
|
|
49
50
|
}
|
|
50
51
|
|
|
51
|
-
|
|
52
|
-
return string
|
|
52
|
+
hb_string_T hb_string_trim(hb_string_T string) {
|
|
53
|
+
return hb_string_trim_end(hb_string_trim_start(string));
|
|
53
54
|
}
|
|
54
55
|
|
|
55
|
-
|
|
56
|
-
|
|
56
|
+
bool hb_string_is_blank(hb_string_T string) {
|
|
57
|
+
if (hb_string_is_empty(string)) { return true; }
|
|
57
58
|
|
|
58
|
-
|
|
59
|
+
for (uint32_t i = 0; i < string.length; i++) {
|
|
60
|
+
if (!is_whitespace(string.data[i])) { return false; }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return true;
|
|
59
64
|
}
|
|
60
65
|
|
|
61
|
-
hb_string_T
|
|
62
|
-
|
|
66
|
+
hb_string_T hb_string_copy(hb_string_T string, hb_allocator_T* allocator) {
|
|
67
|
+
if (hb_string_is_null(string)) { return HB_STRING_NULL; }
|
|
68
|
+
if (hb_string_is_empty(string)) { return HB_STRING_EMPTY; }
|
|
69
|
+
|
|
70
|
+
char* copy = hb_allocator_strndup(allocator, string.data, string.length);
|
|
71
|
+
|
|
72
|
+
return (hb_string_T) { .data = copy, .length = string.length };
|
|
63
73
|
}
|
|
64
74
|
|
|
65
75
|
char* hb_string_to_c_string_using_malloc(hb_string_T string) {
|
|
66
|
-
|
|
67
|
-
char* buffer = malloc(string_length_in_bytes + sizeof(char) * 1);
|
|
76
|
+
if (hb_string_is_null(string)) { return NULL; }
|
|
68
77
|
|
|
69
|
-
|
|
78
|
+
size_t length = string.length;
|
|
79
|
+
char* buffer = malloc(length + 1);
|
|
70
80
|
|
|
71
|
-
buffer
|
|
81
|
+
if (!buffer) { return NULL; }
|
|
82
|
+
|
|
83
|
+
if (length > 0) { memcpy(buffer, string.data, length); }
|
|
84
|
+
|
|
85
|
+
buffer[length] = '\0';
|
|
72
86
|
|
|
73
87
|
return buffer;
|
|
74
88
|
}
|