Skip to content

Commit

Permalink
Fix USVString conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Mayr committed Aug 9, 2015
1 parent 595a791 commit 454ce97
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,33 @@ conversions["ByteString"] = function (V, opts) {
};

conversions["USVString"] = function (V) {
return String(V);
const S = String(V);
const n = S.length;
const U = [];
for (let i = 0; i < n; ++i) {
const c = S.charCodeAt(i);
if (c < 0xD800 || c > 0xDFFF) {
U.push(String.fromCodePoint(c));
} else if (0xDC00 <= c && c <= 0xDFFF) {
U.push(String.fromCodePoint(0xFFFD));
} else {
if (i === n - 1) {
U.push(String.fromCodePoint(0xFFFD));
} else {
const d = S.charCodeAt(i + 1);
if (0xDC00 <= d && d <= 0xDFFF) {
const a = c & 0x3FF;
const b = d & 0x3FF;
U.push(String.fromCodePoint((2 << 15) + (2 << 9) * a + b));
++i;
} else {
U.push(String.fromCodePoint(0xFFFD));
}
}
}
}

return U;
};

conversions["Date"] = function (V, opts) {
Expand Down

0 comments on commit 454ce97

Please sign in to comment.