Skip to content

Commit

Permalink
Documentation for pm_string_t
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Nov 1, 2023
1 parent bec5ca3 commit ff1d2ec
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 35 deletions.
3 changes: 1 addition & 2 deletions ext/prism/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions fuzz/regexp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

void
harness(const uint8_t *input, size_t size) {
pm_string_list_t capture_list;
pm_string_list_init(&capture_list);
pm_string_list_t capture_list = { 0 };
pm_regexp_named_capture_group_names(input, size, &capture_list, false, &pm_encoding_utf_8);
pm_string_list_free(&capture_list);
}
7 changes: 0 additions & 7 deletions include/prism/util/pm_string_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ typedef struct {
pm_string_t *strings;
} 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.
*
Expand Down
15 changes: 6 additions & 9 deletions src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -14706,10 +14706,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);

Expand All @@ -14718,14 +14717,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);
Expand Down
23 changes: 8 additions & 15 deletions src/util/pm_string_list.c
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit ff1d2ec

Please sign in to comment.