diff options
-rw-r--r-- | core/fxcrt/bytestring.h | 8 | ||||
-rw-r--r-- | core/fxcrt/bytestring_unittest.cpp | 4 | ||||
-rw-r--r-- | core/fxcrt/widestring.h | 8 | ||||
-rw-r--r-- | core/fxcrt/widestring_unittest.cpp | 4 |
4 files changed, 24 insertions, 0 deletions
diff --git a/core/fxcrt/bytestring.h b/core/fxcrt/bytestring.h index c68d2f6991..247238573d 100644 --- a/core/fxcrt/bytestring.h +++ b/core/fxcrt/bytestring.h @@ -84,6 +84,14 @@ class ByteString { return ByteStringView(raw_str(), GetLength()); } + // Explicit conversion to span. + // Note: Any subsequent modification of |this| will invalidate the result. + pdfium::span<const char> AsSpan() const { + return m_pData ? pdfium::span<const char>(m_pData->m_String, + m_pData->m_nDataLength) + : pdfium::span<const char>(); + } + // Note: Any subsequent modification of |this| will invalidate iterators. const_iterator begin() const { return m_pData ? m_pData->m_String : nullptr; } const_iterator end() const { diff --git a/core/fxcrt/bytestring_unittest.cpp b/core/fxcrt/bytestring_unittest.cpp index 8dab4f0cba..d030535edd 100644 --- a/core/fxcrt/bytestring_unittest.cpp +++ b/core/fxcrt/bytestring_unittest.cpp @@ -23,6 +23,10 @@ TEST(ByteString, ElementAccess) { EXPECT_DEATH({ abc[3]; }, ".*"); #endif + pdfium::span<const char> abc_span = abc.AsSpan(); + EXPECT_EQ(3u, abc_span.size()); + EXPECT_EQ(0, memcmp(abc_span.data(), "abc", 3)); + ByteString mutable_abc = abc; EXPECT_EQ(abc.c_str(), mutable_abc.c_str()); EXPECT_EQ('a', mutable_abc[0]); diff --git a/core/fxcrt/widestring.h b/core/fxcrt/widestring.h index f6c24375a0..b668b0292c 100644 --- a/core/fxcrt/widestring.h +++ b/core/fxcrt/widestring.h @@ -79,6 +79,14 @@ class WideString { return WideStringView(c_str(), GetLength()); } + // Explicit conversion to span. + // Note: Any subsequent modification of |this| will invalidate the result. + pdfium::span<const wchar_t> AsSpan() const { + return m_pData ? pdfium::span<const wchar_t>(m_pData->m_String, + m_pData->m_nDataLength) + : pdfium::span<const wchar_t>(); + } + // Note: Any subsequent modification of |this| will invalidate iterators. const_iterator begin() const { return m_pData ? m_pData->m_String : nullptr; } const_iterator end() const { diff --git a/core/fxcrt/widestring_unittest.cpp b/core/fxcrt/widestring_unittest.cpp index ad91249c8e..ec0a55fc1e 100644 --- a/core/fxcrt/widestring_unittest.cpp +++ b/core/fxcrt/widestring_unittest.cpp @@ -21,6 +21,10 @@ TEST(WideString, ElementAccess) { EXPECT_DEATH({ abc[4]; }, ".*"); #endif + pdfium::span<const wchar_t> abc_span = abc.AsSpan(); + EXPECT_EQ(3u, abc_span.size()); + EXPECT_EQ(0, wmemcmp(abc_span.data(), L"abc", 3)); + WideString mutable_abc = abc; EXPECT_EQ(abc.c_str(), mutable_abc.c_str()); EXPECT_EQ(L'a', mutable_abc[0]); |