Skip to content

Commit ee16286

Browse files
committed
Remove string type enum
Since we don't manually allocate/free strings anymore, we don't need the string type enum and all the complexity that comes with it.
1 parent c78b360 commit ee16286

File tree

10 files changed

+26
-53
lines changed

10 files changed

+26
-53
lines changed

ext/rbs_extension/rbs_string_bridging.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "rbs_string_bridging.h"
22

33
rbs_string_t rbs_string_from_ruby_string(VALUE ruby_string) {
4-
return rbs_string_shared_new(StringValueCStr(ruby_string), RSTRING_END(ruby_string));
4+
return rbs_string_new(StringValueCStr(ruby_string), RSTRING_END(ruby_string));
55
}
66

77
VALUE rbs_string_to_ruby_string(rbs_string_t *self, rb_encoding *encoding) {

include/rbs/rbs_string.h

+2-17
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,17 @@
88
typedef struct {
99
const char *start;
1010
const char *end;
11-
12-
enum rbs_string_type {
13-
/** This string is a constant string, and should not be freed. */
14-
RBS_STRING_CONSTANT,
15-
/** This is a slice of another string, and should not be freed. */
16-
RBS_STRING_SHARED,
17-
/** This string owns its memory, and will be freed when the allocator is freed. */
18-
RBS_STRING_OWNED,
19-
} type;
2011
} rbs_string_t;
2112

2213
#define RBS_STRING_NULL ((rbs_string_t) { \
2314
.start = NULL, \
2415
.end = NULL, \
25-
.type = RBS_STRING_CONSTANT, \
2616
})
2717

2818
/**
29-
* Returns a new `rbs_string_t` struct that points to the given C string without owning it.
30-
*/
31-
rbs_string_t rbs_string_shared_new(const char *start, const char *end);
32-
33-
/**
34-
* Returns a new `rbs_string_t` struct that owns its memory.
19+
* Returns a new `rbs_string_t` struct
3520
*/
36-
rbs_string_t rbs_string_owned_new(const char *start, const char *end);
21+
rbs_string_t rbs_string_new(const char *start, const char *end);
3722

