summaryrefslogtreecommitdiff
path: root/core/fxcrt
diff options
context:
space:
mode:
Diffstat (limited to 'core/fxcrt')
-rw-r--r--core/fxcrt/fx_basic_bstring.cpp7
-rw-r--r--core/fxcrt/fx_basic_bstring_unittest.cpp25
-rw-r--r--core/fxcrt/fx_basic_util.cpp2
-rw-r--r--core/fxcrt/fx_basic_wstring.cpp21
-rw-r--r--core/fxcrt/fx_basic_wstring_unittest.cpp25
-rw-r--r--core/fxcrt/include/fx_string.h12
-rw-r--r--core/fxcrt/include/fx_system.h3
7 files changed, 75 insertions, 20 deletions
diff --git a/core/fxcrt/fx_basic_bstring.cpp b/core/fxcrt/fx_basic_bstring.cpp
index cd7b616fbe..18e240bc84 100644
--- a/core/fxcrt/fx_basic_bstring.cpp
+++ b/core/fxcrt/fx_basic_bstring.cpp
@@ -673,11 +673,12 @@ FX_STRSIZE CFX_ByteString::Find(FX_CHAR ch, FX_STRSIZE nStart) const {
if (!m_pData)
return -1;
- if (nStart >= m_pData->m_nDataLength)
+ if (nStart < 0 || nStart >= m_pData->m_nDataLength)
return -1;
- const FX_CHAR* pStr = FXSYS_strchr(m_pData->m_String + nStart, ch);
- return pStr ? (int)(pStr - m_pData->m_String) : -1;
+ const FX_CHAR* pStr = static_cast<const FX_CHAR*>(
+ memchr(m_pData->m_String + nStart, ch, m_pData->m_nDataLength - nStart));
+ return pStr ? pStr - m_pData->m_String : -1;
}
FX_STRSIZE CFX_ByteString::ReverseFind(FX_CHAR ch) const {
diff --git a/core/fxcrt/fx_basic_bstring_unittest.cpp b/core/fxcrt/fx_basic_bstring_unittest.cpp
index 087c264940..fe98b53b45 100644
--- a/core/fxcrt/fx_basic_bstring_unittest.cpp
+++ b/core/fxcrt/fx_basic_bstring_unittest.cpp
@@ -802,6 +802,31 @@ TEST(fxcrt, ByteStringCGetID) {
EXPECT_EQ(0u, longer_string.GetID(-1000000));
}
+TEST(fxcrt, ByteStringCFind) {
+ CFX_ByteStringC null_string;
+ EXPECT_EQ(-1, null_string.Find('a'));
+ EXPECT_EQ(-1, null_string.Find(0));
+
+ CFX_ByteStringC empty_string("");
+ EXPECT_EQ(-1, empty_string.Find('a'));
+ EXPECT_EQ(-1, empty_string.Find(0));
+
+ CFX_ByteStringC single_string("a");
+ EXPECT_EQ(0, single_string.Find('a'));
+ EXPECT_EQ(-1, single_string.Find('b'));
+ EXPECT_EQ(-1, single_string.Find(0));
+
+ CFX_ByteStringC longer_string("abccc");
+ EXPECT_EQ(0, longer_string.Find('a'));
+ EXPECT_EQ(2, longer_string.Find('c'));
+ EXPECT_EQ(-1, longer_string.Find(0));
+
+ CFX_ByteStringC hibyte_string(
+ "ab\x8c"
+ "def");
+ EXPECT_EQ(2, hibyte_string.Find('\x8c'));
+}
+
TEST(fxcrt, ByteStringCMid) {
CFX_ByteStringC null_string;
EXPECT_EQ(null_string, null_string.Mid(0, 1));
diff --git a/core/fxcrt/fx_basic_util.cpp b/core/fxcrt/fx_basic_util.cpp
index 1cf966589d..f69185653f 100644
--- a/core/fxcrt/fx_basic_util.cpp
+++ b/core/fxcrt/fx_basic_util.cpp
@@ -95,7 +95,7 @@ void CFX_PrivateData::ClearAll() {
m_DataList.RemoveAll();
}
void FX_atonum(const CFX_ByteStringC& strc, FX_BOOL& bInteger, void* pData) {
- if (!FXSYS_memchr(strc.raw_str(), '.', strc.GetLength())) {
+ if (strc.Find('.') == -1) {
bInteger = TRUE;
int cc = 0, integer = 0;
const FX_CHAR* str = strc.c_str();
diff --git a/core/fxcrt/fx_basic_wstring.cpp b/core/fxcrt/fx_basic_wstring.cpp
index 354bb5014a..03cb0599d5 100644
--- a/core/fxcrt/fx_basic_wstring.cpp
+++ b/core/fxcrt/fx_basic_wstring.cpp
@@ -638,11 +638,12 @@ FX_STRSIZE CFX_WideString::Find(FX_WCHAR ch, FX_STRSIZE nStart) const {
if (!m_pData)
return -1;
- if (nStart >= m_pData->m_nDataLength)
+ if (nStart < 0 || nStart >= m_pData->m_nDataLength)
return -1;
- const FX_WCHAR* pStr = FXSYS_wcschr(m_pData->m_String + nStart, ch);
- return pStr ? (int)(pStr - m_pData->m_String) : -1;
+ const FX_WCHAR* pStr =
+ wmemchr(m_pData->m_String + nStart, ch, m_pData->m_nDataLength - nStart);
+ return pStr ? pStr - m_pData->m_String : -1;
}
FX_STRSIZE CFX_WideString::Find(const CFX_WideStringC& pSub,
@@ -858,19 +859,13 @@ FX_STRSIZE CFX_WideString::WStringLength(const unsigned short* str) {
}
void CFX_WideString::TrimRight(const CFX_WideStringC& pTargets) {
- if (!m_pData || pTargets.IsEmpty()) {
+ if (IsEmpty() || pTargets.IsEmpty())
return;
- }
+
FX_STRSIZE pos = GetLength();
- if (pos < 1) {
- return;
- }
- while (pos) {
- if (!FXSYS_wcschr(pTargets.c_str(), m_pData->m_String[pos - 1])) {
- break;
- }
+ while (pos && pTargets.Find(m_pData->m_String[pos - 1]) != -1)
pos--;
- }
+
if (pos < m_pData->m_nDataLength) {
ReallocBeforeWrite(m_pData->m_nDataLength);
m_pData->m_String[pos] = 0;
diff --git a/core/fxcrt/fx_basic_wstring_unittest.cpp b/core/fxcrt/fx_basic_wstring_unittest.cpp
index 02281afd36..4b6f4a5800 100644
--- a/core/fxcrt/fx_basic_wstring_unittest.cpp
+++ b/core/fxcrt/fx_basic_wstring_unittest.cpp
@@ -831,6 +831,31 @@ TEST(fxcrt, WideStringCOperatorNE) {
EXPECT_TRUE(c_string3 != wide_string_c);
}
+TEST(fxcrt, WideStringCFind) {
+ CFX_WideStringC null_string;
+ EXPECT_EQ(-1, null_string.Find(L'a'));
+ EXPECT_EQ(-1, null_string.Find(0));
+
+ CFX_WideStringC empty_string(L"");
+ EXPECT_EQ(-1, empty_string.Find(L'a'));
+ EXPECT_EQ(-1, empty_string.Find(0));
+
+ CFX_WideStringC single_string(L"a");
+ EXPECT_EQ(0, single_string.Find(L'a'));
+ EXPECT_EQ(-1, single_string.Find(L'b'));
+ EXPECT_EQ(-1, single_string.Find(0));
+
+ CFX_WideStringC longer_string(L"abccc");
+ EXPECT_EQ(0, longer_string.Find(L'a'));
+ EXPECT_EQ(2, longer_string.Find(L'c'));
+ EXPECT_EQ(-1, longer_string.Find(0));
+
+ CFX_WideStringC hibyte_string(
+ L"ab\xff08"
+ L"def");
+ EXPECT_EQ(2, hibyte_string.Find(L'\xff08'));
+}
+
TEST(fxcrt, WideStringFormatWidth) {
{
CFX_WideString str;
diff --git a/core/fxcrt/include/fx_string.h b/core/fxcrt/include/fx_string.h
index 66a8039a8d..fbfb91f759 100644
--- a/core/fxcrt/include/fx_string.h
+++ b/core/fxcrt/include/fx_string.h
@@ -100,6 +100,12 @@ class CFX_ByteStringC {
uint8_t GetAt(FX_STRSIZE index) const { return m_Ptr[index]; }
+ FX_STRSIZE Find(FX_CHAR ch) const {
+ const uint8_t* found =
+ static_cast<const uint8_t*>(memchr(m_Ptr, ch, m_Length));
+ return found ? found - m_Ptr : -1;
+ }
+
CFX_ByteStringC Mid(FX_STRSIZE index, FX_STRSIZE count = -1) const {
if (index < 0) {
index = 0;
@@ -417,6 +423,12 @@ class CFX_WideStringC {
return CFX_WideStringC(m_Ptr, count);
}
+ FX_STRSIZE Find(FX_WCHAR ch) const {
+ const FX_WCHAR* found =
+ static_cast<const FX_WCHAR*>(wmemchr(m_Ptr, ch, m_Length));
+ return found ? found - m_Ptr : -1;
+ }
+
CFX_WideStringC Mid(FX_STRSIZE index, FX_STRSIZE count = -1) const {
if (index < 0) {
index = 0;
diff --git a/core/fxcrt/include/fx_system.h b/core/fxcrt/include/fx_system.h
index 795a0c2794..b4659d14e4 100644
--- a/core/fxcrt/include/fx_system.h
+++ b/core/fxcrt/include/fx_system.h
@@ -123,7 +123,6 @@ void FXSYS_vsnprintf(char* str, size_t size, const char* fmt, va_list ap);
#define FXSYS_sprintf DO_NOT_USE_SPRINTF_DIE_DIE_DIE
#define FXSYS_vsprintf DO_NOT_USE_VSPRINTF_DIE_DIE_DIE
-#define FXSYS_strchr strchr
#define FXSYS_strncmp strncmp
#define FXSYS_strcmp strcmp
#define FXSYS_strcpy strcpy
@@ -166,7 +165,6 @@ extern "C" {
#endif
#define FXSYS_wcscmp wcscmp
-#define FXSYS_wcschr wcschr
#define FXSYS_wcsstr wcsstr
#define FXSYS_wcsncmp wcsncmp
#define FXSYS_vswprintf vswprintf
@@ -176,7 +174,6 @@ extern "C" {
#define FXSYS_memcpy memcpy
#define FXSYS_memmove memmove
#define FXSYS_memset memset
-#define FXSYS_memchr memchr
#define FXSYS_qsort qsort
#define FXSYS_bsearch bsearch