util: backport C++20 std::span and use it in various APIs
Implementation taken from span-lite by Martin Moene,
commit 337af6e23f6d3264136c16565546244da23159ba
Change-Id: Icfd0ba6841cbf6ef7870c31c881df940da9faf7e
diff --git a/ndn-cxx/security/transform/buffer-source.cpp b/ndn-cxx/security/transform/buffer-source.cpp
index 36f5d2d..515964f 100644
--- a/ndn-cxx/security/transform/buffer-source.cpp
+++ b/ndn-cxx/security/transform/buffer-source.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2020 Regents of the University of California.
+ * Copyright (c) 2013-2021 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -25,8 +25,13 @@
namespace security {
namespace transform {
+BufferSource::BufferSource(span<const uint8_t> buffer)
+ : m_bufs({buffer})
+{
+}
+
BufferSource::BufferSource(const uint8_t* buf, size_t size)
- : m_bufs({{buf, size}})
+ : BufferSource(make_span(buf, size))
{
}
@@ -35,11 +40,6 @@
{
}
-BufferSource::BufferSource(const Buffer& buffer)
- : m_bufs({{buffer.data(), buffer.size()}})
-{
-}
-
BufferSource::BufferSource(InputBuffers buffers)
: m_bufs(std::move(buffers))
{
@@ -50,14 +50,10 @@
{
BOOST_ASSERT(m_next != nullptr);
- for (const auto& buffer : m_bufs) {
- const uint8_t* buf = buffer.first;
- size_t size = buffer.second;
-
- while (size > 0) {
- size_t nBytesWritten = m_next->write(buf, size);
- buf += nBytesWritten;
- size -= nBytesWritten;
+ for (auto buffer : m_bufs) {
+ while (!buffer.empty()) {
+ size_t nBytesWritten = m_next->write(buffer);
+ buffer = buffer.last(buffer.size() - nBytesWritten);
}
}