Skip to content

Commit

Permalink
[ruby] use WCHAR_MAX to determine the encoding of std::wstring.
Browse files Browse the repository at this point in the history
  • Loading branch information
tamuratak committed Feb 19, 2017
1 parent bfe7204 commit 5722d81
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
32 changes: 7 additions & 25 deletions Lib/ruby/rubywstrings.swg
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
/* -----------------------------------------------------------------------------
* rubywstrings.swg
*
* Currently, Ruby does not support Unicode or WChar properly, so these
* are still treated as char arrays for now.
* There are other libraries available that add support to this in
* ruby including WString, FXString, etc.
* ----------------------------------------------------------------------------- */

/* ------------------------------------------------------------
* utility methods for wchar_t strings
* ------------------------------------------------------------ */

%fragment("SWIG_AsWCharPtrAndSize","header",fragment="<wchar.h>",fragment="SWIG_pwchar_descriptor",fragment="SWIG_AsCharPtrAndSize") {
SWIGINTERN int
SWIG_AsWCharPtrAndSize(VALUE obj, wchar_t **cptr, size_t *psize, int *alloc)
{
VALUE tmp;
VALUE tmp = rb_str_conv_enc(obj, rb_enc_get(obj),
rb_to_encoding(rb_str_new_cstr( SWIG_RUBY_WSTRING_ENCODING )) );
*alloc = SWIG_NEWOBJ;
if(sizeof(wchar_t) == 4) {
tmp = rb_str_conv_enc(obj, rb_enc_get(obj), rb_to_encoding(rb_str_new_cstr("UTF-32")) );
} else if (sizeof(wchar_t) == 2) {
tmp = rb_str_conv_enc(obj, rb_enc_get(obj), rb_to_encoding(rb_str_new_cstr("UTF-16")) );
} else {
rb_raise(rb_eRuntimeError, "unsupported wchar_t size");
}
return SWIG_AsCharPtrAndSize( tmp, (char**)cptr, psize, alloc);

return SWIG_AsCharPtrAndSize(tmp, (char**)cptr, psize, alloc);
}
}

Expand All @@ -35,18 +22,13 @@ SWIG_FromWCharPtrAndSize(const wchar_t * carray, size_t size)
{
VALUE ret = SWIG_FromCharPtrAndSize( (const char*)carray, size);
rb_encoding* enc = rb_default_internal_encoding();
if(sizeof(wchar_t) == 4) {
rb_enc_associate(ret, rb_to_encoding(rb_str_new_cstr("UTF-32")));
} else if (sizeof(wchar_t) == 2) {
rb_enc_associate(ret, rb_to_encoding(rb_str_new_cstr("UTF-16")));
} else {
rb_raise(rb_eRuntimeError, "unsupported wchar_t size");
}

rb_enc_associate(ret, rb_to_encoding(rb_str_new_cstr( SWIG_RUBY_WSTRING_ENCODING )) );

if( !enc ) {
enc = rb_to_encoding(rb_str_new_cstr("UTF-8"));
enc = rb_to_encoding(rb_str_new_cstr( SWIG_RUBY_INTERNAL_ENCODING ));
}
return rb_str_conv_enc(ret, rb_enc_get(ret), enc );
return rb_str_conv_enc(ret, rb_enc_get(ret), enc);
}
}

Expand Down
25 changes: 25 additions & 0 deletions Lib/ruby/std_wstring.i
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,31 @@ extern "C" {
#include "ruby/encoding.h"
#endif

/**
* The internal encoding of std::wstring is defined based on
* the size of wchar_t. If it is not appropriate for your library,
* SWIG_RUBY_WSTRING_ENCODING must be given when compiling.
*/
#ifndef SWIG_RUBY_WSTRING_ENCODING

#if WCHAR_MAX == 0x7fff || WCHAR_MAX == 0xffff
#define SWIG_RUBY_WSTRING_ENCODING "UTF-16"
#elif WCHAR_MAX == 0x7fffffff || WCHAR_MAX == 0xffffffff
#define SWIG_RUBY_WSTRING_ENCODING "UTF-32"
#else
#error unsupported wchar_t size. SWIG_RUBY_WSTRING_ENCODING must be given.
#endif

#endif

/**
* If Encoding.default_internal is nil, this encoding will be used
* when coverting from std::wstring to String object in Ruby.
*/
#ifndef SWIG_RUBY_INTERNAL_ENCODING
#define SWIG_RUBY_INTERNAL_ENCODING "UTF-8"
#endif

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 5722d81

Please sign in to comment.