From cf89beec6f7cf08a260e6d0383455a89f4cb2722 Mon Sep 17 00:00:00 2001 From: Mathias Bynens Date: Wed, 30 Nov 2011 09:44:00 +0100 Subject: [PATCH] punycode: Update to v0.2.1 --- lib/punycode.js | 146 +++++++++++++++++++------------------------------------- 1 file changed, 48 insertions(+), 98 deletions(-) diff --git a/lib/punycode.js b/lib/punycode.js index b6122a9..315f1e4 100644 --- a/lib/punycode.js +++ b/lib/punycode.js @@ -4,21 +4,20 @@ * Available under MIT license */ -;(function(window) { +;(function(root) { /** - * The `Punycode` object. - * @name Punycode + * The `punycode` object. + * @name punycode * @type Object */ - var Punycode, + var punycode, - /** Detect free variables `define`, `exports`, and `require` */ + /** Detect free variables `define`, `exports`, `module` and `require` */ freeDefine = typeof define == 'function' && typeof define.amd == 'object' && define.amd && define, - freeExports = typeof exports == 'object' && exports && - (typeof global == 'object' && global && global == global.global && - (window = global), exports), + freeExports = typeof exports == 'object' && exports, + freeModule = typeof module == 'object' && module, freeRequire = typeof require == 'function' && require, /** Highest positive signed 32-bit float value */ @@ -35,7 +34,7 @@ delimiter = '-', // '\x2D' /** Regular expressions */ - regexASCII = /[^\x20-\x7e]/, + regexNonASCII = /[^ -~]/, // matches unprintable ASCII chars + non-ASCII chars regexPunycode = /^xn--/, /** Error messages */ @@ -50,7 +49,10 @@ /** Convenience shortcuts */ baseMinusTMin = base - tMin, floor = Math.floor, - stringFromCharCode = String.fromCharCode; + stringFromCharCode = String.fromCharCode, + + /** Temporary variable */ + key; /*--------------------------------------------------------------------------*/ @@ -97,8 +99,9 @@ /** * Creates an array containing the decimal code points of each character in * the string. - * @see `Punycode.utf16.encode` - * @memberOf Punycode.utf16 + * @see `punycode.utf16.encode` + * @see + * @memberOf punycode.utf16 * @name decode * @param {String} string The Unicode input string. * @returns {Array} The new array. @@ -125,8 +128,9 @@ /** * Creates a string based on an array of decimal code points. - * @see `Punycode.utf16.decode` - * @memberOf Punycode.utf16 + * @see `punycode.utf16.decode` + * @see + * @memberOf punycode.utf16 * @name encode * @param {Array} codePoints The array of decimal code points. * @returns {String} The new string. @@ -215,25 +219,13 @@ /** * Converts a Punycode string of ASCII code points to a string of Unicode * code points. - * @memberOf Punycode + * @memberOf punycode * @param {String} input The Punycode string of ASCII code points. - * @param {Boolean} preserveCase A boolean value indicating if character - * case should be preserved or not. * @returns {String} The resulting string of Unicode code points. */ - function decode(input, preserveCase) { + function decode(input) { // Don't use UTF-16 var output = [], - /** - * The `caseFlags` array needs room for at least `output.length` values, - * or it can be `undefined` if the case information is not needed. A - * truthy value suggests that the corresponding Unicode character be - * forced to uppercase (if possible), while falsy values suggest that it - * be forced to lowercase (if possible). ASCII code points are output - * already in the proper case, but their flags will be set appropriately - * so that applying the flags would be harmless. - */ - caseFlags = [], inputLength = input.length, out, i = 0, @@ -261,9 +253,6 @@ } for (j = 0; j < basic; ++j) { - if (preserveCase) { - caseFlags[output.length] = input.charCodeAt(j) - 65 < 26; - } // if it's not a basic code point if (input.charCodeAt(j) >= 0x80) { error('not-basic'); @@ -271,8 +260,8 @@ output.push(input.charCodeAt(j)); } - // Main decoding loop: start just after the last delimiter if any basic - // code points were copied; start at the beginning otherwise. + // Main decoding loop: start just after the last delimiter if any basic code + // points were copied; start at the beginning otherwise. for (index = basic > 0 ? basic + 1 : 0; index < inputLength; /* no final expression */) { @@ -322,36 +311,21 @@ i %= out; // Insert `n` at position `i` of the output - // The case of the last character determines `uppercase` flag - if (preserveCase) { - caseFlags.splice(i, 0, input.charCodeAt(index - 1) - 65 < 26); - } - - output.splice(i, 0, n); - i++; + output.splice(i++, 0, n); } - if (preserveCase) { - for (i = 0, length = output.length; i < length; i++) { - if (caseFlags[i]) { - output[i] = (stringFromCharCode(output[i]).toUpperCase()).charCodeAt(0); - } - } - } return utf16encode(output); } /** * Converts a string of Unicode code points to a Punycode string of ASCII * code points. - * @memberOf Punycode + * @memberOf punycode * @param {String} input The string of Unicode code points. - * @param {Boolean} preserveCase A boolean value indicating if character - * case should be preserved or not. * @returns {String} The resulting Punycode string of ASCII code points. */ - function encode(input, preserveCase) { + function encode(input) { var n, delta, handledCPCount, @@ -363,18 +337,6 @@ k, t, currentValue, - /** - * The `caseFlags` array will hold `inputLength` boolean values, where - * `true` suggests that the corresponding Unicode character be forced - * to uppercase after being decoded (if possible), and `false` - * suggests that it be forced to lowercase (if possible). ASCII code - * points are encoded literally, except that ASCII letters are forced - * to uppercase or lowercase according to the corresponding uppercase - * flags. If `caseFlags` remains `undefined` then ASCII letters are - * left as they are, and other code points are treated as if their - * uppercase flags were `true`. - */ - caseFlags, output = [], /** `inputLength` will hold the number of code points in `input`. */ inputLength, @@ -383,24 +345,12 @@ baseMinusT, qMinusT; - if (preserveCase) { - // Preserve case, step 1 of 2: get a list of the unaltered string - caseFlags = utf16decode(input); - } - // Convert the input in UTF-16 to Unicode input = utf16decode(input); // Cache the length inputLength = input.length; - if (preserveCase) { - // Preserve case, step 2 of 2: modify the list to true/false - for (j = 0; j < inputLength; j++) { - caseFlags[j] = input[j] != caseFlags[j]; - } - } - // Initialize the state n = initialN; delta = 0; @@ -410,11 +360,7 @@ for (j = 0; j < inputLength; ++j) { currentValue = input[j]; if (currentValue < 0x80) { - output.push( - stringFromCharCode( - caseFlags ? encodeBasic(currentValue, caseFlags[j]) : currentValue - ) - ); + output.push(stringFromCharCode(currentValue)); } } @@ -433,7 +379,6 @@ // All non-basic code points < n have been handled already. Find the next // larger one: - for (m = maxInt, j = 0; j < inputLength; ++j) { currentValue = input[j]; if (currentValue >= n && currentValue < m) { @@ -473,7 +418,7 @@ q = floor(qMinusT / baseMinusT); } - output.push(stringFromCharCode(digitToBasic(q, preserveCase && caseFlags[j] ? 1 : 0))); + output.push(stringFromCharCode(digitToBasic(q, 0))); bias = adapt(delta, handledCPCountPlusOne, handledCPCount == basicLength); delta = 0; ++handledCPCount; @@ -492,7 +437,7 @@ * Punycoded parts of the domain name will be converted, i.e. it doesn't * matter if you call it on a string that has already been converted to * Unicode. - * @memberOf Punycode + * @memberOf punycode * @param {String} domain The Punycode domain name to convert to Unicode. * @returns {String} The Unicode representation of the given Punycode * string. @@ -509,13 +454,13 @@ * Converts a Unicode string representing a domain name to Punycode. Only the * non-ASCII parts of the domain name will be converted, i.e. it doesn't * matter if you call it with a domain that's already in ASCII. - * @memberOf Punycode + * @memberOf punycode * @param {String} domain The domain name to convert, as a Unicode string. * @returns {String} The Punycode representation of the given domain name. */ function toASCII(domain) { return mapDomain(domain, function(string) { - return regexASCII.test(string) + return regexNonASCII.test(string) ? 'xn--' + encode(string) : string; }); @@ -524,12 +469,17 @@ /*--------------------------------------------------------------------------*/ /** Define the public API */ - Punycode = { - 'version': '0.1.1', + punycode = { + /** + * A string representing the current Punycode.js version number. + * @memberOf punycode + * @type String + */ + 'version': '0.2.1', /** * An object of methods to convert from JavaScript's internal character * representation to Unicode and back. - * @memberOf Punycode + * @memberOf punycode * @type Object */ 'utf16': { @@ -542,23 +492,23 @@ 'toUnicode': toUnicode }; - /** Expose Punycode */ + /** Expose `punycode` */ if (freeExports) { - if (typeof module == 'object' && module && module.exports == freeExports) { - // in Node.js - module.exports = Punycode; + if (freeModule && freeModule.exports == freeExports) { + // in Node.js or Ringo 0.8+ + freeModule.exports = punycode; } else { - // in Narwhal or Ringo - freeExports.Punycode = Punycode; + // in Narwhal or Ringo 0.7- + for (key in punycode) { + punycode.hasOwnProperty(key) && (freeExports[key] = punycode[key]); + } } } else if (freeDefine) { // via curl.js or RequireJS - freeDefine(function() { - return Punycode; - }); + define('punycode', punycode); } else { // in a browser or Rhino - window.Punycode = Punycode; + root.punycode = punycode; } }(this)); -- 2.7.4