Module: IDN::Idna

Defined in:
ext/idna.c

Constant Summary collapse

ACE_PREFIX =
rb_str_new2(IDNA_ACE_PREFIX)
ALLOW_UNASSIGNED =
INT2FIX(IDNA_ALLOW_UNASSIGNED)
USE_STD3_ASCII_RULES =
INT2FIX(IDNA_USE_STD3_ASCII_RULES)

Class Method Summary collapse

Class Method Details

.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.

Returns:

  • (String)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'ext/idna.c', line 72

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;
}

.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.

Returns:

  • (String)


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'ext/idna.c', line 112

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;
}