diff --git a/util.js b/util.js --- a/util.js +++ b/util.js @@ -32,7 +32,7 @@ function bytes2hex(arr){ function str2utf8(s){ let res=[]; let c=s.codePointAt(0); - for(let i=0;c!==undefined;i++,c=s.codePointAt(i)){ + for(let i=0; c!==undefined; i++,c=s.codePointAt(i)){ if(c<0x80){res.push(c);} else if(c<0x800){ res.push(0b11000000|(c>>>6)); @@ -49,11 +49,44 @@ function str2utf8(s){ res.push(0b10000000|((c>>>6)&0b111111)); res.push(0b10000000|(c&0b111111)); } + if(c>0xffff){i++;} // skip surrogate } return res; } -/*console.log(str2utf8("$").map(x=>x.toString(16))); -console.log(str2utf8("¢").map(x=>x.toString(16))); -console.log(str2utf8("€").map(x=>x.toString(16))); -console.log(str2utf8("𐍈").map(x=>x.toString(16)));*/ +function utf82str(arr){ + let res=[]; + for(let i=0;iString.fromCodePoint(x)).join(""); +} + +if(typeof module!=='undefined'&&module.hasOwnProperty('exports')){ + module.exports.bytes2int32=bytes2int32; + module.exports.bytes2int32s=bytes2int32s; + module.exports.int322bytes=int322bytes; + module.exports.int32s2bytes=int32s2bytes; + module.exports.bytes2hex=bytes2hex; + module.exports.str2utf8=str2utf8; + module.exports.utf82str=utf82str; +}