aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Prichard <rprichard@google.com>2024-02-15 03:28:26 +0000
committerRyan Prichard <rprichard@google.com>2024-02-15 14:52:06 -0800
commitb1b998be4ec447f3086e7fd6a7f78eaec66a1c45 (patch)
treef879926d84da38e068e5113157611ac0ce74087d
parent20d2be8672d24bfb441d075f82cc317d17d601f8 (diff)
downloadlibcppbor-master.tar.gz
ViewBstr: replace std::string_view<uint8_t> with std::spanHEADmastermain
In newer versions of libc++, std::char_traits<T> is no longer defined for non-character types, and a result, std::basic_string_view<uint8_t> is also no longer defined. See https://discourse.llvm.org/t/deprecating-std-string-t-for-non-character-t/66779. Trusty defaults to C++20, and this code uses std::span from C++20, so stop pinning the language mode to C++17. Bug: 175635923 Test: trusty/vendor/google/aosp/scripts/build.py qemu-generic-arm64-test-debug Change-Id: Ic926a5c194f089891333597c81383cd6b69cf9d5
-rw-r--r--include/cppbor/cppbor.h16
-rw-r--r--rules.mk2
-rw-r--r--tests/cppbor_test.cpp11
3 files changed, 17 insertions, 12 deletions
diff --git a/include/cppbor/cppbor.h b/include/cppbor/cppbor.h
index 8338441..35facc0 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>
@@ -435,7 +437,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:
@@ -444,8 +446,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)
@@ -461,7 +463,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;
@@ -474,14 +478,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/rules.mk b/rules.mk
index c5a19d8..2fbbbd9 100644
--- a/rules.mk
+++ b/rules.mk
@@ -18,8 +18,6 @@ LOCAL_DIR := $(GET_LOCAL_DIR)
MODULE := $(LOCAL_DIR)
-MODULE_CPPFLAGS := -std=c++17
-
MODULE_SRCS := \
$(LOCAL_DIR)/src/cppbor.cpp \
$(LOCAL_DIR)/src/cppbor_parse.cpp
diff --git a/tests/cppbor_test.cpp b/tests/cppbor_test.cpp
index 68778dc..a193cea 100644
--- a/tests/cppbor_test.cpp
+++ b/tests/cppbor_test.cpp
@@ -942,8 +942,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());
@@ -957,7 +957,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) {
@@ -1079,7 +1082,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);