|
35 | 35 | /** Error messages */ |
36 | 36 | errors = { |
37 | 37 | 'overflow': 'Overflow: input needs wider integers to process.', |
38 | | - 'ucs2decode': 'UCS-2(decode): illegal sequence', |
39 | | - 'ucs2encode': 'UCS-2(encode): illegal value', |
40 | 38 | 'not-basic': 'Illegal input >= 0x80 (not a basic code point)', |
41 | 39 | 'invalid-input': 'Invalid input' |
42 | 40 | }, |
|
112 | 110 | extra; |
113 | 111 | while (counter < length) { |
114 | 112 | value = string.charCodeAt(counter++); |
115 | | - if ((value & 0xF800) == 0xD800) { |
| 113 | + if ((value & 0xF800) == 0xD800 && counter < length) { |
| 114 | + // high surrogate, and there is a next character |
116 | 115 | extra = string.charCodeAt(counter++); |
117 | | - if ((value & 0xFC00) != 0xD800 || (extra & 0xFC00) != 0xDC00) { |
118 | | - error('ucs2decode'); |
| 116 | + if ((extra & 0xFC00) == 0xDC00) { // low surrogate |
| 117 | + output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000); |
| 118 | + } else { |
| 119 | + output.push(value, extra); |
119 | 120 | } |
120 | | - value = ((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000; |
| 121 | + } else { |
| 122 | + output.push(value); |
121 | 123 | } |
122 | | - output.push(value); |
123 | 124 | } |
124 | 125 | return output; |
125 | 126 | } |
|
135 | 136 | function ucs2encode(array) { |
136 | 137 | return map(array, function(value) { |
137 | 138 | var output = ''; |
138 | | - if ((value & 0xF800) == 0xD800) { |
139 | | - error('ucs2encode'); |
140 | | - } |
141 | 139 | if (value > 0xFFFF) { |
142 | 140 | value -= 0x10000; |
143 | 141 | output += stringFromCharCode(value >>> 10 & 0x3FF | 0xD800); |
|
472 | 470 | * @memberOf punycode |
473 | 471 | * @type String |
474 | 472 | */ |
475 | | - 'version': '1.0.0', |
| 473 | + 'version': '1.1.1', |
476 | 474 | /** |
477 | 475 | * An object of methods to convert from JavaScript's internal character |
478 | 476 | * representation (UCS-2) to decimal Unicode code points, and back. |
|
0 commit comments