ext/glib2/rbglib-bytes.c in glib2-3.3.7 vs ext/glib2/rbglib-bytes.c in glib2-3.3.8
- old
+ new
@@ -1,8 +1,8 @@
/* -*- c-file-style: "ruby"; indent-tabs-mode: nil -*- */
/*
- * Copyright (C) 2017 Ruby-GNOME2 Project Team
+ * Copyright (C) 2017-2019 Ruby-GNOME Project Team
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
@@ -26,26 +26,47 @@
#if GLIB_CHECK_VERSION(2, 32, 0)
static VALUE RG_TARGET_NAMESPACE;
static VALUE
+rg_s_try_convert(VALUE self, VALUE value)
+{
+ if (NIL_P(value)) {
+ return value;
+ }
+
+ if (RVAL2CBOOL(rb_obj_is_kind_of(value, self))) {
+ return value;
+ }
+
+ if (RB_TYPE_P(value, RUBY_T_STRING)) {
+ ID id_new;
+ CONST_ID(id_new, "new");
+ return rb_funcall(self, id_new, 1, value);
+ }
+
+ return Qnil;
+}
+
+static VALUE
rg_initialize(int argc, VALUE *argv, VALUE self)
{
GBytes *bytes;
VALUE rb_data;
rb_scan_args(argc, argv, "01", &rb_data);
if (NIL_P(rb_data)) {
bytes = g_bytes_new(NULL, 0);
} else {
- const gchar *data = RVAL2CSTR_PTR(rb_data);
- if (RB_OBJ_FROZEN(rb_data)) {
- bytes = g_bytes_new_static(data, RSTRING_LEN(rb_data));
- rb_iv_set(self, "source", rb_data);
- } else {
- bytes = g_bytes_new(data, RSTRING_LEN(rb_data));
+ const gchar *data;
+ if (!RB_OBJ_FROZEN(rb_data)) {
+ rb_data = rb_str_dup(rb_data);
+ rb_str_freeze(rb_data);
}
+ data = RVAL2CSTR_PTR(rb_data);
+ bytes = g_bytes_new_static(data, RSTRING_LEN(rb_data));
+ rb_iv_set(self, "source", rb_data);
}
G_INITIALIZE(self, bytes);
return Qnil;
}
@@ -95,9 +116,11 @@
void
Init_glib_bytes(void)
{
#if GLIB_CHECK_VERSION(2, 32, 0)
RG_TARGET_NAMESPACE = G_DEF_CLASS(G_TYPE_BYTES, "Bytes", mGLib);
+
+ RG_DEF_SMETHOD(try_convert, 1);
RG_DEF_METHOD(initialize, -1);
RG_DEF_METHOD(to_s, 0);
RG_DEF_ALIAS("to_str", "to_s");
RG_DEF_METHOD(size, 0);