3823
/**
3924
* Copies a portion of the input string into a new owned string.

src/ast.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -1279,7 +1279,7 @@ void rbs_node_destroy(rbs_node_t *any_node) {
12791279
case RBS_AST_ANNOTATION: {
12801280
rbs_ast_annotation_t *node = (rbs_ast_annotation_t *)any_node;
12811281

1282-
// string is a string, so it will be freed when the allocator is freed.
1282+
// node->string is a `rbs_string_t` that will be freed by the arena allocator.
12831283
break;
12841284
}
12851285
#line 202 "prism/templates/src/ast.c.erb"
@@ -1290,7 +1290,7 @@ void rbs_node_destroy(rbs_node_t *any_node) {
12901290
case RBS_AST_COMMENT: {
12911291
rbs_ast_comment_t *node = (rbs_ast_comment_t *)any_node;
12921292

1293-
// string is a string, so it will be freed when the allocator is freed.
1293+
// node->string is a `rbs_string_t` that will be freed by the arena allocator.
12941294
break;
12951295
}
12961296
#line 202 "prism/templates/src/ast.c.erb"
@@ -1471,7 +1471,7 @@ void rbs_node_destroy(rbs_node_t *any_node) {
14711471
case RBS_AST_INTEGER: {
14721472
rbs_ast_integer_t *node = (rbs_ast_integer_t *)any_node;
14731473

1474-
// string_representation is a string, so it will be freed when the allocator is freed.
1474+
// node->string_representation is a `rbs_string_t` that will be freed by the arena allocator.
14751475
break;
14761476
}
14771477
#line 202 "prism/templates/src/ast.c.erb"
@@ -1698,7 +1698,7 @@ void rbs_node_destroy(rbs_node_t *any_node) {
16981698
case RBS_AST_STRING: {
16991699
rbs_ast_string_t *node = (rbs_ast_string_t *)any_node;
17001700

1701-
// string is a string, so it will be freed when the allocator is freed.
1701+
// node->string is a `rbs_string_t` that will be freed by the arena allocator.
17021702
break;
17031703
}
17041704
#line 202 "prism/templates/src/ast.c.erb"

src/lexstate.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,10 @@ unsigned int peek(lexstate *state) {
110110
state->last_char = '\0';
111111
return 0;
112112
} else {
113-
rbs_string_t str = {
114-
.start = state->string.start + state->current.byte_pos,
115-
.end = state->string.end,
116-
.type = RBS_STRING_SHARED,
117-
};
113+
rbs_string_t str = rbs_string_new(
114+
state->string.start + state->current.byte_pos,
115+
state->string.end
116+
);
118117
unsigned int c = utf8_to_codepoint(str);
119118
state->last_char = c;
120119
return c;

src/parser.c

+5-6
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ static rbs_string_t rbs_parser_peek_current_token(parserstate *state) {
113113
const char *start = state->lexstate->string.start + rg.start.byte_pos;
114114
size_t length = rg.end.byte_pos - rg.start.byte_pos;
115115

116-
return rbs_string_shared_new(start, start + length);
116+
return rbs_string_new(start, start + length);
117117
}
118118

119119
static rbs_constant_id_t rbs_constant_pool_insert_string(rbs_constant_pool_t *self, rbs_string_t string) {
@@ -1552,11 +1552,10 @@ static bool parse_annotation(parserstate *state, rbs_ast_annotation_t **annotati
15521552
state->lexstate->encoding->char_width((const uint8_t *) "%", (size_t) 1) +
15531553
state->lexstate->encoding->char_width((const uint8_t *) "a", (size_t) 1);
15541554

1555-
rbs_string_t str = {
1556-
.start = state->lexstate->string.start + rg.start.byte_pos + offset_bytes,
1557-
.end = state->lexstate->string.end,
1558-
.type = RBS_STRING_SHARED,
1559-
};
1555+
rbs_string_t str = rbs_string_new(
1556+
state->lexstate->string.start + rg.start.byte_pos + offset_bytes,
1557+
state->lexstate->string.end
1558+
);
15601559
unsigned int open_char = utf8_to_codepoint(str);
15611560

15621561
unsigned int close_char;

src/parserstate.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,10 @@ static rbs_ast_comment_t *parse_comment_lines(parserstate *state, comment *com)
186186
const char *comment_start = state->lexstate->string.start + tok.range.start.byte_pos + hash_bytes;
187187
size_t comment_bytes = RANGE_BYTES(tok.range) - hash_bytes;
188188

189-
rbs_string_t str = {
190-
.start = comment_start,
191-
.end = state->lexstate->string.end,
192-
.type = RBS_STRING_SHARED,
193-
};
189+
rbs_string_t str = rbs_string_new(
190+
comment_start,
191+
state->lexstate->string.end
192+
);
194193
unsigned char c = utf8_to_codepoint(str);
195194

196195
if (c == ' ') {

src/rbs_buffer.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void rbs_buffer_append_string(rbs_buffer_t *buffer, const char *value, size_t le
5656
}
5757

5858
rbs_string_t rbs_buffer_to_string(rbs_buffer_t *buffer) {
59-
return rbs_string_shared_new(buffer->value, buffer->value + buffer->length);
59+
return rbs_string_new(buffer->value, buffer->value + buffer->length);
6060
}
6161

6262
void rbs_buffer_free(rbs_buffer_t *buffer) {

src/rbs_string.c

+3-12
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,10 @@
55
#include <stdio.h>
66
#include <ctype.h>
77

8-
rbs_string_t rbs_string_shared_new(const char *start, const char *end) {
8+
rbs_string_t rbs_string_new(const char *start, const char *end) {
99
return (rbs_string_t) {
1010
.start = start,
1111
.end = end,
12-
.type = RBS_STRING_SHARED,
13-
};
14-
}
15-
16-
rbs_string_t rbs_string_owned_new(const char *start, const char *end) {
17-
return (rbs_string_t) {
18-
.start = start,
19-
.end = end,
20-
.type = RBS_STRING_OWNED,
2112
};
2213
}
2314

@@ -26,7 +17,7 @@ rbs_string_t rbs_string_copy_slice(rbs_allocator_t *allocator, rbs_string_t *sel
2617
strncpy(buffer, self->start + start_inset, length);
2718
buffer[length] = '\0';
2819

29-
return rbs_string_owned_new(buffer, buffer + length);
20+
return rbs_string_new(buffer, buffer + length);
3021
}
3122

3223
rbs_string_t rbs_string_strip_whitespace(rbs_allocator_t *allocator, rbs_string_t *self) {
@@ -36,7 +27,7 @@ rbs_string_t rbs_string_strip_whitespace(rbs_allocator_t *allocator, rbs_string_
3627
}
3728

3829
if (new_start == self->end) { // Handle empty string case
39-
return rbs_string_shared_new(new_start, new_start);
30+
return rbs_string_new(new_start, new_start);
4031
}
4132

4233
const char *new_end = self->end - 1;

src/rbs_unescape.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ rbs_string_t unescape_string(rbs_allocator_t *allocator, rbs_string_t string, bo
103103
}
104104
}
105105
output[j] = '\0';
106-
return rbs_string_owned_new(output, output + j);
106+
return rbs_string_new(output, output + j);
107107
}
108108

109109
rbs_string_t rbs_unquote_string(rbs_allocator_t *allocator, rbs_string_t input) {

templates/src/ast.c.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ void rbs_node_destroy(rbs_node_t *any_node) {
206206
<%- node.fields.each do |field| -%>
207207
<%- case field.c_type -%>
208208
<%- when "rbs_string" -%>
209-
// <%= field.c_name %> is a string, so it will be freed when the allocator is freed.
209+
// node-><%= field.c_name %> is a `rbs_string_t` that will be freed by the arena allocator.
210210
<%- when "rbs_node_list" -%>
211211
rbs_node_list_free(node-><%= field.c_name %>);
212212
<%- when "rbs_hash" -%>

0 commit comments

Comments
 (0)