2 // based on https://github.com/bestiejs/punycode.js/blob/master/punycode.js
3 var global = require('../internals/global');
4 var uncurryThis = require('../internals/function-uncurry-this');
6 var maxInt = 2147483647; // aka. 0x7FFFFFFF or 2^31-1
13 var initialN = 128; // 0x80
14 var delimiter = '-'; // '\x2D'
15 var regexNonASCII = /[^\0-\u007E]/; // non-ASCII chars
16 var regexSeparators = /[.\u3002\uFF0E\uFF61]/g; // RFC 3490 separators
17 var OVERFLOW_ERROR = 'Overflow: input needs wider integers to process';
18 var baseMinusTMin = base - tMin;
20 var RangeError = global.RangeError;
21 var exec = uncurryThis(regexSeparators.exec);
22 var floor = Math.floor;
23 var fromCharCode = String.fromCharCode;
24 var charCodeAt = uncurryThis(''.charCodeAt);
25 var join = uncurryThis([].join);
26 var push = uncurryThis([].push);
27 var replace = uncurryThis(''.replace);
28 var split = uncurryThis(''.split);
29 var toLowerCase = uncurryThis(''.toLowerCase);
32 * Creates an array containing the numeric code points of each Unicode
33 * character in the string. While JavaScript uses UCS-2 internally,
34 * this function will convert a pair of surrogate halves (each of which
35 * UCS-2 exposes as separate characters) into a single code point,
38 var ucs2decode = function (string) {
41 var length = string.length;
42 while (counter < length) {
43 var value = charCodeAt(string, counter++);
44 if (value >= 0xD800 && value <= 0xDBFF && counter < length) {
45 // It's a high surrogate, and there is a next character.
46 var extra = charCodeAt(string, counter++);
47 if ((extra & 0xFC00) == 0xDC00) { // Low surrogate.
48 push(output, ((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
50 // It's an unmatched surrogate; only append this code unit, in case the
51 // next code unit is the high surrogate of a surrogate pair.
63 * Converts a digit/integer into a basic code point.
65 var digitToBasic = function (digit) {
66 // 0..25 map to ASCII a..z or A..Z
67 // 26..35 map to ASCII 0..9
68 return digit + 22 + 75 * (digit < 26);
72 * Bias adaptation function as per section 3.4 of RFC 3492.
73 * https://tools.ietf.org/html/rfc3492#section-3.4
75 var adapt = function (delta, numPoints, firstTime) {
77 delta = firstTime ? floor(delta / damp) : delta >> 1;
78 delta += floor(delta / numPoints);
79 while (delta > baseMinusTMin * tMax >> 1) {
80 delta = floor(delta / baseMinusTMin);
83 return floor(k + (baseMinusTMin + 1) * delta / (delta + skew));
87 * Converts a string of Unicode symbols (e.g. a domain name label) to a
88 * Punycode string of ASCII-only symbols.
90 var encode = function (input) {
93 // Convert the input in UCS-2 to an array of Unicode code points.
94 input = ucs2decode(input);
97 var inputLength = input.length;
99 // Initialize the state.
102 var bias = initialBias;
105 // Handle the basic code points.
106 for (i = 0; i < input.length; i++) {
107 currentValue = input[i];
108 if (currentValue < 0x80) {
109 push(output, fromCharCode(currentValue));
113 var basicLength = output.length; // number of basic code points.
114 var handledCPCount = basicLength; // number of code points that have been handled;
116 // Finish the basic string with a delimiter unless it's empty.
118 push(output, delimiter);
121 // Main encoding loop:
122 while (handledCPCount < inputLength) {
123 // All non-basic code points < n have been handled already. Find the next larger one:
125 for (i = 0; i < input.length; i++) {
126 currentValue = input[i];
127 if (currentValue >= n && currentValue < m) {
132 // Increase `delta` enough to advance the decoder's <n,i> state to <m,0>, but guard against overflow.
133 var handledCPCountPlusOne = handledCPCount + 1;
134 if (m - n > floor((maxInt - delta) / handledCPCountPlusOne)) {
135 throw RangeError(OVERFLOW_ERROR);
138 delta += (m - n) * handledCPCountPlusOne;
141 for (i = 0; i < input.length; i++) {
142 currentValue = input[i];
143 if (currentValue < n && ++delta > maxInt) {
144 throw RangeError(OVERFLOW_ERROR);
146 if (currentValue == n) {
147 // Represent delta as a generalized variable-length integer.
151 var t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias);
154 var baseMinusT = base - t;
155 push(output, fromCharCode(digitToBasic(t + qMinusT % baseMinusT)));
156 q = floor(qMinusT / baseMinusT);
160 push(output, fromCharCode(digitToBasic(q)));
161 bias = adapt(delta, handledCPCountPlusOne, handledCPCount == basicLength);
170 return join(output, '');
173 module.exports = function (input) {
175 var labels = split(replace(toLowerCase(input), regexSeparators, '\u002E'), '.');
177 for (i = 0; i < labels.length; i++) {
179 push(encoded, exec(regexNonASCII, label) ? 'xn--' + encode(label) : label);
181 return join(encoded, '.');