1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[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!!!
This commit is contained in:
Sutou Kouhei 2021-05-20 06:29:26 +09:00 committed by Nobuyoshi Nakada
parent 8c905349bb
commit 9988f6ac4e
No known key found for this signature in database
GPG key ID: 7CD2805BFA3770C6
2 changed files with 42 additions and 0 deletions

View file

@ -1,5 +1,6 @@
#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)
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)
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 */

View file

@ -113,5 +113,15 @@ module Fiddle
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