* Converts a string of Unicode symbols to a Punycode string of ASCII-only * symbols. * @memberOf punycode * @param {String} input The string of Unicode symbols. * @returns {String} The resulting Punycode string of ASCII-only symbols.
(input)
| 1750 | * @returns {String} The resulting Punycode string of ASCII-only symbols. |
| 1751 | */ |
| 1752 | function encode(input) { |
| 1753 | var n, |
| 1754 | delta, |
| 1755 | handledCPCount, |
| 1756 | basicLength, |
| 1757 | bias, |
| 1758 | j, |
| 1759 | m, |
| 1760 | q, |
| 1761 | k, |
| 1762 | t, |
| 1763 | currentValue, |
| 1764 | output = [], |
| 1765 | /** `inputLength` will hold the number of code points in `input`. */ |
| 1766 | inputLength, |
| 1767 | /** Cached calculation results */ |
| 1768 | handledCPCountPlusOne, |
| 1769 | baseMinusT, |
| 1770 | qMinusT; |
| 1771 | |
| 1772 | // Convert the input in UCS-2 to Unicode |
| 1773 | input = ucs2decode(input); |
| 1774 | |
| 1775 | // Cache the length |
| 1776 | inputLength = input.length; |
| 1777 | |
| 1778 | // Initialize the state |
| 1779 | n = initialN; |
| 1780 | delta = 0; |
| 1781 | bias = initialBias; |
| 1782 | |
| 1783 | // Handle the basic code points |
| 1784 | for (j = 0; j < inputLength; ++j) { |
| 1785 | currentValue = input[j]; |
| 1786 | if (currentValue < 0x80) { |
| 1787 | output.push(stringFromCharCode(currentValue)); |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | handledCPCount = basicLength = output.length; |
| 1792 | |
| 1793 | // `handledCPCount` is the number of code points that have been handled; |
| 1794 | // `basicLength` is the number of basic code points. |
| 1795 | |
| 1796 | // Finish the basic string - if it is not empty - with a delimiter |
| 1797 | if (basicLength) { |
| 1798 | output.push(delimiter); |
| 1799 | } |
| 1800 | |
| 1801 | // Main encoding loop: |
| 1802 | while (handledCPCount < inputLength) { |
| 1803 | |
| 1804 | // All non-basic code points < n have been handled already. Find the next |
| 1805 | // larger one: |
| 1806 | for (m = maxInt, j = 0; j < inputLength; ++j) { |
| 1807 | currentValue = input[j]; |
| 1808 | if (currentValue >= n && currentValue < m) { |
| 1809 | m = currentValue; |
no test coverage detected