Skip to content

Commit 8f59fc2

Browse files
vinistockkddnewton
andcommitted
Consider source encoding for slice
Co-authored-by: Kevin Newton <kddnewton@users.noreply.github.com>
1 parent 9285989 commit 8f59fc2

File tree

5 files changed

+18
-6
lines changed

5 files changed

+18
-6
lines changed

ext/yarp/extension.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ parse_input(yp_string_t *input, const char *filepath) {
347347
yp_node_t *node = yp_parse(&parser);
348348
rb_encoding *encoding = rb_enc_find(parser.encoding.name);
349349

350-
VALUE source = yp_source_new(&parser);
350+
VALUE source = yp_source_new(&parser, encoding);
351351
VALUE result_argv[] = {
352352
yp_ast_new(&parser, node, encoding),
353353
parser_comments(&parser, source),

ext/yarp/extension.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <ruby/encoding.h>
88
#include "yarp.h"
99

10-
VALUE yp_source_new(yp_parser_t *parser);
10+
VALUE yp_source_new(yp_parser_t *parser, rb_encoding *encoding);
1111
VALUE yp_token_new(yp_parser_t *parser, yp_token_t *token, rb_encoding *encoding, VALUE source);
1212
VALUE yp_ast_new(yp_parser_t *parser, yp_node_t *node, rb_encoding *encoding);
1313

templates/ext/yarp/api_node.c.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ yp_string_new(yp_string_t *string, rb_encoding *encoding) {
3838

3939
// Create a YARP::Source object from the given parser.
4040
VALUE
41-
yp_source_new(yp_parser_t *parser) {
42-
VALUE source = rb_str_new((const char *) parser->start, parser->end - parser->start);
41+
yp_source_new(yp_parser_t *parser, rb_encoding *encoding) {
42+
VALUE source = rb_enc_str_new((const char *) parser->start, parser->end - parser->start, encoding);
4343
VALUE offsets = rb_ary_new_capa(parser->newline_list.size);
4444

4545
for (size_t index = 0; index < parser->newline_list.size; index++) {
@@ -78,7 +78,7 @@ yp_node_stack_pop(yp_node_stack_node_t **stack) {
7878

7979
VALUE
8080
yp_ast_new(yp_parser_t *parser, yp_node_t *node, rb_encoding *encoding) {
81-
VALUE source = yp_source_new(parser);
81+
VALUE source = yp_source_new(parser, encoding);
8282
ID *constants = calloc(parser->constant_pool.size, sizeof(ID));
8383

8484
for (size_t index = 0; index < parser->constant_pool.capacity; index++) {

templates/lib/yarp/serialize.rb.erb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ module YARP
1818
PATCH_VERSION = 0
1919

2020
def self.load(input, serialized)
21-
Loader.new(Source.new(input), serialized).load_result
21+
input = input.dup
22+
source = Source.new(input)
23+
loader = Loader.new(source, serialized)
24+
result = loader.load_result
25+
26+
input.force_encoding(loader.encoding)
27+
result
2228
end
2329

2430
def self.load_tokens(source, serialized)

test/yarp/encoding_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,11 @@ def test_first_lexed_token
9696
encoding = YARP.lex("# encoding: ascii-8bit").value[0][0].value.encoding
9797
assert_equal Encoding.find("ascii-8bit"), encoding
9898
end
99+
100+
def test_slice_encoding
101+
slice = YARP.parse("# encoding: Shift_JIS\nア").value.slice
102+
assert_equal (+"ア").force_encoding(Encoding::SHIFT_JIS), slice
103+
assert_equal Encoding::SHIFT_JIS, slice.encoding
104+
end
99105
end
100106
end

0 commit comments

Comments
 (0)