From bde3ba99dd1bce653718efc02cb33cf5a81f291e Mon Sep 17 00:00:00 2001 From: Julien Marrec Date: Tue, 8 Jun 2021 09:22:03 +0200 Subject: [PATCH 1/2] Add the official rubystrings.swg (from swig 4.0.1) before modification --- ruby/rubystrings.swg | 57 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 ruby/rubystrings.swg diff --git a/ruby/rubystrings.swg b/ruby/rubystrings.swg new file mode 100644 index 00000000000..3adf0008bcb --- /dev/null +++ b/ruby/rubystrings.swg @@ -0,0 +1,57 @@ +/* ------------------------------------------------------------ + * utility methods for char strings + * ------------------------------------------------------------ */ + +%fragment("SWIG_AsCharPtrAndSize","header",fragment="SWIG_pchar_descriptor") { +SWIGINTERN int +SWIG_AsCharPtrAndSize(VALUE obj, char** cptr, size_t* psize, int *alloc) +{ + if (TYPE(obj) == T_STRING) { + char *cstr = StringValuePtr(obj); + size_t size = RSTRING_LEN(obj) + 1; + if (cptr) { + if (alloc) { + if (*alloc == SWIG_NEWOBJ) { + *cptr = %new_copy_array(cstr, size, char); + } else { + *cptr = cstr; + *alloc = SWIG_OLDOBJ; + } + } + } + if (psize) *psize = size; + return SWIG_OK; + } else { + swig_type_info* pchar_descriptor = SWIG_pchar_descriptor(); + if (pchar_descriptor) { + void* vptr = 0; + if (SWIG_ConvertPtr(obj, &vptr, pchar_descriptor, 0) == SWIG_OK) { + if (cptr) *cptr = (char *)vptr; + if (psize) *psize = vptr ? (strlen((char*)vptr) + 1) : 0; + if (alloc) *alloc = SWIG_OLDOBJ; + return SWIG_OK; + } + } + } + return SWIG_TypeError; +} +} + +%fragment("SWIG_FromCharPtrAndSize","header",fragment="SWIG_pchar_descriptor") { +SWIGINTERNINLINE VALUE +SWIG_FromCharPtrAndSize(const char* carray, size_t size) +{ + if (carray) { + if (size > LONG_MAX) { + swig_type_info* pchar_descriptor = SWIG_pchar_descriptor(); + return pchar_descriptor ? + SWIG_NewPointerObj(%const_cast(carray,char *), pchar_descriptor, 0) : Qnil; + } else { + return rb_str_new(carray, %numeric_cast(size,long)); + } + } else { + return Qnil; + } +} +} + From dbc181c01393f6622f8d27834aaa3007703af017 Mon Sep 17 00:00:00 2001 From: Julien Marrec Date: Tue, 8 Jun 2021 09:24:43 +0200 Subject: [PATCH 2/2] Fix #3514 - replace rubystrings.swg fragment to use rb_utf8_str_new instead of rb_str_new This swg will be picked BEFORE the system one. It is already added as an include directory in ProjectMacros.cmake. fragment are only defined once (first in is kept). The %include directory works by checking the include dirs before the swig library location, cf http://www.swig.org/Doc4.0/Library.html#Library_nn2 --- ruby/rubystrings.swg | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/ruby/rubystrings.swg b/ruby/rubystrings.swg index 3adf0008bcb..f6ce8635423 100644 --- a/ruby/rubystrings.swg +++ b/ruby/rubystrings.swg @@ -1,5 +1,5 @@ /* ------------------------------------------------------------ - * utility methods for char strings + * utility methods for char strings * ------------------------------------------------------------ */ %fragment("SWIG_AsCharPtrAndSize","header",fragment="SWIG_pchar_descriptor") { @@ -7,16 +7,16 @@ SWIGINTERN int SWIG_AsCharPtrAndSize(VALUE obj, char** cptr, size_t* psize, int *alloc) { if (TYPE(obj) == T_STRING) { - char *cstr = StringValuePtr(obj); + char *cstr = StringValuePtr(obj); size_t size = RSTRING_LEN(obj) + 1; if (cptr) { if (alloc) { - if (*alloc == SWIG_NEWOBJ) { - *cptr = %new_copy_array(cstr, size, char); - } else { - *cptr = cstr; - *alloc = SWIG_OLDOBJ; - } + if (*alloc == SWIG_NEWOBJ) { + *cptr = %new_copy_array(cstr, size, char); + } else { + *cptr = cstr; + *alloc = SWIG_OLDOBJ; + } } } if (psize) *psize = size; @@ -26,28 +26,28 @@ SWIG_AsCharPtrAndSize(VALUE obj, char** cptr, size_t* psize, int *alloc) if (pchar_descriptor) { void* vptr = 0; if (SWIG_ConvertPtr(obj, &vptr, pchar_descriptor, 0) == SWIG_OK) { - if (cptr) *cptr = (char *)vptr; - if (psize) *psize = vptr ? (strlen((char*)vptr) + 1) : 0; - if (alloc) *alloc = SWIG_OLDOBJ; - return SWIG_OK; + if (cptr) *cptr = (char *)vptr; + if (psize) *psize = vptr ? (strlen((char*)vptr) + 1) : 0; + if (alloc) *alloc = SWIG_OLDOBJ; + return SWIG_OK; } } - } + } return SWIG_TypeError; } } %fragment("SWIG_FromCharPtrAndSize","header",fragment="SWIG_pchar_descriptor") { -SWIGINTERNINLINE VALUE +SWIGINTERNINLINE VALUE SWIG_FromCharPtrAndSize(const char* carray, size_t size) { if (carray) { if (size > LONG_MAX) { swig_type_info* pchar_descriptor = SWIG_pchar_descriptor(); - return pchar_descriptor ? - SWIG_NewPointerObj(%const_cast(carray,char *), pchar_descriptor, 0) : Qnil; + return pchar_descriptor ? + SWIG_NewPointerObj(%const_cast(carray,char *), pchar_descriptor, 0) : Qnil; } else { - return rb_str_new(carray, %numeric_cast(size,long)); + return rb_utf8_str_new(carray, %numeric_cast(size,long)); } } else { return Qnil;