ruby-changes:66775
From: Sutou <ko1@a...>
Date: Tue, 13 Jul 2021 19:38:28 +0900 (JST)
Subject: [ruby-changes:66775] 9988f6ac4e (master): [ruby/fiddle] Add Fiddle::MemoryView#to_s (https://github.com/ruby/fiddle/pull/78)
https://git.ruby-lang.org/ruby.git/commit/?id=9988f6ac4e From 9988f6ac4e887437b5a76f0a1c27fa6f5c17c039 Mon Sep 17 00:00:00 2001 From: Sutou Kouhei <kou@c...> Date: Thu, 20 May 2021 06:29:26 +0900 Subject: [ruby/fiddle] Add Fiddle::MemoryView#to_s (https://github.com/ruby/fiddle/pull/78) Fix https://github.com/ruby/fiddle/pull/74 Reported by dsisnero. Thanks!!! --- ext/fiddle/memory_view.c | 32 ++++++++++++++++++++++++++++++++ test/fiddle/test_memory_view.rb | 10 ++++++++++ 2 files changed, 42 insertions(+) diff --git a/ext/fiddle/memory_view.c b/ext/fiddle/memory_view.c index 172b1f1..2a9f45f 100644 --- a/ext/fiddle/memory_view.c +++ b/ext/fiddle/memory_view.c @@ -1,5 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ext/fiddle/memory_view.c#L1 #include <stdbool.h> #include <ruby/ruby.h> +#include <ruby/encoding.h> #ifdef HAVE_RUBY_MEMORY_VIEW_H # include <ruby/memory_view.h> @@ -233,6 +234,36 @@ rb_fiddle_memview_aref(int argc, VALUE *argv, VALUE obj) https://github.com/ruby/ruby/blob/trunk/ext/fiddle/memory_view.c#L234 return rb_memory_view_extract_item_members(ptr, data->members, data->n_members); } +static VALUE +rb_fiddle_memview_to_s(VALUE self) +{ + struct memview_data *data; + const char *raw_data; + long byte_size; + VALUE string; + + TypedData_Get_Struct(self, + struct memview_data, + &fiddle_memview_data_type, + data); + + if (NIL_P(data->view.obj)) { + raw_data = NULL; + byte_size = 0; + } else { + raw_data = data->view.data; + byte_size = data->view.byte_size; + } + + string = rb_enc_str_new_static(raw_data, byte_size, rb_ascii8bit_encoding()); + { + ID id_memory_view; + CONST_ID(id_memory_view, "memory_view"); + rb_ivar_set(string, id_memory_view, self); + } + return rb_obj_freeze(string); +} + void Init_fiddle_memory_view(void) { @@ -249,6 +280,7 @@ Init_fiddle_memory_view(void) https://github.com/ruby/ruby/blob/trunk/ext/fiddle/memory_view.c#L280 rb_define_method(rb_cMemoryView, "strides", rb_fiddle_memview_get_strides, 0); rb_define_method(rb_cMemoryView, "sub_offsets", rb_fiddle_memview_get_sub_offsets, 0); rb_define_method(rb_cMemoryView, "[]", rb_fiddle_memview_aref, -1); + rb_define_method(rb_cMemoryView, "to_s", rb_fiddle_memview_to_s, 0); } #endif /* FIDDLE_MEMORY_VIEW */ diff --git a/test/fiddle/test_memory_view.rb b/test/fiddle/test_memory_view.rb index ffea8a9..6777a04 100644 --- a/test/fiddle/test_memory_view.rb +++ b/test/fiddle/test_memory_view.rb @@ -113,5 +113,15 @@ module Fiddle https://github.com/ruby/ruby/blob/trunk/test/fiddle/test_memory_view.rb#L113 assert_equal([-1, -2], mview[1, 0]) assert_equal([-7, -8], mview[1, 3]) end + + def test_to_s + # U+3042 HIRAGANA LETTER A + data = "\u{3042}" + ptr = Pointer[data] + mview = MemoryView.new(ptr) + string = mview.to_s + assert_equal([data.b, true], + [string, string.frozen?]) + end end end -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/