[ruby] use WCHAR_MAX to determine the encoding of std::wstring.
This commit is contained in:
parent
bfe7204576
commit
5722d81a09
2 changed files with 32 additions and 25 deletions
|
|
@ -1,13 +1,6 @@
|
|||
/* -----------------------------------------------------------------------------
|
||||
* 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
|
||||
* ------------------------------------------------------------ */
|
||||
|
||||
|
|
@ -15,17 +8,11 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue