summaryrefslogtreecommitdiff
path: root/core/templates
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <remi@verschelde.fr>2022-01-18 13:22:35 +0100
committerGitHub <noreply@github.com>2022-01-18 13:22:35 +0100
commit9912492e937d2454a21ec7465538aab2431ac249 (patch)
tree4afd7886de16fabdd4da07f3922f68c55b0cb6a8 /core/templates
parent366d3930ac96db5561afd23b1063085fccf6dcd6 (diff)
parentc6cefb1b79d207af1bc78ce20c01b5788e806252 (diff)
Merge pull request #56668 from akien-mga/array-slice-nicer-bound-checks
Diffstat (limited to 'core/templates')
-rw-r--r--core/templates/vector.h23
1 files changed, 14 insertions, 9 deletions
diff --git a/core/templates/vector.h b/core/templates/vector.h
index e53c502f67..bd4c6ade86 100644
--- a/core/templates/vector.h
+++ b/core/templates/vector.h
@@ -42,6 +42,7 @@
#include "core/templates/search_array.h"
#include "core/templates/sort_array.h"
+#include <climits>
#include <initializer_list>
template <class T>
@@ -144,25 +145,29 @@ public:
return ret;
}
- Vector<T> slice(int p_begin, int p_end) const {
+ Vector<T> slice(int p_begin, int p_end = INT_MAX) const {
Vector<T> result;
- if (p_end < 0) {
- p_end += size() + 1;
- }
+ const int s = size();
- ERR_FAIL_INDEX_V(p_begin, size(), result);
- ERR_FAIL_INDEX_V(p_end, size() + 1, result);
+ int begin = CLAMP(p_begin, -s, s);
+ if (begin < 0) {
+ begin += s;
+ }
+ int end = CLAMP(p_end, -s, s);
+ if (end < 0) {
+ end += s;
+ }
- ERR_FAIL_COND_V(p_begin > p_end, result);
+ ERR_FAIL_COND_V(begin > end, result);
- int result_size = p_end - p_begin;
+ int result_size = end - begin;
result.resize(result_size);
const T *const r = ptr();
T *const w = result.ptrw();
for (int i = 0; i < result_size; ++i) {
- w[i] = r[p_begin + i];
+ w[i] = r[begin + i];
}
return result;