Skip to content

Commit 08081dd

Browse files
committed
Use yp_memchr in regexp parsing
1 parent 74ef7a7 commit 08081dd

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

ext/yarp/extension.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ named_captures(VALUE self, VALUE source) {
379379
yp_string_list_t string_list;
380380
yp_string_list_init(&string_list);
381381

382-
if (!yp_regexp_named_capture_group_names(RSTRING_PTR(source), RSTRING_LEN(source), &string_list)) {
382+
if (!yp_regexp_named_capture_group_names(RSTRING_PTR(source), RSTRING_LEN(source), &string_list, false, &yp_encoding_utf_8)) {
383383
yp_string_list_free(&string_list);
384384
return Qnil;
385385
}

include/yarp/regexp.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include "yarp/defines.h"
55
#include "yarp/parser.h"
6+
#include "yarp/enc/yp_encoding.h"
7+
#include "yarp/util/yp_memchr.h"
68
#include "yarp/util/yp_string_list.h"
79
#include "yarp/util/yp_string.h"
810

@@ -12,6 +14,6 @@
1214

1315
// Parse a regular expression and extract the names of all of the named capture
1416
// groups.
15-
YP_EXPORTED_FUNCTION bool yp_regexp_named_capture_group_names(const char *source, size_t size, yp_string_list_t *named_captures);
17+
YP_EXPORTED_FUNCTION bool yp_regexp_named_capture_group_names(const char *source, size_t size, yp_string_list_t *named_captures, bool encoding_changed, yp_encoding_t *encoding);
1618

1719
#endif

src/regexp.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@ typedef struct {
66
const char *cursor;
77
const char *end;
88
yp_string_list_t *named_captures;
9+
bool encoding_changed;
10+
yp_encoding_t *encoding;
911
} yp_regexp_parser_t;
1012

1113
// This initializes a new parser with the given source.
1214
static void
13-
yp_regexp_parser_init(yp_regexp_parser_t *parser, const char *start, const char *end, yp_string_list_t *named_captures) {
15+
yp_regexp_parser_init(yp_regexp_parser_t *parser, const char *start, const char *end, yp_string_list_t *named_captures, bool encoding_changed, yp_encoding_t *encoding) {
1416
*parser = (yp_regexp_parser_t) {
1517
.start = start,
1618
.cursor = start,
1719
.end = end,
18-
.named_captures = named_captures
20+
.named_captures = named_captures,
21+
.encoding_changed = encoding_changed,
22+
.encoding = encoding
1923
};
2024
}
2125

@@ -60,7 +64,8 @@ yp_regexp_char_find(yp_regexp_parser_t *parser, char value) {
6064
if (yp_regexp_char_is_eof(parser)) {
6165
return false;
6266
}
63-
const char *end = (const char *) memchr(parser->cursor, value, (size_t) (parser->end - parser->cursor));
67+
68+
const char *end = (const char *) yp_memchr(parser->cursor, value, (size_t) (parser->end - parser->cursor), parser->encoding_changed, parser->encoding);
6469
if (end == NULL) {
6570
return false;
6671
}
@@ -542,8 +547,8 @@ yp_regexp_parse_pattern(yp_regexp_parser_t *parser) {
542547
// Parse a regular expression and extract the names of all of the named capture
543548
// groups.
544549
YP_EXPORTED_FUNCTION bool
545-
yp_regexp_named_capture_group_names(const char *source, size_t size, yp_string_list_t *named_captures) {
550+
yp_regexp_named_capture_group_names(const char *source, size_t size, yp_string_list_t *named_captures, bool encoding_changed, yp_encoding_t *encoding) {
546551
yp_regexp_parser_t parser;
547-
yp_regexp_parser_init(&parser, source, source + size, named_captures);
552+
yp_regexp_parser_init(&parser, source, source + size, named_captures, encoding_changed, encoding);
548553
return yp_regexp_parse_pattern(&parser);
549554
}

src/yarp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12605,7 +12605,7 @@ parse_expression_infix(yp_parser_t *parser, yp_node_t *node, yp_binding_power_t
1260512605

1260612606
yp_location_t *content_loc = &((yp_regular_expression_node_t *) node)->content_loc;
1260712607

12608-
if (yp_regexp_named_capture_group_names(content_loc->start, (size_t) (content_loc->end - content_loc->start), &named_captures)) {
12608+
if (yp_regexp_named_capture_group_names(content_loc->start, (size_t) (content_loc->end - content_loc->start), &named_captures, parser->encoding_changed, &parser->encoding)) {
1260912609
for (size_t index = 0; index < named_captures.length; index++) {
1261012610
yp_string_t *name = &named_captures.strings[index];
1261112611
assert(name->type == YP_STRING_SHARED);

0 commit comments

Comments
 (0)