aboutsummaryrefslogtreecommitdiff
path: root/include/openssl/span.h
diff options
context:
space:
mode:
authorDavid Benjamin <davidben@google.com>2021-06-22 23:00:49 -0400
committerAdam Langley <agl@google.com>2021-08-16 18:20:28 +0000
commit006f20ad7f9a6ce53b44390c0689f3690bf73ad1 (patch)
treebb51b1761c10616d1fc41534eed1dd0df72e48de /include/openssl/span.h
parent2e68a05c9943a8dec1758d4a393b2ae906fd3295 (diff)
downloadboringssl-006f20ad7f9a6ce53b44390c0689f3690bf73ad1.zip
boringssl-006f20ad7f9a6ce53b44390c0689f3690bf73ad1.tar.gz
boringssl-006f20ad7f9a6ce53b44390c0689f3690bf73ad1.tar.bz2
Add Span::first() and Span::last().
absl::Span, base::span, and std::span have first() and last() methods which give prefixes and suffixes. first() just saves 5 characters, but last() is nicer to write than subspan() for suffixes. Unlike subspan(), they also do not have clipping behavior, so we're guaranteed the length is correct. The clipping behavior comes from absl::Span::subspan() and is not present in std::span or base::span. I've left it in, in case we switch to absl::Span in the future, but I imagine absl::Span will need to migrate this at some point. Change-Id: I042dd6c566b6d753ec6de9d84e8c09ac7c270267 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/48905 Reviewed-by: Adam Langley <agl@google.com>
Diffstat (limited to 'include/openssl/span.h')
-rw-r--r--include/openssl/span.h21
1 files changed, 20 insertions, 1 deletions
diff --git a/include/openssl/span.h b/include/openssl/span.h
index 7410bf9..6b1b232 100644
--- a/include/openssl/span.h
+++ b/include/openssl/span.h
@@ -158,11 +158,30 @@ class Span : private internal::SpanBase<const T> {
Span subspan(size_t pos = 0, size_t len = npos) const {
if (pos > size_) {
- abort(); // absl::Span throws an exception here.
+ // absl::Span throws an exception here. Note std::span and Chromium
+ // base::span additionally forbid pos + len being out of range, with a
+ // special case at npos/dynamic_extent, while absl::Span::subspan clips
+ // the span. For now, we align with absl::Span in case we switch to it in
+ // the future.
+ abort();
}
return Span(data_ + pos, std::min(size_ - pos, len));
}
+ Span first(size_t len) {
+ if (len > size_) {
+ abort();
+ }
+ return Span(data_, len);
+ }
+
+ Span last(size_t len) {
+ if (len > size_) {
+ abort();
+ }
+ return Span(data_ + size_ - len, len);
+ }
+
private:
T *data_;
size_t size_;