admin管理员组文章数量:1022736
Internet Explorer doesn't implement ArrayBuffer.prototype.slice
. Amazingly, they don't plan on implementing it any time soon. As such, are there any shims out there for this function? If not then this shall be the canonical answer on the internet as soon as I or someone else implements one.
Internet Explorer doesn't implement ArrayBuffer.prototype.slice
. Amazingly, they don't plan on implementing it any time soon. As such, are there any shims out there for this function? If not then this shall be the canonical answer on the internet as soon as I or someone else implements one.
- What's the most straightforward way to copy an ArrayBuffer object? does offer a shim – Bergi Commented Jan 29, 2014 at 19:08
-
@Bergi: ah thanks. that's an awful shim though, uses a for loop instead of
set
- perhaps worth it to put an answer there instead – Claudiu Commented Jan 29, 2014 at 19:14
2 Answers
Reset to default 4This seems to do the trick. Open to suggestions.
if (!ArrayBuffer.prototype.slice) {
//Returns a new ArrayBuffer whose contents are a copy of this ArrayBuffer's
//bytes from `begin`, inclusive, up to `end`, exclusive
ArrayBuffer.prototype.slice = function (begin, end) {
//If `begin` is unspecified, Chrome assumes 0, so we do the same
if (begin === void 0) {
begin = 0;
}
//If `end` is unspecified, the new ArrayBuffer contains all
//bytes from `begin` to the end of this ArrayBuffer.
if (end === void 0) {
end = this.byteLength;
}
//Chrome converts the values to integers via flooring
begin = Math.floor(begin);
end = Math.floor(end);
//If either `begin` or `end` is negative, it refers to an
//index from the end of the array, as opposed to from the beginning.
if (begin < 0) {
begin += this.byteLength;
}
if (end < 0) {
end += this.byteLength;
}
//The range specified by the `begin` and `end` values is clamped to the
//valid index range for the current array.
begin = Math.min(Math.max(0, begin), this.byteLength);
end = Math.min(Math.max(0, end), this.byteLength);
//If the puted length of the new ArrayBuffer would be negative, it
//is clamped to zero.
if (end - begin <= 0) {
return new ArrayBuffer(0);
}
var result = new ArrayBuffer(end - begin);
var resultBytes = new Uint8Array(result);
var sourceBytes = new Uint8Array(this, begin, end - begin);
resultBytes.set(sourceBytes);
return result;
};
}
Slightly adapted as per ments version of index.js from ttaubert's Arraybuffer-slice github repo
if (!ArrayBuffer.prototype.slice) {
ArrayBuffer.prototype.slice = function (begin, end) {
var len = this.byteLength;
begin = (begin|0) || 0;
end = end === (void 0) ? len : (end|0);
// Handle negative values.
if (begin < 0) begin = Math.max(begin + len, 0);
if (end < 0) end = Math.max(end + len, 0);
if (len === 0 || begin >= len || begin >= end) {
return new ArrayBuffer(0);
}
var length = Math.min(len - begin, end - begin);
var target = new ArrayBuffer(length);
var targetArray = new Uint8Array(target);
targetArray.set(new Uint8Array(this, begin, length));
return target;
};
}
Internet Explorer doesn't implement ArrayBuffer.prototype.slice
. Amazingly, they don't plan on implementing it any time soon. As such, are there any shims out there for this function? If not then this shall be the canonical answer on the internet as soon as I or someone else implements one.
Internet Explorer doesn't implement ArrayBuffer.prototype.slice
. Amazingly, they don't plan on implementing it any time soon. As such, are there any shims out there for this function? If not then this shall be the canonical answer on the internet as soon as I or someone else implements one.
- What's the most straightforward way to copy an ArrayBuffer object? does offer a shim – Bergi Commented Jan 29, 2014 at 19:08
-
@Bergi: ah thanks. that's an awful shim though, uses a for loop instead of
set
- perhaps worth it to put an answer there instead – Claudiu Commented Jan 29, 2014 at 19:14
2 Answers
Reset to default 4This seems to do the trick. Open to suggestions.
if (!ArrayBuffer.prototype.slice) {
//Returns a new ArrayBuffer whose contents are a copy of this ArrayBuffer's
//bytes from `begin`, inclusive, up to `end`, exclusive
ArrayBuffer.prototype.slice = function (begin, end) {
//If `begin` is unspecified, Chrome assumes 0, so we do the same
if (begin === void 0) {
begin = 0;
}
//If `end` is unspecified, the new ArrayBuffer contains all
//bytes from `begin` to the end of this ArrayBuffer.
if (end === void 0) {
end = this.byteLength;
}
//Chrome converts the values to integers via flooring
begin = Math.floor(begin);
end = Math.floor(end);
//If either `begin` or `end` is negative, it refers to an
//index from the end of the array, as opposed to from the beginning.
if (begin < 0) {
begin += this.byteLength;
}
if (end < 0) {
end += this.byteLength;
}
//The range specified by the `begin` and `end` values is clamped to the
//valid index range for the current array.
begin = Math.min(Math.max(0, begin), this.byteLength);
end = Math.min(Math.max(0, end), this.byteLength);
//If the puted length of the new ArrayBuffer would be negative, it
//is clamped to zero.
if (end - begin <= 0) {
return new ArrayBuffer(0);
}
var result = new ArrayBuffer(end - begin);
var resultBytes = new Uint8Array(result);
var sourceBytes = new Uint8Array(this, begin, end - begin);
resultBytes.set(sourceBytes);
return result;
};
}
Slightly adapted as per ments version of index.js from ttaubert's Arraybuffer-slice github repo
if (!ArrayBuffer.prototype.slice) {
ArrayBuffer.prototype.slice = function (begin, end) {
var len = this.byteLength;
begin = (begin|0) || 0;
end = end === (void 0) ? len : (end|0);
// Handle negative values.
if (begin < 0) begin = Math.max(begin + len, 0);
if (end < 0) end = Math.max(end + len, 0);
if (len === 0 || begin >= len || begin >= end) {
return new ArrayBuffer(0);
}
var length = Math.min(len - begin, end - begin);
var target = new ArrayBuffer(length);
var targetArray = new Uint8Array(target);
targetArray.set(new Uint8Array(this, begin, length));
return target;
};
}
本文标签: javascriptArrayBufferprototypeslice shim for IEStack Overflow
版权声明:本文标题:javascript - ArrayBuffer.prototype.slice shim for IE? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745545407a2155370.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论