diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/fxcrt/bytestring.cpp | 87 | ||||
-rw-r--r-- | core/fxcrt/bytestring.h | 14 | ||||
-rw-r--r-- | core/fxcrt/bytestring_unittest.cpp | 118 | ||||
-rw-r--r-- | core/fxcrt/widestring.cpp | 72 | ||||
-rw-r--r-- | core/fxcrt/widestring.h | 14 | ||||
-rw-r--r-- | core/fxcrt/widestring_unittest.cpp | 120 |
6 files changed, 266 insertions, 159 deletions
diff --git a/core/fxcrt/bytestring.cpp b/core/fxcrt/bytestring.cpp index e868678c9f..324a2801e4 100644 --- a/core/fxcrt/bytestring.cpp +++ b/core/fxcrt/bytestring.cpp @@ -27,6 +27,8 @@ template struct std::hash<ByteString>; namespace { +constexpr char kTrimChars[] = "\x09\x0a\x0b\x0c\x0d\x20"; + const char* FX_strstr(const char* haystack, int haystack_len, const char* needle, @@ -98,7 +100,7 @@ static_assert(sizeof(ByteString) <= sizeof(char*), // static ByteString ByteString::FormatInteger(int i) { char buf[32]; - FXSYS_snprintf(buf, 32, "%d", i); + FXSYS_snprintf(buf, sizeof(buf), "%d", i); return ByteString(buf); } @@ -728,42 +730,32 @@ int ByteString::Compare(const ByteStringView& str) const { return 0; } -void ByteString::TrimRight(const ByteStringView& pTargets) { - if (!m_pData || pTargets.IsEmpty()) - return; +void ByteString::Trim() { + TrimRight(kTrimChars); + TrimLeft(kTrimChars); +} - size_t pos = GetLength(); - if (pos == 0) - return; +void ByteString::Trim(char target) { + ByteStringView targets(target); + TrimRight(targets); + TrimLeft(targets); +} - while (pos) { - size_t i = 0; - while (i < pTargets.GetLength() && - pTargets[i] != m_pData->m_String[pos - 1]) { - i++; - } - if (i == pTargets.GetLength()) { - break; - } - pos--; - } - if (pos < m_pData->m_nDataLength) { - ReallocBeforeWrite(m_pData->m_nDataLength); - m_pData->m_String[pos] = 0; - m_pData->m_nDataLength = pos; - } +void ByteString::Trim(const ByteStringView& targets) { + TrimRight(targets); + TrimLeft(targets); } -void ByteString::TrimRight(char chTarget) { - TrimRight(ByteStringView(chTarget)); +void ByteString::TrimLeft() { + TrimLeft(kTrimChars); } -void ByteString::TrimRight() { - TrimRight("\x09\x0a\x0b\x0c\x0d\x20"); +void ByteString::TrimLeft(char target) { + TrimLeft(ByteStringView(target)); } -void ByteString::TrimLeft(const ByteStringView& pTargets) { - if (!m_pData || pTargets.IsEmpty()) +void ByteString::TrimLeft(const ByteStringView& targets) { + if (!m_pData || targets.IsEmpty()) return; size_t len = GetLength(); @@ -773,12 +765,10 @@ void ByteString::TrimLeft(const ByteStringView& pTargets) { size_t pos = 0; while (pos < len) { size_t i = 0; - while (i < pTargets.GetLength() && pTargets[i] != m_pData->m_String[pos]) { + while (i < targets.GetLength() && targets[i] != m_pData->m_String[pos]) i++; - } - if (i == pTargets.GetLength()) { + if (i == targets.GetLength()) break; - } pos++; } if (pos) { @@ -790,12 +780,35 @@ void ByteString::TrimLeft(const ByteStringView& pTargets) { } } -void ByteString::TrimLeft(char chTarget) { - TrimLeft(ByteStringView(chTarget)); +void ByteString::TrimRight() { + TrimRight(kTrimChars); } -void ByteString::TrimLeft() { - TrimLeft("\x09\x0a\x0b\x0c\x0d\x20"); +void ByteString::TrimRight(char target) { + TrimRight(ByteStringView(target)); +} + +void ByteString::TrimRight(const ByteStringView& targets) { + if (!m_pData || targets.IsEmpty()) + return; + + size_t pos = GetLength(); + if (pos == 0) + return; + + while (pos) { + size_t i = 0; + while (i < targets.GetLength() && targets[i] != m_pData->m_String[pos - 1]) + i++; + if (i == targets.GetLength()) + break; + pos--; + } + if (pos < m_pData->m_nDataLength) { + ReallocBeforeWrite(m_pData->m_nDataLength); + m_pData->m_String[pos] = 0; + m_pData->m_nDataLength = pos; + } } std::ostream& operator<<(std::ostream& os, const ByteString& str) { diff --git a/core/fxcrt/bytestring.h b/core/fxcrt/bytestring.h index 1acece3aee..bd414fc76f 100644 --- a/core/fxcrt/bytestring.h +++ b/core/fxcrt/bytestring.h @@ -167,13 +167,17 @@ class ByteString { void MakeLower(); void MakeUpper(); - void TrimRight(); - void TrimRight(char chTarget); - void TrimRight(const ByteStringView& lpszTargets); + void Trim(); + void Trim(char target); + void Trim(const ByteStringView& targets); void TrimLeft(); - void TrimLeft(char chTarget); - void TrimLeft(const ByteStringView& lpszTargets); + void TrimLeft(char target); + void TrimLeft(const ByteStringView& targets); + + void TrimRight(); + void TrimRight(char target); + void TrimRight(const ByteStringView& targets); size_t Replace(const ByteStringView& lpszOld, const ByteStringView& lpszNew); diff --git a/core/fxcrt/bytestring_unittest.cpp b/core/fxcrt/bytestring_unittest.cpp index 08c0e9211c..b45ee0fd5e 100644 --- a/core/fxcrt/bytestring_unittest.cpp +++ b/core/fxcrt/bytestring_unittest.cpp @@ -703,41 +703,75 @@ TEST(ByteString, UpperLower) { EXPECT_EQ("", empty); } -TEST(ByteString, TrimRight) { +TEST(ByteString, Trim) { 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); + fred.Trim(); + EXPECT_EQ("FRED", fred); + fred.Trim('E'); + EXPECT_EQ("FRED", fred); + fred.Trim('F'); + EXPECT_EQ("RED", fred); + fred.Trim("ERP"); + EXPECT_EQ("D", fred); ByteString blank(" "); - blank.TrimRight("ERP"); + blank.Trim("ERP"); EXPECT_EQ(" ", blank); - blank.TrimRight('E'); + blank.Trim('E'); EXPECT_EQ(" ", blank); - blank.TrimRight(); + blank.Trim(); EXPECT_EQ("", blank); ByteString empty; - empty.TrimRight("ERP"); + empty.Trim("ERP"); EXPECT_EQ("", empty); - empty.TrimRight('E'); + empty.Trim('E'); EXPECT_EQ("", empty); - empty.TrimRight(); + empty.Trim(); EXPECT_EQ("", empty); + + ByteString abc(" ABCCBA "); + abc.Trim("A"); + EXPECT_EQ(" ABCCBA ", abc); + abc.Trim(" A"); + EXPECT_EQ("BCCB", abc); } -TEST(ByteString, TrimRightCopies) { +TEST(ByteString, TrimLeft) { + 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); + + ByteString blank(" "); + blank.TrimLeft("ERP"); + EXPECT_EQ(" ", blank); + blank.TrimLeft('E'); + EXPECT_EQ(" ", blank); + blank.TrimLeft(); + EXPECT_EQ("", blank); + + ByteString empty; + empty.TrimLeft("ERP"); + EXPECT_EQ("", empty); + empty.TrimLeft('E'); + EXPECT_EQ("", empty); + empty.TrimLeft(); + EXPECT_EQ("", empty); +} + +TEST(ByteString, TrimLeftCopies) { { // With a single reference, no copy takes place. ByteString fred(" FRED "); const char* old_buffer = fred.c_str(); - fred.TrimRight(); - EXPECT_EQ(" FRED", fred); + fred.TrimLeft(); + EXPECT_EQ("FRED ", fred); EXPECT_EQ(old_buffer, fred.c_str()); } { @@ -745,8 +779,8 @@ TEST(ByteString, TrimRightCopies) { ByteString fred(" FRED "); ByteString other_fred = fred; const char* old_buffer = fred.c_str(); - fred.TrimRight(); - EXPECT_EQ(" FRED", fred); + fred.TrimLeft(); + EXPECT_EQ("FRED ", fred); EXPECT_EQ(" FRED ", other_fred); EXPECT_NE(old_buffer, fred.c_str()); } @@ -755,48 +789,48 @@ TEST(ByteString, TrimRightCopies) { ByteString fred("FRED"); ByteString other_fred = fred; const char* old_buffer = fred.c_str(); - fred.TrimRight(); + fred.TrimLeft(); EXPECT_EQ("FRED", fred); EXPECT_EQ("FRED", other_fred); EXPECT_EQ(old_buffer, fred.c_str()); } } -TEST(ByteString, TrimLeft) { +TEST(ByteString, TrimRight) { 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); + 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); ByteString blank(" "); - blank.TrimLeft("ERP"); + blank.TrimRight("ERP"); EXPECT_EQ(" ", blank); - blank.TrimLeft('E'); + blank.TrimRight('E'); EXPECT_EQ(" ", blank); - blank.TrimLeft(); + blank.TrimRight(); EXPECT_EQ("", blank); ByteString empty; - empty.TrimLeft("ERP"); + empty.TrimRight("ERP"); EXPECT_EQ("", empty); - empty.TrimLeft('E'); + empty.TrimRight('E'); EXPECT_EQ("", empty); - empty.TrimLeft(); + empty.TrimRight(); EXPECT_EQ("", empty); } -TEST(ByteString, TrimLeftCopies) { +TEST(ByteString, TrimRightCopies) { { // With a single reference, no copy takes place. ByteString fred(" FRED "); const char* old_buffer = fred.c_str(); - fred.TrimLeft(); - EXPECT_EQ("FRED ", fred); + fred.TrimRight(); + EXPECT_EQ(" FRED", fred); EXPECT_EQ(old_buffer, fred.c_str()); } { @@ -804,8 +838,8 @@ TEST(ByteString, TrimLeftCopies) { ByteString fred(" FRED "); ByteString other_fred = fred; const char* old_buffer = fred.c_str(); - fred.TrimLeft(); - EXPECT_EQ("FRED ", fred); + fred.TrimRight(); + EXPECT_EQ(" FRED", fred); EXPECT_EQ(" FRED ", other_fred); EXPECT_NE(old_buffer, fred.c_str()); } @@ -814,7 +848,7 @@ TEST(ByteString, TrimLeftCopies) { ByteString fred("FRED"); ByteString other_fred = fred; const char* old_buffer = fred.c_str(); - fred.TrimLeft(); + fred.TrimRight(); EXPECT_EQ("FRED", fred); EXPECT_EQ("FRED", other_fred); EXPECT_EQ(old_buffer, fred.c_str()); diff --git a/core/fxcrt/widestring.cpp b/core/fxcrt/widestring.cpp index d33ed1ad6c..2caa3b4213 100644 --- a/core/fxcrt/widestring.cpp +++ b/core/fxcrt/widestring.cpp @@ -31,6 +31,8 @@ template struct std::hash<WideString>; namespace { +constexpr wchar_t kTrimChars[] = L"\x09\x0a\x0b\x0c\x0d\x20"; + const wchar_t* FX_wcsstr(const wchar_t* haystack, int haystack_len, const wchar_t* needle, @@ -958,32 +960,33 @@ size_t WideString::WStringLength(const unsigned short* str) { return len; } -void WideString::TrimRight(const WideStringView& pTargets) { - if (IsEmpty() || pTargets.IsEmpty()) - return; +void WideString::Trim() { + TrimRight(kTrimChars); + TrimLeft(kTrimChars); +} - size_t pos = GetLength(); - while (pos && pTargets.Contains(m_pData->m_String[pos - 1])) - pos--; +void WideString::Trim(wchar_t target) { + wchar_t str[2] = {target, 0}; + TrimRight(str); + TrimLeft(str); +} - if (pos < m_pData->m_nDataLength) { - ReallocBeforeWrite(m_pData->m_nDataLength); - m_pData->m_String[pos] = 0; - m_pData->m_nDataLength = pos; - } +void WideString::Trim(const WideStringView& targets) { + TrimRight(targets); + TrimLeft(targets); } -void WideString::TrimRight(wchar_t chTarget) { - wchar_t str[2] = {chTarget, 0}; - TrimRight(str); +void WideString::TrimLeft() { + TrimLeft(kTrimChars); } -void WideString::TrimRight() { - TrimRight(L"\x09\x0a\x0b\x0c\x0d\x20"); +void WideString::TrimLeft(wchar_t target) { + wchar_t str[2] = {target, 0}; + TrimLeft(str); } -void WideString::TrimLeft(const WideStringView& pTargets) { - if (!m_pData || pTargets.IsEmpty()) +void WideString::TrimLeft(const WideStringView& targets) { + if (!m_pData || targets.IsEmpty()) return; size_t len = GetLength(); @@ -993,13 +996,12 @@ void WideString::TrimLeft(const WideStringView& pTargets) { size_t pos = 0; while (pos < len) { size_t i = 0; - while (i < pTargets.GetLength() && - pTargets.CharAt(i) != m_pData->m_String[pos]) { + while (i < targets.GetLength() && + targets.CharAt(i) != m_pData->m_String[pos]) { i++; } - if (i == pTargets.GetLength()) { + if (i == targets.GetLength()) break; - } pos++; } if (!pos) @@ -1012,14 +1014,30 @@ void WideString::TrimLeft(const WideStringView& pTargets) { m_pData->m_nDataLength = nDataLength; } -void WideString::TrimLeft(wchar_t chTarget) { - wchar_t str[2] = {chTarget, 0}; - TrimLeft(str); +void WideString::TrimRight() { + TrimRight(kTrimChars); } -void WideString::TrimLeft() { - TrimLeft(L"\x09\x0a\x0b\x0c\x0d\x20"); +void WideString::TrimRight(wchar_t target) { + wchar_t str[2] = {target, 0}; + TrimRight(str); } + +void WideString::TrimRight(const WideStringView& targets) { + if (IsEmpty() || targets.IsEmpty()) + return; + + size_t pos = GetLength(); + while (pos && targets.Contains(m_pData->m_String[pos - 1])) + pos--; + + if (pos < m_pData->m_nDataLength) { + ReallocBeforeWrite(m_pData->m_nDataLength); + m_pData->m_String[pos] = 0; + m_pData->m_nDataLength = pos; + } +} + float FX_wtof(const wchar_t* str, int len) { if (len == 0) { return 0.0; diff --git a/core/fxcrt/widestring.h b/core/fxcrt/widestring.h index 9d856c72c9..f48c32379a 100644 --- a/core/fxcrt/widestring.h +++ b/core/fxcrt/widestring.h @@ -147,13 +147,17 @@ class WideString { void MakeLower(); void MakeUpper(); - void TrimRight(); - void TrimRight(wchar_t chTarget); - void TrimRight(const WideStringView& pTargets); + void Trim(); + void Trim(wchar_t target); + void Trim(const WideStringView& targets); void TrimLeft(); - void TrimLeft(wchar_t chTarget); - void TrimLeft(const WideStringView& pTargets); + void TrimLeft(wchar_t target); + void TrimLeft(const WideStringView& targets); + + void TrimRight(); + void TrimRight(wchar_t target); + void TrimRight(const WideStringView& targets); void Reserve(size_t len); wchar_t* GetBuffer(size_t len); diff --git a/core/fxcrt/widestring_unittest.cpp b/core/fxcrt/widestring_unittest.cpp index aaa6d9cae7..39337aa7a7 100644 --- a/core/fxcrt/widestring_unittest.cpp +++ b/core/fxcrt/widestring_unittest.cpp @@ -628,41 +628,75 @@ TEST(WideString, UpperLower) { EXPECT_EQ(L"", empty); } -TEST(WideString, TrimRight) { +TEST(WideString, Trim) { 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); + fred.Trim(); + EXPECT_EQ(L"FRED", fred); + fred.Trim(L'E'); + EXPECT_EQ(L"FRED", fred); + fred.Trim(L'F'); + EXPECT_EQ(L"RED", fred); + fred.Trim(L"ERP"); + EXPECT_EQ(L"D", fred); WideString blank(L" "); - blank.TrimRight(L"ERP"); + blank.Trim(L"ERP"); EXPECT_EQ(L" ", blank); - blank.TrimRight(L'E'); + blank.Trim(L'E'); EXPECT_EQ(L" ", blank); - blank.TrimRight(); + blank.Trim(); EXPECT_EQ(L"", blank); WideString empty; - empty.TrimRight(L"ERP"); + empty.Trim(L"ERP"); EXPECT_EQ(L"", empty); - empty.TrimRight(L'E'); + empty.Trim(L'E'); EXPECT_EQ(L"", empty); - empty.TrimRight(); + empty.Trim(); EXPECT_EQ(L"", empty); + + WideString abc(L" ABCCBA "); + abc.Trim(L"A"); + EXPECT_EQ(L" ABCCBA ", abc); + abc.Trim(L" A"); + EXPECT_EQ(L"BCCB", abc); } -TEST(WideString, TrimRightCopies) { +TEST(WideString, TrimLeft) { + 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); + + 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); + + 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(WideString, TrimLeftCopies) { { // With a single reference, no copy takes place. WideString fred(L" FRED "); const wchar_t* old_buffer = fred.c_str(); - fred.TrimRight(); - EXPECT_EQ(L" FRED", fred); + fred.TrimLeft(); + EXPECT_EQ(L"FRED ", fred); EXPECT_EQ(old_buffer, fred.c_str()); } { @@ -670,8 +704,8 @@ TEST(WideString, TrimRightCopies) { WideString fred(L" FRED "); WideString other_fred = fred; const wchar_t* old_buffer = fred.c_str(); - fred.TrimRight(); - EXPECT_EQ(L" FRED", fred); + fred.TrimLeft(); + EXPECT_EQ(L"FRED ", fred); EXPECT_EQ(L" FRED ", other_fred); EXPECT_NE(old_buffer, fred.c_str()); } @@ -680,48 +714,48 @@ TEST(WideString, TrimRightCopies) { WideString fred(L"FRED"); WideString other_fred = fred; const wchar_t* old_buffer = fred.c_str(); - fred.TrimRight(); + fred.TrimLeft(); EXPECT_EQ(L"FRED", fred); EXPECT_EQ(L"FRED", other_fred); EXPECT_EQ(old_buffer, fred.c_str()); } } -TEST(WideString, TrimLeft) { +TEST(WideString, TrimRight) { 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); + 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); WideString blank(L" "); - blank.TrimLeft(L"ERP"); + blank.TrimRight(L"ERP"); EXPECT_EQ(L" ", blank); - blank.TrimLeft(L'E'); + blank.TrimRight(L'E'); EXPECT_EQ(L" ", blank); - blank.TrimLeft(); + blank.TrimRight(); EXPECT_EQ(L"", blank); WideString empty; - empty.TrimLeft(L"ERP"); + empty.TrimRight(L"ERP"); EXPECT_EQ(L"", empty); - empty.TrimLeft(L'E'); + empty.TrimRight(L'E'); EXPECT_EQ(L"", empty); - empty.TrimLeft(); + empty.TrimRight(); EXPECT_EQ(L"", empty); } -TEST(WideString, TrimLeftCopies) { +TEST(WideString, TrimRightCopies) { { // With a single reference, no copy takes place. WideString fred(L" FRED "); const wchar_t* old_buffer = fred.c_str(); - fred.TrimLeft(); - EXPECT_EQ(L"FRED ", fred); + fred.TrimRight(); + EXPECT_EQ(L" FRED", fred); EXPECT_EQ(old_buffer, fred.c_str()); } { @@ -729,8 +763,8 @@ TEST(WideString, TrimLeftCopies) { WideString fred(L" FRED "); WideString other_fred = fred; const wchar_t* old_buffer = fred.c_str(); - fred.TrimLeft(); - EXPECT_EQ(L"FRED ", fred); + fred.TrimRight(); + EXPECT_EQ(L" FRED", fred); EXPECT_EQ(L" FRED ", other_fred); EXPECT_NE(old_buffer, fred.c_str()); } @@ -739,7 +773,7 @@ TEST(WideString, TrimLeftCopies) { WideString fred(L"FRED"); WideString other_fred = fred; const wchar_t* old_buffer = fred.c_str(); - fred.TrimLeft(); + fred.TrimRight(); EXPECT_EQ(L"FRED", fred); EXPECT_EQ(L"FRED", other_fred); EXPECT_EQ(old_buffer, fred.c_str()); @@ -894,7 +928,7 @@ TEST(WideString, UTF16LE_Encode) { struct UTF16LEEncodeCase { WideString ws; ByteString bs; - } utf16le_encode_cases[] = { + } const utf16le_encode_cases[] = { {L"", ByteString("\0\0", 2)}, {L"abc", ByteString("a\0b\0c\0\0\0", 8)}, {L"abcdef", ByteString("a\0b\0c\0d\0e\0f\0\0\0", 14)}, |