Move buffer to string converting to utils

This commit is contained in:
Dávid Szabó 2016-09-23 14:55:39 +02:00 committed by Julian Descottes
parent 23a0768f8d
commit d4f038f97b
2 changed files with 23 additions and 5 deletions

View File

@ -15,11 +15,7 @@
var description = piskel.getDescriptor().description;
var serialized = pskl.utils.Serializer.serializePiskel(piskel);
var serializedString = '';
var uint16 = new Uint16Array(serialized);
for (var i = 0, length = uint16.length; i < length; i++) {
serializedString += String.fromCharCode(uint16[i]);
}
var serializedString = pskl.utils.convertBufferToString(serialized);
if (pskl.app.localStorageService.getPiskel(name)) {
var confirmOverwrite = window.confirm('There is already a piskel saved as ' + name + '. Overwrite ?');

View File

@ -253,4 +253,26 @@ if (!Uint32Array.prototype.fill) {
return string;
};
ns.convertBufferToString = function (inputBuffer) {
if (typeof inputBuffer.buffer !== 'undefined') {
inputBuffer = inputBuffer.buffer;
}
var buffer;
if (inputBuffer.byteLength % 2 === 1) {
buffer = ArrayBuffer.transfer(inputBuffer, inputBuffer.byteLength + 1);
} else {
buffer = inputBuffer;
}
var string = '';
var uint16 = new Uint16Array(buffer);
for (var i = 0, length = uint16.length; i < length; i++) {
string += String.fromCharCode(uint16[i]);
}
return string;
};
})();