addressable 2.9.1 → 2.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +244 -23
- data/README.md +36 -0
- data/lib/addressable/idna/native.rb +60 -11
- data/lib/addressable/idna/pure.rb +257 -103
- data/lib/addressable/template.rb +2288 -150
- data/lib/addressable/unicode_normalization.rb +58 -0
- data/lib/addressable/uri.rb +510 -152
- data/lib/addressable/version.rb +50 -2
- metadata +4 -3
|
@@ -17,6 +17,8 @@
|
|
|
17
17
|
#++
|
|
18
18
|
|
|
19
19
|
|
|
20
|
+
require "addressable/unicode_normalization"
|
|
21
|
+
|
|
20
22
|
module Addressable
|
|
21
23
|
module IDNA
|
|
22
24
|
# This module is loosely based on idn_actionmailer by Mick Staugaard,
|
|
@@ -55,9 +57,12 @@ module Addressable
|
|
|
55
57
|
| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
|
|
56
58
|
)/mnx
|
|
57
59
|
|
|
58
|
-
# Characters that nameprep
|
|
59
|
-
#
|
|
60
|
-
# toASCII
|
|
60
|
+
# Characters that nameprep, the IDNA2003 profile of stringprep, doesn't
|
|
61
|
+
# allow in a label (Section 5 of RFC 3491, since obsoleted by RFC 5891),
|
|
62
|
+
# or that it maps to nothing, so they can't be in a label that toASCII
|
|
63
|
+
# produces. Some code points are in more than one table; they're
|
|
64
|
+
# listed only under the first, since Ruby warns about a duplicated range
|
|
65
|
+
# in a character class.
|
|
61
66
|
NAMEPREP_PROHIBITED = Regexp.new("[" + [
|
|
62
67
|
# Table C.1.2, non-ASCII space characters
|
|
63
68
|
"\u00A0\u1680\u2000-\u200B\u202F\u205F\u3000",
|
|
@@ -70,41 +75,174 @@ module Addressable
|
|
|
70
75
|
"\uFDD0-\uFDEF" + (0..16).map do |plane|
|
|
71
76
|
[plane * 0x10000 + 0xFFFE, 0x2D, plane * 0x10000 + 0xFFFF].pack("U*")
|
|
72
77
|
end.join,
|
|
73
|
-
# Table C.6, inappropriate for plain text
|
|
74
|
-
"\
|
|
78
|
+
# Table C.6, inappropriate for plain text (U+FFF9 to U+FFFC are in C.2.2)
|
|
79
|
+
"\uFFFD",
|
|
75
80
|
# Table C.7, inappropriate for canonical representation
|
|
76
81
|
"\u2FF0-\u2FFB",
|
|
77
|
-
# Table C.8, change display properties or are deprecated
|
|
78
|
-
|
|
82
|
+
# Table C.8, change display properties or are deprecated (U+206A to
|
|
83
|
+
# U+206F are in C.2.2)
|
|
84
|
+
"\u0340\u0341\u200E\u200F\u202A-\u202E",
|
|
79
85
|
# Table C.9, tagging characters
|
|
80
86
|
"\u{E0001}\u{E0020}-\u{E007F}",
|
|
81
|
-
# Table B.1, commonly mapped to nothing
|
|
82
|
-
|
|
87
|
+
# Table B.1, commonly mapped to nothing (U+200B is in C.1.2, and U+200C,
|
|
88
|
+
# U+200D, U+2060 and U+FEFF are in C.2.2)
|
|
89
|
+
"\u00AD\u034F\u1806\u180B-\u180D\uFE00-\uFE0F"
|
|
83
90
|
].join + "]")
|
|
84
91
|
|
|
85
92
|
# Characters that change how a label displays without being visible
|
|
86
93
|
# themselves: controls, format characters such as bidi overrides and
|
|
87
94
|
# isolates, and default ignorable code points, including those added to
|
|
88
|
-
# Unicode after the version nameprep is based on.
|
|
89
|
-
|
|
95
|
+
# Unicode after the version nameprep is based on. Format characters that
|
|
96
|
+
# are also default ignorable are left out of the second set, since Ruby
|
|
97
|
+
# warns about a duplicated range in a character class.
|
|
98
|
+
DISPLAY_CHANGING = /[\p{Cc}\p{Cf}[\p{Default_Ignorable_Code_Point}&&\P{Cf}]]/
|
|
99
|
+
|
|
100
|
+
# The label separators of IDNA2003 (Section 3.1 of RFC 3490, since
|
|
101
|
+
# obsoleted by RFC 5890 and RFC 5891). A label that decodes to one of
|
|
102
|
+
# them would display as two labels.
|
|
103
|
+
LABEL_SEPARATORS = /[.\u3002\uFF0E\uFF61]/
|
|
104
|
+
|
|
105
|
+
# Characters with the bidirectional category R or AL (Table D.1 of
|
|
106
|
+
# RFC 3454, since obsoleted by PRECIS, RFC 8264).
|
|
107
|
+
RAND_AL_CAT = Regexp.new("[" + [
|
|
108
|
+
"\u05BE\u05C0\u05C3\u05D0-\u05EA\u05F0-\u05F4\u061B\u061F\u0621-\u063A",
|
|
109
|
+
"\u0640-\u064A\u066D-\u066F\u0671-\u06D5\u06DD\u06E5\u06E6\u06FA-\u06FE",
|
|
110
|
+
"\u0700-\u070D\u0710\u0712-\u072C\u0780-\u07A5\u07B1\u200F\uFB1D",
|
|
111
|
+
"\uFB1F-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44",
|
|
112
|
+
"\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFC",
|
|
113
|
+
"\uFE70-\uFE74\uFE76-\uFEFC"
|
|
114
|
+
].join + "]")
|
|
115
|
+
|
|
116
|
+
# Characters with the bidirectional category L (Table D.2 of RFC 3454,
|
|
117
|
+
# leaving out the surrogates, which can't be in a UTF-8 string).
|
|
118
|
+
L_CAT = Regexp.new("[" + [
|
|
119
|
+
"\u0041-\u005A\u0061-\u007A\u00AA\u00B5\u00BA\u00C0-\u00D6\u00D8-\u00F6",
|
|
120
|
+
"\u00F8-\u0220\u0222-\u0233\u0250-\u02AD\u02B0-\u02B8\u02BB-\u02C1",
|
|
121
|
+
"\u02D0\u02D1\u02E0-\u02E4\u02EE\u037A\u0386\u0388-\u038A\u038C",
|
|
122
|
+
"\u038E-\u03A1\u03A3-\u03CE\u03D0-\u03F5\u0400-\u0482\u048A-\u04CE",
|
|
123
|
+
"\u04D0-\u04F5\u04F8\u04F9\u0500-\u050F\u0531-\u0556\u0559-\u055F",
|
|
124
|
+
"\u0561-\u0587\u0589\u0903\u0905-\u0939\u093D-\u0940\u0949-\u094C\u0950",
|
|
125
|
+
"\u0958-\u0961\u0964-\u0970\u0982\u0983\u0985-\u098C\u098F\u0990",
|
|
126
|
+
"\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BE-\u09C0\u09C7\u09C8",
|
|
127
|
+
"\u09CB\u09CC\u09D7\u09DC\u09DD\u09DF-\u09E1\u09E6-\u09F1\u09F4-\u09FA",
|
|
128
|
+
"\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33",
|
|
129
|
+
"\u0A35\u0A36\u0A38\u0A39\u0A3E-\u0A40\u0A59-\u0A5C\u0A5E\u0A66-\u0A6F",
|
|
130
|
+
"\u0A72-\u0A74\u0A83\u0A85-\u0A8B\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8",
|
|
131
|
+
"\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABD-\u0AC0\u0AC9\u0ACB\u0ACC",
|
|
132
|
+
"\u0AD0\u0AE0\u0AE6-\u0AEF\u0B02\u0B03\u0B05-\u0B0C\u0B0F\u0B10",
|
|
133
|
+
"\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B36-\u0B39\u0B3D\u0B3E\u0B40",
|
|
134
|
+
"\u0B47\u0B48\u0B4B\u0B4C\u0B57\u0B5C\u0B5D\u0B5F-\u0B61\u0B66-\u0B70",
|
|
135
|
+
"\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C",
|
|
136
|
+
"\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB5\u0BB7-\u0BB9",
|
|
137
|
+
"\u0BBE\u0BBF\u0BC1\u0BC2\u0BC6-\u0BC8\u0BCA-\u0BCC\u0BD7\u0BE7-\u0BF2",
|
|
138
|
+
"\u0C01-\u0C03\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C33",
|
|
139
|
+
"\u0C35-\u0C39\u0C41-\u0C44\u0C60\u0C61\u0C66-\u0C6F\u0C82\u0C83",
|
|
140
|
+
"\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9",
|
|
141
|
+
"\u0CBE\u0CC0-\u0CC4\u0CC7\u0CC8\u0CCA\u0CCB\u0CD5\u0CD6\u0CDE",
|
|
142
|
+
"\u0CE0\u0CE1\u0CE6-\u0CEF\u0D02\u0D03\u0D05-\u0D0C\u0D0E-\u0D10",
|
|
143
|
+
"\u0D12-\u0D28\u0D2A-\u0D39\u0D3E-\u0D40\u0D46-\u0D48\u0D4A-\u0D4C",
|
|
144
|
+
"\u0D57\u0D60\u0D61\u0D66-\u0D6F\u0D82\u0D83\u0D85-\u0D96\u0D9A-\u0DB1",
|
|
145
|
+
"\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0DCF-\u0DD1\u0DD8-\u0DDF",
|
|
146
|
+
"\u0DF2-\u0DF4\u0E01-\u0E30\u0E32\u0E33\u0E40-\u0E46\u0E4F-\u0E5B",
|
|
147
|
+
"\u0E81\u0E82\u0E84\u0E87\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F",
|
|
148
|
+
"\u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA\u0EAB\u0EAD-\u0EB0\u0EB2\u0EB3\u0EBD",
|
|
149
|
+
"\u0EC0-\u0EC4\u0EC6\u0ED0-\u0ED9\u0EDC\u0EDD\u0F00-\u0F17\u0F1A-\u0F34",
|
|
150
|
+
"\u0F36\u0F38\u0F3E-\u0F47\u0F49-\u0F6A\u0F7F\u0F85\u0F88-\u0F8B",
|
|
151
|
+
"\u0FBE-\u0FC5\u0FC7-\u0FCC\u0FCF\u1000-\u1021\u1023-\u1027\u1029\u102A",
|
|
152
|
+
"\u102C\u1031\u1038\u1040-\u1057\u10A0-\u10C5\u10D0-\u10F8\u10FB",
|
|
153
|
+
"\u1100-\u1159\u115F-\u11A2\u11A8-\u11F9\u1200-\u1206\u1208-\u1246",
|
|
154
|
+
"\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1286\u1288",
|
|
155
|
+
"\u128A-\u128D\u1290-\u12AE\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0",
|
|
156
|
+
"\u12C2-\u12C5\u12C8-\u12CE\u12D0-\u12D6\u12D8-\u12EE\u12F0-\u130E",
|
|
157
|
+
"\u1310\u1312-\u1315\u1318-\u131E\u1320-\u1346\u1348-\u135A",
|
|
158
|
+
"\u1361-\u137C\u13A0-\u13F4\u1401-\u1676\u1681-\u169A\u16A0-\u16F0",
|
|
159
|
+
"\u1700-\u170C\u170E-\u1711\u1720-\u1731\u1735\u1736\u1740-\u1751",
|
|
160
|
+
"\u1760-\u176C\u176E-\u1770\u1780-\u17B6\u17BE-\u17C5\u17C7\u17C8",
|
|
161
|
+
"\u17D4-\u17DA\u17DC\u17E0-\u17E9\u1810-\u1819\u1820-\u1877",
|
|
162
|
+
"\u1880-\u18A8\u1E00-\u1E9B\u1EA0-\u1EF9\u1F00-\u1F15\u1F18-\u1F1D",
|
|
163
|
+
"\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D",
|
|
164
|
+
"\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC",
|
|
165
|
+
"\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC",
|
|
166
|
+
"\u200E\u2071\u207F\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124",
|
|
167
|
+
"\u2126\u2128\u212A-\u212D\u212F-\u2131\u2133-\u2139\u213D-\u213F",
|
|
168
|
+
"\u2145-\u2149\u2160-\u2183\u2336-\u237A\u2395\u249C-\u24E9",
|
|
169
|
+
"\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303C\u3041-\u3096",
|
|
170
|
+
"\u309D-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312C\u3131-\u318E",
|
|
171
|
+
"\u3190-\u31B7\u31F0-\u321C\u3220-\u3243\u3260-\u327B\u327F-\u32B0",
|
|
172
|
+
"\u32C0-\u32CB\u32D0-\u32FE\u3300-\u3376\u337B-\u33DD\u33E0-\u33FE",
|
|
173
|
+
"\u3400-\u4DB5\u4E00-\u9FA5\uA000-\uA48C\uAC00-\uD7A3\uE000-\uFA2D",
|
|
174
|
+
"\uFA30-\uFA6A\uFB00-\uFB06\uFB13-\uFB17\uFF21-\uFF3A\uFF41-\uFF5A",
|
|
175
|
+
"\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC",
|
|
176
|
+
"\u{10300}-\u{1031E}\u{10320}-\u{10323}\u{10330}-\u{1034A}",
|
|
177
|
+
"\u{10400}-\u{10425}\u{10428}-\u{1044D}\u{1D000}-\u{1D0F5}",
|
|
178
|
+
"\u{1D100}-\u{1D126}\u{1D12A}-\u{1D166}\u{1D16A}-\u{1D172}",
|
|
179
|
+
"\u{1D183}\u{1D184}\u{1D18C}-\u{1D1A9}\u{1D1AE}-\u{1D1DD}",
|
|
180
|
+
"\u{1D400}-\u{1D454}\u{1D456}-\u{1D49C}\u{1D49E}\u{1D49F}\u{1D4A2}",
|
|
181
|
+
"\u{1D4A5}\u{1D4A6}\u{1D4A9}-\u{1D4AC}\u{1D4AE}-\u{1D4B9}\u{1D4BB}",
|
|
182
|
+
"\u{1D4BD}-\u{1D4C0}\u{1D4C2}\u{1D4C3}\u{1D4C5}-\u{1D505}",
|
|
183
|
+
"\u{1D507}-\u{1D50A}\u{1D50D}-\u{1D514}\u{1D516}-\u{1D51C}",
|
|
184
|
+
"\u{1D51E}-\u{1D539}\u{1D53B}-\u{1D53E}\u{1D540}-\u{1D544}\u{1D546}",
|
|
185
|
+
"\u{1D54A}-\u{1D550}\u{1D552}-\u{1D6A3}\u{1D6A8}-\u{1D7C9}",
|
|
186
|
+
"\u{20000}-\u{2A6D6}\u{2F800}-\u{2FA1D}\u{F0000}-\u{FFFFD}",
|
|
187
|
+
"\u{100000}-\u{10FFFD}"
|
|
188
|
+
].join + "]")
|
|
189
|
+
|
|
190
|
+
# A host name has only ASCII letters, digits and hyphens, and doesn't
|
|
191
|
+
# start or end with a hyphen. RFC 3987 says to check an IRI's host names
|
|
192
|
+
# with these rules (STD3, section 4.1 of RFC 3490), so a label that
|
|
193
|
+
# decodes to anything else isn't displayed.
|
|
194
|
+
NOT_STD3 = /[\x00-\x2C\x2E\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]|\A-|-\z/
|
|
195
|
+
|
|
196
|
+
# Characters that nameprep maps to nothing (table B.1 of RFC 3454), such
|
|
197
|
+
# as SOFT HYPHEN and ZERO WIDTH JOINER.
|
|
198
|
+
MAPPED_TO_NOTHING =
|
|
199
|
+
/[\u00AD\u034F\u1806\u180B-\u180D\u200B-\u200D\u2060\uFE00-\uFE0F\uFEFF]/
|
|
200
|
+
|
|
201
|
+
# A label is empty after nameprep or breaks the bidi rule, as libidn
|
|
202
|
+
# reports with IDN::Idna::IdnaError.
|
|
203
|
+
class InvalidLabel < StandardError; end
|
|
90
204
|
|
|
91
205
|
# :startdoc:
|
|
92
206
|
|
|
93
207
|
# Converts from a Unicode internationalized domain name to an ASCII
|
|
94
|
-
# domain name as described in RFC 3490
|
|
208
|
+
# domain name as described in IDNA2003 (RFC 3490, since obsoleted by
|
|
209
|
+
# RFC 5890 and RFC 5891).
|
|
95
210
|
def self.to_ascii(input)
|
|
96
211
|
input = input.to_s unless input.is_a?(String)
|
|
97
|
-
input = input.dup.force_encoding(Encoding::UTF_8)
|
|
212
|
+
input = input.dup.force_encoding(Encoding::UTF_8)
|
|
213
|
+
# A label made only of characters that nameprep maps to nothing would
|
|
214
|
+
# be left empty, so it's invalid, as it is for libidn. A label that's
|
|
215
|
+
# already empty, as in "a..b", is kept.
|
|
216
|
+
empty = input.split(LABEL_SEPARATORS, -1).any? do |label|
|
|
217
|
+
!label.empty? && label.gsub(MAPPED_TO_NOTHING, '').empty?
|
|
218
|
+
end
|
|
219
|
+
if empty
|
|
220
|
+
raise InvalidLabel,
|
|
221
|
+
"A label is empty after removing the characters nameprep maps " \
|
|
222
|
+
"to nothing."
|
|
223
|
+
end
|
|
224
|
+
input = input.gsub(MAPPED_TO_NOTHING, '')
|
|
225
|
+
input = UnicodeNormalization.normalize(input, :nfkc)
|
|
226
|
+
# NFKC maps U+FF0E to a dot and U+FF61 to U+3002. Map U+3002 to a dot
|
|
227
|
+
# too, so that each label separator of IDNA2003 (Section 3.1 of
|
|
228
|
+
# RFC 3490) separates labels.
|
|
229
|
+
input = input.tr("\u3002", ".")
|
|
98
230
|
if input.respond_to?(:force_encoding)
|
|
99
231
|
input.force_encoding(Encoding::ASCII_8BIT)
|
|
100
232
|
end
|
|
101
233
|
if input =~ UTF8_REGEX && input =~ UTF8_REGEX_MULTIBYTE
|
|
102
|
-
parts = unicode_downcase(input).split('.')
|
|
234
|
+
parts = unicode_downcase(input).split('.', -1)
|
|
103
235
|
parts.map! do |part|
|
|
104
236
|
if part.respond_to?(:force_encoding)
|
|
105
237
|
part.force_encoding(Encoding::ASCII_8BIT)
|
|
106
238
|
end
|
|
107
239
|
if part =~ UTF8_REGEX && part =~ UTF8_REGEX_MULTIBYTE
|
|
240
|
+
# A label that breaks the bidi rule is invalid, as it is for
|
|
241
|
+
# libidn.
|
|
242
|
+
if bidi_rule_broken?(part.dup.force_encoding(Encoding::UTF_8))
|
|
243
|
+
raise InvalidLabel,
|
|
244
|
+
"A label breaks the bidi rule of Section 6 of RFC 3454."
|
|
245
|
+
end
|
|
108
246
|
ACE_PREFIX + punycode_encode(part)
|
|
109
247
|
else
|
|
110
248
|
part
|
|
@@ -117,21 +255,25 @@ module Addressable
|
|
|
117
255
|
end
|
|
118
256
|
|
|
119
257
|
# Converts from an ASCII domain name to a Unicode internationalized
|
|
120
|
-
# domain name as described in RFC 3490
|
|
258
|
+
# domain name as described in IDNA2003 (RFC 3490, since obsoleted by
|
|
259
|
+
# RFC 5890 and RFC 5891).
|
|
121
260
|
def self.to_unicode(input)
|
|
122
261
|
input = input.to_s unless input.is_a?(String)
|
|
123
|
-
parts = input.split('.')
|
|
262
|
+
parts = input.split('.', -1)
|
|
124
263
|
parts.map! do |part|
|
|
125
264
|
if part =~ /^#{ACE_PREFIX}(.+)/
|
|
126
265
|
begin
|
|
127
266
|
decoded = punycode_decode(part[/^#{ACE_PREFIX}(.+)/, 1])
|
|
128
267
|
# A label that doesn't convert back to itself isn't a valid
|
|
129
|
-
# A-label (Section
|
|
130
|
-
#
|
|
131
|
-
#
|
|
132
|
-
# unchanged too
|
|
268
|
+
# A-label (Section 5.3 of RFC 5891). That includes a label that
|
|
269
|
+
# decodes to only ASCII characters. A label with a character that
|
|
270
|
+
# would change how it displays, or that a host name can't have, is
|
|
271
|
+
# returned unchanged too, as is a label with a label separator or
|
|
272
|
+
# one that breaks the bidi rule.
|
|
133
273
|
if to_ascii(decoded).downcase != part.downcase ||
|
|
134
|
-
decoded =~ NAMEPREP_PROHIBITED || decoded =~ DISPLAY_CHANGING
|
|
274
|
+
decoded =~ NAMEPREP_PROHIBITED || decoded =~ DISPLAY_CHANGING ||
|
|
275
|
+
decoded =~ LABEL_SEPARATORS || decoded =~ NOT_STD3 ||
|
|
276
|
+
bidi_rule_broken?(decoded)
|
|
135
277
|
part
|
|
136
278
|
else
|
|
137
279
|
decoded
|
|
@@ -139,6 +281,7 @@ module Addressable
|
|
|
139
281
|
rescue Addressable::IDNA::PunycodeBadInput,
|
|
140
282
|
Addressable::IDNA::PunycodeBigOutput,
|
|
141
283
|
Addressable::IDNA::PunycodeOverflow,
|
|
284
|
+
Addressable::IDNA::InvalidLabel,
|
|
142
285
|
RangeError, ArgumentError
|
|
143
286
|
# toUnicode is explicitly defined as never-fails by the spec
|
|
144
287
|
part
|
|
@@ -164,6 +307,16 @@ module Addressable
|
|
|
164
307
|
deprecate :unicode_normalize_kc, "String#unicode_normalize(:nfkc)", 2023, 4
|
|
165
308
|
end
|
|
166
309
|
|
|
310
|
+
# A label with a right-to-left character must not have a left-to-right
|
|
311
|
+
# one, and must start and end with a right-to-left one (Section 6 of
|
|
312
|
+
# RFC 3454). Otherwise it can display in a different order than it's
|
|
313
|
+
# written.
|
|
314
|
+
def self.bidi_rule_broken?(label)
|
|
315
|
+
return false unless label =~ RAND_AL_CAT
|
|
316
|
+
label =~ L_CAT || label[0] !~ RAND_AL_CAT || label[-1] !~ RAND_AL_CAT
|
|
317
|
+
end
|
|
318
|
+
private_class_method :bidi_rule_broken?
|
|
319
|
+
|
|
167
320
|
##
|
|
168
321
|
# Unicode aware downcase method.
|
|
169
322
|
#
|
|
@@ -4462,53 +4615,58 @@ module Addressable
|
|
|
4462
4615
|
# Input needs wider integers to process.
|
|
4463
4616
|
class PunycodeOverflow < StandardError; end
|
|
4464
4617
|
|
|
4618
|
+
# The errors .to_ascii raises for a domain name it can't convert.
|
|
4619
|
+
CONVERSION_ERRORS = [
|
|
4620
|
+
InvalidLabel, PunycodeBadInput, PunycodeBigOutput, PunycodeOverflow
|
|
4621
|
+
].freeze
|
|
4622
|
+
|
|
4465
4623
|
def self.punycode_encode(unicode)
|
|
4466
4624
|
unicode = unicode.to_s unless unicode.is_a?(String)
|
|
4467
4625
|
input = unicode.unpack("U*")
|
|
4468
|
-
output = [
|
|
4469
|
-
|
|
4470
|
-
output_length = [ACE_MAX_LENGTH]
|
|
4626
|
+
output = []
|
|
4627
|
+
max_out = ACE_MAX_LENGTH
|
|
4471
4628
|
|
|
4472
|
-
#
|
|
4473
|
-
|
|
4474
|
-
|
|
4475
|
-
|
|
4476
|
-
|
|
4629
|
+
# The positions of each non-basic code point, and a Fenwick tree of
|
|
4630
|
+
# the positions of the code points handled so far, so that counting
|
|
4631
|
+
# the handled code points before a position doesn't take a pass over
|
|
4632
|
+
# the input.
|
|
4633
|
+
positions = {}
|
|
4634
|
+
handled = Array.new(input.size + 1, 0)
|
|
4477
4635
|
|
|
4478
4636
|
# Handle the basic code points:
|
|
4479
|
-
|
|
4480
|
-
if punycode_basic?(
|
|
4481
|
-
if max_out -
|
|
4637
|
+
input.each_with_index do |codepoint, j|
|
|
4638
|
+
if punycode_basic?(codepoint)
|
|
4639
|
+
if max_out - output.size < 2
|
|
4482
4640
|
raise PunycodeBigOutput,
|
|
4483
4641
|
"Output would exceed the space provided."
|
|
4484
4642
|
end
|
|
4485
|
-
output
|
|
4486
|
-
|
|
4643
|
+
output << codepoint
|
|
4644
|
+
handled[j + 1] = 1
|
|
4645
|
+
else
|
|
4646
|
+
(positions[codepoint] ||= []) << j
|
|
4487
4647
|
end
|
|
4488
4648
|
end
|
|
4649
|
+
(1..input.size).each do |i|
|
|
4650
|
+
parent = i + (i & -i)
|
|
4651
|
+
handled[parent] += handled[i] if parent <= input.size
|
|
4652
|
+
end
|
|
4489
4653
|
|
|
4490
|
-
h = b =
|
|
4654
|
+
h = b = output.size
|
|
4491
4655
|
|
|
4492
4656
|
# h is the number of code points that have been handled, b is the
|
|
4493
|
-
# number of basic code points, and
|
|
4494
|
-
# that have been output.
|
|
4495
|
-
|
|
4496
|
-
if b > 0
|
|
4497
|
-
output[out] = PUNYCODE_DELIMITER
|
|
4498
|
-
out += 1
|
|
4499
|
-
end
|
|
4657
|
+
# number of basic code points, and output.size is the number of
|
|
4658
|
+
# characters that have been output.
|
|
4500
4659
|
|
|
4501
|
-
|
|
4660
|
+
output << PUNYCODE_DELIMITER if b > 0
|
|
4502
4661
|
|
|
4503
|
-
|
|
4504
|
-
|
|
4505
|
-
# handled already. Find the next larger one:
|
|
4662
|
+
# Main encoding loop: handle each non-basic code point in ascending
|
|
4663
|
+
# order.
|
|
4506
4664
|
|
|
4507
|
-
|
|
4508
|
-
|
|
4509
|
-
|
|
4510
|
-
end
|
|
4665
|
+
n = PUNYCODE_INITIAL_N
|
|
4666
|
+
delta = 0
|
|
4667
|
+
bias = PUNYCODE_INITIAL_BIAS
|
|
4511
4668
|
|
|
4669
|
+
positions.keys.sort.each do |m|
|
|
4512
4670
|
# Increase delta enough to advance the decoder's
|
|
4513
4671
|
# <n,i> state to <m,0>, but guard against overflow:
|
|
4514
4672
|
|
|
@@ -4518,47 +4676,56 @@ module Addressable
|
|
|
4518
4676
|
delta += (m - n) * (h + 1)
|
|
4519
4677
|
n = m
|
|
4520
4678
|
|
|
4521
|
-
|
|
4522
|
-
|
|
4523
|
-
|
|
4524
|
-
|
|
4525
|
-
|
|
4526
|
-
|
|
4527
|
-
|
|
4528
|
-
|
|
4679
|
+
# Each occurrence of n adds the handled code points since the last
|
|
4680
|
+
# one, which RFC 3492 counts as code points less than n.
|
|
4681
|
+
handled_before_pass = h
|
|
4682
|
+
counted = 0
|
|
4683
|
+
positions[n].each do |position|
|
|
4684
|
+
before = 0
|
|
4685
|
+
i = position
|
|
4686
|
+
while i > 0
|
|
4687
|
+
before += handled[i]
|
|
4688
|
+
i -= i & -i
|
|
4529
4689
|
end
|
|
4690
|
+
delta += before - counted
|
|
4691
|
+
counted = before
|
|
4530
4692
|
|
|
4531
|
-
|
|
4532
|
-
# Represent delta as a generalized variable-length integer:
|
|
4693
|
+
# Represent delta as a generalized variable-length integer:
|
|
4533
4694
|
|
|
4534
|
-
|
|
4535
|
-
|
|
4536
|
-
|
|
4537
|
-
|
|
4538
|
-
|
|
4539
|
-
end
|
|
4540
|
-
t = (
|
|
4541
|
-
if k <= bias
|
|
4542
|
-
PUNYCODE_TMIN
|
|
4543
|
-
elsif k >= bias + PUNYCODE_TMAX
|
|
4544
|
-
PUNYCODE_TMAX
|
|
4545
|
-
else
|
|
4546
|
-
k - bias
|
|
4547
|
-
end
|
|
4548
|
-
)
|
|
4549
|
-
break if q < t
|
|
4550
|
-
output[out] =
|
|
4551
|
-
punycode_encode_digit(t + (q - t) % (PUNYCODE_BASE - t))
|
|
4552
|
-
out += 1
|
|
4553
|
-
q = (q - t) / (PUNYCODE_BASE - t)
|
|
4554
|
-
k += PUNYCODE_BASE
|
|
4695
|
+
q = delta; k = PUNYCODE_BASE
|
|
4696
|
+
while true
|
|
4697
|
+
if output.size >= max_out
|
|
4698
|
+
raise PunycodeBigOutput,
|
|
4699
|
+
"Output would exceed the space provided."
|
|
4555
4700
|
end
|
|
4701
|
+
t = (
|
|
4702
|
+
if k <= bias
|
|
4703
|
+
PUNYCODE_TMIN
|
|
4704
|
+
elsif k >= bias + PUNYCODE_TMAX
|
|
4705
|
+
PUNYCODE_TMAX
|
|
4706
|
+
else
|
|
4707
|
+
k - bias
|
|
4708
|
+
end
|
|
4709
|
+
)
|
|
4710
|
+
break if q < t
|
|
4711
|
+
output <<
|
|
4712
|
+
punycode_encode_digit(t + (q - t) % (PUNYCODE_BASE - t))
|
|
4713
|
+
q = (q - t) / (PUNYCODE_BASE - t)
|
|
4714
|
+
k += PUNYCODE_BASE
|
|
4715
|
+
end
|
|
4556
4716
|
|
|
4557
|
-
|
|
4558
|
-
|
|
4559
|
-
|
|
4560
|
-
|
|
4561
|
-
|
|
4717
|
+
output << punycode_encode_digit(q)
|
|
4718
|
+
bias = punycode_adapt(delta, h + 1, h == b)
|
|
4719
|
+
delta = 0
|
|
4720
|
+
h += 1
|
|
4721
|
+
end
|
|
4722
|
+
delta += handled_before_pass - counted
|
|
4723
|
+
|
|
4724
|
+
positions[n].each do |position|
|
|
4725
|
+
i = position + 1
|
|
4726
|
+
while i <= input.size
|
|
4727
|
+
handled[i] += 1
|
|
4728
|
+
i += i & -i
|
|
4562
4729
|
end
|
|
4563
4730
|
end
|
|
4564
4731
|
|
|
@@ -4566,20 +4733,7 @@ module Addressable
|
|
|
4566
4733
|
n += 1
|
|
4567
4734
|
end
|
|
4568
4735
|
|
|
4569
|
-
|
|
4570
|
-
|
|
4571
|
-
outlen = out
|
|
4572
|
-
outlen.times do |j|
|
|
4573
|
-
c = output[j]
|
|
4574
|
-
unless c >= 0 && c <= 127
|
|
4575
|
-
raise StandardError, "Invalid output char."
|
|
4576
|
-
end
|
|
4577
|
-
unless PUNYCODE_PRINT_ASCII[c]
|
|
4578
|
-
raise PunycodeBadInput, "Input is invalid."
|
|
4579
|
-
end
|
|
4580
|
-
end
|
|
4581
|
-
|
|
4582
|
-
output[0..outlen].map { |x| x.chr }.join("").sub(/\0+\z/, "")
|
|
4736
|
+
output.pack("C*").force_encoding(Encoding::US_ASCII)
|
|
4583
4737
|
end
|
|
4584
4738
|
private_class_method :punycode_encode
|
|
4585
4739
|
|
|
@@ -4724,11 +4878,11 @@ module Addressable
|
|
|
4724
4878
|
# (for use in representing integers) in the range 0 to
|
|
4725
4879
|
# base - 1, or PUNYCODE_BASE if codepoint does not represent a value.
|
|
4726
4880
|
def self.punycode_decode_digit(codepoint)
|
|
4727
|
-
if codepoint
|
|
4881
|
+
if codepoint >= 48 && codepoint <= 57
|
|
4728
4882
|
codepoint - 22
|
|
4729
|
-
elsif codepoint
|
|
4883
|
+
elsif codepoint >= 65 && codepoint <= 90
|
|
4730
4884
|
codepoint - 65
|
|
4731
|
-
elsif codepoint
|
|
4885
|
+
elsif codepoint >= 97 && codepoint <= 122
|
|
4732
4886
|
codepoint - 97
|
|
4733
4887
|
else
|
|
4734
4888
|
PUNYCODE_BASE
|