Use a trivial local implementation of span<T> if the platform doesn't am: 21b0e921c3

Original change: https://android-review.googlesource.com/c/platform/system/libcppbor/+/3388499

Change-Id: I2963378ee89ca22d9259b75f5a73ec8d73f3bafd
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/include/cppbor/cppbor.h b/include/cppbor/cppbor.h
index 271e0e6..d57b837 100644
--- a/include/cppbor/cppbor.h
+++ b/include/cppbor/cppbor.h
@@ -25,11 +25,16 @@
 #include <iterator>
 #include <memory>
 #include <numeric>
-#include <span>
 #include <string>
 #include <string_view>
 #include <vector>
 
+#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L || __cplusplus >= 202002L
+#include <span>
+#else  // not ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L || __cplusplus >= 202002L
+#include "span.h"
+#endif  // not ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L || __cplusplus >= 202002L
+
 #ifdef OS_WINDOWS
 #include <basetsd.h>
 
@@ -45,6 +50,10 @@
 
 namespace cppbor {
 
+#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L || __cplusplus >= 202002L
+using std::span;
+#endif  // ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L || __cplusplus >= 202002L
+
 enum MajorType : uint8_t {
     UINT = 0 << 5,
     NINT = 1 << 5,
@@ -455,7 +464,7 @@
 };
 
 /**
- * ViewBstr is a read-only version of Bstr backed by std::span
+ * ViewBstr is a read-only version of Bstr backed by span
  */
 class ViewBstr : public Item {
   public:
@@ -465,7 +474,7 @@
     explicit ViewBstr() {}
 
     // Construct from a span of uint8_t values
-    explicit ViewBstr(std::span<const uint8_t> v) : mView(std::move(v)) {}
+    explicit ViewBstr(span<const uint8_t> v) : mView(std::move(v)) {}
 
     // Construct from a string_view
     explicit ViewBstr(std::string_view v)
@@ -495,14 +504,14 @@
         encodeValue(encodeCallback);
     }
 
-    const std::span<const uint8_t>& view() const { return mView; }
+    const 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::span<const uint8_t> mView;
+    span<const uint8_t> mView;
 };
 
 /**
diff --git a/include/cppbor/span.h b/include/cppbor/span.h
new file mode 100644
index 0000000..ed1ed15
--- /dev/null
+++ b/include/cppbor/span.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2024 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <cstddef>
+
+#if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L || __cplusplus >= 202002L
+#error This trivial span.h should not be used if the platform supports std::span
+#endif  // ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L || __cplusplus >= 202002L
+
+namespace cppbor {
+
+template <class T>
+class span {
+  public:
+    constexpr span() : mBegin(nullptr), mLen(0) {}
+    explicit constexpr span(T* begin, size_t len) : mBegin(begin), mLen(len) {}
+
+    constexpr T* begin() const noexcept { return mBegin; }
+    constexpr T* end() const noexcept { return mBegin + mLen; }
+    constexpr T* data() const noexcept { return mBegin; }
+
+    constexpr size_t size() const noexcept { return mLen; }
+
+  private:
+    T* mBegin;
+    size_t mLen;
+};
+
+}  // namespace cppbor