Skip to content

Commit e0708c4

Browse files
committed
Small optimization for parser_lex_magic_comment
1 parent 1dd9853 commit e0708c4

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

src/prism.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7316,11 +7316,13 @@ pm_char_is_magic_comment_key_delimiter(const uint8_t b) {
73167316
*/
73177317
static inline const uint8_t *
73187318
parser_lex_magic_comment_emacs_marker(pm_parser_t *parser, const uint8_t *cursor, const uint8_t *end) {
7319-
while ((cursor + 3 <= end) && (cursor = pm_memchr(cursor, '-', (size_t) (end - cursor), parser->encoding_changed, parser->encoding)) != NULL) {
7320-
if (cursor + 3 <= end && cursor[1] == '*' && cursor[2] == '-') {
7321-
return cursor;
7319+
// Scan for '*' as the middle character, since it is rarer than '-' in
7320+
// typical comments and avoids repeated memchr calls for '-' that hit
7321+
// dashes in words like "foo-bar".
7322+
while ((cursor + 3 <= end) && (cursor = pm_memchr(cursor + 1, '*', (size_t) (end - cursor - 1), parser->encoding_changed, parser->encoding)) != NULL) {
7323+
if (cursor[-1] == '-' && cursor + 1 < end && cursor[1] == '-') {
7324+
return cursor - 1;
73227325
}
7323-
cursor++;
73247326
}
73257327
return NULL;
73267328
}
@@ -7357,6 +7359,13 @@ parser_lex_magic_comment(pm_parser_t *parser, bool semantic_token_seen) {
73577359
// have a magic comment.
73587360
return false;
73597361
}
7362+
} else {
7363+
// Non-emacs magic comments must contain a colon for `key: value`.
7364+
// Reject early if there is no colon to avoid scanning the entire
7365+
// comment character-by-character.
7366+
if (pm_memchr(start, ':', (size_t) (end - start), parser->encoding_changed, parser->encoding) == NULL) {
7367+
return false;
7368+
}
73607369
}
73617370

73627371
cursor = start;

0 commit comments

Comments
 (0)