Module: Addressable::IDNA

Extended by:
Gem::Deprecate
Defined in:
lib/addressable/idna/pure.rb,
lib/addressable/idna/native.rb

Defined Under Namespace

Classes: PunycodeBadInput, PunycodeBigOutput, PunycodeOverflow

Constant Summary collapse

UNICODE_TABLE =
File.expand_path(
  File.join(File.dirname(__FILE__), '../../..', 'data/unicode.data')
)
ACE_PREFIX =
"xn--"
UTF8_REGEX =
/\A(?:
[\x09\x0A\x0D\x20-\x7E]               # ASCII
| [\xC2-\xDF][\x80-\xBF]              # non-overlong 2-byte
| \xE0[\xA0-\xBF][\x80-\xBF]          # excluding overlongs
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}   # straight 3-byte
| \xED[\x80-\x9F][\x80-\xBF]          # excluding surrogates
| \xF0[\x90-\xBF][\x80-\xBF]{2}       # planes 1-3
| [\xF1-\xF3][\x80-\xBF]{3}           # planes 4nil5
| \xF4[\x80-\x8F][\x80-\xBF]{2}       # plane 16
)*\z/mnx
UTF8_REGEX_MULTIBYTE =
/(?:
[\xC2-\xDF][\x80-\xBF]                # non-overlong 2-byte
| \xE0[\xA0-\xBF][\x80-\xBF]          # excluding overlongs
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}   # straight 3-byte
| \xED[\x80-\x9F][\x80-\xBF]          # excluding surrogates
| \xF0[\x90-\xBF][\x80-\xBF]{2}       # planes 1-3
| [\xF1-\xF3][\x80-\xBF]{3}           # planes 4nil5
| \xF4[\x80-\x8F][\x80-\xBF]{2}       # plane 16
)/mnx
UNICODE_DATA_COMBINING_CLASS =
0
UNICODE_DATA_EXCLUSION =
1
UNICODE_DATA_CANONICAL =
2
UNICODE_DATA_COMPATIBILITY =
3
UNICODE_DATA_UPPERCASE =
4
UNICODE_DATA_LOWERCASE =
5
UNICODE_DATA_TITLECASE =
6
COMPOSITION_TABLE =
{}
UNICODE_MAX_LENGTH =
256
ACE_MAX_LENGTH =
256
PUNYCODE_BASE =
36
PUNYCODE_TMIN =
1
PUNYCODE_TMAX =
26
PUNYCODE_SKEW =
38
PUNYCODE_DAMP =
700
PUNYCODE_INITIAL_BIAS =
72
PUNYCODE_INITIAL_N =
0x80
PUNYCODE_DELIMITER =
0x2D
PUNYCODE_MAXINT =
1 << 64
PUNYCODE_PRINT_ASCII =
"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" +
"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" +
" !\"\#$%&'()*+,-./" +
"0123456789:;<=>?" +
"@ABCDEFGHIJKLMNO" +
"PQRSTUVWXYZ[\\]^_" +
"`abcdefghijklmno" +
"pqrstuvwxyz{|}~\n"

Class Method Summary collapse

Class Method Details

.to_ascii(value) ⇒ Object

Converts from a Unicode internationalized domain name to an ASCII domain name as described in RFC 3490.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/addressable/idna/pure.rb', line 67

def self.to_ascii(input)
  input = input.to_s unless input.is_a?(String)
  input = input.dup.force_encoding(Encoding::UTF_8).unicode_normalize(:nfkc)
  if input.respond_to?(:force_encoding)
    input.force_encoding(Encoding::ASCII_8BIT)
  end
  if input =~ UTF8_REGEX && input =~ UTF8_REGEX_MULTIBYTE
    parts = unicode_downcase(input).split('.')
    parts.map! do |part|
      if part.respond_to?(:force_encoding)
        part.force_encoding(Encoding::ASCII_8BIT)
      end
      if part =~ UTF8_REGEX && part =~ UTF8_REGEX_MULTIBYTE
        ACE_PREFIX + punycode_encode(part)
      else
        part
      end
    end
    parts.join('.')
  else
    input
  end
end

.to_unicode(value) ⇒ Object

Converts from an ASCII domain name to a Unicode internationalized domain name as described in RFC 3490.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/addressable/idna/pure.rb', line 93

def self.to_unicode(input)
  input = input.to_s unless input.is_a?(String)
  parts = input.split('.')
  parts.map! do |part|
    if part =~ /^#{ACE_PREFIX}(.+)/
      begin
        punycode_decode(part[/^#{ACE_PREFIX}(.+)/, 1])
      rescue Addressable::IDNA::PunycodeBadInput
        # toUnicode is explicitly defined as never-fails by the spec
        part
      end
    else
      part
    end
  end
  output = parts.join('.')
  if output.respond_to?(:force_encoding)
    output.force_encoding(Encoding::UTF_8)
  end
  output
end

.unicode_normalize_kc(value) ⇒ Object

Deprecated.

Use String#unicode_normalize(:nfkc) instead



117
118
119
# File 'lib/addressable/idna/pure.rb', line 117

def unicode_normalize_kc(value)
  value.to_s.unicode_normalize(:nfkc)
end