summaryrefslogtreecommitdiff
path: root/prism
diff options
context:
space:
mode:
authorKevin Newton <kddnewton@gmail.com>2023-10-30 23:15:31 -0400
committerKevin Newton <kddnewton@gmail.com>2023-11-01 13:10:29 -0400
commit8302f9986caf6679c728ae2b31b1211401f48d28 (patch)
tree6cb1c95569ab49eab35a8b581fd5f302fb1adbab /prism
parentb67994d6bf88fcfd66fc0368a03a92aa81fe8b11 (diff)
[ruby/prism] Documentation for pm_string_t
https://github.com/ruby/prism/commit/ff1d2ec579
Diffstat (limited to 'prism')
-rw-r--r--prism/extension.c3
-rw-r--r--prism/prism.c15
-rw-r--r--prism/util/pm_string_list.c23
-rw-r--r--prism/util/pm_string_list.h7
4 files changed, 15 insertions, 33 deletions
diff --git a/prism/extension.c b/prism/extension.c
index db9fe78f34..824f7ad04e 100644
--- a/prism/extension.c
+++ b/prism/extension.c
@@ -613,8 +613,7 @@ parse_lex_file(VALUE self, VALUE filepath) {
*/
static VALUE
named_captures(VALUE self, VALUE source) {
- pm_string_list_t string_list;
- pm_string_list_init(&string_list);
+ pm_string_list_t string_list = { 0 };
if (!pm_regexp_named_capture_group_names((const uint8_t *) RSTRING_PTR(source), RSTRING_LEN(source), &string_list, false, &pm_encoding_utf_8)) {
pm_string_list_free(&string_list);
diff --git a/prism/prism.c b/prism/prism.c
index 4adc66ef7c..b988b6b7fe 100644
--- a/prism/prism.c
+++ b/prism/prism.c
@@ -14716,10 +14716,9 @@ parse_call_operator_write(pm_parser_t *parser, pm_call_node_t *call_node, const
// match write node.
static pm_node_t *
parse_regular_expression_named_captures(pm_parser_t *parser, const pm_string_t *content, pm_call_node_t *call) {
- pm_string_list_t named_captures;
- pm_string_list_init(&named_captures);
-
+ pm_string_list_t named_captures = { 0 };
pm_node_t *result;
+
if (pm_regexp_named_capture_group_names(pm_string_source(content), pm_string_length(content), &named_captures, parser->encoding_changed, &parser->encoding) && (named_captures.length > 0)) {
pm_match_write_node_t *match = pm_match_write_node_create(parser, call);
@@ -14728,14 +14727,12 @@ parse_regular_expression_named_captures(pm_parser_t *parser, const pm_string_t *
pm_constant_id_t local;
if (content->type == PM_STRING_SHARED) {
- // If the unescaped string is a slice of the source,
- // then we can copy the names directly. The pointers
- // will line up.
+ // If the unescaped string is a slice of the source, then we can
+ // copy the names directly. The pointers will line up.
local = pm_parser_local_add_location(parser, name->source, name->source + name->length);
} else {
- // Otherwise, the name is a slice of the malloc-ed
- // owned string, in which case we need to copy it
- // out into a new string.
+ // Otherwise, the name is a slice of the malloc-ed owned string,
+ // in which case we need to copy it out into a new string.
size_t length = pm_string_length(name);
void *memory = malloc(length);
diff --git a/prism/util/pm_string_list.c b/prism/util/pm_string_list.c
index fa7f20619f..d49e4ed734 100644
--- a/prism/util/pm_string_list.c
+++ b/prism/util/pm_string_list.c
@@ -1,26 +1,19 @@
#include "prism/util/pm_string_list.h"
/**
- * Initialize a pm_string_list_t with its default values.
- */
-void
-pm_string_list_init(pm_string_list_t *string_list) {
- string_list->length = 0;
- string_list->capacity = 1;
- string_list->strings = (pm_string_t *) malloc(sizeof(pm_string_t));
-}
-
-/**
* Append a pm_string_t to the given string list.
*/
void
pm_string_list_append(pm_string_list_t *string_list, pm_string_t *string) {
if (string_list->length + 1 > string_list->capacity) {
- pm_string_t *original_string = string_list->strings;
- string_list->capacity *= 2;
- string_list->strings = (pm_string_t *) malloc(string_list->capacity * sizeof(pm_string_t));
- memcpy(string_list->strings, original_string, (string_list->length) * sizeof(pm_string_t));
- free(original_string);
+ if (string_list->capacity == 0) {
+ string_list->capacity = 1;
+ } else {
+ string_list->capacity *= 2;
+ }
+
+ string_list->strings = realloc(string_list->strings, string_list->capacity * sizeof(pm_string_t));
+ if (string_list->strings == NULL) abort();
}
string_list->strings[string_list->length++] = *string;
diff --git a/prism/util/pm_string_list.h b/prism/util/pm_string_list.h
index 5aa3758775..1f460e5dc9 100644
--- a/prism/util/pm_string_list.h
+++ b/prism/util/pm_string_list.h
@@ -22,13 +22,6 @@ typedef struct {
} pm_string_list_t;
/**
- * Initialize a pm_string_list_t with its default values.
- *
- * @param string_list The string list to initialize.
- */
-PRISM_EXPORTED_FUNCTION void pm_string_list_init(pm_string_list_t *string_list);
-
-/**
* Append a pm_string_t to the given string list.
*
* @param string_list The string list to append to.