diff options
Diffstat (limited to 'core/fxcrt')
-rw-r--r-- | core/fxcrt/fx_basic_bstring.cpp | 25 | ||||
-rw-r--r-- | core/fxcrt/fx_basic_bstring_unittest.cpp | 218 | ||||
-rw-r--r-- | core/fxcrt/fx_basic_wstring.cpp | 25 | ||||
-rw-r--r-- | core/fxcrt/fx_basic_wstring_unittest.cpp | 224 |
4 files changed, 460 insertions, 32 deletions
diff --git a/core/fxcrt/fx_basic_bstring.cpp b/core/fxcrt/fx_basic_bstring.cpp index ab9299694e..10bc05bcc5 100644 --- a/core/fxcrt/fx_basic_bstring.cpp +++ b/core/fxcrt/fx_basic_bstring.cpp @@ -420,27 +420,20 @@ void CFX_ByteString::ConcatCopy(FX_STRSIZE nSrc1Len, pOldData->Release(); } CFX_ByteString CFX_ByteString::Mid(FX_STRSIZE nFirst) const { - if (!m_pData) { + 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 { - if (nFirst < 0) { - nFirst = 0; - } - if (nCount < 0) { - nCount = 0; - } - if (nFirst + nCount > m_pData->m_nDataLength) { - nCount = m_pData->m_nDataLength - nFirst; - } - if (nFirst > m_pData->m_nDataLength) { - nCount = 0; - } - if (nFirst == 0 && nFirst + nCount == m_pData->m_nDataLength) { + if (!m_pData) + return CFX_ByteString(); + + nFirst = std::min(std::max(0, nFirst), m_pData->m_nDataLength); + nCount = std::min(std::max(0, nCount), m_pData->m_nDataLength - nFirst); + if (nFirst == 0 && nCount == m_pData->m_nDataLength) return *this; - } + CFX_ByteString dest; AllocCopy(dest, nCount, nFirst); return dest; diff --git a/core/fxcrt/fx_basic_bstring_unittest.cpp b/core/fxcrt/fx_basic_bstring_unittest.cpp index 834a6e444d..7dc4db5613 100644 --- a/core/fxcrt/fx_basic_bstring_unittest.cpp +++ b/core/fxcrt/fx_basic_bstring_unittest.cpp @@ -315,6 +315,224 @@ TEST(fxcrt, ByteStringConcatInPlace) { EXPECT_EQ("xxxxxx", not_aliased); } +TEST(fxcrt, ByteStringRemove) { + CFX_ByteString freed("FREED"); + freed.Remove('E'); + EXPECT_EQ("FRD", freed); + freed.Remove('F'); + EXPECT_EQ("RD", freed); + freed.Remove('D'); + EXPECT_EQ("R", freed); + freed.Remove('X'); + EXPECT_EQ("R", freed); + freed.Remove('R'); + EXPECT_EQ("", freed); + + CFX_ByteString empty; + empty.Remove('X'); + EXPECT_EQ("", empty); +} + +TEST(fxcrt, ByteStringReplace) { + CFX_ByteString fred("FRED"); + fred.Replace("FR", "BL"); + EXPECT_EQ("BLED", fred); + fred.Replace("D", "DDY"); + EXPECT_EQ("BLEDDY", fred); + fred.Replace("LEDD", ""); + EXPECT_EQ("BY", fred); + fred.Replace("X", "CLAMS"); + EXPECT_EQ("BY", fred); + fred.Replace("BY", "HI"); + EXPECT_EQ("HI", fred); + fred.Replace("", "CLAMS"); + EXPECT_EQ("HI", fred); + fred.Replace("HI", ""); + EXPECT_EQ("", fred); +} + +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); + { + CFX_ByteString empty; + empty.Insert(-1, 'X'); + EXPECT_EQ("X", empty); + } + { + CFX_ByteString empty; + empty.Insert(0, 'X'); + EXPECT_EQ("X", empty); + } + { + CFX_ByteString empty; + empty.Insert(5, 'X'); + EXPECT_EQ("X", empty); + } +} + +TEST(fxcrt, ByteStringDelete) { + CFX_ByteString fred("FRED"); + fred.Delete(0, 2); + EXPECT_EQ("ED", fred); + fred.Delete(1); + EXPECT_EQ("E", fred); + fred.Delete(-1); + EXPECT_EQ("", fred); + fred.Delete(1); + EXPECT_EQ("", fred); + + CFX_ByteString empty; + empty.Delete(0); + EXPECT_EQ("", empty); + empty.Delete(-1); + EXPECT_EQ("", empty); + empty.Delete(1); + EXPECT_EQ("", empty); +} + +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("F", fred.Mid(0, 1)); + EXPECT_EQ("R", fred.Mid(1, 1)); + EXPECT_EQ("E", fred.Mid(2, 1)); + EXPECT_EQ("D", fred.Mid(3, 1)); + EXPECT_EQ("FR", fred.Mid(0, 2)); + EXPECT_EQ("FRED", fred.Mid(0, 4)); + EXPECT_EQ("FRED", fred.Mid(0, 10)); + + EXPECT_EQ("FR", fred.Mid(-1, 2)); + EXPECT_EQ("RED", fred.Mid(1, 4)); + EXPECT_EQ("", fred.Mid(4, 1)); + + 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) { + CFX_ByteString fred("FRED"); + EXPECT_EQ("", fred.Left(0)); + EXPECT_EQ("F", fred.Left(1)); + EXPECT_EQ("FR", fred.Left(2)); + EXPECT_EQ("FRE", fred.Left(3)); + EXPECT_EQ("FRED", fred.Left(4)); + + EXPECT_EQ("FRED", fred.Left(5)); + EXPECT_EQ("", fred.Left(-1)); + + CFX_ByteString empty; + EXPECT_EQ("", empty.Left(0)); + EXPECT_EQ("", empty.Left(1)); + EXPECT_EQ("", empty.Left(-1)); +} + +TEST(fxcrt, ByteStringRight) { + CFX_ByteString fred("FRED"); + EXPECT_EQ("", fred.Right(0)); + EXPECT_EQ("D", fred.Right(1)); + EXPECT_EQ("ED", fred.Right(2)); + EXPECT_EQ("RED", fred.Right(3)); + EXPECT_EQ("FRED", fred.Right(4)); + + EXPECT_EQ("FRED", fred.Right(5)); + EXPECT_EQ("", fred.Right(-1)); + + CFX_ByteString empty; + EXPECT_EQ("", empty.Right(0)); + EXPECT_EQ("", empty.Right(1)); + EXPECT_EQ("", empty.Right(-1)); +} + +TEST(fxcrt, ByteStringUpperLower) { + CFX_ByteString fred("F-Re.42D"); + fred.MakeLower(); + EXPECT_EQ("f-re.42d", fred); + fred.MakeUpper(); + EXPECT_EQ("F-RE.42D", fred); + + CFX_ByteString empty; + empty.MakeLower(); + EXPECT_EQ("", empty); + empty.MakeUpper(); + EXPECT_EQ("", empty); +} + +TEST(fxcrt, ByteStringTrimRight) { + CFX_ByteString fred(" FRED "); + fred.TrimRight(); + EXPECT_EQ(" FRED", fred); + fred.TrimRight('E'); + EXPECT_EQ(" FRED", fred); + fred.TrimRight('D'); + EXPECT_EQ(" FRE", fred); + fred.TrimRight("ERP"); + EXPECT_EQ(" F", fred); + + CFX_ByteString blank(" "); + blank.TrimRight("ERP"); + EXPECT_EQ(" ", blank); + blank.TrimRight('E'); + EXPECT_EQ(" ", blank); + blank.TrimRight(); + EXPECT_EQ("", blank); + + CFX_ByteString empty; + empty.TrimRight("ERP"); + EXPECT_EQ("", empty); + empty.TrimRight('E'); + EXPECT_EQ("", empty); + empty.TrimRight(); + EXPECT_EQ("", empty); +} + +TEST(fxcrt, ByteStringTrimLeft) { + CFX_ByteString fred(" FRED "); + fred.TrimLeft(); + EXPECT_EQ("FRED ", fred); + fred.TrimLeft('E'); + EXPECT_EQ("FRED ", fred); + fred.TrimLeft('F'); + EXPECT_EQ("RED ", fred); + fred.TrimLeft("ERP"); + EXPECT_EQ("D ", fred); + + CFX_ByteString blank(" "); + blank.TrimLeft("ERP"); + EXPECT_EQ(" ", blank); + blank.TrimLeft('E'); + EXPECT_EQ(" ", blank); + blank.TrimLeft(); + EXPECT_EQ("", blank); + + CFX_ByteString empty; + empty.TrimLeft("ERP"); + EXPECT_EQ("", empty); + empty.TrimLeft('E'); + EXPECT_EQ("", empty); + empty.TrimLeft(); + EXPECT_EQ("", empty); +} + TEST(fxcrt, ByteStringCNotNull) { CFX_ByteStringC string3("abc"); CFX_ByteStringC string6("abcdef"); diff --git a/core/fxcrt/fx_basic_wstring.cpp b/core/fxcrt/fx_basic_wstring.cpp index aa62b5e356..642b75eafe 100644 --- a/core/fxcrt/fx_basic_wstring.cpp +++ b/core/fxcrt/fx_basic_wstring.cpp @@ -441,27 +441,20 @@ CFX_WideString CFX_WideString::Left(FX_STRSIZE nCount) const { return dest; } 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 { - if (!m_pData) { + if (!m_pData) return CFX_WideString(); - } - if (nFirst < 0) { - nFirst = 0; - } - if (nCount < 0) { - nCount = 0; - } - if (nFirst + nCount > m_pData->m_nDataLength) { - nCount = m_pData->m_nDataLength - nFirst; - } - if (nFirst > m_pData->m_nDataLength) { - nCount = 0; - } - if (nFirst == 0 && nFirst + nCount == m_pData->m_nDataLength) { + + nFirst = std::min(std::max(nFirst, 0), m_pData->m_nDataLength); + nCount = std::min(std::max(nCount, 0), m_pData->m_nDataLength - nFirst); + if (nFirst == 0 && nCount == m_pData->m_nDataLength) return *this; - } + CFX_WideString dest; AllocCopy(dest, nCount, nFirst); return dest; diff --git a/core/fxcrt/fx_basic_wstring_unittest.cpp b/core/fxcrt/fx_basic_wstring_unittest.cpp index a64d7a91e9..8f0084619d 100644 --- a/core/fxcrt/fx_basic_wstring_unittest.cpp +++ b/core/fxcrt/fx_basic_wstring_unittest.cpp @@ -276,6 +276,230 @@ TEST(fxcrt, WideStringConcatInPlace) { EXPECT_EQ(L"xxxxxx", not_aliased); } +TEST(fxcrt, WideStringRemove) { + CFX_WideString freed(L"FREED"); + freed.Remove(L'E'); + EXPECT_EQ(L"FRD", freed); + freed.Remove(L'F'); + EXPECT_EQ(L"RD", freed); + freed.Remove(L'D'); + EXPECT_EQ(L"R", freed); + freed.Remove(L'X'); + EXPECT_EQ(L"R", freed); + freed.Remove(L'R'); + EXPECT_EQ(L"", freed); + + CFX_WideString empty; + empty.Remove(L'X'); + EXPECT_EQ(L"", empty); +} + +TEST(fxcrt, WideStringReplace) { + CFX_WideString fred(L"FRED"); + fred.Replace(L"FR", L"BL"); + EXPECT_EQ(L"BLED", fred); + fred.Replace(L"D", L"DDY"); + EXPECT_EQ(L"BLEDDY", fred); + fred.Replace(L"LEDD", L""); + EXPECT_EQ(L"BY", fred); + fred.Replace(L"X", L"CLAMS"); + EXPECT_EQ(L"BY", fred); + fred.Replace(L"BY", L"HI"); + EXPECT_EQ(L"HI", fred); + fred.Replace(L"", L"CLAMS"); + EXPECT_EQ(L"HI", fred); + fred.Replace(L"HI", L""); + EXPECT_EQ(L"", fred); +} + +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); + + { + CFX_WideString empty; + empty.Insert(-1, 'X'); + EXPECT_EQ(L"X", empty); + } + { + CFX_WideString empty; + empty.Insert(0, 'X'); + EXPECT_EQ(L"X", empty); + } + { + CFX_WideString empty; + empty.Insert(5, 'X'); + EXPECT_EQ(L"X", empty); + } +} + +TEST(fxcrt, WideStringDelete) { + CFX_WideString fred(L"FRED"); + fred.Delete(0, 2); + EXPECT_EQ(L"ED", fred); + fred.Delete(1); + EXPECT_EQ(L"E", fred); + fred.Delete(-1); + EXPECT_EQ(L"", fred); + fred.Delete(1); + EXPECT_EQ(L"", fred); + + CFX_WideString empty; + empty.Delete(0); + EXPECT_EQ(L"", empty); + empty.Delete(-1); + EXPECT_EQ(L"", empty); + empty.Delete(1); + EXPECT_EQ(L"", empty); +} + +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"F", fred.Mid(0, 1)); + EXPECT_EQ(L"R", fred.Mid(1, 1)); + EXPECT_EQ(L"E", fred.Mid(2, 1)); + EXPECT_EQ(L"D", fred.Mid(3, 1)); + EXPECT_EQ(L"FR", fred.Mid(0, 2)); + EXPECT_EQ(L"FRED", fred.Mid(0, 4)); + EXPECT_EQ(L"FRED", fred.Mid(0, 10)); + + EXPECT_EQ(L"FR", fred.Mid(-1, 2)); + EXPECT_EQ(L"RED", fred.Mid(1, 4)); + EXPECT_EQ(L"", fred.Mid(4, 1)); + + 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) { + CFX_WideString fred(L"FRED"); + EXPECT_EQ(L"", fred.Left(0)); + EXPECT_EQ(L"F", fred.Left(1)); + EXPECT_EQ(L"FR", fred.Left(2)); + EXPECT_EQ(L"FRE", fred.Left(3)); + EXPECT_EQ(L"FRED", fred.Left(4)); + + EXPECT_EQ(L"FRED", fred.Left(5)); + EXPECT_EQ(L"", fred.Left(-1)); + + CFX_WideString empty; + EXPECT_EQ(L"", empty.Left(0)); + EXPECT_EQ(L"", empty.Left(1)); + EXPECT_EQ(L"", empty.Left(-1)); +} + +TEST(fxcrt, WideStringRight) { + CFX_WideString fred(L"FRED"); + EXPECT_EQ(L"", fred.Right(0)); + EXPECT_EQ(L"D", fred.Right(1)); + EXPECT_EQ(L"ED", fred.Right(2)); + EXPECT_EQ(L"RED", fred.Right(3)); + EXPECT_EQ(L"FRED", fred.Right(4)); + + EXPECT_EQ(L"FRED", fred.Right(5)); + EXPECT_EQ(L"", fred.Right(-1)); + + CFX_WideString empty; + EXPECT_EQ(L"", empty.Right(0)); + EXPECT_EQ(L"", empty.Right(1)); + EXPECT_EQ(L"", empty.Right(-1)); +} + +TEST(fxcrt, WideStringUpperLower) { + CFX_WideString fred(L"F-Re.42D"); + fred.MakeLower(); + EXPECT_EQ(L"f-re.42d", fred); + fred.MakeUpper(); + EXPECT_EQ(L"F-RE.42D", fred); + + CFX_WideString empty; + empty.MakeLower(); + EXPECT_EQ(L"", empty); + empty.MakeUpper(); + EXPECT_EQ(L"", empty); +} + +TEST(fxcrt, WideStringTrimRight) { + CFX_WideString fred(L" FRED "); + fred.TrimRight(); + EXPECT_EQ(L" FRED", fred); + fred.TrimRight(L'E'); + EXPECT_EQ(L" FRED", fred); + fred.TrimRight(L'D'); + EXPECT_EQ(L" FRE", fred); + fred.TrimRight(L"ERP"); + EXPECT_EQ(L" F", fred); + + CFX_WideString blank(L" "); + blank.TrimRight(L"ERP"); + EXPECT_EQ(L" ", blank); + blank.TrimRight(L'E'); + EXPECT_EQ(L" ", blank); + blank.TrimRight(); + EXPECT_EQ(L"", blank); + + CFX_WideString empty; + empty.TrimRight(L"ERP"); + EXPECT_EQ(L"", empty); + empty.TrimRight(L'E'); + EXPECT_EQ(L"", empty); + empty.TrimRight(); + EXPECT_EQ(L"", empty); +} + +TEST(fxcrt, WideStringTrimLeft) { + CFX_WideString fred(L" FRED "); + fred.TrimLeft(); + EXPECT_EQ(L"FRED ", fred); + fred.TrimLeft(L'E'); + EXPECT_EQ(L"FRED ", fred); + fred.TrimLeft(L'F'); + EXPECT_EQ(L"RED ", fred); + fred.TrimLeft(L"ERP"); + EXPECT_EQ(L"D ", fred); + + CFX_WideString blank(L" "); + blank.TrimLeft(L"ERP"); + EXPECT_EQ(L" ", blank); + blank.TrimLeft(L'E'); + EXPECT_EQ(L" ", blank); + blank.TrimLeft(); + EXPECT_EQ(L"", blank); + + CFX_WideString empty; + empty.TrimLeft(L"ERP"); + EXPECT_EQ(L"", empty); + empty.TrimLeft(L'E'); + EXPECT_EQ(L"", empty); + empty.TrimLeft(); + EXPECT_EQ(L"", empty); +} + TEST(fxcrt, WideStringUTF16LE_Encode) { struct UTF16LEEncodeCase { CFX_WideString ws; |