History

Fixed up ArrayBuffer::slice() behaviour

Atlante45 committed 12 years ago

Commit 96a13901 parent f9b739a2 files · +27 −3Browse files

Showing the changes to libraries/script-engine/src/ArrayBufferPrototype.cpp (1 of 2 files). Show all

Unified · Split

libraries/script-engine/src/ArrayBufferPrototype.cpp+24−1
@@ -9,8 +9,11 @@ · 8 unmodified lines
99 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
1010 //
1111
12+#include <QDebug>
1213 #include <QScriptEngine>
1314
15+#include <glm/glm.hpp>
16+
1417 #include "ArrayBufferClass.h"
1518
1619 #include "ArrayBufferPrototype.h"
@@ -21,7 +24,27 @@ ArrayBufferPrototype::ArrayBufferPrototype(QObject* parent) : QObject(parent) { · 4 unmodified lines
2124 }
2225
2326 QByteArray ArrayBufferPrototype::slice(long begin, long end) const {
24− return thisArrayBuffer()->mid(begin, end);
27+ QByteArray* ba = thisArrayBuffer();
28+ // if indices < 0 then they start from the end of the array
29+ begin = (begin < 0) ? ba->size() + begin : begin;
30+ end = (end < 0) ? ba->size() + end : end;
31+
32+ // here we clamp the indices to fit the array
33+ begin = glm::clamp(begin, 0l, (long)(ba->size() - 1));
34+ end = glm::clamp(end, 0l, (long)(ba->size() - 1));
35+
36+ return (end - begin > 0) ? ba->mid(begin, end - begin) : QByteArray();
37+}
38+
39+QByteArray ArrayBufferPrototype::slice(long begin) const {
40+ QByteArray* ba = thisArrayBuffer();
41+ // if indices < 0 then they start from the end of the array
42+ begin = (begin < 0) ? ba->size() + begin : begin;
43+
44+ // here we clamp the indices to fit the array
45+ begin = glm::clamp(begin, 0l, (long)(ba->size() - 1));
46+
47+ return ba->mid(begin, -1);
2548 }
2649
2750 QByteArray* ArrayBufferPrototype::thisArrayBuffer() const {