Skip to content

Commit

Permalink
fix some buglets in surrogate pair decoding
Browse files Browse the repository at this point in the history
First, the correct constant to use is 0x10000, aka Math.pow(2,20).
Second, arrange to skip the low surrogate.  Finally, fix the math in
stringFromCode as well, and remove an extraneous semicolon.
  • Loading branch information
tromey committed Feb 26, 2015
1 parent 433a225 commit 0175f03
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions parse-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ function preprocess(str) {
// Decode a surrogate pair into an astral codepoint.
var lead = code - 0xd800;
var trail = str.charCodeAt(i+1) - 0xdc00;
code = Math.pow(2, 21) + lead * Math.pow(2, 10) + trail;
code = Math.pow(2, 20) + lead * Math.pow(2, 10) + trail;
i++;
}
codepoints.push(code);
}
Expand All @@ -57,9 +58,9 @@ function preprocess(str) {
function stringFromCode(code) {
if(code <= 0xffff) return String.fromCharCode(code);
// Otherwise, encode astral char as surrogate pair.
code -= Math.pow(2, 21);
code -= Math.pow(2, 20);
var lead = Math.floor(code/Math.pow(2, 10)) + 0xd800;
var trail = code % Math.pow(2, 10); + 0xdc00;
var trail = code % Math.pow(2, 10) + 0xdc00;
return String.fromCharCode(lead) + String.fromCharCode(trail);
}

Expand Down

0 comments on commit 0175f03

Please sign in to comment.