diff options
author | Ryan Prichard <rprichard@google.com> | 2024-02-15 22:11:19 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2024-02-15 22:11:19 +0000 |
commit | 7fe83b4b21129d9f74dacafa69748e8b898fabde (patch) | |
tree | 499f78023d5887a5c8a04a452f0c17812101231d | |
parent | 74b1ec3457a8c6ebcecfa8daf7a0e7583bdf475d (diff) | |
parent | ac7516b33e516a3ea41e7dcdde323dc7082c4c95 (diff) | |
download | libcppbor-7fe83b4b21129d9f74dacafa69748e8b898fabde.tar.gz |
Revert^2 "ViewBstr: replace std::string_view<uint8_t> with std::span" am: ac7516b33e
Original change: https://android-review.googlesource.com/c/platform/system/libcppbor/+/2961988
Change-Id: Ic9a4963134d6374a50dbb3ffb751309d3498bde8
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r-- | include/cppbor/cppbor.h | 16 | ||||
-rw-r--r-- | tests/cppbor_test.cpp | 11 |
2 files changed, 17 insertions, 10 deletions
diff --git a/include/cppbor/cppbor.h b/include/cppbor/cppbor.h index f7a2af0..e0962db 100644 --- a/include/cppbor/cppbor.h +++ b/include/cppbor/cppbor.h @@ -16,12 +16,14 @@ #pragma once +#include <algorithm> #include <cassert> #include <cstdint> #include <functional> #include <iterator> #include <memory> #include <numeric> +#include <span> #include <string> #include <string_view> #include <vector> @@ -440,7 +442,7 @@ class Bstr : public Item { }; /** - * ViewBstr is a read-only version of Bstr backed by std::string_view + * ViewBstr is a read-only version of Bstr backed by std::span */ class ViewBstr : public Item { public: @@ -449,8 +451,8 @@ class ViewBstr : public Item { // Construct an empty ViewBstr explicit ViewBstr() {} - // Construct from a string_view of uint8_t values - explicit ViewBstr(std::basic_string_view<uint8_t> v) : mView(std::move(v)) {} + // Construct from a span of uint8_t values + explicit ViewBstr(std::span<const uint8_t> v) : mView(std::move(v)) {} // Construct from a string_view explicit ViewBstr(std::string_view v) @@ -466,7 +468,9 @@ class ViewBstr : public Item { ViewBstr(const uint8_t* begin, const uint8_t* end) : mView(begin, std::distance(begin, end)) {} - bool operator==(const ViewBstr& other) const& { return mView == other.mView; } + bool operator==(const ViewBstr& other) const& { + return std::equal(mView.begin(), mView.end(), other.mView.begin(), other.mView.end()); + } MajorType type() const override { return kMajorType; } using Item::asViewBstr; @@ -479,14 +483,14 @@ class ViewBstr : public Item { encodeValue(encodeCallback); } - const std::basic_string_view<uint8_t>& view() const { return mView; } + const std::span<const uint8_t>& view() const { return mView; } std::unique_ptr<Item> clone() const override { return std::make_unique<ViewBstr>(mView); } private: void encodeValue(EncodeCallback encodeCallback) const; - std::basic_string_view<uint8_t> mView; + std::span<const uint8_t> mView; }; /** diff --git a/tests/cppbor_test.cpp b/tests/cppbor_test.cpp index aaa8bc5..a75acc8 100644 --- a/tests/cppbor_test.cpp +++ b/tests/cppbor_test.cpp @@ -969,8 +969,8 @@ TEST(ConvertTest, ViewTstr) { TEST(ConvertTest, ViewBstr) { array<uint8_t, 3> vec{0x23, 0x24, 0x22}; - basic_string_view<uint8_t> sv(vec.data(), vec.size()); - unique_ptr<Item> item = details::makeItem(ViewBstr(sv)); + span<const uint8_t> view(vec.data(), vec.size()); + unique_ptr<Item> item = details::makeItem(ViewBstr(view)); EXPECT_EQ(BSTR, item->type()); EXPECT_EQ(nullptr, item->asInt()); @@ -986,7 +986,10 @@ TEST(ConvertTest, ViewBstr) { EXPECT_EQ(nullptr, item->asViewTstr()); EXPECT_NE(nullptr, item->asViewBstr()); - EXPECT_EQ(sv, item->asViewBstr()->view()); + auto toVec = [](span<const uint8_t> view) { + return std::vector<uint8_t>(view.begin(), view.end()); + }; + EXPECT_EQ(toVec(view), toVec(item->asViewBstr()->view())); } TEST(CloningTest, Uint) { @@ -1111,7 +1114,7 @@ TEST(CloningTest, ViewTstr) { TEST(CloningTest, ViewBstr) { array<uint8_t, 5> vec{1, 2, 3, 255, 0}; - basic_string_view<uint8_t> sv(vec.data(), vec.size()); + span<const uint8_t> sv(vec.data(), vec.size()); ViewBstr item(sv); auto clone = item.clone(); EXPECT_EQ(clone->type(), BSTR); |