Showing the changes to libraries/script-engine/src/ArrayBufferPrototype.cpp (1 of 2 files). Show all
Unified · Split
| @@ -9,8 +9,11 @@ · 8 unmodified lines | ||
| 9 | 9 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html |
| 10 | 10 | // |
| 11 | 11 | |
| 12 | +#include <QDebug> | |
| 12 | 13 | #include <QScriptEngine> |
| 13 | 14 | |
| 15 | +#include <glm/glm.hpp> | |
| 16 | + | |
| 14 | 17 | #include "ArrayBufferClass.h" |
| 15 | 18 | |
| 16 | 19 | #include "ArrayBufferPrototype.h" |
| @@ -21,7 +24,27 @@ ArrayBufferPrototype::ArrayBufferPrototype(QObject* parent) : QObject(parent) { · 4 unmodified lines | ||
| 21 | 24 | } |
| 22 | 25 | |
| 23 | 26 | 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); | |
| 25 | 48 | } |
| 26 | 49 | |
| 27 | 50 | QByteArray* ArrayBufferPrototype::thisArrayBuffer() const { |