massive update, probably broken
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / core-js / internals / string-punycode-to-ascii.js
1 'use strict';
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');
5
6 var maxInt = 2147483647; // aka. 0x7FFFFFFF or 2^31-1
7 var base = 36;
8 var tMin = 1;
9 var tMax = 26;
10 var skew = 38;
11 var damp = 700;
12 var initialBias = 72;
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;
19
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);
30
31 /**
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,
36  * matching UTF-16.
37  */
38 var ucs2decode = function (string) {
39   var output = [];
40   var counter = 0;
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);
49       } else {
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.
52         push(output, value);
53         counter--;
54       }
55     } else {
56       push(output, value);
57     }
58   }
59   return output;
60 };
61
62 /**
63  * Converts a digit/integer into a basic code point.
64  */
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);
69 };
70
71 /**
72  * Bias adaptation function as per section 3.4 of RFC 3492.
73  * https://tools.ietf.org/html/rfc3492#section-3.4
74  */
75 var adapt = function (delta, numPoints, firstTime) {
76   var k = 0;
77   delta = firstTime ? floor(delta / damp) : delta >> 1;
78   delta += floor(delta / numPoints);
79   while (delta > baseMinusTMin * tMax >> 1) {
80     delta = floor(delta / baseMinusTMin);
81     k += base;
82   }
83   return floor(k + (baseMinusTMin + 1) * delta / (delta + skew));
84 };
85
86 /**
87  * Converts a string of Unicode symbols (e.g. a domain name label) to a
88  * Punycode string of ASCII-only symbols.
89  */
90 var encode = function (input) {
91   var output = [];
92
93   // Convert the input in UCS-2 to an array of Unicode code points.
94   input = ucs2decode(input);
95
96   // Cache the length.
97   var inputLength = input.length;
98
99   // Initialize the state.
100   var n = initialN;
101   var delta = 0;
102   var bias = initialBias;
103   var i, currentValue;
104
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));
110     }
111   }
112
113   var basicLength = output.length; // number of basic code points.
114   var handledCPCount = basicLength; // number of code points that have been handled;
115
116   // Finish the basic string with a delimiter unless it's empty.
117   if (basicLength) {
118     push(output, delimiter);
119   }
120
121   // Main encoding loop:
122   while (handledCPCount < inputLength) {
123     // All non-basic code points < n have been handled already. Find the next larger one:
124     var m = maxInt;
125     for (i = 0; i < input.length; i++) {
126       currentValue = input[i];
127       if (currentValue >= n && currentValue < m) {
128         m = currentValue;
129       }
130     }
131
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);
136     }
137
138     delta += (m - n) * handledCPCountPlusOne;
139     n = m;
140
141     for (i = 0; i < input.length; i++) {
142       currentValue = input[i];
143       if (currentValue < n && ++delta > maxInt) {
144         throw RangeError(OVERFLOW_ERROR);
145       }
146       if (currentValue == n) {
147         // Represent delta as a generalized variable-length integer.
148         var q = delta;
149         var k = base;
150         while (true) {
151           var t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias);
152           if (q < t) break;
153           var qMinusT = q - t;
154           var baseMinusT = base - t;
155           push(output, fromCharCode(digitToBasic(t + qMinusT % baseMinusT)));
156           q = floor(qMinusT / baseMinusT);
157           k += base;
158         }
159
160         push(output, fromCharCode(digitToBasic(q)));
161         bias = adapt(delta, handledCPCountPlusOne, handledCPCount == basicLength);
162         delta = 0;
163         handledCPCount++;
164       }
165     }
166
167     delta++;
168     n++;
169   }
170   return join(output, '');
171 };
172
173 module.exports = function (input) {
174   var encoded = [];
175   var labels = split(replace(toLowerCase(input), regexSeparators, '\u002E'), '.');
176   var i, label;
177   for (i = 0; i < labels.length; i++) {
178     label = labels[i];
179     push(encoded, exec(regexNonASCII, label) ? 'xn--' + encode(label) : label);
180   }
181   return join(encoded, '.');
182 };