react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / punycode / punycode.js
80713 views/*! http://mths.be/punycode v1.2.4 by @mathias */1;(function(root) {23/** Detect free variables */4var freeExports = typeof exports == 'object' && exports;5var freeModule = typeof module == 'object' && module &&6module.exports == freeExports && module;7var freeGlobal = typeof global == 'object' && global;8if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) {9root = freeGlobal;10}1112/**13* The `punycode` object.14* @name punycode15* @type Object16*/17var punycode,1819/** Highest positive signed 32-bit float value */20maxInt = 2147483647, // aka. 0x7FFFFFFF or 2^31-12122/** Bootstring parameters */23base = 36,24tMin = 1,25tMax = 26,26skew = 38,27damp = 700,28initialBias = 72,29initialN = 128, // 0x8030delimiter = '-', // '\x2D'3132/** Regular expressions */33regexPunycode = /^xn--/,34regexNonASCII = /[^ -~]/, // unprintable ASCII chars + non-ASCII chars35regexSeparators = /\x2E|\u3002|\uFF0E|\uFF61/g, // RFC 3490 separators3637/** Error messages */38errors = {39'overflow': 'Overflow: input needs wider integers to process',40'not-basic': 'Illegal input >= 0x80 (not a basic code point)',41'invalid-input': 'Invalid input'42},4344/** Convenience shortcuts */45baseMinusTMin = base - tMin,46floor = Math.floor,47stringFromCharCode = String.fromCharCode,4849/** Temporary variable */50key;5152/*--------------------------------------------------------------------------*/5354/**55* A generic error utility function.56* @private57* @param {String} type The error type.58* @returns {Error} Throws a `RangeError` with the applicable error message.59*/60function error(type) {61throw RangeError(errors[type]);62}6364/**65* A generic `Array#map` utility function.66* @private67* @param {Array} array The array to iterate over.68* @param {Function} callback The function that gets called for every array69* item.70* @returns {Array} A new array of values returned by the callback function.71*/72function map(array, fn) {73var length = array.length;74while (length--) {75array[length] = fn(array[length]);76}77return array;78}7980/**81* A simple `Array#map`-like wrapper to work with domain name strings.82* @private83* @param {String} domain The domain name.84* @param {Function} callback The function that gets called for every85* character.86* @returns {Array} A new string of characters returned by the callback87* function.88*/89function mapDomain(string, fn) {90return map(string.split(regexSeparators), fn).join('.');91}9293/**94* Creates an array containing the numeric code points of each Unicode95* character in the string. While JavaScript uses UCS-2 internally,96* this function will convert a pair of surrogate halves (each of which97* UCS-2 exposes as separate characters) into a single code point,98* matching UTF-16.99* @see `punycode.ucs2.encode`100* @see <http://mathiasbynens.be/notes/javascript-encoding>101* @memberOf punycode.ucs2102* @name decode103* @param {String} string The Unicode input string (UCS-2).104* @returns {Array} The new array of code points.105*/106function ucs2decode(string) {107var output = [],108counter = 0,109length = string.length,110value,111extra;112while (counter < length) {113value = string.charCodeAt(counter++);114if (value >= 0xD800 && value <= 0xDBFF && counter < length) {115// high surrogate, and there is a next character116extra = string.charCodeAt(counter++);117if ((extra & 0xFC00) == 0xDC00) { // low surrogate118output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);119} else {120// unmatched surrogate; only append this code unit, in case the next121// code unit is the high surrogate of a surrogate pair122output.push(value);123counter--;124}125} else {126output.push(value);127}128}129return output;130}131132/**133* Creates a string based on an array of numeric code points.134* @see `punycode.ucs2.decode`135* @memberOf punycode.ucs2136* @name encode137* @param {Array} codePoints The array of numeric code points.138* @returns {String} The new Unicode string (UCS-2).139*/140function ucs2encode(array) {141return map(array, function(value) {142var output = '';143if (value > 0xFFFF) {144value -= 0x10000;145output += stringFromCharCode(value >>> 10 & 0x3FF | 0xD800);146value = 0xDC00 | value & 0x3FF;147}148output += stringFromCharCode(value);149return output;150}).join('');151}152153/**154* Converts a basic code point into a digit/integer.155* @see `digitToBasic()`156* @private157* @param {Number} codePoint The basic numeric code point value.158* @returns {Number} The numeric value of a basic code point (for use in159* representing integers) in the range `0` to `base - 1`, or `base` if160* the code point does not represent a value.161*/162function basicToDigit(codePoint) {163if (codePoint - 48 < 10) {164return codePoint - 22;165}166if (codePoint - 65 < 26) {167return codePoint - 65;168}169if (codePoint - 97 < 26) {170return codePoint - 97;171}172return base;173}174175/**176* Converts a digit/integer into a basic code point.177* @see `basicToDigit()`178* @private179* @param {Number} digit The numeric value of a basic code point.180* @returns {Number} The basic code point whose value (when used for181* representing integers) is `digit`, which needs to be in the range182* `0` to `base - 1`. If `flag` is non-zero, the uppercase form is183* used; else, the lowercase form is used. The behavior is undefined184* if `flag` is non-zero and `digit` has no uppercase form.185*/186function digitToBasic(digit, flag) {187// 0..25 map to ASCII a..z or A..Z188// 26..35 map to ASCII 0..9189return digit + 22 + 75 * (digit < 26) - ((flag != 0) << 5);190}191192/**193* Bias adaptation function as per section 3.4 of RFC 3492.194* http://tools.ietf.org/html/rfc3492#section-3.4195* @private196*/197function adapt(delta, numPoints, firstTime) {198var k = 0;199delta = firstTime ? floor(delta / damp) : delta >> 1;200delta += floor(delta / numPoints);201for (/* no initialization */; delta > baseMinusTMin * tMax >> 1; k += base) {202delta = floor(delta / baseMinusTMin);203}204return floor(k + (baseMinusTMin + 1) * delta / (delta + skew));205}206207/**208* Converts a Punycode string of ASCII-only symbols to a string of Unicode209* symbols.210* @memberOf punycode211* @param {String} input The Punycode string of ASCII-only symbols.212* @returns {String} The resulting string of Unicode symbols.213*/214function decode(input) {215// Don't use UCS-2216var output = [],217inputLength = input.length,218out,219i = 0,220n = initialN,221bias = initialBias,222basic,223j,224index,225oldi,226w,227k,228digit,229t,230/** Cached calculation results */231baseMinusT;232233// Handle the basic code points: let `basic` be the number of input code234// points before the last delimiter, or `0` if there is none, then copy235// the first basic code points to the output.236237basic = input.lastIndexOf(delimiter);238if (basic < 0) {239basic = 0;240}241242for (j = 0; j < basic; ++j) {243// if it's not a basic code point244if (input.charCodeAt(j) >= 0x80) {245error('not-basic');246}247output.push(input.charCodeAt(j));248}249250// Main decoding loop: start just after the last delimiter if any basic code251// points were copied; start at the beginning otherwise.252253for (index = basic > 0 ? basic + 1 : 0; index < inputLength; /* no final expression */) {254255// `index` is the index of the next character to be consumed.256// Decode a generalized variable-length integer into `delta`,257// which gets added to `i`. The overflow checking is easier258// if we increase `i` as we go, then subtract off its starting259// value at the end to obtain `delta`.260for (oldi = i, w = 1, k = base; /* no condition */; k += base) {261262if (index >= inputLength) {263error('invalid-input');264}265266digit = basicToDigit(input.charCodeAt(index++));267268if (digit >= base || digit > floor((maxInt - i) / w)) {269error('overflow');270}271272i += digit * w;273t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias);274275if (digit < t) {276break;277}278279baseMinusT = base - t;280if (w > floor(maxInt / baseMinusT)) {281error('overflow');282}283284w *= baseMinusT;285286}287288out = output.length + 1;289bias = adapt(i - oldi, out, oldi == 0);290291// `i` was supposed to wrap around from `out` to `0`,292// incrementing `n` each time, so we'll fix that now:293if (floor(i / out) > maxInt - n) {294error('overflow');295}296297n += floor(i / out);298i %= out;299300// Insert `n` at position `i` of the output301output.splice(i++, 0, n);302303}304305return ucs2encode(output);306}307308/**309* Converts a string of Unicode symbols to a Punycode string of ASCII-only310* symbols.311* @memberOf punycode312* @param {String} input The string of Unicode symbols.313* @returns {String} The resulting Punycode string of ASCII-only symbols.314*/315function encode(input) {316var n,317delta,318handledCPCount,319basicLength,320bias,321j,322m,323q,324k,325t,326currentValue,327output = [],328/** `inputLength` will hold the number of code points in `input`. */329inputLength,330/** Cached calculation results */331handledCPCountPlusOne,332baseMinusT,333qMinusT;334335// Convert the input in UCS-2 to Unicode336input = ucs2decode(input);337338// Cache the length339inputLength = input.length;340341// Initialize the state342n = initialN;343delta = 0;344bias = initialBias;345346// Handle the basic code points347for (j = 0; j < inputLength; ++j) {348currentValue = input[j];349if (currentValue < 0x80) {350output.push(stringFromCharCode(currentValue));351}352}353354handledCPCount = basicLength = output.length;355356// `handledCPCount` is the number of code points that have been handled;357// `basicLength` is the number of basic code points.358359// Finish the basic string - if it is not empty - with a delimiter360if (basicLength) {361output.push(delimiter);362}363364// Main encoding loop:365while (handledCPCount < inputLength) {366367// All non-basic code points < n have been handled already. Find the next368// larger one:369for (m = maxInt, j = 0; j < inputLength; ++j) {370currentValue = input[j];371if (currentValue >= n && currentValue < m) {372m = currentValue;373}374}375376// Increase `delta` enough to advance the decoder's <n,i> state to <m,0>,377// but guard against overflow378handledCPCountPlusOne = handledCPCount + 1;379if (m - n > floor((maxInt - delta) / handledCPCountPlusOne)) {380error('overflow');381}382383delta += (m - n) * handledCPCountPlusOne;384n = m;385386for (j = 0; j < inputLength; ++j) {387currentValue = input[j];388389if (currentValue < n && ++delta > maxInt) {390error('overflow');391}392393if (currentValue == n) {394// Represent delta as a generalized variable-length integer395for (q = delta, k = base; /* no condition */; k += base) {396t = k <= bias ? tMin : (k >= bias + tMax ? tMax : k - bias);397if (q < t) {398break;399}400qMinusT = q - t;401baseMinusT = base - t;402output.push(403stringFromCharCode(digitToBasic(t + qMinusT % baseMinusT, 0))404);405q = floor(qMinusT / baseMinusT);406}407408output.push(stringFromCharCode(digitToBasic(q, 0)));409bias = adapt(delta, handledCPCountPlusOne, handledCPCount == basicLength);410delta = 0;411++handledCPCount;412}413}414415++delta;416++n;417418}419return output.join('');420}421422/**423* Converts a Punycode string representing a domain name to Unicode. Only the424* Punycoded parts of the domain name will be converted, i.e. it doesn't425* matter if you call it on a string that has already been converted to426* Unicode.427* @memberOf punycode428* @param {String} domain The Punycode domain name to convert to Unicode.429* @returns {String} The Unicode representation of the given Punycode430* string.431*/432function toUnicode(domain) {433return mapDomain(domain, function(string) {434return regexPunycode.test(string)435? decode(string.slice(4).toLowerCase())436: string;437});438}439440/**441* Converts a Unicode string representing a domain name to Punycode. Only the442* non-ASCII parts of the domain name will be converted, i.e. it doesn't443* matter if you call it with a domain that's already in ASCII.444* @memberOf punycode445* @param {String} domain The domain name to convert, as a Unicode string.446* @returns {String} The Punycode representation of the given domain name.447*/448function toASCII(domain) {449return mapDomain(domain, function(string) {450return regexNonASCII.test(string)451? 'xn--' + encode(string)452: string;453});454}455456/*--------------------------------------------------------------------------*/457458/** Define the public API */459punycode = {460/**461* A string representing the current Punycode.js version number.462* @memberOf punycode463* @type String464*/465'version': '1.2.4',466/**467* An object of methods to convert from JavaScript's internal character468* representation (UCS-2) to Unicode code points, and back.469* @see <http://mathiasbynens.be/notes/javascript-encoding>470* @memberOf punycode471* @type Object472*/473'ucs2': {474'decode': ucs2decode,475'encode': ucs2encode476},477'decode': decode,478'encode': encode,479'toASCII': toASCII,480'toUnicode': toUnicode481};482483/** Expose `punycode` */484// Some AMD build optimizers, like r.js, check for specific condition patterns485// like the following:486if (487typeof define == 'function' &&488typeof define.amd == 'object' &&489define.amd490) {491define('punycode', function() {492return punycode;493});494} else if (freeExports && !freeExports.nodeType) {495if (freeModule) { // in Node.js or RingoJS v0.8.0+496freeModule.exports = punycode;497} else { // in Narwhal or RingoJS v0.7.0-498for (key in punycode) {499punycode.hasOwnProperty(key) && (freeExports[key] = punycode[key]);500}501}502} else { // in Rhino or a web browser503root.punycode = punycode;504}505506}(this));507508509