From 5d4d00fe40fde5de6e70d8bd6c540a11c536d8cf Mon Sep 17 00:00:00 2001 From: rkotten Date: Tue, 26 Sep 2023 11:35:37 +0200 Subject: [PATCH 1/3] +rfc: Some refactoring regarding string comparison --- src/Config/Config.cpp | 6 ++---- src/Edit.c | 17 ++++++++--------- src/EncodingDetection.cpp | 3 +-- src/Helpers.h | 34 ++++++++++------------------------ src/MuiLanguage.c | 4 ++-- src/Notepad3.c | 10 +++++----- src/Styles.c | 3 +-- 7 files changed, 29 insertions(+), 48 deletions(-) diff --git a/src/Config/Config.cpp b/src/Config/Config.cpp index 2c3c59041..cd15e93e4 100644 --- a/src/Config/Config.cpp +++ b/src/Config/Config.cpp @@ -401,7 +401,7 @@ static inline void popCheckStackHead(LPCSTR fctname) UNREFERENCED_PARAMETER(fctname); // NDEBUG mode iniOpen_t* pOpenBy = NULL; STACK_POP(s_pOpenStackHead, pOpenBy); - assert(StringCchCompareXA(fctname, pOpenBy->fctname) == 0); + assert(StrCmpA(fctname, pOpenBy->fctname) == 0); FreeMem(pOpenBy); } @@ -589,12 +589,10 @@ extern "C" bool IniSectionClear(LPCWSTR lpSectionName, bool bRemoveEmpty) extern "C" bool IniClearAllSections(LPCWSTR lpPrefix, bool bRemoveEmpty) { - size_t const len = StringCchLen(lpPrefix, 0); - CSimpleIni::TNamesDepend Sections; s_INI.GetAllSections(Sections); for (const auto& section : Sections) { - if (StringCchCompareNI(section.pItem, len, lpPrefix, len) == 0) { + if (StrCmpIW(section.pItem, lpPrefix) == 0) { IniSectionClear(section.pItem, bRemoveEmpty); } } diff --git a/src/Edit.c b/src/Edit.c index dd50ce788..421ffcebf 100644 --- a/src/Edit.c +++ b/src/Edit.c @@ -3797,8 +3797,8 @@ void EditToggleLineCommentsSimple(LPCWSTR pwszComment, bool bInsertAtStart, LnCm } const char* tchBuf = SciCall_GetRangePointer(iIndentPos, cchComment + 1); - bool const bHasLnCmnt = (StrCmpNA(tchBuf, mszComment, (int)cchComment) == 0); - bool const bHasPrefix = (StrCmpNA(tchBuf, mszPrefix, (int)cchPrefix) == 0); + bool const bHasLnCmnt = IsSameCharSequence(tchBuf, mszComment, cchComment); + bool const bHasPrefix = IsSameCharSequence(tchBuf, mszPrefix, (int)cchPrefix); int iAction = (mode == LNC_TOGGLE) ? ((bHasLnCmnt || bHasPrefix) ? LNC_REMOVE : LNC_ADD) : mode; switch (iAction) { @@ -3918,7 +3918,7 @@ void EditToggleLineCommentsExtended(LPCWSTR pwszComment, bool bInsertAtStart) } const char* tchBuf = SciCall_GetRangePointer(iIndentPos, cchComment + 1); - if (StrCmpNIA(tchBuf, mszComment, (int)cchComment) == 0) { + if (IsSameCharSequence(tchBuf, mszComment, cchComment)) { // remove comment chars DocPos const iSelPos = iIndentPos + cchComment; switch (iAction) { @@ -4615,9 +4615,8 @@ void EditRemoveDuplicateLines(HWND hwnd, bool bRemoveEmptyLines) if (bRemoveEmptyLines || (iCmpLnLen > 0)) { DocPos const iBegCmpLine = SciCall_PositionFromLine(iCompareLine); const char* const pCompareLine = SciCall_GetRangePointer(iBegCmpLine, iCmpLnLen + 1); - if (iCurLnLen == iCmpLnLen) { - if (StringCchCompareNA(pCurrentLine, iCurLnLen, pCompareLine, iCmpLnLen) == 0) { + if (IsSameCharSequence(pCurrentLine, pCompareLine, iCmpLnLen)) { SciCall_SetTargetRange(SciCall_GetLineEndPosition(iPrevLine), SciCall_GetLineEndPosition(iCompareLine)); SciCall_ReplaceTarget(0, ""); --iCompareLine; // proactive preventing progress to avoid comparison line skip @@ -5879,7 +5878,7 @@ static LPCWSTR _EditGetFindStrg(HWND hwnd, const LPEDITFINDREPLACE lpefr, bool b static char* _GetReplaceString(HWND hwnd, CLPCEDITFINDREPLACE lpefr, int* iReplaceMsg) { char* pszReplace = NULL; // replace text of arbitrary size - if (Settings.ReplaceByClipboardTag && (StringCchCompareXW(StrgGet(lpefr->chReplaceTemplate), L"^c")) == 0) + if (Settings.ReplaceByClipboardTag && (StrCmpW(StrgGet(lpefr->chReplaceTemplate), L"^c")) == 0) { *iReplaceMsg = SciCall_GetChangeHistory() ? SCI_REPLACETARGETMINIMAL : SCI_REPLACETARGET; pszReplace = EditGetClipboardText(hwnd, true, NULL, NULL); @@ -7878,11 +7877,11 @@ typedef struct WLIST { static int wordcmp(PWLIST a, PWLIST b) { - return StringCchCompareXA(a->word, b->word); + return StrCmpA(a->word, b->word); } static int wordcmpi(PWLIST a, PWLIST b) { - return StringCchCompareXIA(a->word, b->word); + return StrCmpIA(a->word, b->word); } // ---------------------------------------------- @@ -8069,7 +8068,7 @@ bool EditAutoCompleteWord(HWND hwnd, bool autoInsert) if (iNumWords) { - //if ((iNumWords == 1) && (StringCchCompareXIA(pRoot, pListHead->word) == 0)) { + //if ((iNumWords == 1) && (StrCmpIA(pRoot, pListHead->word) == 0)) { // return true; //} diff --git a/src/EncodingDetection.cpp b/src/EncodingDetection.cpp index f3180e986..12769f660 100644 --- a/src/EncodingDetection.cpp +++ b/src/EncodingDetection.cpp @@ -1212,8 +1212,7 @@ extern "C" bool FileVars_ParseStr(const char* pszData, const char* pszName, char extern "C" bool FileVars_IsUTF8(LPFILEVARS lpfv) { if (lpfv->mask & FV_ENCODING) { - if (StringCchCompareNIA(lpfv->chEncoding, COUNTOF(lpfv->chEncoding), "utf-8", CONSTSTRGLEN("utf-8")) == 0 || - StringCchCompareNIA(lpfv->chEncoding, COUNTOF(lpfv->chEncoding), "utf8", CONSTSTRGLEN("utf8")) == 0) { + if (StrCmpIA(lpfv->chEncoding, "utf-8") == 0 || StrCmpIA(lpfv->chEncoding, "utf8") == 0) { return true; } } diff --git a/src/Helpers.h b/src/Helpers.h index cccf43ba4..b6b7dc126 100644 --- a/src/Helpers.h +++ b/src/Helpers.h @@ -731,33 +731,19 @@ static inline void StrReplChrA(CHAR* pStrg, const CHAR chSearch, const CHAR chRe //==== StrSafe lstrcmp(),lstrcmpi() ============================================= -// NOTE: !!! differences in AutoCompleteList depending compare functions (CRT vs. Shlwapi)) !!! -#define StringCchCompareNW(s1, l1, s2, l2) StrCmpNW((s1), (s2), min_i((int)(l1), (int)(l2))) -#define StringCchCompareXW(s1, s2) StrCmpW((s1), (s2)) - -#define StringCchCompareNIW(s1, l1, s2, l2) StrCmpNIW((s1), (s2), min_i((int)(l1), (int)(l2))) -#define StringCchCompareXIW(s1, s2) StrCmpIW((s1), (s2)) - -#define StringCchCompareNA(s1, l1, s2, l2) StrCmpNA((s1), (s2), min_i((int)(l1), (int)(l2))) -// #define StringCchCompareNA(s1,l1,s2,l2) strncmp((s1),(s2),min_s((l1),(l2))) -#define StringCchCompareXA(s1, s2) StrCmpA((s1), (s2)) -// #define StringCchCompareXA(s1,s2) strcmp((s1),(s2)) - -#define StringCchCompareNIA(s1, l1, s2, l2) StrCmpNIA((s1), (s2), min_i((int)(l1), (int)(l2))) -// #define StringCchCompareNIA(s1,l1,s2,l2) _strnicmp((s1),(s2),min_s((l1),(l2))) -#define StringCchCompareXIA(s1, s2) StrCmpIA((s1), (s2)) -// #define StringCchCompareXIA(s1,s2) _stricmp((s1),(s2)) +__forceinline bool IsSameCharSequence(const char* pSrc, const char* pCmp, CONST DocPos len) { + DocPos i = 0; + for (i = 0; (i < len) && (pSrc[i] == pCmp[i]); ++i) {} + return (i == len); +} +// NOTE: !!! differences in AutoCompleteList depending compare functions (CRT (lstrcmp(),lstrcmpi()) vs. Shlwapi)) !!! #if defined(UNICODE) || defined(_UNICODE) -#define StringCchCompareN(s1,l1,s2,l2) StringCchCompareNW((s1),(l1),(s2),(l2)) -#define StringCchCompareX(s1,s2) StringCchCompareXW((s1),(s2)) -#define StringCchCompareNI(s1,l1,s2,l2) StringCchCompareNIW((s1),(l1),(s2),(l2)) -#define StringCchCompareXI(s1,s2) StringCchCompareXIW((s1),(s2)) +#define StringCchCompareX(s1, s2) StrCmpW((s1), (s2)) +#define StringCchCompareXI(s1, s2) StrCmpIW((s1), (s2)) #else -#define StringCchCompareN(s1,l1,s2,l2) StringCchCompareNA((s1),(l1),(s2),(l2)) -#define StringCchCompareX(s1,s2) StringCchCompareXA((s1),(s2)) -#define StringCchCompareNI(s1,l1,s2,l2) StringCchCompareNIA((s1),(l1),(s2),(l2)) -#define StringCchCompareXI(s1,s2) StringCchCompareXIA((s1),(s2)) +#define StringCchCompareX(s1, s2) StrCmpA((s1), (s2)) +#define StringCchCompareXI(s1, s2) StrCmpIA((s1), (s2)) #endif diff --git a/src/MuiLanguage.c b/src/MuiLanguage.c index e5083cdfa..eaed524ea 100644 --- a/src/MuiLanguage.c +++ b/src/MuiLanguage.c @@ -187,7 +187,7 @@ void SetMuiLanguage(const unsigned muiLngIndex) { StringCchCopyW(Settings2.PreferredLanguageLocaleName, COUNTOF(Settings2.PreferredLanguageLocaleName), pLocaleName); if (Globals.bCanSaveIniFile) { - if (StringCchCompareXIW(Settings2.PreferredLanguageLocaleName, Default_PreferredLanguageLocaleName) != 0) { + if (StrCmpIW(Settings2.PreferredLanguageLocaleName, Default_PreferredLanguageLocaleName) != 0) { IniFileSetString(Paths.IniFile, Constants.Settings2_Section, SettingName, Settings2.PreferredLanguageLocaleName); } else { IniFileDelete(Paths.IniFile, Constants.Settings2_Section, SettingName, false); @@ -349,7 +349,7 @@ unsigned LoadLanguageResources(LPCWSTR pLocaleName) { unsigned iLngIndex = MuiLanguages_CountOf(); WCHAR tchAvailLngs[2 * (LOCALE_NAME_MAX_LENGTH + 1)] = { L'\0' }; for (unsigned lng = 0; lng < MuiLanguages_CountOf(); ++lng) { - if (StringCchCompareXIW(MUI_LanguageDLLs[lng].LocaleName, pLocaleName) == 0) { + if (StrCmpIW(MUI_LanguageDLLs[lng].LocaleName, pLocaleName) == 0) { if (MUI_LanguageDLLs[lng].bHasDLL && (lng > 0)) { StringCchCatW(tchAvailLngs, COUNTOF(tchAvailLngs), MUI_LanguageDLLs[lng].LocaleName); StringCchCatW(tchAvailLngs, COUNTOF(tchAvailLngs), L";"); diff --git a/src/Notepad3.c b/src/Notepad3.c index d04d2e8a5..bbd9a1d3b 100644 --- a/src/Notepad3.c +++ b/src/Notepad3.c @@ -1593,7 +1593,7 @@ static BOOL CALLBACK _EnumWndProc(HWND hwnd, LPARAM lParam) if (GetClassName(hwnd, szClassName, COUNTOF(szClassName))) { - if (StringCchCompareNIW(szClassName, COUNTOF(szClassName), s_wchWndClass, COUNTOF(s_wchWndClass)) == 0) { + if (StrCmpW(szClassName, s_wchWndClass) == 0) { UINT const iReuseLock = GetDlgItemInt(hwnd, IDC_REUSELOCK, NULL, FALSE); if ((GetTicks_ms() - iReuseLock) >= REUSEWINDOWLOCKTIMEOUT) { @@ -1621,7 +1621,7 @@ static BOOL CALLBACK _EnumWndProc2(HWND hwnd, LPARAM lParam) if (GetClassName(hwnd, szClassName, COUNTOF(szClassName))) { - if (StringCchCompareNIW(szClassName, COUNTOF(szClassName), s_wchWndClass, COUNTOF(s_wchWndClass)) == 0) { + if (StrCmpW(szClassName, s_wchWndClass) == 0) { UINT const iReuseLock = GetDlgItemInt(hwnd, IDC_REUSELOCK, NULL, FALSE); if ((GetTicks_ms() - iReuseLock) >= REUSEWINDOWLOCKTIMEOUT) { @@ -8456,7 +8456,7 @@ static void _HandleAutoCloseTags() bool isNonClosingTag = false; for (int i = 0; ((i < cntCount) && !isNonClosingTag); ++i) { - isNonClosingTag = (StringCchCompareXIA(replaceBuf, nonClosingTags[i]) == 0); + isNonClosingTag = (StrCmpIA(replaceBuf, nonClosingTags[i]) == 0); } if ((cchIns > 3) && !isNonClosingTag) { EditReplaceSelection(replaceBuf, false); @@ -11389,7 +11389,7 @@ static inline bool IsFileVarLogFile() if (SciCall_GetTextLength() >= 4) { char tch[5] = { '\0', '\0', '\0', '\0', '\0' }; SciCall_GetText(COUNTOF(tch) - 1, tch); - return (StringCchCompareXA(tch, ".LOG") == 0); + return (StrCmpA(tch, ".LOG") == 0); } return false; } @@ -12131,7 +12131,7 @@ static BOOL CALLBACK _EnumWndCountProc(HWND hwnd, LPARAM lParam) { WCHAR szClassName[64] = { L'\0' }; if (GetClassName(hwnd, szClassName, COUNTOF(szClassName))) { - if (StringCchCompareNIW(szClassName, COUNTOF(szClassName), s_wchWndClass, COUNTOF(s_wchWndClass)) == 0) { + if (StrCmpW(szClassName, s_wchWndClass) == 0) { *(int*)lParam += 1; } } diff --git a/src/Styles.c b/src/Styles.c index 674828c4d..7df0ed512 100644 --- a/src/Styles.c +++ b/src/Styles.c @@ -2539,8 +2539,7 @@ bool Style_SetLexerFromFile(HWND hwnd, const HPATHL hpath) WCHAR wchMode[MICRO_BUFFER] = { L'\0' }; MultiByteToWideCharEx(Encoding_SciCP, 0, Globals.fvCurFile.chMode, -1, wchMode, MICRO_BUFFER); - if (!Flags.NoCGIGuess && (StringCchCompareNI(wchMode,COUNTOF(wchMode),L"cgi", CONSTSTRGLEN(L"cgi")) == 0 || - StringCchCompareNI(wchMode,COUNTOF(wchMode),L"fcgi", CONSTSTRGLEN(L"fcgi")) == 0)) { + if (!Flags.NoCGIGuess && (StrCmpIW(wchMode, L"cgi") == 0 || StrCmpIW(wchMode, L"fcgi") == 0)) { char tchText[256] = { '\0' }; SciCall_GetText(COUNTOF(tchText) - 1, tchText); StrTrimA(tchText," \t\n\r"); From c63cd705b739242f5a2ef7105f0a84560666c416 Mon Sep 17 00:00:00 2001 From: rkotten Date: Tue, 26 Sep 2023 12:03:49 +0200 Subject: [PATCH 2/3] +upd: Lexilla v5.2.7 --- lexilla/cppcheck.suppress | 18 +++++++++++------- lexilla/doc/Lexilla.html | 8 ++++---- lexilla/doc/LexillaDownload.html | 10 +++++----- lexilla/doc/LexillaHistory.html | 15 ++++++++++++++- lexilla/lexers/LexR.cxx | 3 ++- lexilla/src/Lexilla/Info.plist | 2 +- .../Lexilla/Lexilla.xcodeproj/project.pbxproj | 4 ++-- lexilla/src/LexillaVersion.rc | 4 ++-- lexilla/version.txt | 2 +- 9 files changed, 42 insertions(+), 24 deletions(-) diff --git a/lexilla/cppcheck.suppress b/lexilla/cppcheck.suppress index 9e1f8ad8e..6b7aa14ba 100644 --- a/lexilla/cppcheck.suppress +++ b/lexilla/cppcheck.suppress @@ -1,6 +1,6 @@ // File to suppress cppcheck warnings for files that will not be fixed. // Does not suppress warnings where an additional occurrence of the warning may be of interest. -// Configured for cppcheck 2.11 +// Configured for cppcheck 2.12 // Coding style is to use assignments in constructor when there are many // members to initialize or the initialization is complex or has comments. @@ -35,8 +35,8 @@ checkLevelNormal:lexilla/lexers/LexHTML.cxx checkLevelNormal:lexilla/lexers/LexPerl.cxx checkLevelNormal:lexilla/lexers/LexRuby.cxx -// Cppcheck wrongly assumes string_view::remove_prefix can not empty the view -knownConditionTrueFalse:lexilla/test/TestLexers.cxx +// Physically but not logically const. +constVariablePointer:lexilla/examples/CheckLexilla/CheckLexilla.c // Suppress most lexer warnings since the lexers are maintained by others redundantCondition:lexilla/lexers/LexA68k.cxx @@ -62,13 +62,14 @@ constParameterPointer:lexilla/lexers/LexCoffeeScript.cxx knownConditionTrueFalse:lexilla/lexers/LexCoffeeScript.cxx constParameterReference:lexilla/lexers/LexCPP.cxx variableScope:lexilla/lexers/LexCSS.cxx +knownConditionTrueFalse:lexilla/lexers/LexDataflex.cxx variableScope:lexilla/lexers/LexDataflex.cxx knownConditionTrueFalse:lexilla/lexers/LexECL.cxx variableScope:lexilla/lexers/LexECL.cxx +constParameter:lexilla/lexers/LexEDIFACT.cxx constParameterPointer:lexilla/lexers/LexEDIFACT.cxx knownConditionTrueFalse:lexilla/lexers/LexEiffel.cxx variableScope:lexilla/lexers/LexErlang.cxx -knownConditionTrueFalse:lexilla/lexers/LexErrorList.cxx knownConditionTrueFalse:lexilla/lexers/LexEScript.cxx constParameter:lexilla/lexers/LexFortran.cxx constParameterReference:lexilla/lexers/LexFortran.cxx @@ -82,14 +83,17 @@ knownConditionTrueFalse:lexilla/lexers/LexHex.cxx constParameterReference:lexilla/lexers/LexHTML.cxx constVariable:lexilla/lexers/LexHollywood.cxx variableScope:lexilla/lexers/LexInno.cxx +constParameterPointer:lexilla/lexers/LexJulia.cxx constParameterReference:lexilla/lexers/LexJulia.cxx knownConditionTrueFalse:lexilla/lexers/LexJulia.cxx unreadVariable:lexilla/lexers/LexJulia.cxx variableScope:lexilla/lexers/LexJulia.cxx variableScope:lexilla/lexers/LexLaTeX.cxx constParameterReference:lexilla/lexers/LexLaTeX.cxx +constParameterPointer:lexilla/lexers/LexMagik.cxx constParameterReference:lexilla/lexers/LexMagik.cxx constParameterReference:lexilla/lexers/LexMarkdown.cxx +constParameterPointer:lexilla/lexers/LexMatlab.cxx constParameterReference:lexilla/lexers/LexMatlab.cxx unreadVariable:lexilla/lexers/LexMatlab.cxx variableScope:lexilla/lexers/LexMatlab.cxx @@ -128,9 +132,7 @@ constParameterReference:lexilla/lexers/LexSTTXT.cxx knownConditionTrueFalse:lexilla/lexers/LexTACL.cxx clarifyCalculation:lexilla/lexers/LexTADS3.cxx constParameterReference:lexilla/lexers/LexTADS3.cxx -unreadVariable:lexilla/lexers/LexTCL.cxx invalidscanf:lexilla/lexers/LexTCMD.cxx -constParameterPointer:lexilla/lexers/LexTCMD.cxx constParameterReference:lexilla/lexers/LexTeX.cxx variableScope:lexilla/lexers/LexTeX.cxx knownConditionTrueFalse:lexilla/lexers/LexTxt2tags.cxx @@ -166,7 +168,6 @@ constParameterCallback:lexilla/lexers/LexVB.cxx constVariableReference:lexilla/lexers/LexCSS.cxx constVariableReference:lexilla/lexers/LexCrontab.cxx constVariableReference:lexilla/lexers/LexGui4Cli.cxx -constVariableReference:lexilla/lexers/LexKix.cxx constVariableReference:lexilla/lexers/LexMetapost.cxx constVariableReference:lexilla/lexers/LexOpal.cxx @@ -179,5 +180,8 @@ constVariableReference:lexilla/lexers/LexOpal.cxx *:lexilla/test/unit/UnitTester.cxx *:lexilla/test/unit/unitTest.cxx +// Tests often test things that are always true +knownConditionTrueFalse:lexilla/test/unit/testCharacterSet.cxx + // cppcheck fails REQUIRE from Catch comparisonOfFuncReturningBoolError:lexilla/test/unit/*.cxx diff --git a/lexilla/doc/Lexilla.html b/lexilla/doc/Lexilla.html index 99f6e738c..a7b18c2ff 100644 --- a/lexilla/doc/Lexilla.html +++ b/lexilla/doc/Lexilla.html @@ -9,7 +9,7 @@ - +