Module | IDN::Idna |
In: |
ext/idna.c
|
The Idna module of LibIDN Ruby Bindings.
require 'idn' include IDN puts 'ACE-Prefix: ' + Idna::ACE_PREFIX domain = Idna.toUnicode('xn--rksmrgs-5wao1o.josefsson.org', Idna::USE_STD3_ASCII_RULES | Idna::ALLOW_UNASSIGNED)
ACE_PREFIX
ALLOW_UNASSIGNED
USE_STD3_ASCII_RULES
ACE_PREFIX | = | rb_str_new2(IDNA_ACE_PREFIX) |
ALLOW_UNASSIGNED | = | INT2FIX(IDNA_ALLOW_UNASSIGNED) |
USE_STD3_ASCII_RULES | = | INT2FIX(IDNA_USE_STD3_ASCII_RULES) |
Converts a domain name in UTF-8 format into an ASCII string. The domain name may contain several labels, separated by dots.
Raises IDN::Idna::IdnaError on failure.
/* * call-seq: * IDN::Idna.toASCII(string, flags=nil) => string * * Converts a domain name in UTF-8 format into an ASCII string. The domain * name may contain several labels, separated by dots. * * Raises IDN::Idna::IdnaError on failure. */ static VALUE toASCII(int argc, VALUE argv[], VALUE self) { int rc; char *buf; VALUE str, flags, retv; rb_scan_args(argc, argv, "11", &str, &flags); str = rb_check_convert_type(str, T_STRING, "String", "to_s"); if (flags != Qnil) { Check_Type(flags, T_FIXNUM); flags = FIX2INT(flags); } else { flags = 0x0000; } rc = idna_to_ascii_8z(RSTRING(str)->ptr, &buf, flags); if (rc != IDNA_SUCCESS) { xfree(buf); rb_raise(eIdnaError, "%s (%d)", idna_strerror(rc), rc); return Qnil; } retv = rb_str_new2(buf); xfree(buf); return retv; }
Converts a possibly ACE encoded domain name in UTF-8 format into an UTF-8 string. The domain name may contain several labels, separated by dots.
Raises IDN::Idna::IdnaError on failure.
/* * call-seq: * IDN::Idna.toUnicode(string, flags=nil) => string * * Converts a possibly ACE encoded domain name in UTF-8 format into an * UTF-8 string. The domain name may contain several labels, separated * by dots. * * Raises IDN::Idna::IdnaError on failure. */ static VALUE toUnicode(int argc, VALUE argv[], VALUE self) { int rc; char *buf; VALUE str, flags, retv; rb_scan_args(argc, argv, "11", &str, &flags); str = rb_check_convert_type(str, T_STRING, "String", "to_s"); if (flags != Qnil) { Check_Type(flags, T_FIXNUM); flags = FIX2INT(flags); } else { flags = 0x0000; } rc = idna_to_unicode_8z8z(RSTRING(str)->ptr, &buf, flags); if (rc != IDNA_SUCCESS) { xfree(buf); rb_raise(eIdnaError, "%s (%d)", idna_strerror(rc), rc); return Qnil; } retv = rb_str_new2(buf); xfree(buf); return retv; }