diff options
Diffstat (limited to 'core/fxcrt')
-rw-r--r-- | core/fxcrt/cfx_bytestring.cpp | 24 | ||||
-rw-r--r-- | core/fxcrt/cfx_bytestring.h | 2 | ||||
-rw-r--r-- | core/fxcrt/cfx_bytestring_unittest.cpp | 70 | ||||
-rw-r--r-- | core/fxcrt/cfx_widestring.cpp | 24 | ||||
-rw-r--r-- | core/fxcrt/cfx_widestring.h | 2 | ||||
-rw-r--r-- | core/fxcrt/cfx_widestring_unittest.cpp | 80 |
6 files changed, 136 insertions, 66 deletions
diff --git a/core/fxcrt/cfx_bytestring.cpp b/core/fxcrt/cfx_bytestring.cpp index 5dcaf613a0..e031c87d2d 100644 --- a/core/fxcrt/cfx_bytestring.cpp +++ b/core/fxcrt/cfx_bytestring.cpp @@ -518,18 +518,18 @@ void CFX_ByteString::Format(const char* pFormat, ...) { va_end(argList); } -FX_STRSIZE CFX_ByteString::Insert(FX_STRSIZE nIndex, char ch) { - FX_STRSIZE nNewLength = m_pData ? m_pData->m_nDataLength : 0; - nIndex = std::max(nIndex, 0); - nIndex = std::min(nIndex, nNewLength); - nNewLength++; - - ReallocBeforeWrite(nNewLength); - memmove(m_pData->m_String + nIndex + 1, m_pData->m_String + nIndex, - nNewLength - nIndex); - m_pData->m_String[nIndex] = ch; - m_pData->m_nDataLength = nNewLength; - return nNewLength; +FX_STRSIZE CFX_ByteString::Insert(FX_STRSIZE index, char ch) { + const FX_STRSIZE cur_length = m_pData ? m_pData->m_nDataLength : 0; + if (index != pdfium::clamp(index, 0, cur_length)) + return cur_length; + + const FX_STRSIZE new_length = cur_length + 1; + ReallocBeforeWrite(new_length); + memmove(m_pData->m_String + index + 1, m_pData->m_String + index, + new_length - index); + m_pData->m_String[index] = ch; + m_pData->m_nDataLength = new_length; + return new_length; } CFX_ByteString CFX_ByteString::Right(FX_STRSIZE nCount) const { diff --git a/core/fxcrt/cfx_bytestring.h b/core/fxcrt/cfx_bytestring.h index 8bd9f39fc0..df1b8309fc 100644 --- a/core/fxcrt/cfx_bytestring.h +++ b/core/fxcrt/cfx_bytestring.h @@ -118,6 +118,8 @@ class CFX_ByteString { void SetAt(FX_STRSIZE nIndex, char ch); FX_STRSIZE Insert(FX_STRSIZE index, char ch); + FX_STRSIZE InsertAtFront(char ch) { return Insert(0, ch); } + FX_STRSIZE InsertAtBack(char ch) { return Insert(GetLength(), ch); } FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1); void Format(const char* lpszFormat, ...); diff --git a/core/fxcrt/cfx_bytestring_unittest.cpp b/core/fxcrt/cfx_bytestring_unittest.cpp index 4379519046..89c8ac6060 100644 --- a/core/fxcrt/cfx_bytestring_unittest.cpp +++ b/core/fxcrt/cfx_bytestring_unittest.cpp @@ -387,32 +387,68 @@ TEST(fxcrt, ByteStringReplace) { TEST(fxcrt, ByteStringInsert) { CFX_ByteString fred("FRED"); - fred.Insert(-1, 'X'); - EXPECT_EQ("XFRED", fred); - fred.Insert(0, 'S'); - EXPECT_EQ("SXFRED", fred); - fred.Insert(2, 'T'); - EXPECT_EQ("SXTFRED", fred); - fred.Insert(5, 'U'); - EXPECT_EQ("SXTFRUED", fred); - fred.Insert(8, 'V'); - EXPECT_EQ("SXTFRUEDV", fred); - fred.Insert(12, 'P'); - EXPECT_EQ("SXTFRUEDVP", fred); + EXPECT_EQ(4, fred.Insert(-1, 'X')); + EXPECT_EQ("FRED", fred); + EXPECT_EQ(5, fred.Insert(0, 'S')); + EXPECT_EQ("SFRED", fred); + EXPECT_EQ(6, fred.Insert(1, 'T')); + EXPECT_EQ("STFRED", fred); + EXPECT_EQ(7, fred.Insert(4, 'U')); + EXPECT_EQ("STFRUED", fred); + EXPECT_EQ(8, fred.Insert(7, 'V')); + EXPECT_EQ("STFRUEDV", fred); + EXPECT_EQ(8, fred.Insert(12, 'P')); + EXPECT_EQ("STFRUEDV", fred); { CFX_ByteString empty; - empty.Insert(-1, 'X'); - EXPECT_EQ("X", empty); + EXPECT_EQ(0, empty.Insert(-1, 'X')); + EXPECT_NE("X", empty); } { CFX_ByteString empty; - empty.Insert(0, 'X'); + EXPECT_EQ(1, empty.Insert(0, 'X')); EXPECT_EQ("X", empty); } { CFX_ByteString empty; - empty.Insert(5, 'X'); - EXPECT_EQ("X", empty); + EXPECT_EQ(0, empty.Insert(5, 'X')); + EXPECT_NE("X", empty); + } +} + +TEST(fxcrt, ByteStringInsertAtFrontAndInsertAtBack) { + { + CFX_ByteString empty; + EXPECT_EQ(1, empty.InsertAtFront('D')); + EXPECT_EQ("D", empty); + EXPECT_EQ(2, empty.InsertAtFront('E')); + EXPECT_EQ("ED", empty); + EXPECT_EQ(3, empty.InsertAtFront('R')); + EXPECT_EQ("RED", empty); + EXPECT_EQ(4, empty.InsertAtFront('F')); + EXPECT_EQ("FRED", empty); + } + { + CFX_ByteString empty; + EXPECT_EQ(1, empty.InsertAtBack('F')); + EXPECT_EQ("F", empty); + EXPECT_EQ(2, empty.InsertAtBack('R')); + EXPECT_EQ("FR", empty); + EXPECT_EQ(3, empty.InsertAtBack('E')); + EXPECT_EQ("FRE", empty); + EXPECT_EQ(4, empty.InsertAtBack('D')); + EXPECT_EQ("FRED", empty); + } + { + CFX_ByteString empty; + EXPECT_EQ(1, empty.InsertAtBack('E')); + EXPECT_EQ("E", empty); + EXPECT_EQ(2, empty.InsertAtFront('R')); + EXPECT_EQ("RE", empty); + EXPECT_EQ(3, empty.InsertAtBack('D')); + EXPECT_EQ("RED", empty); + EXPECT_EQ(4, empty.InsertAtFront('F')); + EXPECT_EQ("FRED", empty); } } diff --git a/core/fxcrt/cfx_widestring.cpp b/core/fxcrt/cfx_widestring.cpp index b83752369b..6c079b354e 100644 --- a/core/fxcrt/cfx_widestring.cpp +++ b/core/fxcrt/cfx_widestring.cpp @@ -680,18 +680,18 @@ void CFX_WideString::Format(const wchar_t* pFormat, ...) { va_end(argList); } -FX_STRSIZE CFX_WideString::Insert(FX_STRSIZE nIndex, wchar_t ch) { - FX_STRSIZE nNewLength = m_pData ? m_pData->m_nDataLength : 0; - nIndex = std::max(nIndex, 0); - nIndex = std::min(nIndex, nNewLength); - nNewLength++; - - ReallocBeforeWrite(nNewLength); - wmemmove(m_pData->m_String + nIndex + 1, m_pData->m_String + nIndex, - nNewLength - nIndex); - m_pData->m_String[nIndex] = ch; - m_pData->m_nDataLength = nNewLength; - return nNewLength; +FX_STRSIZE CFX_WideString::Insert(FX_STRSIZE index, wchar_t ch) { + const FX_STRSIZE cur_length = m_pData ? m_pData->m_nDataLength : 0; + if (index != pdfium::clamp(index, 0, cur_length)) + return cur_length; + + const FX_STRSIZE new_length = cur_length + 1; + ReallocBeforeWrite(new_length); + wmemmove(m_pData->m_String + index + 1, m_pData->m_String + index, + new_length - index); + m_pData->m_String[index] = ch; + m_pData->m_nDataLength = new_length; + return new_length; } CFX_WideString CFX_WideString::Right(FX_STRSIZE nCount) const { diff --git a/core/fxcrt/cfx_widestring.h b/core/fxcrt/cfx_widestring.h index 02045c5c09..ccb1e752f7 100644 --- a/core/fxcrt/cfx_widestring.h +++ b/core/fxcrt/cfx_widestring.h @@ -120,6 +120,8 @@ class CFX_WideString { CFX_WideString Right(FX_STRSIZE count) const; FX_STRSIZE Insert(FX_STRSIZE index, wchar_t ch); + FX_STRSIZE InsertAtFront(wchar_t ch) { return Insert(0, ch); } + FX_STRSIZE InsertAtBack(wchar_t ch) { return Insert(GetLength(), ch); } FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1); void Format(const wchar_t* lpszFormat, ...); diff --git a/core/fxcrt/cfx_widestring_unittest.cpp b/core/fxcrt/cfx_widestring_unittest.cpp index b743a17fd1..a763f8abbd 100644 --- a/core/fxcrt/cfx_widestring_unittest.cpp +++ b/core/fxcrt/cfx_widestring_unittest.cpp @@ -347,38 +347,68 @@ TEST(fxcrt, WideStringReplace) { TEST(fxcrt, WideStringInsert) { CFX_WideString fred(L"FRED"); - fred.Insert(-1, 'X'); - EXPECT_EQ(L"XFRED", fred); - - fred.Insert(0, 'S'); - EXPECT_EQ(L"SXFRED", fred); - - fred.Insert(2, 'T'); - EXPECT_EQ(L"SXTFRED", fred); - - fred.Insert(5, 'U'); - EXPECT_EQ(L"SXTFRUED", fred); - - fred.Insert(8, 'V'); - EXPECT_EQ(L"SXTFRUEDV", fred); - - fred.Insert(12, 'P'); - EXPECT_EQ(L"SXTFRUEDVP", fred); - + EXPECT_EQ(4, fred.Insert(-1, 'X')); + EXPECT_EQ(L"FRED", fred); + EXPECT_EQ(5, fred.Insert(0, 'S')); + EXPECT_EQ(L"SFRED", fred); + EXPECT_EQ(6, fred.Insert(1, 'T')); + EXPECT_EQ(L"STFRED", fred); + EXPECT_EQ(7, fred.Insert(4, 'U')); + EXPECT_EQ(L"STFRUED", fred); + EXPECT_EQ(8, fred.Insert(7, 'V')); + EXPECT_EQ(L"STFRUEDV", fred); + EXPECT_EQ(8, fred.Insert(12, 'P')); + EXPECT_EQ(L"STFRUEDV", fred); { CFX_WideString empty; - empty.Insert(-1, 'X'); - EXPECT_EQ(L"X", empty); + EXPECT_EQ(0, empty.Insert(-1, 'X')); + EXPECT_NE(L"X", empty); } { CFX_WideString empty; - empty.Insert(0, 'X'); + EXPECT_EQ(1, empty.Insert(0, 'X')); EXPECT_EQ(L"X", empty); } { CFX_WideString empty; - empty.Insert(5, 'X'); - EXPECT_EQ(L"X", empty); + EXPECT_EQ(0, empty.Insert(5, 'X')); + EXPECT_NE(L"X", empty); + } +} + +TEST(fxcrt, WideStringInsertAtFrontAndInsertAtBack) { + { + CFX_WideString empty; + EXPECT_EQ(1, empty.InsertAtFront('D')); + EXPECT_EQ(L"D", empty); + EXPECT_EQ(2, empty.InsertAtFront('E')); + EXPECT_EQ(L"ED", empty); + EXPECT_EQ(3, empty.InsertAtFront('R')); + EXPECT_EQ(L"RED", empty); + EXPECT_EQ(4, empty.InsertAtFront('F')); + EXPECT_EQ(L"FRED", empty); + } + { + CFX_WideString empty; + EXPECT_EQ(1, empty.InsertAtBack('F')); + EXPECT_EQ(L"F", empty); + EXPECT_EQ(2, empty.InsertAtBack('R')); + EXPECT_EQ(L"FR", empty); + EXPECT_EQ(3, empty.InsertAtBack('E')); + EXPECT_EQ(L"FRE", empty); + EXPECT_EQ(4, empty.InsertAtBack('D')); + EXPECT_EQ(L"FRED", empty); + } + { + CFX_WideString empty; + EXPECT_EQ(1, empty.InsertAtBack('E')); + EXPECT_EQ(L"E", empty); + EXPECT_EQ(2, empty.InsertAtFront('R')); + EXPECT_EQ(L"RE", empty); + EXPECT_EQ(3, empty.InsertAtBack('D')); + EXPECT_EQ(L"RED", empty); + EXPECT_EQ(4, empty.InsertAtFront('F')); + EXPECT_EQ(L"FRED", empty); } } @@ -720,8 +750,8 @@ TEST(fxcrt, WideStringCOperatorSubscript) { TEST(fxcrt, WideStringCOperatorLT) { CFX_WideStringC empty; CFX_WideStringC a(L"a"); - CFX_WideStringC abc(L"\x0110qq"); // Comes before despite endianness. - CFX_WideStringC def(L"\x1001qq"); // Comes after despite endianness. + CFX_WideStringC abc(L"\x0110qq"); // Comes InsertAtFront despite endianness. + CFX_WideStringC def(L"\x1001qq"); // Comes InsertAtBack despite endianness. EXPECT_FALSE(empty < empty); EXPECT_FALSE(a < a); |