summaryrefslogtreecommitdiff
path: root/core/fxcrt
diff options
context:
space:
mode:
Diffstat (limited to 'core/fxcrt')
-rw-r--r--core/fxcrt/cfx_bytestring.cpp10
-rw-r--r--core/fxcrt/cfx_bytestring.h1
-rw-r--r--core/fxcrt/cfx_bytestring_unittest.cpp11
-rw-r--r--core/fxcrt/cfx_string_c_template.h19
-rw-r--r--core/fxcrt/cfx_widestring.cpp9
-rw-r--r--core/fxcrt/cfx_widestring.h1
-rw-r--r--core/fxcrt/cfx_widestring_unittest.cpp11
-rw-r--r--core/fxcrt/xml/cxml_parser.cpp2
8 files changed, 25 insertions, 39 deletions
diff --git a/core/fxcrt/cfx_bytestring.cpp b/core/fxcrt/cfx_bytestring.cpp
index c88ad51ab3..e7e55de8e3 100644
--- a/core/fxcrt/cfx_bytestring.cpp
+++ b/core/fxcrt/cfx_bytestring.cpp
@@ -121,6 +121,7 @@ static_assert(sizeof(CFX_ByteString) <= sizeof(char*),
"Strings must not require more space than pointers");
CFX_ByteString::CFX_ByteString(const char* pStr, FX_STRSIZE nLen) {
+ ASSERT(nLen >= 0);
if (nLen < 0)
nLen = pStr ? FXSYS_strlen(pStr) : 0;
@@ -129,6 +130,7 @@ CFX_ByteString::CFX_ByteString(const char* pStr, FX_STRSIZE nLen) {
}
CFX_ByteString::CFX_ByteString(const uint8_t* pStr, FX_STRSIZE nLen) {
+ ASSERT(nLen >= 0);
if (nLen > 0) {
m_pData.Reset(
StringData::Create(reinterpret_cast<const char*>(pStr), nLen));
@@ -456,14 +458,8 @@ void CFX_ByteString::Concat(const char* pSrcData, FX_STRSIZE nSrcLen) {
m_pData.Swap(pNewData);
}
-CFX_ByteString CFX_ByteString::Mid(FX_STRSIZE nFirst) const {
- if (!m_pData)
- return CFX_ByteString();
-
- return Mid(nFirst, m_pData->m_nDataLength - nFirst);
-}
-
CFX_ByteString CFX_ByteString::Mid(FX_STRSIZE nFirst, FX_STRSIZE nCount) const {
+ ASSERT(nCount >= 0);
if (!m_pData)
return CFX_ByteString();
diff --git a/core/fxcrt/cfx_bytestring.h b/core/fxcrt/cfx_bytestring.h
index abf8ac80c8..519cee39d4 100644
--- a/core/fxcrt/cfx_bytestring.h
+++ b/core/fxcrt/cfx_bytestring.h
@@ -124,7 +124,6 @@ class CFX_ByteString {
char* GetBuffer(FX_STRSIZE len);
void ReleaseBuffer(FX_STRSIZE len = -1);
- CFX_ByteString Mid(FX_STRSIZE first) const;
CFX_ByteString Mid(FX_STRSIZE first, FX_STRSIZE count) const;
CFX_ByteString Left(FX_STRSIZE count) const;
CFX_ByteString Right(FX_STRSIZE count) const;
diff --git a/core/fxcrt/cfx_bytestring_unittest.cpp b/core/fxcrt/cfx_bytestring_unittest.cpp
index f5d9413b7d..bcf6f7495e 100644
--- a/core/fxcrt/cfx_bytestring_unittest.cpp
+++ b/core/fxcrt/cfx_bytestring_unittest.cpp
@@ -440,10 +440,10 @@ TEST(fxcrt, ByteStringMid) {
CFX_ByteString fred("FRED");
EXPECT_EQ("", fred.Mid(0, 0));
EXPECT_EQ("", fred.Mid(3, 0));
- EXPECT_EQ("FRED", fred.Mid(0));
- EXPECT_EQ("RED", fred.Mid(1));
- EXPECT_EQ("ED", fred.Mid(2));
- EXPECT_EQ("D", fred.Mid(3));
+ EXPECT_EQ("FRED", fred.Mid(0, 4));
+ EXPECT_EQ("RED", fred.Mid(1, 3));
+ EXPECT_EQ("ED", fred.Mid(2, 2));
+ EXPECT_EQ("D", fred.Mid(3, 1));
EXPECT_EQ("F", fred.Mid(0, 1));
EXPECT_EQ("R", fred.Mid(1, 1));
EXPECT_EQ("E", fred.Mid(2, 1));
@@ -458,9 +458,6 @@ TEST(fxcrt, ByteStringMid) {
CFX_ByteString empty;
EXPECT_EQ("", empty.Mid(0, 0));
- EXPECT_EQ("", empty.Mid(0));
- EXPECT_EQ("", empty.Mid(1));
- EXPECT_EQ("", empty.Mid(-1));
}
TEST(fxcrt, ByteStringLeft) {
diff --git a/core/fxcrt/cfx_string_c_template.h b/core/fxcrt/cfx_string_c_template.h
index cd3cd27e75..77f34f638e 100644
--- a/core/fxcrt/cfx_string_c_template.h
+++ b/core/fxcrt/cfx_string_c_template.h
@@ -130,29 +130,32 @@ class CFX_StringCTemplate {
return found ? found - m_Ptr.Get() : -1;
}
- CFX_StringCTemplate Mid(FX_STRSIZE index, FX_STRSIZE count = -1) const {
- index = std::max(0, index);
+ CFX_StringCTemplate Mid(FX_STRSIZE index, FX_STRSIZE count) const {
+ ASSERT(count >= 0);
if (index > m_Length)
return CFX_StringCTemplate();
- if (count < 0 || count > m_Length - index)
- count = m_Length - index;
+ index = pdfium::clamp(index, 0, m_Length);
+ count = pdfium::clamp(count, 0, m_Length - index);
+ if (count == 0)
+ return CFX_StringCTemplate();
return CFX_StringCTemplate(m_Ptr.Get() + index, count);
}
CFX_StringCTemplate Left(FX_STRSIZE count) const {
- if (count <= 0)
+ count = pdfium::clamp(count, 0, m_Length);
+ if (count == 0)
return CFX_StringCTemplate();
- return CFX_StringCTemplate(m_Ptr.Get(), std::min(count, m_Length));
+ return CFX_StringCTemplate(m_Ptr.Get(), count);
}
CFX_StringCTemplate Right(FX_STRSIZE count) const {
- if (count <= 0)
+ count = pdfium::clamp(count, 0, m_Length);
+ if (count == 0)
return CFX_StringCTemplate();
- count = std::min(count, m_Length);
return CFX_StringCTemplate(m_Ptr.Get() + m_Length - count, count);
}
diff --git a/core/fxcrt/cfx_widestring.cpp b/core/fxcrt/cfx_widestring.cpp
index 6a4fbb9846..d7c1ef23ea 100644
--- a/core/fxcrt/cfx_widestring.cpp
+++ b/core/fxcrt/cfx_widestring.cpp
@@ -291,6 +291,7 @@ CFX_WideString::CFX_WideString(CFX_WideString&& other) noexcept {
}
CFX_WideString::CFX_WideString(const wchar_t* pStr, FX_STRSIZE nLen) {
+ ASSERT(nLen >= 0);
if (nLen < 0)
nLen = pStr ? FXSYS_wcslen(pStr) : 0;
@@ -605,14 +606,8 @@ CFX_ByteString CFX_WideString::UTF16LE_Encode() const {
return result;
}
-CFX_WideString CFX_WideString::Mid(FX_STRSIZE nFirst) const {
- if (!m_pData)
- return CFX_WideString();
-
- return Mid(nFirst, m_pData->m_nDataLength - nFirst);
-}
-
CFX_WideString CFX_WideString::Mid(FX_STRSIZE nFirst, FX_STRSIZE nCount) const {
+ ASSERT(nCount >= 0);
if (!m_pData)
return CFX_WideString();
diff --git a/core/fxcrt/cfx_widestring.h b/core/fxcrt/cfx_widestring.h
index 0e477bfc8b..938b1e7958 100644
--- a/core/fxcrt/cfx_widestring.h
+++ b/core/fxcrt/cfx_widestring.h
@@ -112,7 +112,6 @@ class CFX_WideString {
int Compare(const CFX_WideString& str) const;
int CompareNoCase(const wchar_t* str) const;
- CFX_WideString Mid(FX_STRSIZE first) const;
CFX_WideString Mid(FX_STRSIZE first, FX_STRSIZE count) const;
CFX_WideString Left(FX_STRSIZE count) const;
CFX_WideString Right(FX_STRSIZE count) const;
diff --git a/core/fxcrt/cfx_widestring_unittest.cpp b/core/fxcrt/cfx_widestring_unittest.cpp
index 4cd71d9997..1d6d110497 100644
--- a/core/fxcrt/cfx_widestring_unittest.cpp
+++ b/core/fxcrt/cfx_widestring_unittest.cpp
@@ -406,10 +406,10 @@ TEST(fxcrt, WideStringMid) {
CFX_WideString fred(L"FRED");
EXPECT_EQ(L"", fred.Mid(0, 0));
EXPECT_EQ(L"", fred.Mid(3, 0));
- EXPECT_EQ(L"FRED", fred.Mid(0));
- EXPECT_EQ(L"RED", fred.Mid(1));
- EXPECT_EQ(L"ED", fred.Mid(2));
- EXPECT_EQ(L"D", fred.Mid(3));
+ EXPECT_EQ(L"FRED", fred.Mid(0, 4));
+ EXPECT_EQ(L"RED", fred.Mid(1, 3));
+ EXPECT_EQ(L"ED", fred.Mid(2, 2));
+ EXPECT_EQ(L"D", fred.Mid(3, 1));
EXPECT_EQ(L"F", fred.Mid(0, 1));
EXPECT_EQ(L"R", fred.Mid(1, 1));
EXPECT_EQ(L"E", fred.Mid(2, 1));
@@ -424,9 +424,6 @@ TEST(fxcrt, WideStringMid) {
CFX_WideString empty;
EXPECT_EQ(L"", empty.Mid(0, 0));
- EXPECT_EQ(L"", empty.Mid(0));
- EXPECT_EQ(L"", empty.Mid(1));
- EXPECT_EQ(L"", empty.Mid(-1));
}
TEST(fxcrt, WideStringLeft) {
diff --git a/core/fxcrt/xml/cxml_parser.cpp b/core/fxcrt/xml/cxml_parser.cpp
index a741d8409e..0166ea8490 100644
--- a/core/fxcrt/xml/cxml_parser.cpp
+++ b/core/fxcrt/xml/cxml_parser.cpp
@@ -87,7 +87,7 @@ void FX_XML_SplitQualifiedName(const CFX_ByteStringC& bsFullName,
bsName = bsFullName;
} else {
bsSpace = bsFullName.Mid(0, iStart);
- bsName = bsFullName.Mid(iStart + 1);
+ bsName = bsFullName.Mid(iStart + 1, bsFullName.GetLength() - (iStart + 1));
}
}