Menu

#89 Use Ruby 1.9 string encodings

None
closed
nobody
None
5
2021-12-18
2012-11-09
Tim Jensen
No

Ruby 1.9 introduced string encodings to improve support for internationalization, but SWIG creates Ruby strings with their encodings set to ASCII-8BIT, which is an alias for BINARY. It would be nice if SWIG created Ruby strings with a more text-friendly encoding, such as UTF-8 (http://www.utf8everywhere.org/), or gave the user some way to select their preferred string encoding.

The following code and its resulting script output illustrates the problem with the default string encoding used with Ruby 1.9. Note that because ASCII-8BIT is a binary encoding, the #to_yaml call outputs the string returned by foobar() as base64 binary data instead of human-readable text.

-----test.i-----
%module Test

%{
#include "test.hpp"
%}

%include <stl.i>
%include "test.hpp"

-----test.hpp-----
std::string foobar()
{
return "foobar";
}

-----rbtest.rb-----
require 'Test'
require 'yaml'

puts Test::foobar.encoding
puts Test::foobar.to_yaml

-----output-----
ASCII-8BIT
--- !binary |-
Zm9vYmFy

Discussion

  • Olly Betts

    Olly Betts - 2021-12-18
    • status: open --> closed
    • Group: -->
     
  • Olly Betts

    Olly Betts - 2021-12-18

    https://github.com/swig/swig/issues/2035 has since been opened for this same problem in the current bug tracker, so closing this as a duplicate.

     

Log in to post a comment.