From f7fe678a4ada859a2e4fbbeeb0b1dff5b5887227 Mon Sep 17 00:00:00 2001 From: tsepez Date: Wed, 11 May 2016 17:35:35 -0700 Subject: Add much-needed Find() method for CFX_*StringC BUG=pdfium:493 Review-Url: https://codereview.chromium.org/1968233002 --- core/fpdfapi/fpdf_page/fpdf_page_parser.cpp | 8 ++--- core/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp | 7 ++-- core/fpdftext/fpdf_text_int.cpp | 4 +-- core/fxcrt/fx_basic_bstring.cpp | 7 ++-- core/fxcrt/fx_basic_bstring_unittest.cpp | 25 ++++++++++++++ core/fxcrt/fx_basic_util.cpp | 2 +- core/fxcrt/fx_basic_wstring.cpp | 21 +++++------- core/fxcrt/fx_basic_wstring_unittest.cpp | 25 ++++++++++++++ core/fxcrt/include/fx_string.h | 12 +++++++ core/fxcrt/include/fx_system.h | 3 -- xfa/fgas/localization/fgas_locale.cpp | 42 +++++++++++------------- 11 files changed, 103 insertions(+), 53 deletions(-) diff --git a/core/fpdfapi/fpdf_page/fpdf_page_parser.cpp b/core/fpdfapi/fpdf_page/fpdf_page_parser.cpp index a13fb5021d..da1355724f 100644 --- a/core/fpdfapi/fpdf_page/fpdf_page_parser.cpp +++ b/core/fpdfapi/fpdf_page/fpdf_page_parser.cpp @@ -271,18 +271,18 @@ int CPDF_StreamContentParser::GetNextParamPos() { } void CPDF_StreamContentParser::AddNameParam(const FX_CHAR* name, int len) { + CFX_ByteStringC bsName(name, len); int index = GetNextParamPos(); if (len > 32) { m_ParamBuf[index].m_Type = ContentParam::OBJECT; - m_ParamBuf[index].m_pObject = - new CPDF_Name(PDF_NameDecode(CFX_ByteStringC(name, len))); + m_ParamBuf[index].m_pObject = new CPDF_Name(PDF_NameDecode(bsName)); } else { m_ParamBuf[index].m_Type = ContentParam::NAME; - if (!FXSYS_memchr(name, '#', len)) { + if (bsName.Find('#') == -1) { FXSYS_memcpy(m_ParamBuf[index].m_Name.m_Buffer, name, len); m_ParamBuf[index].m_Name.m_Len = len; } else { - CFX_ByteString str = PDF_NameDecode(CFX_ByteStringC(name, len)); + CFX_ByteString str = PDF_NameDecode(bsName); FXSYS_memcpy(m_ParamBuf[index].m_Name.m_Buffer, str.c_str(), str.GetLength()); m_ParamBuf[index].m_Name.m_Len = str.GetLength(); diff --git a/core/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp b/core/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp index 466ccaffa5..03975149b0 100644 --- a/core/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp +++ b/core/fpdfapi/fpdf_parser/fpdf_parser_utility.cpp @@ -89,9 +89,9 @@ int32_t GetDirectInteger(CPDF_Dictionary* pDict, const CFX_ByteStringC& key) { CFX_ByteString PDF_NameDecode(const CFX_ByteStringC& bstr) { int size = bstr.GetLength(); const FX_CHAR* pSrc = bstr.c_str(); - if (!FXSYS_memchr(pSrc, '#', size)) { + if (bstr.Find('#') == -1) return bstr; - } + CFX_ByteString result; FX_CHAR* pDestStart = result.GetBuffer(size); FX_CHAR* pDest = pDestStart; @@ -109,9 +109,8 @@ CFX_ByteString PDF_NameDecode(const CFX_ByteStringC& bstr) { } CFX_ByteString PDF_NameDecode(const CFX_ByteString& orig) { - if (!FXSYS_memchr(orig.c_str(), '#', orig.GetLength())) { + if (orig.Find('#') == -1) return orig; - } return PDF_NameDecode(orig.AsStringC()); } diff --git a/core/fpdftext/fpdf_text_int.cpp b/core/fpdftext/fpdf_text_int.cpp index 8e5e75853e..f843da3061 100644 --- a/core/fpdftext/fpdf_text_int.cpp +++ b/core/fpdftext/fpdf_text_int.cpp @@ -2147,7 +2147,7 @@ FX_BOOL CPDF_TextPageFind::ExtractSubString(CFX_WideString& rString, return FALSE; } while (iSubString--) { - lpszFullString = FXSYS_wcschr(lpszFullString, chSep); + lpszFullString = wcschr(lpszFullString, chSep); if (!lpszFullString) { rString.clear(); return FALSE; @@ -2157,7 +2157,7 @@ FX_BOOL CPDF_TextPageFind::ExtractSubString(CFX_WideString& rString, lpszFullString++; } } - const FX_WCHAR* lpchEnd = FXSYS_wcschr(lpszFullString, chSep); + const FX_WCHAR* lpchEnd = wcschr(lpszFullString, chSep); int nLen = lpchEnd ? (int)(lpchEnd - lpszFullString) : (int)FXSYS_wcslen(lpszFullString); ASSERT(nLen >= 0); 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( + 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(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(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 diff --git a/xfa/fgas/localization/fgas_locale.cpp b/xfa/fgas/localization/fgas_locale.cpp index 521b53536e..2336b20ee3 100644 --- a/xfa/fgas/localization/fgas_locale.cpp +++ b/xfa/fgas/localization/fgas_locale.cpp @@ -60,21 +60,11 @@ static const FX_WCHAR gs_wsTimeSymbols[] = L"hHkKMSFAzZ"; static const FX_WCHAR gs_wsDateSymbols[] = L"DJMEeGgYwW"; static const FX_WCHAR gs_wsConstChars[] = L",-:/. "; -static FX_STRSIZE FX_Local_Find(const CFX_WideStringC& wsSymbols, - FX_WCHAR ch, - FX_STRSIZE nStart = 0) { - FX_STRSIZE nLength = wsSymbols.GetLength(); - if (nLength < 1 || nStart > nLength) { - return -1; - } - const FX_WCHAR* lpsz = - (const FX_WCHAR*)FXSYS_wcschr(wsSymbols.c_str() + nStart, ch); - return (lpsz == NULL) ? -1 : (FX_STRSIZE)(lpsz - wsSymbols.c_str()); -} static const FX_WCHAR* const gs_LocalNumberSymbols[] = { L"decimal", L"grouping", L"percent", L"minus", L"zero", L"currencySymbol", L"currencyName", }; + IFX_Locale* IFX_Locale::Create(CXML_Element* pLocaleData) { return new CFX_Locale(pLocaleData); } @@ -575,10 +565,11 @@ FX_LOCALECATEGORY CFX_FormatString::GetCategory( int32_t iLenf = wsPattern.GetLength(); const FX_WCHAR* pStr = wsPattern.c_str(); FX_BOOL bBraceOpen = FALSE; + CFX_WideStringC wsConstChars(gs_wsConstChars); while (ccf < iLenf) { if (pStr[ccf] == '\'') { FX_GetLiteralText(pStr, ccf, iLenf); - } else if (!bBraceOpen && FX_Local_Find(gs_wsConstChars, pStr[ccf]) < 0) { + } else if (!bBraceOpen && wsConstChars.Find(pStr[ccf]) == -1) { CFX_WideString wsCategory(pStr[ccf]); ccf++; while (TRUE) { @@ -662,12 +653,13 @@ IFX_Locale* CFX_FormatString::GetTextFormat(const CFX_WideString& wsPattern, int32_t iLenf = wsPattern.GetLength(); const FX_WCHAR* pStr = wsPattern.c_str(); FX_BOOL bBrackOpen = FALSE; + CFX_WideStringC wsConstChars(gs_wsConstChars); while (ccf < iLenf) { if (pStr[ccf] == '\'') { int32_t iCurChar = ccf; FX_GetLiteralText(pStr, ccf, iLenf); wsPurgePattern += CFX_WideStringC(pStr + iCurChar, ccf - iCurChar + 1); - } else if (!bBrackOpen && FX_Local_Find(gs_wsConstChars, pStr[ccf]) < 0) { + } else if (!bBrackOpen && wsConstChars.Find(pStr[ccf]) == -1) { CFX_WideString wsSearchCategory(pStr[ccf]); ccf++; while (ccf < iLenf && pStr[ccf] != '{' && pStr[ccf] != '.' && @@ -719,12 +711,13 @@ IFX_Locale* CFX_FormatString::GetNumericFormat(const CFX_WideString& wsPattern, const FX_WCHAR* pStr = wsPattern.c_str(); FX_BOOL bFindDot = FALSE; FX_BOOL bBrackOpen = FALSE; + CFX_WideStringC wsConstChars(gs_wsConstChars); while (ccf < iLenf) { if (pStr[ccf] == '\'') { int32_t iCurChar = ccf; FX_GetLiteralText(pStr, ccf, iLenf); wsPurgePattern += CFX_WideStringC(pStr + iCurChar, ccf - iCurChar + 1); - } else if (!bBrackOpen && FX_Local_Find(gs_wsConstChars, pStr[ccf]) < 0) { + } else if (!bBrackOpen && wsConstChars.Find(pStr[ccf]) == -1) { CFX_WideString wsCategory(pStr[ccf]); ccf++; while (ccf < iLenf && pStr[ccf] != '{' && pStr[ccf] != '.' && @@ -2153,13 +2146,14 @@ FX_DATETIMETYPE CFX_FormatString::GetDateTimeFormat( const FX_WCHAR* pStr = wsPattern.c_str(); int32_t iFindCategory = 0; FX_BOOL bBraceOpen = FALSE; + CFX_WideStringC wsConstChars(gs_wsConstChars); while (ccf < iLenf) { if (pStr[ccf] == '\'') { int32_t iCurChar = ccf; FX_GetLiteralText(pStr, ccf, iLenf); wsTempPattern += CFX_WideStringC(pStr + iCurChar, ccf - iCurChar + 1); } else if (!bBraceOpen && iFindCategory != 3 && - FX_Local_Find(gs_wsConstChars, pStr[ccf]) < 0) { + wsConstChars.Find(pStr[ccf]) == -1) { CFX_WideString wsCategory(pStr[ccf]); ccf++; while (ccf < iLenf && pStr[ccf] != '{' && pStr[ccf] != '.' && @@ -2290,6 +2284,7 @@ static FX_BOOL FX_ParseLocaleDate(const CFX_WideString& wsDate, int32_t len = wsDate.GetLength(); const FX_WCHAR* strf = wsDatePattern.c_str(); int32_t lenf = wsDatePattern.GetLength(); + CFX_WideStringC wsDateSymbols(gs_wsDateSymbols); while (cc < len && ccf < lenf) { if (strf[ccf] == '\'') { CFX_WideString wsLiteral = FX_GetLiteralText(strf, ccf, lenf); @@ -2301,10 +2296,9 @@ static FX_BOOL FX_ParseLocaleDate(const CFX_WideString& wsDate, cc += iLiteralLen; ccf++; continue; - } else if (FX_Local_Find(gs_wsDateSymbols, strf[ccf]) < 0) { - if (strf[ccf] != str[cc]) { + } else if (wsDateSymbols.Find(strf[ccf]) == -1) { + if (strf[ccf] != str[cc]) return FALSE; - } cc++; ccf++; continue; @@ -2509,6 +2503,7 @@ static FX_BOOL FX_ParseLocaleTime(const CFX_WideString& wsTime, int lenf = wsTimePattern.GetLength(); FX_BOOL bHasA = FALSE; FX_BOOL bPM = FALSE; + CFX_WideStringC wsTimeSymbols(gs_wsTimeSymbols); while (cc < len && ccf < lenf) { if (strf[ccf] == '\'') { CFX_WideString wsLiteral = FX_GetLiteralText(strf, ccf, lenf); @@ -2520,10 +2515,9 @@ static FX_BOOL FX_ParseLocaleTime(const CFX_WideString& wsTime, cc += iLiteralLen; ccf++; continue; - } else if (FX_Local_Find(gs_wsTimeSymbols, strf[ccf]) == -1) { - if (strf[ccf] != str[cc]) { + } else if (wsTimeSymbols.Find(strf[ccf]) == -1) { + if (strf[ccf] != str[cc]) return FALSE; - } cc++; ccf++; continue; @@ -3947,12 +3941,13 @@ static FX_BOOL FX_DateFormat(const CFX_WideString& wsDatePattern, int32_t ccf = 0; const FX_WCHAR* strf = wsDatePattern.c_str(); int32_t lenf = wsDatePattern.GetLength(); + CFX_WideStringC wsDateSymbols(gs_wsDateSymbols); while (ccf < lenf) { if (strf[ccf] == '\'') { wsResult += FX_GetLiteralText(strf, ccf, lenf); ccf++; continue; - } else if (FX_Local_Find(gs_wsDateSymbols, strf[ccf]) < 0) { + } else if (wsDateSymbols.Find(strf[ccf]) == -1) { wsResult += strf[ccf++]; continue; } @@ -4073,12 +4068,13 @@ static FX_BOOL FX_TimeFormat(const CFX_WideString& wsTimePattern, bPM = TRUE; } } + CFX_WideStringC wsTimeSymbols(gs_wsTimeSymbols); while (ccf < lenf) { if (strf[ccf] == '\'') { wsResult += FX_GetLiteralText(strf, ccf, lenf); ccf++; continue; - } else if (FX_Local_Find(gs_wsTimeSymbols, strf[ccf]) < 0) { + } else if (wsTimeSymbols.Find(strf[ccf]) == -1) { wsResult += strf[ccf++]; continue; } -- cgit v1.2.3