From 7439517516a0bb3833ee95ddb9d71051f981347c Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Wed, 18 Apr 2018 19:04:20 +0000 Subject: Add AsRawSpan() to ByteString. Also tidy AsSpan() for Byte/Wide strings. Change-Id: I1853d31a59bc7f46de81295cde2e1062b91badec Reviewed-on: https://pdfium-review.googlesource.com/30911 Commit-Queue: Tom Sepez Reviewed-by: dsinclair --- core/fxcrt/bytestring.h | 8 +++++--- core/fxcrt/bytestring_unittest.cpp | 17 +++++++++++++++++ core/fxcrt/widestring.h | 5 ++--- core/fxcrt/widestring_unittest.cpp | 6 ++++++ 4 files changed, 30 insertions(+), 6 deletions(-) (limited to 'core') diff --git a/core/fxcrt/bytestring.h b/core/fxcrt/bytestring.h index 247238573d..99b87b1d4c 100644 --- a/core/fxcrt/bytestring.h +++ b/core/fxcrt/bytestring.h @@ -87,9 +87,11 @@ class ByteString { // Explicit conversion to span. // Note: Any subsequent modification of |this| will invalidate the result. pdfium::span AsSpan() const { - return m_pData ? pdfium::span(m_pData->m_String, - m_pData->m_nDataLength) - : pdfium::span(); + return pdfium::make_span(m_pData ? m_pData->m_String : nullptr, + GetLength()); + } + pdfium::span AsRawSpan() const { + return pdfium::make_span(raw_str(), GetLength()); } // Note: Any subsequent modification of |this| will invalidate iterators. diff --git a/core/fxcrt/bytestring_unittest.cpp b/core/fxcrt/bytestring_unittest.cpp index d030535edd..ed3f375434 100644 --- a/core/fxcrt/bytestring_unittest.cpp +++ b/core/fxcrt/bytestring_unittest.cpp @@ -27,6 +27,10 @@ TEST(ByteString, ElementAccess) { EXPECT_EQ(3u, abc_span.size()); EXPECT_EQ(0, memcmp(abc_span.data(), "abc", 3)); + pdfium::span abc_raw_span = abc.AsRawSpan(); + EXPECT_EQ(3u, abc_raw_span.size()); + EXPECT_EQ(0, memcmp(abc_raw_span.data(), "abc", 3)); + ByteString mutable_abc = abc; EXPECT_EQ(abc.c_str(), mutable_abc.c_str()); EXPECT_EQ('a', mutable_abc[0]); @@ -1527,8 +1531,21 @@ TEST(ByteString, Empty) { ByteString empty_str; EXPECT_TRUE(empty_str.IsEmpty()); EXPECT_EQ(0u, empty_str.GetLength()); + const char* cstr = empty_str.c_str(); + EXPECT_NE(nullptr, cstr); EXPECT_EQ(0u, strlen(cstr)); + + const uint8_t* rstr = empty_str.raw_str(); + EXPECT_EQ(nullptr, rstr); + + pdfium::span cspan = empty_str.AsSpan(); + EXPECT_TRUE(cspan.empty()); + EXPECT_EQ(nullptr, cspan.data()); + + pdfium::span rspan = empty_str.AsRawSpan(); + EXPECT_TRUE(rspan.empty()); + EXPECT_EQ(nullptr, rspan.data()); } TEST(ByteString, InitializerList) { diff --git a/core/fxcrt/widestring.h b/core/fxcrt/widestring.h index b668b0292c..4b097c4430 100644 --- a/core/fxcrt/widestring.h +++ b/core/fxcrt/widestring.h @@ -82,9 +82,8 @@ class WideString { // Explicit conversion to span. // Note: Any subsequent modification of |this| will invalidate the result. pdfium::span AsSpan() const { - return m_pData ? pdfium::span(m_pData->m_String, - m_pData->m_nDataLength) - : pdfium::span(); + return pdfium::make_span(m_pData ? m_pData->m_String : nullptr, + GetLength()); } // Note: Any subsequent modification of |this| will invalidate iterators. diff --git a/core/fxcrt/widestring_unittest.cpp b/core/fxcrt/widestring_unittest.cpp index ec0a55fc1e..4572305530 100644 --- a/core/fxcrt/widestring_unittest.cpp +++ b/core/fxcrt/widestring_unittest.cpp @@ -1353,8 +1353,14 @@ TEST(WideString, Empty) { WideString empty_str; EXPECT_TRUE(empty_str.IsEmpty()); EXPECT_EQ(0u, empty_str.GetLength()); + const wchar_t* cstr = empty_str.c_str(); + EXPECT_NE(nullptr, cstr); EXPECT_EQ(0u, wcslen(cstr)); + + pdfium::span cspan = empty_str.AsSpan(); + EXPECT_TRUE(cspan.empty()); + EXPECT_EQ(nullptr, cspan.data()); } TEST(CFX_WidString, InitializerList) { -- cgit v1.2.3