Add new base::span<> functions to pdfium::span<>.
Keep more-or-less in sync with base/, but simplify somewhat in
keeping with pdfium's simpler representations. Then use these in
a few places as appropriate.
-- add as_writable_bytes()
-- add byte_span_from_ref()
Change-Id: I300459caed48995e2ae9c6192c3fe431b8d574ca
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/116210
Reviewed-by: Thomas Sepez <tsepez@google.com>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fdrm/fx_crypt_unittest.cpp b/core/fdrm/fx_crypt_unittest.cpp
index 35eed47..b4528b4 100644
--- a/core/fdrm/fx_crypt_unittest.cpp
+++ b/core/fdrm/fx_crypt_unittest.cpp
@@ -55,8 +55,9 @@
}
TEST(FXCRYPT, MD5GenerateOneByteData) {
+ const char c = 'a';
uint8_t digest[16];
- CRYPT_MD5Generate(pdfium::as_bytes(pdfium::make_span("a", 1u)), digest);
+ CRYPT_MD5Generate(pdfium::byte_span_from_ref(c), digest);
static constexpr uint8_t kExpected[] = {0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1,
0xb6, 0xa8, 0x31, 0xc3, 0x99, 0xe2,
diff --git a/core/fpdfapi/parser/cpdf_syntax_parser_unittest.cpp b/core/fpdfapi/parser/cpdf_syntax_parser_unittest.cpp
index c176f18..3e48c63 100644
--- a/core/fpdfapi/parser/cpdf_syntax_parser_unittest.cpp
+++ b/core/fpdfapi/parser/cpdf_syntax_parser_unittest.cpp
@@ -133,9 +133,9 @@
{
// Just ending character.
- static const uint8_t data[] = ">";
+ const char gt = '>';
CPDF_SyntaxParser parser(pdfium::MakeRetain<CFX_ReadOnlySpanStream>(
- pdfium::make_span(data, 1u)));
+ pdfium::byte_span_from_ref(gt)));
EXPECT_EQ("", parser.ReadHexString());
EXPECT_EQ(1, parser.GetPos());
}
diff --git a/third_party/base/containers/span.h b/third_party/base/containers/span.h
index bfead3d..2c8b90e 100644
--- a/third_party/base/containers/span.h
+++ b/third_party/base/containers/span.h
@@ -180,7 +180,9 @@
//
// Additions beyond the C++ standard draft
// - as_byte_span() function.
+// - as_writable_byte_span() function.
// - span_from_ref() function.
+// - byte_span_from_ref() function.
// [span], class template span
template <typename T>
@@ -365,6 +367,19 @@
return span<T>(&single_object, 1u);
}
+// `byte_span_from_ref` converts a reference to T into a span of uint8_t of
+// length sizeof(T). This is a non-std helper that is a sugar for
+// `as_writable_bytes(span_from_ref(x))`.
+template <typename T>
+static constexpr span<const uint8_t> byte_span_from_ref(
+ const T& single_object) noexcept {
+ return as_bytes(span<const T>(&single_object, 1u));
+}
+template <typename T>
+static constexpr span<uint8_t> byte_span_from_ref(T& single_object) noexcept {
+ return as_writable_bytes(span<T>(&single_object, 1u));
+}
+
// Convenience function for converting an object which is itself convertible
// to span into a span of bytes (i.e. span of const uint8_t). Typically used
// to convert std::string or string-objects holding chars, or std::vector
@@ -375,6 +390,16 @@
return as_bytes(make_span(arg));
}
+// Convenience function for converting an object which is itself convertible
+// to span into a span of mutable bytes (i.e. span of uint8_t). Typically used
+// to convert std::string or string-objects holding chars, or std::vector
+// or vector-like objects holding other scalar types, prior to passing them
+// into an API that requires mutable byte spans.
+template <typename T>
+constexpr span<uint8_t> as_writable_byte_span(T& arg) {
+ return as_writable_bytes(make_span(arg));
+}
+
} // namespace pdfium
#endif // THIRD_PARTY_BASE_CONTAINERS_SPAN_H_