From bf3d48e18261595ac19057319f378bfd44928045 Mon Sep 17 00:00:00 2001 From: Jemma Issroff Date: Fri, 25 Aug 2023 10:32:12 -0700 Subject: [PATCH] [ruby/yarp] Allow whitespace after "(en)coding" before ":", as in "encoding :" https://github.com/ruby/yarp/commit/d39a998182 --- test/yarp/encoding_test.rb | 7 +++++++ yarp/yarp.c | 12 +++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/test/yarp/encoding_test.rb b/test/yarp/encoding_test.rb index 95d7bd3223..c96a08e60e 100644 --- a/test/yarp/encoding_test.rb +++ b/test/yarp/encoding_test.rb @@ -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 diff --git a/yarp/yarp.c b/yarp/yarp.c index 3ec2b1d2b1..a7b2290aa2 100644 --- a/yarp/yarp.c +++ b/yarp/yarp.c @@ -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++;