forked from darrachequesne/notepack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use TextDecoder/TextEncoder where available
- Loading branch information
Showing
3 changed files
with
120 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
'use strict'; | ||
/* globals TextDecoder, TextEncoder */ | ||
|
||
var read = function read(view, offset, length) { | ||
var string = '', chr = 0; | ||
for (var i = offset, end = offset + length; i < end; i++) { | ||
var byte = view.getUint8(i); | ||
if ((byte & 0x80) === 0x00) { | ||
string += String.fromCharCode(byte); | ||
continue; | ||
} | ||
if ((byte & 0xe0) === 0xc0) { | ||
string += String.fromCharCode( | ||
((byte & 0x1f) << 6) | | ||
(view.getUint8(++i) & 0x3f) | ||
); | ||
continue; | ||
} | ||
if ((byte & 0xf0) === 0xe0) { | ||
string += String.fromCharCode( | ||
((byte & 0x0f) << 12) | | ||
((view.getUint8(++i) & 0x3f) << 6) | | ||
((view.getUint8(++i) & 0x3f) << 0) | ||
); | ||
continue; | ||
} | ||
if ((byte & 0xf8) === 0xf0) { | ||
chr = ((byte & 0x07) << 18) | | ||
((view.getUint8(++i) & 0x3f) << 12) | | ||
((view.getUint8(++i) & 0x3f) << 6) | | ||
((view.getUint8(++i) & 0x3f) << 0); | ||
if (chr >= 0x010000) { // surrogate pair | ||
chr -= 0x010000; | ||
string += String.fromCharCode((chr >>> 10) + 0xD800, (chr & 0x3FF) + 0xDC00); | ||
} else { | ||
string += String.fromCharCode(chr); | ||
} | ||
continue; | ||
} | ||
throw new Error('Invalid byte ' + byte.toString(16)); | ||
} | ||
return string; | ||
}; | ||
|
||
if (typeof TextDecoder !== 'undefined') { | ||
var decoder = new TextDecoder(); | ||
read = function read(view, offset, length) { | ||
var arr = new Uint8Array(view.buffer || view, offset, length); | ||
return decoder.decode(arr); | ||
}; | ||
} | ||
|
||
var write = function write(view, offset, str) { | ||
var c = 0; | ||
for (var i = 0, l = str.length; i < l; i++) { | ||
c = str.charCodeAt(i); | ||
if (c < 0x80) { | ||
view.setUint8(offset++, c); | ||
} | ||
else if (c < 0x800) { | ||
view.setUint8(offset++, 0xc0 | (c >> 6)); | ||
view.setUint8(offset++, 0x80 | (c & 0x3f)); | ||
} | ||
else if (c < 0xd800 || c >= 0xe000) { | ||
view.setUint8(offset++, 0xe0 | (c >> 12)); | ||
view.setUint8(offset++, 0x80 | (c >> 6) & 0x3f); | ||
view.setUint8(offset++, 0x80 | (c & 0x3f)); | ||
} | ||
else { | ||
i++; | ||
c = 0x10000 + (((c & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff)); | ||
view.setUint8(offset++, 0xf0 | (c >> 18)); | ||
view.setUint8(offset++, 0x80 | (c >> 12) & 0x3f); | ||
view.setUint8(offset++, 0x80 | (c >> 6) & 0x3f); | ||
view.setUint8(offset++, 0x80 | (c & 0x3f)); | ||
} | ||
} | ||
}; | ||
|
||
if (typeof TextEncoder !== 'undefined') { | ||
var encoder = new TextEncoder(); | ||
write = function write(view, offset, str) { | ||
var arr = new Uint8Array(view.buffer || view, offset); | ||
var encoded = encoder.encode(str); | ||
arr.set(encoded); | ||
}; | ||
} | ||
|
||
function length(str) { | ||
var c = 0, length = 0; | ||
for (var i = 0, l = str.length; i < l; i++) { | ||
c = str.charCodeAt(i); | ||
if (c < 0x80) { | ||
length += 1; | ||
} | ||
else if (c < 0x800) { | ||
length += 2; | ||
} | ||
else if (c < 0xd800 || c >= 0xe000) { | ||
length += 3; | ||
} | ||
else { | ||
i++; | ||
length += 4; | ||
} | ||
} | ||
return length; | ||
} | ||
|
||
module.exports = { | ||
read: read, | ||
write: write, | ||
length: length | ||
}; |