summaryrefslogtreecommitdiff
path: root/core/fxcrt
diff options
context:
space:
mode:
authorRyan Harrison <rharrison@chromium.org>2017-08-23 10:39:35 -0400
committerChromium commit bot <commit-bot@chromium.org>2017-08-23 15:11:19 +0000
commit12db7515f17228798d1aa38fce0fee3e7d2d36b6 (patch)
treee291daf9e6a88ba0248670b9f1ba3a555f052538 /core/fxcrt
parent3bb0a34cc75abe49a59c6390353957bbb5c5ab38 (diff)
downloadpdfium-12db7515f17228798d1aa38fce0fee3e7d2d36b6.tar.xz
Convert string Find methods to return an Optional
The Find and ReverseFind methods for WideString, WideStringC, ByteString, and ByteStringC have been converted from returning a raw FX_STRSIZE, to returning Optional<FX_STRSIZE>, so that success/failure can be indicated without using FX_STRNPOS. This allows for removing FX_STRNPOS and by association makes the conversion of FX_STRSIZE to size_t easier, since it forces checking the return value of Find to be explictly done as well as taking the error value out of the range of FX_STRSIZE. New Contains methods have been added for cases where the success or failure is all the call site to Find cared about, and the actual position was ignored. BUG=pdfium:828 Change-Id: Id827e508c8660affa68cc08a13d96121369364b7 Reviewed-on: https://pdfium-review.googlesource.com/11350 Commit-Queue: Ryan Harrison <rharrison@chromium.org> Reviewed-by: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'core/fxcrt')
-rw-r--r--core/fxcrt/cfx_bytestring.cpp47
-rw-r--r--core/fxcrt/cfx_bytestring.h16
-rw-r--r--core/fxcrt/cfx_bytestring_unittest.cpp117
-rw-r--r--core/fxcrt/cfx_string_c_template.h10
-rw-r--r--core/fxcrt/cfx_widestring.cpp39
-rw-r--r--core/fxcrt/cfx_widestring.h15
-rw-r--r--core/fxcrt/cfx_widestring_unittest.cpp82
-rw-r--r--core/fxcrt/fx_basic_util.cpp2
-rw-r--r--core/fxcrt/fx_system.h4
-rw-r--r--core/fxcrt/xml/cfx_xmlelement.cpp14
-rw-r--r--core/fxcrt/xml/cxml_parser.cpp8
11 files changed, 269 insertions, 85 deletions
diff --git a/core/fxcrt/cfx_bytestring.cpp b/core/fxcrt/cfx_bytestring.cpp
index cd049dcc5d..f6074e961b 100644
--- a/core/fxcrt/cfx_bytestring.cpp
+++ b/core/fxcrt/cfx_bytestring.cpp
@@ -564,43 +564,48 @@ CFX_ByteString CFX_ByteString::Left(FX_STRSIZE nCount) const {
return dest;
}
-FX_STRSIZE CFX_ByteString::Find(char ch, FX_STRSIZE nStart) const {
+pdfium::Optional<FX_STRSIZE> CFX_ByteString::Find(char ch,
+ FX_STRSIZE nStart) const {
if (!m_pData)
- return FX_STRNPOS;
+ return pdfium::Optional<FX_STRSIZE>();
if (nStart < 0 || nStart >= m_pData->m_nDataLength)
- return FX_STRNPOS;
+ return pdfium::Optional<FX_STRSIZE>();
const char* pStr = static_cast<const char*>(
memchr(m_pData->m_String + nStart, ch, m_pData->m_nDataLength - nStart));
- return pStr ? pStr - m_pData->m_String : FX_STRNPOS;
+ return pStr ? pdfium::Optional<FX_STRSIZE>(
+ static_cast<FX_STRSIZE>(pStr - m_pData->m_String))
+ : pdfium::Optional<FX_STRSIZE>();
}
-FX_STRSIZE CFX_ByteString::ReverseFind(char ch) const {
+pdfium::Optional<FX_STRSIZE> CFX_ByteString::Find(const CFX_ByteStringC& pSub,
+ FX_STRSIZE nStart) const {
if (!m_pData)
- return FX_STRNPOS;
-
- FX_STRSIZE nLength = m_pData->m_nDataLength;
- while (nLength--) {
- if (m_pData->m_String[nLength] == ch)
- return nLength;
- }
- return FX_STRNPOS;
-}
-
-FX_STRSIZE CFX_ByteString::Find(const CFX_ByteStringC& pSub,
- FX_STRSIZE nStart) const {
- if (!m_pData)
- return FX_STRNPOS;
+ return pdfium::Optional<FX_STRSIZE>();
FX_STRSIZE nLength = m_pData->m_nDataLength;
if (nStart > nLength)
- return FX_STRNPOS;
+ return pdfium::Optional<FX_STRSIZE>();
const char* pStr =
FX_strstr(m_pData->m_String + nStart, m_pData->m_nDataLength - nStart,
pSub.unterminated_c_str(), pSub.GetLength());
- return pStr ? (int)(pStr - m_pData->m_String) : FX_STRNPOS;
+ return pStr ? pdfium::Optional<FX_STRSIZE>(
+ static_cast<FX_STRSIZE>(pStr - m_pData->m_String))
+ : pdfium::Optional<FX_STRSIZE>();
+}
+
+pdfium::Optional<FX_STRSIZE> CFX_ByteString::ReverseFind(char ch) const {
+ if (!m_pData)
+ return pdfium::Optional<FX_STRSIZE>();
+
+ FX_STRSIZE nLength = m_pData->m_nDataLength;
+ while (nLength--) {
+ if (m_pData->m_String[nLength] == ch)
+ return pdfium::Optional<FX_STRSIZE>(nLength);
+ }
+ return pdfium::Optional<FX_STRSIZE>();
}
void CFX_ByteString::MakeLower() {
diff --git a/core/fxcrt/cfx_bytestring.h b/core/fxcrt/cfx_bytestring.h
index 0e8e006a3c..10675be7a5 100644
--- a/core/fxcrt/cfx_bytestring.h
+++ b/core/fxcrt/cfx_bytestring.h
@@ -16,6 +16,7 @@
#include "core/fxcrt/cfx_string_data_template.h"
#include "core/fxcrt/fx_memory.h"
#include "core/fxcrt/fx_system.h"
+#include "third_party/base/optional.h"
class CFX_WideString;
@@ -130,9 +131,18 @@ class CFX_ByteString {
CFX_ByteString Left(FX_STRSIZE count) const;
CFX_ByteString Right(FX_STRSIZE count) const;
- FX_STRSIZE Find(const CFX_ByteStringC& lpszSub, FX_STRSIZE start = 0) const;
- FX_STRSIZE Find(char ch, FX_STRSIZE start = 0) const;
- FX_STRSIZE ReverseFind(char ch) const;
+ pdfium::Optional<FX_STRSIZE> Find(const CFX_ByteStringC& lpszSub,
+ FX_STRSIZE start = 0) const;
+ pdfium::Optional<FX_STRSIZE> Find(char ch, FX_STRSIZE start = 0) const;
+ pdfium::Optional<FX_STRSIZE> ReverseFind(char ch) const;
+
+ bool Contains(const CFX_ByteStringC& lpszSub, FX_STRSIZE start = 0) const {
+ return Find(lpszSub, start).has_value();
+ }
+
+ bool Contains(char ch, FX_STRSIZE start = 0) const {
+ return Find(ch, start).has_value();
+ }
void MakeLower();
void MakeUpper();
diff --git a/core/fxcrt/cfx_bytestring_unittest.cpp b/core/fxcrt/cfx_bytestring_unittest.cpp
index 35f407f6ec..e7281546fb 100644
--- a/core/fxcrt/cfx_bytestring_unittest.cpp
+++ b/core/fxcrt/cfx_bytestring_unittest.cpp
@@ -317,7 +317,7 @@ TEST(fxcrt, ByteStringCNull) {
EXPECT_EQ(null_string, assigned_null_string);
CFX_ByteStringC assigned_nullptr_string("initially not nullptr");
- assigned_nullptr_string = (const char*)nullptr;
+ assigned_nullptr_string = nullptr;
EXPECT_FALSE(assigned_nullptr_string.raw_str());
EXPECT_EQ(assigned_nullptr_string.GetLength(), 0);
EXPECT_TRUE(assigned_nullptr_string.IsEmpty());
@@ -568,6 +568,89 @@ TEST(fxcrt, ByteStringRight) {
EXPECT_EQ("", empty.Right(-1));
}
+TEST(fxcrt, ByteStringFind) {
+ CFX_ByteString null_string;
+ EXPECT_FALSE(null_string.Find('a').has_value());
+ EXPECT_FALSE(null_string.Find('\0').has_value());
+
+ CFX_ByteString empty_string("");
+ EXPECT_FALSE(empty_string.Find('a').has_value());
+ EXPECT_FALSE(empty_string.Find('\0').has_value());
+
+ pdfium::Optional<FX_STRSIZE> result;
+ CFX_ByteString single_string("a");
+ result = single_string.Find('a');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(0, result.value());
+ EXPECT_FALSE(single_string.Find('b').has_value());
+ EXPECT_FALSE(single_string.Find('\0').has_value());
+
+ CFX_ByteString longer_string("abccc");
+ result = longer_string.Find('a');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(0, result.value());
+ result = longer_string.Find('c');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(2, result.value());
+ result = longer_string.Find('c', 3);
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(3, result.value());
+ EXPECT_FALSE(longer_string.Find('d').has_value());
+ EXPECT_FALSE(longer_string.Find('\0').has_value());
+
+ result = longer_string.Find("ab");
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(0, result.value());
+ result = longer_string.Find("ccc");
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(2, result.value());
+ result = longer_string.Find("cc", 3);
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(3, result.value());
+ EXPECT_FALSE(longer_string.Find("d").has_value());
+
+ CFX_ByteString hibyte_string(
+ "ab\x8c"
+ "def");
+ result = hibyte_string.Find('\x8c');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(2, result.value());
+}
+
+TEST(fxcrt, ByteStringReverseFind) {
+ CFX_ByteString null_string;
+ EXPECT_FALSE(null_string.ReverseFind('a').has_value());
+ EXPECT_FALSE(null_string.ReverseFind('\0').has_value());
+
+ CFX_ByteString empty_string("");
+ EXPECT_FALSE(empty_string.ReverseFind('a').has_value());
+ EXPECT_FALSE(empty_string.ReverseFind('\0').has_value());
+
+ pdfium::Optional<FX_STRSIZE> result;
+ CFX_ByteString single_string("a");
+ result = single_string.ReverseFind('a');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(0, result.value());
+ EXPECT_FALSE(single_string.ReverseFind('b').has_value());
+ EXPECT_FALSE(single_string.ReverseFind('\0').has_value());
+
+ CFX_ByteString longer_string("abccc");
+ result = longer_string.ReverseFind('a');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(0, result.value());
+ result = longer_string.ReverseFind('c');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(4, result.value());
+ EXPECT_FALSE(longer_string.ReverseFind('\0').has_value());
+
+ CFX_ByteString hibyte_string(
+ "ab\x8c"
+ "def");
+ result = hibyte_string.ReverseFind('\x8c');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(2, result.value());
+}
+
TEST(fxcrt, ByteStringUpperLower) {
CFX_ByteString fred("F-Re.42D");
fred.MakeLower();
@@ -882,27 +965,37 @@ TEST(fxcrt, ByteStringCGetID) {
TEST(fxcrt, ByteStringCFind) {
CFX_ByteStringC null_string;
- EXPECT_EQ(FX_STRNPOS, null_string.Find('a'));
- EXPECT_EQ(FX_STRNPOS, null_string.Find(0));
+ EXPECT_FALSE(null_string.Find('a').has_value());
+ EXPECT_FALSE(null_string.Find('\0').has_value());
CFX_ByteStringC empty_string("");
- EXPECT_EQ(FX_STRNPOS, empty_string.Find('a'));
- EXPECT_EQ(FX_STRNPOS, empty_string.Find(0));
+ EXPECT_FALSE(empty_string.Find('a').has_value());
+ EXPECT_FALSE(empty_string.Find('\0').has_value());
+ pdfium::Optional<FX_STRSIZE> result;
CFX_ByteStringC single_string("a");
- EXPECT_EQ(0, single_string.Find('a'));
- EXPECT_EQ(FX_STRNPOS, single_string.Find('b'));
- EXPECT_EQ(FX_STRNPOS, single_string.Find(0));
+ result = single_string.Find('a');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(0, result.value());
+ EXPECT_FALSE(single_string.Find('b').has_value());
+ EXPECT_FALSE(single_string.Find('\0').has_value());
CFX_ByteStringC longer_string("abccc");
- EXPECT_EQ(0, longer_string.Find('a'));
- EXPECT_EQ(2, longer_string.Find('c'));
- EXPECT_EQ(FX_STRNPOS, longer_string.Find(0));
+ result = longer_string.Find('a');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(0, result.value());
+ result = longer_string.Find('c');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(2, result.value());
+ EXPECT_FALSE(longer_string.Find('d').has_value());
+ EXPECT_FALSE(longer_string.Find('\0').has_value());
CFX_ByteStringC hibyte_string(
"ab\x8c"
"def");
- EXPECT_EQ(2, hibyte_string.Find('\x8c'));
+ result = hibyte_string.Find('\x8c');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(2, result.value());
}
TEST(fxcrt, ByteStringCMid) {
diff --git a/core/fxcrt/cfx_string_c_template.h b/core/fxcrt/cfx_string_c_template.h
index 99948e90a0..db8b274410 100644
--- a/core/fxcrt/cfx_string_c_template.h
+++ b/core/fxcrt/cfx_string_c_template.h
@@ -9,10 +9,12 @@
#include <algorithm>
#include <type_traits>
+#include <utility>
#include <vector>
#include "core/fxcrt/cfx_unowned_ptr.h"
#include "core/fxcrt/fx_system.h"
+#include "third_party/base/optional.h"
#include "third_party/base/stl_util.h"
// An immutable string with caller-provided storage which must outlive the
@@ -129,12 +131,16 @@ class CFX_StringCTemplate {
return static_cast<CharType>(m_Ptr.Get()[index]);
}
- FX_STRSIZE Find(CharType ch) const {
+ pdfium::Optional<FX_STRSIZE> Find(CharType ch) const {
const UnsignedType* found = reinterpret_cast<const UnsignedType*>(FXSYS_chr(
reinterpret_cast<const CharType*>(m_Ptr.Get()), ch, m_Length));
- return found ? found - m_Ptr.Get() : FX_STRNPOS;
+
+ return found ? pdfium::Optional<FX_STRSIZE>(found - m_Ptr.Get())
+ : pdfium::Optional<FX_STRSIZE>();
}
+ bool Contains(CharType ch) const { return Find(ch).has_value(); }
+
CFX_StringCTemplate Mid(FX_STRSIZE index, FX_STRSIZE count) const {
ASSERT(count >= 0);
if (index > m_Length)
diff --git a/core/fxcrt/cfx_widestring.cpp b/core/fxcrt/cfx_widestring.cpp
index 01fc8c764e..76fdf24b2c 100644
--- a/core/fxcrt/cfx_widestring.cpp
+++ b/core/fxcrt/cfx_widestring.cpp
@@ -54,7 +54,8 @@ const wchar_t* FX_wcsstr(const wchar_t* haystack,
return nullptr;
}
-FX_STRSIZE GuessSizeForVSWPrintf(const wchar_t* pFormat, va_list argList) {
+pdfium::Optional<FX_STRSIZE> GuessSizeForVSWPrintf(const wchar_t* pFormat,
+ va_list argList) {
FX_STRSIZE nMaxLen = 0;
for (const wchar_t* pStr = pFormat; *pStr != 0; pStr++) {
if (*pStr != '%' || *(pStr = pStr + 1) == '%') {
@@ -78,7 +79,7 @@ FX_STRSIZE GuessSizeForVSWPrintf(const wchar_t* pFormat, va_list argList) {
++pStr;
}
if (nWidth < 0 || nWidth > 128 * 1024)
- return FX_STRNPOS;
+ return pdfium::Optional<FX_STRSIZE>();
int nPrecision = 0;
if (*pStr == '.') {
pStr++;
@@ -92,7 +93,7 @@ FX_STRSIZE GuessSizeForVSWPrintf(const wchar_t* pFormat, va_list argList) {
}
}
if (nPrecision < 0 || nPrecision > 128 * 1024)
- return FX_STRNPOS;
+ return pdfium::Optional<FX_STRSIZE>();
int nModifier = 0;
if (*pStr == L'I' && *(pStr + 1) == L'6' && *(pStr + 2) == L'4') {
pStr += 3;
@@ -241,7 +242,7 @@ FX_STRSIZE GuessSizeForVSWPrintf(const wchar_t* pFormat, va_list argList) {
nMaxLen += nItemLen;
}
nMaxLen += 32; // Fudge factor.
- return nMaxLen;
+ return pdfium::Optional<FX_STRSIZE>(nMaxLen);
}
#ifndef NDEBUG
@@ -656,9 +657,10 @@ void CFX_WideString::FormatV(const wchar_t* pFormat, va_list argList) {
FX_STRSIZE nMaxLen = vswprintf(nullptr, 0, pFormat, argListCopy);
va_end(argListCopy);
if (nMaxLen <= 0) {
- nMaxLen = GuessSizeForVSWPrintf(pFormat, argListCopy);
- if (nMaxLen == FX_STRNPOS)
+ auto guess = GuessSizeForVSWPrintf(pFormat, argListCopy);
+ if (!guess.has_value())
return;
+ nMaxLen = guess.value();
}
while (nMaxLen < 32 * 1024) {
FX_VA_COPY(argListCopy, argList);
@@ -717,31 +719,36 @@ CFX_WideString CFX_WideString::Left(FX_STRSIZE nCount) const {
return dest;
}
-FX_STRSIZE CFX_WideString::Find(wchar_t ch, FX_STRSIZE nStart) const {
+pdfium::Optional<FX_STRSIZE> CFX_WideString::Find(wchar_t ch,
+ FX_STRSIZE nStart) const {
if (!m_pData)
- return FX_STRNPOS;
+ return pdfium::Optional<FX_STRSIZE>();
if (nStart < 0 || nStart >= m_pData->m_nDataLength)
- return FX_STRNPOS;
+ return pdfium::Optional<FX_STRSIZE>();
const wchar_t* pStr =
wmemchr(m_pData->m_String + nStart, ch, m_pData->m_nDataLength - nStart);
- return pStr ? pStr - m_pData->m_String : FX_STRNPOS;
+ return pStr ? pdfium::Optional<FX_STRSIZE>(
+ static_cast<FX_STRSIZE>(pStr - m_pData->m_String))
+ : pdfium::Optional<FX_STRSIZE>();
}
-FX_STRSIZE CFX_WideString::Find(const CFX_WideStringC& pSub,
- FX_STRSIZE nStart) const {
+pdfium::Optional<FX_STRSIZE> CFX_WideString::Find(const CFX_WideStringC& pSub,
+ FX_STRSIZE nStart) const {
if (!m_pData)
- return FX_STRNPOS;
+ return pdfium::Optional<FX_STRSIZE>();
FX_STRSIZE nLength = m_pData->m_nDataLength;
if (nStart > nLength)
- return FX_STRNPOS;
+ return pdfium::Optional<FX_STRSIZE>();
const wchar_t* pStr =
FX_wcsstr(m_pData->m_String + nStart, m_pData->m_nDataLength - nStart,
pSub.unterminated_c_str(), pSub.GetLength());
- return pStr ? (int)(pStr - m_pData->m_String) : FX_STRNPOS;
+ return pStr ? pdfium::Optional<FX_STRSIZE>(
+ static_cast<FX_STRSIZE>(pStr - m_pData->m_String))
+ : pdfium::Optional<FX_STRSIZE>();
}
void CFX_WideString::MakeLower() {
@@ -942,7 +949,7 @@ void CFX_WideString::TrimRight(const CFX_WideStringC& pTargets) {
return;
FX_STRSIZE pos = GetLength();
- while (pos && pTargets.Find(m_pData->m_String[pos - 1]) != -1)
+ while (pos && pTargets.Contains(m_pData->m_String[pos - 1]))
pos--;
if (pos < m_pData->m_nDataLength) {
diff --git a/core/fxcrt/cfx_widestring.h b/core/fxcrt/cfx_widestring.h
index f9e800d0a2..a6d0eca044 100644
--- a/core/fxcrt/cfx_widestring.h
+++ b/core/fxcrt/cfx_widestring.h
@@ -15,6 +15,7 @@
#include "core/fxcrt/cfx_string_data_template.h"
#include "core/fxcrt/fx_memory.h"
#include "core/fxcrt/fx_system.h"
+#include "third_party/base/optional.h"
class CFX_ByteString;
@@ -142,8 +143,18 @@ class CFX_WideString {
int GetInteger() const;
float GetFloat() const;
- FX_STRSIZE Find(const CFX_WideStringC& pSub, FX_STRSIZE start = 0) const;
- FX_STRSIZE Find(wchar_t ch, FX_STRSIZE start = 0) const;
+ pdfium::Optional<FX_STRSIZE> Find(const CFX_WideStringC& pSub,
+ FX_STRSIZE start = 0) const;
+ pdfium::Optional<FX_STRSIZE> Find(wchar_t ch, FX_STRSIZE start = 0) const;
+
+ bool Contains(const CFX_WideStringC& lpszSub, FX_STRSIZE start = 0) const {
+ return Find(lpszSub, start).has_value();
+ }
+
+ bool Contains(char ch, FX_STRSIZE start = 0) const {
+ return Find(ch, start).has_value();
+ }
+
FX_STRSIZE Replace(const CFX_WideStringC& pOld, const CFX_WideStringC& pNew);
FX_STRSIZE Remove(wchar_t ch);
diff --git a/core/fxcrt/cfx_widestring_unittest.cpp b/core/fxcrt/cfx_widestring_unittest.cpp
index e7206dde06..e688a5334e 100644
--- a/core/fxcrt/cfx_widestring_unittest.cpp
+++ b/core/fxcrt/cfx_widestring_unittest.cpp
@@ -527,6 +527,54 @@ TEST(fxcrt, WideStringRight) {
EXPECT_EQ(L"", empty.Right(-1));
}
+TEST(fxcrt, WideStringFind) {
+ CFX_WideString null_string;
+ EXPECT_FALSE(null_string.Find(L'a').has_value());
+ EXPECT_FALSE(null_string.Find(L'\0').has_value());
+
+ CFX_WideString empty_string(L"");
+ EXPECT_FALSE(empty_string.Find(L'a').has_value());
+ EXPECT_FALSE(empty_string.Find(L'\0').has_value());
+
+ pdfium::Optional<FX_STRSIZE> result;
+ CFX_WideString single_string(L"a");
+ result = single_string.Find(L'a');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(0, result.value());
+ EXPECT_FALSE(single_string.Find(L'b').has_value());
+ EXPECT_FALSE(single_string.Find(L'\0').has_value());
+
+ CFX_WideString longer_string(L"abccc");
+ result = longer_string.Find(L'a');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(0, result.value());
+ result = longer_string.Find(L'c');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(2, result.value());
+ result = longer_string.Find(L'c', 3);
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(3, result.value());
+ EXPECT_FALSE(longer_string.Find(L'\0').has_value());
+
+ result = longer_string.Find(L"ab");
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(0, result.value());
+ result = longer_string.Find(L"ccc");
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(2, result.value());
+ result = longer_string.Find(L"cc", 3);
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(3, result.value());
+ EXPECT_FALSE(longer_string.Find(L"d").has_value());
+
+ CFX_WideString hibyte_string(
+ L"ab\xff8c"
+ L"def");
+ result = hibyte_string.Find(L'\xff8c');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(2, result.value());
+}
+
TEST(fxcrt, WideStringUpperLower) {
CFX_WideString fred(L"F-Re.42D");
fred.MakeLower();
@@ -924,27 +972,37 @@ TEST(fxcrt, WideStringCOperatorNE) {
TEST(fxcrt, WideStringCFind) {
CFX_WideStringC null_string;
- EXPECT_EQ(FX_STRNPOS, null_string.Find(L'a'));
- EXPECT_EQ(FX_STRNPOS, null_string.Find(0));
+ EXPECT_FALSE(null_string.Find(L'a').has_value());
+ EXPECT_FALSE(null_string.Find(L'\0').has_value());
CFX_WideStringC empty_string(L"");
- EXPECT_EQ(FX_STRNPOS, empty_string.Find(L'a'));
- EXPECT_EQ(FX_STRNPOS, empty_string.Find(0));
+ EXPECT_FALSE(empty_string.Find(L'a').has_value());
+ EXPECT_FALSE(empty_string.Find(L'\0').has_value());
+ pdfium::Optional<FX_STRSIZE> result;
CFX_WideStringC single_string(L"a");
- EXPECT_EQ(0, single_string.Find(L'a'));
- EXPECT_EQ(FX_STRNPOS, single_string.Find(L'b'));
- EXPECT_EQ(FX_STRNPOS, single_string.Find(0));
+ result = single_string.Find(L'a');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(0, result.value());
+ EXPECT_FALSE(single_string.Find(L'b').has_value());
+ EXPECT_FALSE(single_string.Find(L'\0').has_value());
CFX_WideStringC longer_string(L"abccc");
- EXPECT_EQ(0, longer_string.Find(L'a'));
- EXPECT_EQ(2, longer_string.Find(L'c'));
- EXPECT_EQ(FX_STRNPOS, longer_string.Find(0));
+ result = longer_string.Find(L'a');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(0, result.value());
+ result = longer_string.Find(L'c');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(2, result.value());
+ EXPECT_FALSE(longer_string.Find(L'd').has_value());
+ EXPECT_FALSE(longer_string.Find(L'\0').has_value());
CFX_WideStringC hibyte_string(
- L"ab\xff08"
+ L"ab\xFF8c"
L"def");
- EXPECT_EQ(2, hibyte_string.Find(L'\xff08'));
+ result = hibyte_string.Find(L'\xFF8c');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(2, result.value());
}
TEST(fxcrt, WideStringCNullIterator) {
diff --git a/core/fxcrt/fx_basic_util.cpp b/core/fxcrt/fx_basic_util.cpp
index 194f6d72ef..05ab20472c 100644
--- a/core/fxcrt/fx_basic_util.cpp
+++ b/core/fxcrt/fx_basic_util.cpp
@@ -14,7 +14,7 @@
#include "third_party/base/ptr_util.h"
bool FX_atonum(const CFX_ByteStringC& strc, void* pData) {
- if (strc.Find('.') != FX_STRNPOS) {
+ if (strc.Contains('.')) {
float* pFloat = static_cast<float*>(pData);
*pFloat = FX_atof(strc);
return false;
diff --git a/core/fxcrt/fx_system.h b/core/fxcrt/fx_system.h
index 47d588c521..ad7e6bbcd7 100644
--- a/core/fxcrt/fx_system.h
+++ b/core/fxcrt/fx_system.h
@@ -121,10 +121,6 @@ typedef int FX_STRSIZE;
#include "third_party/base/numerics/safe_conversions.h"
-// Constant used to indicate failure from find methods and other methods that
-// return FX_STRSIZE.
-constexpr FX_STRSIZE FX_STRNPOS = -1;
-
#define FXSYS_strlen(ptr) pdfium::base::checked_cast<FX_STRSIZE>(strlen(ptr))
#define FXSYS_wcslen(ptr) pdfium::base::checked_cast<FX_STRSIZE>(wcslen(ptr))
diff --git a/core/fxcrt/xml/cfx_xmlelement.cpp b/core/fxcrt/xml/cfx_xmlelement.cpp
index 75bf9ee593..eb60e68893 100644
--- a/core/fxcrt/xml/cfx_xmlelement.cpp
+++ b/core/fxcrt/xml/cfx_xmlelement.cpp
@@ -42,17 +42,15 @@ std::unique_ptr<CFX_XMLNode> CFX_XMLElement::Clone() {
}
CFX_WideString CFX_XMLElement::GetLocalTagName() const {
- FX_STRSIZE iFind = GetName().Find(L':', 0);
- if (iFind == FX_STRNPOS)
- return GetName();
- return GetName().Right(GetName().GetLength() - iFind - 1);
+ auto pos = GetName().Find(L':');
+ return pos.has_value()
+ ? GetName().Right(GetName().GetLength() - pos.value() - 1)
+ : GetName();
}
CFX_WideString CFX_XMLElement::GetNamespacePrefix() const {
- FX_STRSIZE iFind = GetName().Find(L':', 0);
- if (iFind == FX_STRNPOS)
- return CFX_WideString();
- return GetName().Left(iFind);
+ auto pos = GetName().Find(L':');
+ return pos.has_value() ? GetName().Left(pos.value()) : CFX_WideString();
}
CFX_WideString CFX_XMLElement::GetNamespaceURI() const {
diff --git a/core/fxcrt/xml/cxml_parser.cpp b/core/fxcrt/xml/cxml_parser.cpp
index ebd5873b33..18103dfc5e 100644
--- a/core/fxcrt/xml/cxml_parser.cpp
+++ b/core/fxcrt/xml/cxml_parser.cpp
@@ -82,12 +82,12 @@ void FX_XML_SplitQualifiedName(const CFX_ByteStringC& bsFullName,
if (bsFullName.IsEmpty())
return;
- FX_STRSIZE iStart = bsFullName.Find(':');
- if (iStart == FX_STRNPOS) {
+ auto iStart = bsFullName.Find(':');
+ if (!iStart.has_value()) {
bsName = bsFullName;
} else {
- bsSpace = bsFullName.Left(iStart);
- bsName = bsFullName.Right(bsFullName.GetLength() - (iStart + 1));
+ bsSpace = bsFullName.Left(iStart.value());
+ bsName = bsFullName.Right(bsFullName.GetLength() - (iStart.value() + 1));
}
}