diff options
author | Tom Sepez <tsepez@chromium.org> | 2018-07-18 19:28:59 +0000 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2018-07-18 19:28:59 +0000 |
commit | 835d49d1534405075d75068635894ead17af56b8 (patch) | |
tree | 5e07765a1bf9e9136a1248b283a3af43c66b175a | |
parent | f22b4e2f6682fe26113c591a01139a8b5fa4e3bf (diff) | |
download | pdfium-835d49d1534405075d75068635894ead17af56b8.tar.xz |
Add pdfium::span::as_bytes() and as_writable_bytes().
Picks up some enhancements from base/span.h. In turn, also adds
the size_bytes() helper. Differs from base version in that it
works around C++14 enable_if_t<>, and avoids the dynamic_extent
template specialization tricks.
Use it in a few places where appropriate.
Change-Id: I86f72cf0023f2d4317a7afa351fddee601c8f86c
Reviewed-on: https://pdfium-review.googlesource.com/38251
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
-rw-r--r-- | core/fpdfapi/parser/fpdf_parser_decode.cpp | 5 | ||||
-rw-r--r-- | core/fxcrt/bytestring_unittest.cpp | 7 | ||||
-rw-r--r-- | third_party/base/span.h | 18 |
3 files changed, 18 insertions, 12 deletions
diff --git a/core/fpdfapi/parser/fpdf_parser_decode.cpp b/core/fpdfapi/parser/fpdf_parser_decode.cpp index 82d84ab705..9ccca121ad 100644 --- a/core/fpdfapi/parser/fpdf_parser_decode.cpp +++ b/core/fpdfapi/parser/fpdf_parser_decode.cpp @@ -500,9 +500,8 @@ ByteString PDF_EncodeText(const wchar_t* pString, int len) { size_t dest_index = 0; size_t encLen = len * 2 + 2; { - pdfium::span<char> cspan = result.GetBuffer(encLen); - auto dest_buf = pdfium::make_span(reinterpret_cast<uint8_t*>(cspan.data()), - cspan.size()); + pdfium::span<uint8_t> dest_buf = + pdfium::as_writable_bytes(result.GetBuffer(encLen)); dest_buf[dest_index++] = 0xfe; dest_buf[dest_index++] = 0xff; for (int j = 0; j < len; ++j) { diff --git a/core/fxcrt/bytestring_unittest.cpp b/core/fxcrt/bytestring_unittest.cpp index fb5fc0d996..7095a1b799 100644 --- a/core/fxcrt/bytestring_unittest.cpp +++ b/core/fxcrt/bytestring_unittest.cpp @@ -1060,8 +1060,7 @@ TEST(ByteStringView, NotNull) { ByteStringView string3("abc"); ByteStringView string6("abcdef"); ByteStringView alternate_string3("abcdef", 3); - ByteStringView span_string4( - pdfium::span<const uint8_t>(reinterpret_cast<const uint8_t*>("abcd"), 4)); + ByteStringView span_string4(pdfium::as_bytes(pdfium::make_span("abcd", 4))); ByteStringView embedded_nul_string7("abc\0def", 7); ByteStringView illegal_string7("abcdef", 7); @@ -1355,8 +1354,8 @@ TEST(ByteStringView, OperatorEQ) { EXPECT_FALSE(c_string2 == byte_string_c); EXPECT_FALSE(c_string3 == byte_string_c); - pdfium::span<const uint8_t> span5(reinterpret_cast<const uint8_t*>("hello"), - 5); + pdfium::span<const uint8_t> span5( + pdfium::as_bytes(pdfium::make_span("hello", 5))); EXPECT_EQ(byte_string_c.span(), span5); } diff --git a/third_party/base/span.h b/third_party/base/span.h index 0fb627ba8c..ccffbc1e78 100644 --- a/third_party/base/span.h +++ b/third_party/base/span.h @@ -152,10 +152,6 @@ using EnableIfConstSpanCompatibleContainer = // Differences from [views.constants]: // - no dynamic_extent constant // -// Differences from [span.objectrep]: -// - no as_bytes() -// - no as_writeable_bytes() -// // Differences in constants and types: // - no element_type type alias // - no index_type type alias @@ -173,7 +169,6 @@ using EnableIfConstSpanCompatibleContainer = // - using size_t instead of ptrdiff_t for indexing // // Differences from [span.obs]: -// - no size_bytes() // - using size_t instead of ptrdiff_t to represent size() // // Differences from [span.elem]: @@ -244,6 +239,7 @@ class span { // [span.obs], span observers constexpr size_t size() const noexcept { return size_; } + constexpr size_t size_bytes() const noexcept { return size() * sizeof(T); } constexpr bool empty() const noexcept { return size_ == 0; } // [span.elem], span element access @@ -313,6 +309,18 @@ constexpr bool operator>=(span<T> lhs, span<T> rhs) noexcept { return !(lhs < rhs); } +// [span.objectrep], views of object representation +template <typename T> +span<const uint8_t> as_bytes(span<T> s) noexcept { + return {reinterpret_cast<const uint8_t*>(s.data()), s.size_bytes()}; +} + +template <typename T, + typename U = typename std::enable_if<!std::is_const<T>::value>::type> +span<uint8_t> as_writable_bytes(span<T> s) noexcept { + return {reinterpret_cast<uint8_t*>(s.data()), s.size_bytes()}; +} + // Type-deducing helpers for constructing a span. template <typename T> constexpr span<T> make_span(T* data, size_t size) noexcept { |