d-unsed / ruru

Native Ruby extensions written in Rust
MIT License
832 stars 40 forks source link

Full Ruby String Encoding Support #94

Open danielpclark opened 6 years ago

danielpclark commented 6 years ago

The rb_str_new method we have is good for making C strings in to Ruby string objects. From here we can go further and implement the string with rb_str_initialize to help also set the encoding.

VALUE
rb_str_initialize(VALUE str, const char *ptr, long len, rb_encoding *enc)
{
    STR_SET_LEN(str, 0);
    rb_enc_associate(str, enc);
    rb_str_cat(str, ptr, len);
    return str;
}

So I believe we should add the method rb_enc_associate to ruby_sys. We'll also likely need to implement encoding structs and have a C rb_encoding type mapped to ruru::Encoding.

typedef const OnigEncodingType rb_encoding;

The obvious places to look are string.c, encoding.c and encoding.h. But the OnigEncodingType (onigmo.h) is a C Regex thing for helping with encodings so I don't know how complex that would be.