Add ByteBufferReader constructor for ArrayView

Bug: webrtc:14870
Change-Id: I0643af4a44bb2bc26581df971221766316124996
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/327520
Auto-Submit: Per Kjellander <perkj@webrtc.org>
Commit-Queue: Harald Alvestrand <hta@webrtc.org>
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#41182}
This commit is contained in:
Per K 2023-11-17 10:05:36 +01:00 committed by WebRTC LUCI CQ
parent db1d4281d4
commit cf2e08b798
4 changed files with 31 additions and 7 deletions

View File

@ -156,8 +156,12 @@ rtc_library("byte_buffer") {
deps = [
":buffer",
":byte_order",
"../api:array_view",
]
absl_deps = [
"//third_party/abseil-cpp/absl/base:core_headers",
"//third_party/abseil-cpp/absl/strings",
]
absl_deps = [ "//third_party/abseil-cpp/absl/strings" ]
}
rtc_library("buffer_queue") {

View File

@ -19,6 +19,10 @@ ByteBufferWriter::ByteBufferWriter() : ByteBufferWriterT() {}
ByteBufferWriter::ByteBufferWriter(const char* bytes, size_t len)
: ByteBufferWriterT(bytes, len) {}
ByteBufferReader::ByteBufferReader(rtc::ArrayView<const uint8_t> bytes) {
Construct(bytes.data(), bytes.size());
}
ByteBufferReader::ByteBufferReader(const char* bytes, size_t len) {
Construct(reinterpret_cast<const uint8_t*>(bytes), len);
}
@ -27,10 +31,6 @@ ByteBufferReader::ByteBufferReader(const char* bytes) {
Construct(reinterpret_cast<const uint8_t*>(bytes), strlen(bytes));
}
ByteBufferReader::ByteBufferReader(const Buffer& buf) {
Construct(buf.data(), buf.size());
}
ByteBufferReader::ByteBufferReader(const ByteBufferWriter& buf) {
Construct(reinterpret_cast<const uint8_t*>(buf.Data()), buf.Length());
}

View File

@ -16,7 +16,9 @@
#include <string>
#include "absl/base/attributes.h"
#include "absl/strings/string_view.h"
#include "api/array_view.h"
#include "rtc_base/buffer.h"
#include "rtc_base/byte_order.h"
@ -124,11 +126,12 @@ class ByteBufferReader {
public:
ByteBufferReader(const char* bytes, size_t len);
explicit ByteBufferReader(
rtc::ArrayView<const uint8_t> bytes ABSL_ATTRIBUTE_LIFETIME_BOUND);
// Initializes buffer from a zero-terminated string.
explicit ByteBufferReader(const char* bytes);
explicit ByteBufferReader(const Buffer& buf);
explicit ByteBufferReader(const ByteBufferWriter& buf);
ByteBufferReader(const ByteBufferReader&) = delete;

View File

@ -12,6 +12,8 @@
#include <string.h>
#include <cstdint>
#include "rtc_base/arraysize.h"
#include "rtc_base/byte_order.h"
#include "test/gtest.h"
@ -247,4 +249,19 @@ TEST(ByteBufferTest, TestReadWriteUVarint) {
EXPECT_EQ(size, read_buffer.Length());
}
TEST(ByteBufferTest, ReadFromArrayView) {
const uint8_t buf[] = {'a', 'b', 'c'};
ArrayView<const uint8_t> view(buf, 3);
ByteBufferReader read_buffer(view);
uint8_t val;
EXPECT_TRUE(read_buffer.ReadUInt8(&val));
EXPECT_EQ(val, 'a');
EXPECT_TRUE(read_buffer.ReadUInt8(&val));
EXPECT_EQ(val, 'b');
EXPECT_TRUE(read_buffer.ReadUInt8(&val));
EXPECT_EQ(val, 'c');
EXPECT_FALSE(read_buffer.ReadUInt8(&val));
}
} // namespace rtc