[ruby/yarp] Allow whitespace after "(en)coding" before ":", as in "encoding :"

https://github.com/ruby/yarp/commit/d39a998182
This commit is contained in:
Jemma Issroff 2023-08-25 10:32:12 -07:00 committed by git
parent 7d32011399
commit bf3d48e182
2 changed files with 14 additions and 5 deletions

View File

@ -50,6 +50,13 @@ class EncodingTest < Test::Unit::TestCase
assert_equal Encoding.find("utf-8"), actual
end
def test_coding_with_whitespace
result = YARP.parse("# coding \t \r \v : \t \v \r ascii-8bit \nident")
actual = result.value.statements.body.first.name.encoding
assert_equal Encoding.find("ascii-8bit"), actual
end
def test_emacs_style
result = YARP.parse("# -*- coding: utf-8 -*-\nident")
actual = result.value.statements.body.first.name.encoding

View File

@ -4328,11 +4328,13 @@ parser_lex_encoding_comment_start(yp_parser_t *parser, const char *cursor, ptrdi
const char *cursor_limit = cursor + length - key_length + 1;
while ((cursor = yp_memchr(cursor, 'c', (size_t) (cursor_limit - cursor), parser->encoding_changed, &parser->encoding)) != NULL) {
if (
(strncmp(cursor, "coding", key_length - 1) == 0) &&
(cursor[key_length - 1] == ':' || cursor[key_length - 1] == '=')
) {
return cursor + key_length;
if (strncmp(cursor, "coding", key_length - 1) == 0) {
size_t whitespace_after_coding = yp_strspn_inline_whitespace(cursor + key_length - 1, parser->end - (cursor + key_length - 1));
size_t cur_pos = key_length + whitespace_after_coding;
if (cursor[cur_pos - 1] == ':' || cursor[cur_pos - 1] == '=') {
return cursor + cur_pos;
}
}
cursor++;