diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2021-12-07 14:00:59 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-07 14:00:59 +0100 |
commit | 46d384060ef20672e23b3c1ffe947ff7898ecf75 (patch) | |
tree | 3d93fe69a70c24618074e4eb329eff4bf0fd8aca /core/templates | |
parent | a33b85c122fd18534ce556ae02bc87c36817a5b3 (diff) | |
parent | dd30253cdcf9b43e401cb3ba6b973f8890551a81 (diff) |
Merge pull request #35901 from nathanfranke/pool-byte-array-subarray-exclusive
Diffstat (limited to 'core/templates')
-rw-r--r-- | core/templates/vector.h | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/core/templates/vector.h b/core/templates/vector.h index 376d5cbeff..2f51a83848 100644 --- a/core/templates/vector.h +++ b/core/templates/vector.h @@ -143,27 +143,28 @@ public: return ret; } - Vector<T> subarray(int p_from, int p_to) const { - if (p_from < 0) { - p_from = size() + p_from; - } - if (p_to < 0) { - p_to = size() + p_to; + Vector<T> slice(int p_begin, int p_end) const { + Vector<T> result; + + if (p_end < 0) { + p_end += size() + 1; } - ERR_FAIL_INDEX_V(p_from, size(), Vector<T>()); - ERR_FAIL_INDEX_V(p_to, size(), Vector<T>()); + ERR_FAIL_INDEX_V(p_begin, size(), result); + ERR_FAIL_INDEX_V(p_end, size() + 1, result); + + ERR_FAIL_COND_V(p_begin > p_end, result); + + int result_size = p_end - p_begin; + result.resize(result_size); - Vector<T> slice; - int span = 1 + p_to - p_from; - slice.resize(span); - const T *r = ptr(); - T *w = slice.ptrw(); - for (int i = 0; i < span; ++i) { - w[i] = r[p_from + i]; + const T *const r = ptr(); + T *const w = result.ptrw(); + for (int i = 0; i < result_size; ++i) { + w[i] = r[p_begin + i]; } - return slice; + return result; } bool operator==(const Vector<T> &p_arr) const { |