Add pdfium::as_byte_span() helper

Borrow Chromium's base::as_byte_span(), from https://crrev.com/1225162.
Use it where applicable to simplify the code slightly.

Change-Id: I262fd572a6eb9c77f5359b29dd6a8db375201455
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/114012
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/edit/cpdf_creator.cpp b/core/fpdfapi/edit/cpdf_creator.cpp
index ee0b371..95b5dbb 100644
--- a/core/fpdfapi/edit/cpdf_creator.cpp
+++ b/core/fpdfapi/edit/cpdf_creator.cpp
@@ -107,8 +107,7 @@
   buffer[3] = FX_Random_MT_Generate(pContext2);
   FX_Random_MT_Close(pContext1);
   FX_Random_MT_Close(pContext2);
-  return ByteString(
-      ByteStringView(pdfium::as_bytes(pdfium::make_span(buffer))));
+  return ByteString(ByteStringView(pdfium::as_byte_span(buffer)));
 }
 
 bool OutputIndex(IFX_ArchiveStream* archive, FX_FILESIZE offset) {
diff --git a/core/fpdfapi/parser/cpdf_cross_ref_avail_unittest.cpp b/core/fpdfapi/parser/cpdf_cross_ref_avail_unittest.cpp
index 57f3bd4..709f77a 100644
--- a/core/fpdfapi/parser/cpdf_cross_ref_avail_unittest.cpp
+++ b/core/fpdfapi/parser/cpdf_cross_ref_avail_unittest.cpp
@@ -171,7 +171,7 @@
            FXSYS_itoa(static_cast<int>(prev_offset), int_buffer, 10) + ">>\n";
   const FX_FILESIZE last_crossref_offset = static_cast<FX_FILESIZE>(cur_offset);
 
-  auto parser = MakeParserForBuffer(pdfium::as_bytes(pdfium::make_span(table)));
+  auto parser = MakeParserForBuffer(pdfium::as_byte_span(table));
   auto cross_ref_avail =
       std::make_unique<CPDF_CrossRefAvail>(parser.get(), last_crossref_offset);
   EXPECT_EQ(CPDF_DataAvail::kDataAvailable, cross_ref_avail->CheckAvail());
@@ -215,7 +215,7 @@
            "endobj\n";
   const FX_FILESIZE last_crossref_offset = static_cast<FX_FILESIZE>(cur_offset);
 
-  auto parser = MakeParserForBuffer(pdfium::as_bytes(pdfium::make_span(table)));
+  auto parser = MakeParserForBuffer(pdfium::as_byte_span(table));
   auto cross_ref_avail =
       std::make_unique<CPDF_CrossRefAvail>(parser.get(), last_crossref_offset);
   EXPECT_EQ(CPDF_DataAvail::kDataAvailable, cross_ref_avail->CheckAvail());
@@ -264,7 +264,7 @@
            FXSYS_itoa(first_v5_table_offset, int_buffer, 10) + ">>\n";
   const FX_FILESIZE last_crossref_offset = last_v4_table_offset;
 
-  auto parser = MakeParserForBuffer(pdfium::as_bytes(pdfium::make_span(table)));
+  auto parser = MakeParserForBuffer(pdfium::as_byte_span(table));
   auto cross_ref_avail =
       std::make_unique<CPDF_CrossRefAvail>(parser.get(), last_crossref_offset);
   EXPECT_EQ(CPDF_DataAvail::kDataAvailable, cross_ref_avail->CheckAvail());
diff --git a/core/fxcrt/xml/cfx_xmlinstruction_unittest.cpp b/core/fxcrt/xml/cfx_xmlinstruction_unittest.cpp
index 3b7f1d9..37f7405 100644
--- a/core/fxcrt/xml/cfx_xmlinstruction_unittest.cpp
+++ b/core/fxcrt/xml/cfx_xmlinstruction_unittest.cpp
@@ -87,8 +87,8 @@
       "<?acrobat http://www.xfa.org/schema/xfa-template/3.3/ Display:1 ?>\n"
       "<node></node>";
 
-  auto in_stream = pdfium::MakeRetain<CFX_ReadOnlySpanStream>(
-      pdfium::as_bytes(pdfium::make_span(input)));
+  auto in_stream =
+      pdfium::MakeRetain<CFX_ReadOnlySpanStream>(pdfium::as_byte_span(input));
 
   CFX_XMLParser parser(in_stream);
   std::unique_ptr<CFX_XMLDocument> doc = parser.Parse();
@@ -120,8 +120,8 @@
       "<?acrobat http://www.xfa.org/schema/xfa-template/3.3/ Display:1 ?>\n"
       "</node>";
 
-  auto in_stream = pdfium::MakeRetain<CFX_ReadOnlySpanStream>(
-      pdfium::as_bytes(pdfium::make_span(input)));
+  auto in_stream =
+      pdfium::MakeRetain<CFX_ReadOnlySpanStream>(pdfium::as_byte_span(input));
 
   CFX_XMLParser parser(in_stream);
   std::unique_ptr<CFX_XMLDocument> doc = parser.Parse();
diff --git a/third_party/base/containers/span.h b/third_party/base/containers/span.h
index d11a393..68be934 100644
--- a/third_party/base/containers/span.h
+++ b/third_party/base/containers/span.h
@@ -177,6 +177,9 @@
 // Differences from [span.elem]:
 // - no operator ()()
 // - using size_t instead of ptrdiff_t for indexing
+//
+// Additions beyond the C++ standard draft
+// - as_byte_span() function.
 
 // [span], class template span
 template <typename T>
@@ -353,6 +356,16 @@
   return span<T>(container);
 }
 
