mark a. foltz | eca304d | 2023-01-25 13:45:44 -0800 | [diff] [blame^] | 1 | // Copyright 2023 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef PLATFORM_BASE_BYTE_VIEW_H_ |
| 6 | #define PLATFORM_BASE_BYTE_VIEW_H_ |
| 7 | |
| 8 | #include <stddef.h> |
| 9 | |
| 10 | #include <cassert> |
| 11 | #include <cstddef> |
| 12 | #include <cstdint> |
| 13 | #include <vector> |
| 14 | |
| 15 | #include "platform/base/macros.h" |
| 16 | |
| 17 | namespace openscreen { |
| 18 | |
| 19 | // Contains a pointer and length to a span of continguous and unowned bytes. |
| 20 | // The underlying data cannot be modified. |
| 21 | // |
| 22 | // The API is a slimmed-down version of a C++20 std::span<const uint8_t> and is |
| 23 | // intended to be forwards-compatible. Support for iterators and front/back can |
| 24 | // be added as needed; we don't intend to add support for static extents. |
| 25 | // |
| 26 | // NOTE: Although other span implementations allow passing zero to last(), we do |
| 27 | // not, as the behavior is undefined. Callers should explicitly create an empty |
| 28 | // ByteView instead. |
| 29 | // |
| 30 | // TODO(https://issuetracker.google.com/254502854): Replace assert() expressions |
| 31 | // with OSP_CHECK* macros once those are available to files in platform/. |
| 32 | class ByteView { |
| 33 | public: |
| 34 | constexpr ByteView() noexcept = default; |
| 35 | constexpr ByteView(const uint8_t* data, size_t count) |
| 36 | : data_(data), count_(count) {} |
| 37 | explicit ByteView(const std::vector<uint8_t>& v) |
| 38 | : data_(v.data()), count_(v.size()) {} |
| 39 | |
| 40 | constexpr ByteView(const ByteView&) noexcept = default; |
| 41 | constexpr ByteView& operator=(const ByteView&) noexcept = default; |
| 42 | ByteView(ByteView&&) noexcept = default; |
| 43 | ByteView& operator=(ByteView&&) noexcept = default; |
| 44 | ~ByteView() = default; |
| 45 | |
| 46 | constexpr const uint8_t* data() const noexcept { return data_; } |
| 47 | |
| 48 | constexpr const uint8_t& operator[](size_t idx) const { |
| 49 | assert(idx < count_); |
| 50 | return *(data_ + idx); |
| 51 | } |
| 52 | |
| 53 | constexpr size_t size() const { return count_; } |
| 54 | |
| 55 | [[nodiscard]] constexpr bool empty() const { return count_ == 0; } |
| 56 | |
| 57 | constexpr ByteView first(size_t count) const { |
| 58 | assert(count <= count_); |
| 59 | return ByteView(data_, count); |
| 60 | } |
| 61 | |
| 62 | constexpr ByteView last(size_t count) const { |
| 63 | assert(count <= count_); |
| 64 | assert(count != 0); |
| 65 | return ByteView(data_ + (count_ - count), count); |
| 66 | } |
| 67 | |
| 68 | constexpr const uint8_t* begin() const noexcept { return data_; } |
| 69 | constexpr const uint8_t* end() const noexcept { return data_ + count_; } |
| 70 | |
| 71 | void remove_prefix(size_t count) noexcept { |
| 72 | assert(count_ >= count); |
| 73 | data_ += count; |
| 74 | count_ -= count; |
| 75 | } |
| 76 | |
| 77 | void remove_suffix(size_t count) noexcept { |
| 78 | assert(count_ >= count); |
| 79 | count_ -= count; |
| 80 | } |
| 81 | |
| 82 | constexpr ByteView subspan(size_t offset, size_t count) const { |
| 83 | assert(offset + count < count_); |
| 84 | return ByteView(data_ + offset, count); |
| 85 | } |
| 86 | |
| 87 | private: |
| 88 | const uint8_t* data_{nullptr}; |
| 89 | size_t count_{0}; |
| 90 | }; |
| 91 | |
| 92 | } // namespace openscreen |
| 93 | |
| 94 | #endif // PLATFORM_BASE_BYTE_VIEW_H_ |