diff -Nrup ext.bad/idna.c ext/idna.c --- ext.bad/idna.c 2012-02-13 22:16:33.792135714 -0500 +++ ext/idna.c 2012-02-13 23:14:43.562413759 -0500 @@ -24,6 +24,16 @@ #include #include "idn.h" +#include + +#define ENCODED_STR_NEW2(str, encoding) \ + ({ \ + VALUE _string = rb_str_new2((const char *)str); \ + int _enc = rb_enc_find_index(encoding); \ + rb_enc_associate_index(_string, _enc); \ + _string; \ + }) + /* * Document-class: IDN::Idna * The Idna module of LibIDN Ruby Bindings. @@ -85,7 +95,7 @@ static VALUE toASCII(int argc, VALUE arg flags = 0x0000; } - rc = idna_to_ascii_8z(RSTRING(str)->ptr, &buf, flags); + rc = idna_to_ascii_8z(RSTRING_PTR(str), &buf, flags); if (rc != IDNA_SUCCESS) { xfree(buf); @@ -93,7 +103,7 @@ static VALUE toASCII(int argc, VALUE arg return Qnil; } - retv = rb_str_new2(buf); + retv = ENCODED_STR_NEW2(buf, "ASCII-8BIT"); xfree(buf); return retv; } @@ -125,7 +135,7 @@ static VALUE toUnicode(int argc, VALUE a flags = 0x0000; } - rc = idna_to_unicode_8z8z(RSTRING(str)->ptr, &buf, flags); + rc = idna_to_unicode_8z8z(RSTRING_PTR(str), &buf, flags); if (rc != IDNA_SUCCESS) { xfree(buf); @@ -133,7 +143,7 @@ static VALUE toUnicode(int argc, VALUE a return Qnil; } - retv = rb_str_new2(buf); + retv = ENCODED_STR_NEW2(buf, "UTF-8"); xfree(buf); return retv; } diff -Nrup ext.bad/punycode.c ext/punycode.c --- ext.bad/punycode.c 2012-02-13 22:16:33.792135714 -0500 +++ ext/punycode.c 2012-02-13 23:19:39.499714035 -0500 @@ -26,6 +26,14 @@ #include #include "idn.h" +#define ENCODED_STR_NEW(str, len, encoding) \ + ({ \ + VALUE _string = rb_str_new((const char *)str, (long)len); \ + int _enc = rb_enc_find_index(encoding); \ + rb_enc_associate_index(_string, _enc); \ + _string; \ + }) + /* * Document-class: IDN::Punycode * The Punycode module of LibIDN Ruby Bindings. @@ -66,7 +74,7 @@ static VALUE encode(VALUE self, VALUE st VALUE retv; str = rb_check_convert_type(str, T_STRING, "String", "to_s"); - ustr = stringprep_utf8_to_ucs4(RSTRING(str)->ptr, RSTRING(str)->len, &len); + ustr = stringprep_utf8_to_ucs4(RSTRING_PTR(str), RSTRING_LEN(str), &len); while (1) { buf = realloc(buf, buflen); @@ -116,7 +124,7 @@ static VALUE decode(VALUE self, VALUE st str = rb_check_convert_type(str, T_STRING, "String", "to_s"); - len = RSTRING(str)->len; + len = RSTRING_LEN(str); ustr = malloc(len * sizeof(punycode_uint)); if (ustr == NULL) { @@ -124,7 +132,7 @@ static VALUE decode(VALUE self, VALUE st return Qnil; } - rc = punycode_decode(RSTRING(str)->len, RSTRING(str)->ptr, + rc = punycode_decode(RSTRING_LEN(str), RSTRING_PTR(str), &len, ustr, NULL); if (rc != PUNYCODE_SUCCESS) { @@ -134,7 +142,7 @@ static VALUE decode(VALUE self, VALUE st } buf = stringprep_ucs4_to_utf8(ustr, len, NULL, &len); - retv = rb_str_new(buf, len); + retv = ENCODED_STR_NEW(buf, len, "UTF-8"); xfree(ustr); xfree(buf); return retv; diff -Nrup ext.bad/stringprep.c ext/stringprep.c --- ext.bad/stringprep.c 2012-02-13 22:16:33.792135714 -0500 +++ ext/stringprep.c 2012-02-13 22:16:55.618862844 -0500 @@ -64,7 +64,7 @@ static VALUE stringprep_internal(VALUE s VALUE retv; str = rb_check_convert_type(str, T_STRING, "String", "to_s"); - rc = stringprep_profile(RSTRING(str)->ptr, &buf, profile, 0); + rc = stringprep_profile(RSTRING_PTR(str), &buf, profile, 0); if (rc != STRINGPREP_OK) { rb_raise(eStringprepError, "%s (%d)", stringprep_strerror(rc), rc); @@ -135,7 +135,7 @@ static VALUE resourceprep(VALUE self, VA static VALUE with_profile(VALUE self, VALUE str, VALUE profile) { profile = rb_check_convert_type(profile, T_STRING, "String", "to_s"); - return stringprep_internal(str, RSTRING(profile)->ptr); + return stringprep_internal(str, RSTRING_PTR(profile)); } /* @@ -153,7 +153,7 @@ static VALUE nfkc_normalize(VALUE self, VALUE retv; str = rb_check_convert_type(str, T_STRING, "String", "to_s"); - buf = stringprep_utf8_nfkc_normalize(RSTRING(str)->ptr, RSTRING(str)->len); + buf = stringprep_utf8_nfkc_normalize(RSTRING_PTR(str), RSTRING_LEN(str)); retv = rb_str_new2(buf); xfree(buf);