+// 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
+// or vector-like objects holding other scalar types, prior to passing them
+// into an API that requires byte spans.
+template <typename T>
+span<const uint8_t> as_byte_span(const T& arg) {
+  return as_bytes(make_span(arg));
+}
+
 }  // namespace pdfium
 
 #endif  // THIRD_PARTY_BASE_CONTAINERS_SPAN_H_
diff --git a/xfa/fxfa/parser/cxfa_document_builder_unittest.cpp b/xfa/fxfa/parser/cxfa_document_builder_unittest.cpp
index 0ebdb5b..5e75ac7 100644
--- a/xfa/fxfa/parser/cxfa_document_builder_unittest.cpp
+++ b/xfa/fxfa/parser/cxfa_document_builder_unittest.cpp
@@ -48,15 +48,15 @@
 
 TEST_F(CXFA_DocumentBuilderTest, EmptyInput) {
   static const char kInput[] = "";
-  auto stream = pdfium::MakeRetain<CFX_ReadOnlySpanStream>(
-      pdfium::as_bytes(pdfium::make_span(kInput)));
+  auto stream =
+      pdfium::MakeRetain<CFX_ReadOnlySpanStream>(pdfium::as_byte_span(kInput));
   EXPECT_FALSE(ParseAndBuild(stream));
 }
 
 TEST_F(CXFA_DocumentBuilderTest, BadInput) {
   static const char kInput[] = "<<<>bar?>>>>>>>";
-  auto stream = pdfium::MakeRetain<CFX_ReadOnlySpanStream>(
-      pdfium::as_bytes(pdfium::make_span(kInput)));
+  auto stream =
+      pdfium::MakeRetain<CFX_ReadOnlySpanStream>(pdfium::as_byte_span(kInput));
   EXPECT_FALSE(ParseAndBuild(stream));
 }
 
@@ -68,8 +68,8 @@
       "</config>";
   EXPECT_FALSE(GetDoc()->is_scripting());
 
-  auto stream = pdfium::MakeRetain<CFX_ReadOnlySpanStream>(
-      pdfium::as_bytes(pdfium::make_span(kInput)));
+  auto stream =
+      pdfium::MakeRetain<CFX_ReadOnlySpanStream>(pdfium::as_byte_span(kInput));
 
   CXFA_Node* root = ParseAndBuild(stream);
   ASSERT_TRUE(root);
@@ -85,8 +85,8 @@
 
   EXPECT_FALSE(GetDoc()->is_scripting());
 
-  auto stream = pdfium::MakeRetain<CFX_ReadOnlySpanStream>(
-      pdfium::as_bytes(pdfium::make_span(kInput)));
+  auto stream =
+      pdfium::MakeRetain<CFX_ReadOnlySpanStream>(pdfium::as_byte_span(kInput));
 
   CXFA_Node* root = ParseAndBuild(stream);
   ASSERT_TRUE(root);
@@ -101,8 +101,8 @@
 
   EXPECT_FALSE(GetDoc()->is_strict_scoping());
 
-  auto stream = pdfium::MakeRetain<CFX_ReadOnlySpanStream>(
-      pdfium::as_bytes(pdfium::make_span(kInput)));
+  auto stream =
+      pdfium::MakeRetain<CFX_ReadOnlySpanStream>(pdfium::as_byte_span(kInput));
 
   CXFA_Node* root = ParseAndBuild(stream);
   ASSERT_TRUE(root);
@@ -117,8 +117,8 @@
 
   EXPECT_FALSE(GetDoc()->is_strict_scoping());
 
-  auto stream = pdfium::MakeRetain<CFX_ReadOnlySpanStream>(
-      pdfium::as_bytes(pdfium::make_span(kInput)));
+  auto stream =
+      pdfium::MakeRetain<CFX_ReadOnlySpanStream>(pdfium::as_byte_span(kInput));
 
   CXFA_Node* root = ParseAndBuild(stream);
   ASSERT_TRUE(root);
@@ -136,8 +136,8 @@
   EXPECT_FALSE(GetDoc()->is_scripting());
   EXPECT_FALSE(GetDoc()->is_strict_scoping());
 
-  auto stream = pdfium::MakeRetain<CFX_ReadOnlySpanStream>(
-      pdfium::as_bytes(pdfium::make_span(kInput)));
+  auto stream =
+      pdfium::MakeRetain<CFX_ReadOnlySpanStream>(pdfium::as_byte_span(kInput));
 
   CXFA_Node* root = ParseAndBuild(stream);
   ASSERT_TRUE(root);