diff --git a/UZIP.js b/UZIP.js index cf622da..cac960c 100644 --- a/UZIP.js +++ b/UZIP.js @@ -29,6 +29,8 @@ var UZIP = {}; } return out; }, + // Decode UTF-8 bytes into a JavaScript UTF-16 string. The helper below 'readUnicodeCodePoint' is + // for the opposite direction: reading Unicode code points from a JS string. readUTF8 : function(buff, p, l) { var s = "", ns; for(var i=0; i the Unicode code point value + * [1] => how many UTF-16 code units were consumed from the string + */ + readUnicodeCodePoint : function(str, ci) { + var c1 = str.charCodeAt(ci); + // High surrogate: try to combine it with the following low surrogate. + if(c1>=0xd800 && c1<=0xdbff && ci+1 convert the two UTF-16 code units into one + // Unicode code point. + if(c2>=0xdc00 && c2<=0xdfff) return [((c1-0xd800)<<10) + (c2-0xdc00) + 0x10000, 2]; + } + // Lone low surrogate is invalid on its own. Return U+FFFD so callers can + // encode a replacement character instead of emitting malformed UTF-8. + if(c1>=0xdc00 && c1<=0xdfff) return [0xfffd, 1]; + // Plain BMP code point; one UTF-16 code unit maps directly. + return [c1, 1]; + }, writeUTF8 : function(buff, p, str) { var strl = str.length, i=0; - for(var ci=0; ci> 6)); buff[p+i+1] = (128|((code>> 0)&63)); i+=2; } else if((code&(0xffffffff-(1<<16)+1))==0) { buff[p+i] = (224|(code>>12)); buff[p+i+1] = (128|((code>> 6)&63)); buff[p+i+2] = (128|((code>>0)&63)); i+=3; } @@ -51,9 +81,11 @@ var UZIP = {}; }, sizeUTF8 : function(str) { var strl = str.length, i=0; - for(var ci=0; ci