From 432dcb7f1501a3524b2cd8b300de288f701f0d0a Mon Sep 17 00:00:00 2001 From: Don Ho Date: Thu, 19 Jan 2023 02:52:23 +0100 Subject: [PATCH] Use wide char version's function directely (part1) ref: https://github.com/notepad-plus-plus/notepad-plus-plus/pull/12613#discussion_r1045153278 --- PowerEditor/src/MISC/Common/Common.cpp | 6 +- PowerEditor/src/MISC/Common/Common.h | 1 - PowerEditor/src/MISC/Common/Sorters.h | 102 ++++++++---------- PowerEditor/src/NppBigSwitch.cpp | 2 +- PowerEditor/src/Parameters.cpp | 7 +- PowerEditor/src/ScintillaComponent/Buffer.cpp | 2 +- .../src/ScintillaComponent/FindReplaceDlg.cpp | 4 +- .../ScintillaComponent/ScintillaEditView.cpp | 4 +- .../ScintillaComponent/UserDefineDialog.cpp | 2 +- .../src/ScintillaComponent/UserDefineDialog.h | 2 +- PowerEditor/src/TinyXml/tinyxmlparser.cpp | 2 +- .../WinControls/ColourPicker/WordStyleDlg.cpp | 2 +- .../WinControls/PluginsAdmin/pluginsAdmin.cpp | 2 +- .../WinControls/Preference/preferenceDlg.cpp | 2 +- .../src/WinControls/WindowsDlg/WindowsDlg.cpp | 8 +- 15 files changed, 68 insertions(+), 80 deletions(-) diff --git a/PowerEditor/src/MISC/Common/Common.cpp b/PowerEditor/src/MISC/Common/Common.cpp index f290d16ad..fbbec9811 100644 --- a/PowerEditor/src/MISC/Common/Common.cpp +++ b/PowerEditor/src/MISC/Common/Common.cpp @@ -720,7 +720,7 @@ COLORREF getCtrlBgColor(HWND hWnd) generic_string stringToUpper(generic_string strToConvert) { std::transform(strToConvert.begin(), strToConvert.end(), strToConvert.begin(), - [](TCHAR ch){ return static_cast(_totupper(ch)); } + [](wchar_t ch){ return static_cast(towupper(ch)); } ); return strToConvert; } @@ -932,7 +932,7 @@ bool str2Clipboard(const generic_string &str2cpy, HWND hwnd) ::CloseClipboard(); return false; } - _tcscpy_s(pStr, len2Allocate / sizeof(TCHAR), str2cpy.c_str()); + wcscpy_s(pStr, len2Allocate / sizeof(TCHAR), str2cpy.c_str()); ::GlobalUnlock(hglbCopy); // Place the handle on the clipboard. unsigned int clipBoardFormat = CF_UNICODETEXT; @@ -1510,7 +1510,7 @@ HFONT createFont(const TCHAR* fontName, int fontSize, bool isBold, HWND hDestPar if (isBold) logFont.lfWeight = FW_BOLD; - _tcscpy_s(logFont.lfFaceName, fontName); + wcscpy_s(logFont.lfFaceName, fontName); HFONT newFont = CreateFontIndirect(&logFont); diff --git a/PowerEditor/src/MISC/Common/Common.h b/PowerEditor/src/MISC/Common/Common.h index 3fb50b633..d78dec817 100644 --- a/PowerEditor/src/MISC/Common/Common.h +++ b/PowerEditor/src/MISC/Common/Common.h @@ -37,7 +37,6 @@ const bool dirDown = false; #define BCKGRD_COLOR (RGB(255,102,102)) #define TXT_COLOR (RGB(255,255,255)) -#define generic_strtol wcstol #define generic_strncpy wcsncpy #define generic_stricmp wcsicmp #define generic_strncmp wcsncmp diff --git a/PowerEditor/src/MISC/Common/Sorters.h b/PowerEditor/src/MISC/Common/Sorters.h index aed116b1e..785422b5f 100644 --- a/PowerEditor/src/MISC/Common/Sorters.h +++ b/PowerEditor/src/MISC/Common/Sorters.h @@ -29,13 +29,11 @@ private: size_t _toColumn = 0; protected: - bool isDescending() const - { + bool isDescending() const { return _isDescending; - } + }; - generic_string getSortKey(const generic_string& input) - { + generic_string getSortKey(const generic_string& input) { if (isSortingSpecificColumns()) { if (input.length() < _fromColumn) @@ -58,16 +56,14 @@ protected: { return input; } - } + }; - bool isSortingSpecificColumns() - { + bool isSortingSpecificColumns() { return _toColumn != 0; - } + }; public: - ISorter(bool isDescending, size_t fromColumn, size_t toColumn) : _isDescending(isDescending), _fromColumn(fromColumn), _toColumn(toColumn) - { + ISorter(bool isDescending, size_t fromColumn, size_t toColumn) : _isDescending(isDescending), _fromColumn(fromColumn), _toColumn(toColumn) { assert(_fromColumn <= _toColumn); }; virtual ~ISorter() { }; @@ -80,8 +76,7 @@ class LexicographicSorter : public ISorter public: LexicographicSorter(bool isDescending, size_t fromColumn, size_t toColumn) : ISorter(isDescending, fromColumn, toColumn) { }; - std::vector sort(std::vector lines) override - { + std::vector sort(std::vector lines) override { // Note that both branches here are equivalent in the sense that they always give the same answer. // However, if we are *not* sorting specific columns, then we get a 40% speed improvement by not calling // getSortKey() so many times. @@ -115,7 +110,7 @@ public: }); } return lines; - } + }; }; // Implementation of lexicographic sorting of lines, ignoring character casing @@ -124,8 +119,7 @@ class LexicographicCaseInsensitiveSorter : public ISorter public: LexicographicCaseInsensitiveSorter(bool isDescending, size_t fromColumn, size_t toColumn) : ISorter(isDescending, fromColumn, toColumn) { }; - std::vector sort(std::vector lines) override - { + std::vector sort(std::vector lines) override { // Note that both branches here are equivalent in the sense that they always give the same answer. // However, if we are *not* sorting specific columns, then we get a 40% speed improvement by not calling // getSortKey() so many times. @@ -158,7 +152,7 @@ public: }); } return lines; - } + }; }; class IntegerSorter : public ISorter @@ -166,8 +160,7 @@ class IntegerSorter : public ISorter public: IntegerSorter(bool isDescending, size_t fromColumn, size_t toColumn) : ISorter(isDescending, fromColumn, toColumn) { }; - std::vector sort(std::vector lines) override - { + std::vector sort(std::vector lines) override { if (isSortingSpecificColumns()) { std::stable_sort(lines.begin(), lines.end(), [this](generic_string aIn, generic_string bIn) @@ -504,9 +497,8 @@ public: }); } - return lines; - } + }; }; // Convert each line to a number and then sort. @@ -531,8 +523,7 @@ public: #endif } - std::vector sort(std::vector lines) override - { + std::vector sort(std::vector lines) override { // Note that empty lines are filtered out and added back manually to the output at the end. std::vector> nonEmptyInputAsNumbers; std::vector empties; @@ -557,40 +548,44 @@ public: } } } + assert(nonEmptyInputAsNumbers.size() + empties.size() == lines.size()); const bool descending = isDescending(); std::stable_sort(nonEmptyInputAsNumbers.begin(), nonEmptyInputAsNumbers.end(), [descending](std::pair a, std::pair b) - { - if (descending) { - return a.second > b.second; - } - else - { - return a.second < b.second; - } - }); + if (descending) + { + return a.second > b.second; + } + else + { + return a.second < b.second; + } + }); + std::vector output; output.reserve(lines.size()); if (!isDescending()) { output.insert(output.end(), empties.begin(), empties.end()); } + for (auto it = nonEmptyInputAsNumbers.begin(); it != nonEmptyInputAsNumbers.end(); ++it) { output.push_back(lines[it->first]); } + if (isDescending()) { output.insert(output.end(), empties.begin(), empties.end()); } + assert(output.size() == lines.size()); return output; - } + }; protected: - bool considerStringEmpty(const generic_string& input) - { + bool considerStringEmpty(const generic_string& input) { // String has something else than just whitespace. return input.find_first_not_of(TEXT(" \t\r\n")) == std::string::npos; } @@ -614,16 +609,14 @@ public: DecimalCommaSorter(bool isDescending, size_t fromColumn, size_t toColumn) : NumericSorter(isDescending, fromColumn, toColumn) { }; protected: - generic_string prepareStringForConversion(const generic_string& input) override - { + generic_string prepareStringForConversion(const generic_string& input) override { generic_string admissablePart = stringTakeWhileAdmissable(getSortKey(input), TEXT(" \t\r\n0123456789,-")); return stringReplace(admissablePart, TEXT(","), TEXT(".")); - } + }; - double convertStringToNumber(const generic_string& input) override - { + double convertStringToNumber(const generic_string& input) override { return stodLocale(input, _usLocale); - } + }; }; // Converts lines to double before sorting (assumes decimal dot). @@ -633,15 +626,13 @@ public: DecimalDotSorter(bool isDescending, size_t fromColumn, size_t toColumn) : NumericSorter(isDescending, fromColumn, toColumn) { }; protected: - generic_string prepareStringForConversion(const generic_string& input) override - { + generic_string prepareStringForConversion(const generic_string& input) override { return stringTakeWhileAdmissable(getSortKey(input), TEXT(" \t\r\n0123456789.-")); - } + }; - double convertStringToNumber(const generic_string& input) override - { + double convertStringToNumber(const generic_string& input) override { return stodLocale(input, _usLocale); - } + }; }; class ReverseSorter : public ISorter @@ -649,25 +640,24 @@ class ReverseSorter : public ISorter public: ReverseSorter(bool isDescending, size_t fromColumn, size_t toColumn) : ISorter(isDescending, fromColumn, toColumn) { }; - std::vector sort(std::vector lines) override - { + std::vector sort(std::vector lines) override { std::reverse(lines.begin(), lines.end()); return lines; - } + }; }; class RandomSorter : public ISorter { public: unsigned seed; - RandomSorter(bool isDescending, size_t fromColumn, size_t toColumn) : ISorter(isDescending, fromColumn, toColumn) - { + + RandomSorter(bool isDescending, size_t fromColumn, size_t toColumn) : ISorter(isDescending, fromColumn, toColumn) { seed = static_cast(time(NULL)); - } - std::vector sort(std::vector lines) override - { + }; + + std::vector sort(std::vector lines) override { std::shuffle(lines.begin(), lines.end(), std::default_random_engine(seed)); return lines; - } + }; }; diff --git a/PowerEditor/src/NppBigSwitch.cpp b/PowerEditor/src/NppBigSwitch.cpp index 8d87449cb..ed55d2526 100644 --- a/PowerEditor/src/NppBigSwitch.cpp +++ b/PowerEditor/src/NppBigSwitch.cpp @@ -561,7 +561,7 @@ LRESULT Notepad_plus::process(HWND hwnd, UINT message, WPARAM wParam, LPARAM lPa TCHAR longNameFullpath[MAX_PATH]; const TCHAR* pFilePath = reinterpret_cast(lParam); wcscpy_s(longNameFullpath, MAX_PATH, pFilePath); - if (_tcschr(longNameFullpath, '~')) + if (wcschr(longNameFullpath, '~')) { ::GetLongPathName(longNameFullpath, longNameFullpath, MAX_PATH); } diff --git a/PowerEditor/src/Parameters.cpp b/PowerEditor/src/Parameters.cpp index 859ad472e..eb03edf53 100644 --- a/PowerEditor/src/Parameters.cpp +++ b/PowerEditor/src/Parameters.cpp @@ -568,7 +568,7 @@ int strVal(const TCHAR *str, int base) if (!str[0]) return 0; TCHAR *finStr; - int result = generic_strtol(str, &finStr, base); + int result = wcstol(str, &finStr, base); if (*finStr != '\0') return -1; return result; @@ -1680,7 +1680,7 @@ const TCHAR* NppParameters::getUserDefinedLangNameFromExt(TCHAR *ext, TCHAR *ful // Force to use dark mode UDL in dark mode or to use light mode UDL in light mode for (size_t j = 0, len = extVect.size(); j < len; ++j) { - if (!generic_stricmp(extVect[j].c_str(), ext) || (_tcschr(fullName, '.') && !generic_stricmp(extVect[j].c_str(), fullName))) + if (!generic_stricmp(extVect[j].c_str(), ext) || (wcschr(fullName, '.') && !generic_stricmp(extVect[j].c_str(), fullName))) { // preserve ext matched UDL iMatched = i; @@ -3723,7 +3723,6 @@ void NppParameters::feedUserKeywordList(TiXmlNode *node) else if (!lstrcmp(keywordsName, TEXT("Comment"))) { kwl = (valueNode)?valueNode->Value():TEXT(""); - //int len = _tcslen(kwl); basic_string temp{TEXT(" ")}; temp += kwl; @@ -3760,7 +3759,7 @@ void NppParameters::feedUserKeywordList(TiXmlNode *node) if (globalMappper().keywordIdMapper.find(keywordsName) != globalMappper().keywordIdMapper.end()) { id = globalMappper().keywordIdMapper[keywordsName]; - if (_tcslen(kwl) < max_char) + if (wcslen(kwl) < max_char) { wcscpy_s(_userLangArray[_nbUserLang - 1]->_keywordLists[id], kwl); } diff --git a/PowerEditor/src/ScintillaComponent/Buffer.cpp b/PowerEditor/src/ScintillaComponent/Buffer.cpp index 3ffd0b304..5dc06dd76 100644 --- a/PowerEditor/src/ScintillaComponent/Buffer.cpp +++ b/PowerEditor/src/ScintillaComponent/Buffer.cpp @@ -1015,7 +1015,7 @@ bool FileManager::backupCurrentBuffer() TCHAR fullpath[MAX_PATH]; ::GetFullPathName(backupFilePath.c_str(), MAX_PATH, fullpath, NULL); - if (_tcschr(fullpath, '~')) + if (wcschr(fullpath, '~')) { ::GetLongPathName(fullpath, fullpath, MAX_PATH); } diff --git a/PowerEditor/src/ScintillaComponent/FindReplaceDlg.cpp b/PowerEditor/src/ScintillaComponent/FindReplaceDlg.cpp index a0dcefe1d..7b5614df1 100644 --- a/PowerEditor/src/ScintillaComponent/FindReplaceDlg.cpp +++ b/PowerEditor/src/ScintillaComponent/FindReplaceDlg.cpp @@ -5397,9 +5397,9 @@ HWND Progress::open(HWND hCallerWnd, const TCHAR* header) ::UpdateWindow(hwnd); if (header) - _tcscpy_s(_header, _countof(_header), header); + wcscpy_s(_header, _countof(_header), header); else - _tcscpy_s(_header, _countof(_header), cDefaultHeader); + wcscpy_s(_header, _countof(_header), cDefaultHeader); _hThread = ::CreateThread(NULL, 0, threadFunc, this, 0, NULL); if (!_hThread) diff --git a/PowerEditor/src/ScintillaComponent/ScintillaEditView.cpp b/PowerEditor/src/ScintillaComponent/ScintillaEditView.cpp index 20b3a7bb7..38a231622 100644 --- a/PowerEditor/src/ScintillaComponent/ScintillaEditView.cpp +++ b/PowerEditor/src/ScintillaComponent/ScintillaEditView.cpp @@ -2294,7 +2294,7 @@ void ScintillaEditView::getGenericText(TCHAR *dest, size_t destlen, size_t start getText(destA, start, end); size_t cp = execute(SCI_GETCODEPAGE); const TCHAR *destW = wmc.char2wchar(destA, cp); - _tcsncpy_s(dest, destlen, destW, _TRUNCATE); + wcsncpy_s(dest, destlen, destW, _TRUNCATE); delete [] destA; } @@ -2308,7 +2308,7 @@ void ScintillaEditView::getGenericText(TCHAR *dest, size_t destlen, size_t start getText(destA, start, end); size_t cp = execute(SCI_GETCODEPAGE) ; const TCHAR *destW = wmc.char2wchar(destA, cp, mstart, mend); - _tcsncpy_s(dest, destlen, destW, _TRUNCATE); + wcsncpy_s(dest, destlen, destW, _TRUNCATE); delete [] destA; } diff --git a/PowerEditor/src/ScintillaComponent/UserDefineDialog.cpp b/PowerEditor/src/ScintillaComponent/UserDefineDialog.cpp index 0adbf102e..d141956f4 100644 --- a/PowerEditor/src/ScintillaComponent/UserDefineDialog.cpp +++ b/PowerEditor/src/ScintillaComponent/UserDefineDialog.cpp @@ -1894,7 +1894,7 @@ intptr_t CALLBACK StylerDlg::dlgProc(HWND hwnd, UINT message, WPARAM wParam, LPA else { TCHAR *finStr; - style._fontSize = generic_strtol(intStr, &finStr, 10); + style._fontSize = wcstol(intStr, &finStr, 10); if (*finStr != '\0') style._fontSize = -1; } diff --git a/PowerEditor/src/ScintillaComponent/UserDefineDialog.h b/PowerEditor/src/ScintillaComponent/UserDefineDialog.h index 882c72cd6..3bc105176 100644 --- a/PowerEditor/src/ScintillaComponent/UserDefineDialog.h +++ b/PowerEditor/src/ScintillaComponent/UserDefineDialog.h @@ -395,7 +395,7 @@ public : _textValue = text2Set; _txtLen = txtLen; _shouldGotoCenter = bGotoCenter; - if (restrictedChars && _tcslen(restrictedChars)) + if (restrictedChars && wcslen(restrictedChars)) { _restrictedChars = restrictedChars; } diff --git a/PowerEditor/src/TinyXml/tinyxmlparser.cpp b/PowerEditor/src/TinyXml/tinyxmlparser.cpp index 6217d9e92..bae2f31a0 100644 --- a/PowerEditor/src/TinyXml/tinyxmlparser.cpp +++ b/PowerEditor/src/TinyXml/tinyxmlparser.cpp @@ -234,7 +234,7 @@ const TCHAR* TiXmlBase::GetEntity( const TCHAR* p, TCHAR* value ) if (end && end - p <= 3 + 4) { TCHAR* hexend; - auto val = generic_strtol(p + 3, &hexend, 16); + auto val = wcstol(p + 3, &hexend, 16); if (hexend == end) { *value = static_cast(val); diff --git a/PowerEditor/src/WinControls/ColourPicker/WordStyleDlg.cpp b/PowerEditor/src/WinControls/ColourPicker/WordStyleDlg.cpp index a69b6bc16..26d129db7 100644 --- a/PowerEditor/src/WinControls/ColourPicker/WordStyleDlg.cpp +++ b/PowerEditor/src/WinControls/ColourPicker/WordStyleDlg.cpp @@ -710,7 +710,7 @@ void WordStyleDlg::updateFontSize() else { TCHAR *finStr; - style._fontSize = generic_strtol(intStr, &finStr, 10); + style._fontSize = wcstol(intStr, &finStr, 10); if (*finStr != '\0') style._fontSize = STYLE_NOT_USED; } diff --git a/PowerEditor/src/WinControls/PluginsAdmin/pluginsAdmin.cpp b/PowerEditor/src/WinControls/PluginsAdmin/pluginsAdmin.cpp index ee32ef11a..cf8cef3a4 100644 --- a/PowerEditor/src/WinControls/PluginsAdmin/pluginsAdmin.cpp +++ b/PowerEditor/src/WinControls/PluginsAdmin/pluginsAdmin.cpp @@ -70,7 +70,7 @@ bool findStrNoCase(const generic_string & strHaystack, const generic_string & st auto it = std::search( strHaystack.begin(), strHaystack.end(), strNeedle.begin(), strNeedle.end(), - [](TCHAR ch1, TCHAR ch2){return _totupper(ch1) == _totupper(ch2); } + [](wchar_t ch1, wchar_t ch2){return towupper(ch1) == towupper(ch2); } ); return (it != strHaystack.end()); } diff --git a/PowerEditor/src/WinControls/Preference/preferenceDlg.cpp b/PowerEditor/src/WinControls/Preference/preferenceDlg.cpp index b3d8395d6..11ff64747 100644 --- a/PowerEditor/src/WinControls/Preference/preferenceDlg.cpp +++ b/PowerEditor/src/WinControls/Preference/preferenceDlg.cpp @@ -3577,7 +3577,7 @@ intptr_t CALLBACK PrintSubDlg::run_dlgProc(UINT message, WPARAM wParam, LPARAM) if (!intStr[0]) *pVal = 0; else - *pVal = generic_strtol(intStr, NULL, 10); + *pVal = wcstol(intStr, NULL, 10); } break; diff --git a/PowerEditor/src/WinControls/WindowsDlg/WindowsDlg.cpp b/PowerEditor/src/WinControls/WindowsDlg/WindowsDlg.cpp index 0406b98ac..636e3d340 100644 --- a/PowerEditor/src/WinControls/WindowsDlg/WindowsDlg.cpp +++ b/PowerEditor/src/WinControls/WindowsDlg/WindowsDlg.cpp @@ -103,7 +103,7 @@ struct NumericStringEquivalence } if (_istdigit(*str1) && _istdigit(*str2)) { - lcmp = generic_strtol(str1, &p1, 10) - generic_strtol(str2, &p2, 10); + lcmp = wcstol(str1, &p1, 10) - wcstol(str2, &p2, 10); if ( lcmp == 0 ) lcmp = static_cast((p2 - str2) - (p1 - str1)); if ( lcmp != 0 ) @@ -113,11 +113,11 @@ struct NumericStringEquivalence else { if (_istascii(*str1) && _istupper(*str1)) - c1 = _totlower(*str1); + c1 = towlower(*str1); else c1 = *str1; if (_istascii(*str2) && _istupper(*str2)) - c2 = _totlower(*str2); + c2 = towlower(*str2); else c2 = *str2; lcmp = (c1 - c2); @@ -427,7 +427,7 @@ intptr_t CALLBACK WindowsDlg::run_dlgProc(UINT message, WPARAM wParam, LPARAM lP if (static_cast(text.length()) < pLvdi->item.cchTextMax) { // Copy the resulting text to destination with a null terminator. - _tcscpy_s(pLvdi->item.pszText, text.length() + 1, text.c_str()); + wcscpy_s(pLvdi->item.pszText, text.length() + 1, text.c_str()); } } return TRUE;