From af9af11aecc49ccade7c857cf0e2080bbbb153b0 Mon Sep 17 00:00:00 2001 From: Christian Grasser Date: Thu, 5 Dec 2024 21:55:47 +0100 Subject: [PATCH] Refactoring for the better performance & smaller binary size 1. remove deprecated std::codecvt 2. replace s2ws -> string2wstring and ws2s -> wstring2string Fix #15898, close #15913 --- PowerEditor/src/MISC/Common/Common.cpp | 20 +---------------- PowerEditor/src/MISC/Common/Common.h | 3 --- PowerEditor/src/MISC/Common/FileInterface.cpp | 13 ++++------- .../src/MISC/Common/verifySignedfile.cpp | 22 +++++++++---------- .../src/MISC/PluginsManager/PluginsManager.h | 2 +- PowerEditor/src/ScintillaComponent/Buffer.cpp | 7 ++---- .../src/ScintillaComponent/FindReplaceDlg.cpp | 2 +- .../WinControls/Preference/preferenceDlg.cpp | 2 +- PowerEditor/visual.net/notepadPlus.Cpp.props | 2 +- 9 files changed, 22 insertions(+), 51 deletions(-) diff --git a/PowerEditor/src/MISC/Common/Common.cpp b/PowerEditor/src/MISC/Common/Common.cpp index a89432ef9..f19c61741 100644 --- a/PowerEditor/src/MISC/Common/Common.cpp +++ b/PowerEditor/src/MISC/Common/Common.cpp @@ -18,7 +18,6 @@ #include #include #include -#include #include #include "StaticDialog.h" #include "CustomFileDialog.h" @@ -142,8 +141,7 @@ void writeLog(const wchar_t* logFileName, const char* log2write) SYSTEMTIME currentTime = {}; ::GetLocalTime(¤tTime); wstring dateTimeStrW = getDateTimeStrFrom(L"yyyy-MM-dd HH:mm:ss", currentTime); - wstring_convert> converter; - string log2writeStr = converter.to_bytes(dateTimeStrW); + string log2writeStr = wstring2string(dateTimeStrW, CP_UTF8); log2writeStr += " "; log2writeStr += log2write; log2writeStr += "\n"; @@ -1239,22 +1237,6 @@ bool isAssoCommandExisting(LPCTSTR FullPathName) return isAssoCmdExist; } -std::wstring s2ws(const std::string& str) -{ - using convert_typeX = std::codecvt_utf8; - std::wstring_convert converterX("Error in Notepad++ string conversion s2ws!", L"Error in Notepad++ string conversion s2ws!"); - - return converterX.from_bytes(str); -} - -std::string ws2s(const std::wstring& wstr) -{ - using convert_typeX = std::codecvt_utf8; - std::wstring_convert converterX("Error in Notepad++ string conversion ws2s!", L"Error in Notepad++ string conversion ws2s!"); - - return converterX.to_bytes(wstr); -} - bool deleteFileOrFolder(const wstring& f2delete) { auto len = f2delete.length(); diff --git a/PowerEditor/src/MISC/Common/Common.h b/PowerEditor/src/MISC/Common/Common.h index bb843c05b..cc7c608c0 100644 --- a/PowerEditor/src/MISC/Common/Common.h +++ b/PowerEditor/src/MISC/Common/Common.h @@ -181,9 +181,6 @@ HWND CreateToolTipRect(int toolID, HWND hWnd, HINSTANCE hInst, const PTSTR pszTe bool isCertificateValidated(const std::wstring & fullFilePath, const std::wstring & subjectName2check); bool isAssoCommandExisting(LPCTSTR FullPathName); -std::wstring s2ws(const std::string& str); -std::string ws2s(const std::wstring& wstr); - bool deleteFileOrFolder(const std::wstring& f2delete); void getFilesInFolder(std::vector& files, const std::wstring& extTypeFilter, const std::wstring& inFolder); diff --git a/PowerEditor/src/MISC/Common/FileInterface.cpp b/PowerEditor/src/MISC/Common/FileInterface.cpp index f7c585032..ddc657d23 100644 --- a/PowerEditor/src/MISC/Common/FileInterface.cpp +++ b/PowerEditor/src/MISC/Common/FileInterface.cpp @@ -15,7 +15,6 @@ // along with this program. If not, see . #include -#include #include #include "FileInterface.h" #include "Parameters.h" @@ -27,8 +26,7 @@ Win32_IO_File::Win32_IO_File(const wchar_t *fname) if (fname) { std::wstring fn = fname; - std::wstring_convert> converter; - _path = converter.to_bytes(fn); + _path = wstring2string(fn, CP_UTF8); WIN32_FILE_ATTRIBUTE_DATA attributes_original{}; attributes_original.dwFileAttributes = INVALID_FILE_ATTRIBUTES; @@ -127,8 +125,7 @@ void Win32_IO_File::close() if ((dwRet == 0) || (dwRet >= cchPathBuf)) { // probably insufficient path-buffer length, the classic style must suffice - std::wstring_convert> converter; - curFilePath = converter.from_bytes(_path); + curFilePath = string2wstring(_path, CP_UTF8); } else { @@ -156,8 +153,7 @@ Please try using another storage and also check if your saved data is not corrup std::wstring nppIssueLog = nppParam.getUserPath(); pathAppend(nppIssueLog, nppFlushFileBuffersFailsLog); - std::wstring_convert> converter; - std::string errNumberMsgA = converter.to_bytes(errNumberMsg); + std::string errNumberMsgA = wstring2string(errNumberMsg, CP_UTF8); writeLog(nppIssueLog.c_str(), errNumberMsgA.c_str()); } @@ -239,8 +235,7 @@ bool Win32_IO_File::write(const void *wbuf, size_t buf_size) std::string msg = _path; msg += " written failed: "; std::wstring lastErrorMsg = GetLastErrorAsString(::GetLastError()); - std::wstring_convert> converter; - msg += converter.to_bytes(lastErrorMsg); + msg += wstring2string(lastErrorMsg, CP_UTF8); writeLog(nppIssueLog.c_str(), msg.c_str()); } diff --git a/PowerEditor/src/MISC/Common/verifySignedfile.cpp b/PowerEditor/src/MISC/Common/verifySignedfile.cpp index 26d65059a..162a2555a 100644 --- a/PowerEditor/src/MISC/Common/verifySignedfile.cpp +++ b/PowerEditor/src/MISC/Common/verifySignedfile.cpp @@ -112,7 +112,7 @@ bool SecurityGuard::verifySignedLibrary(const std::wstring& filepath) if (doLogCertifError) { string dmsg("VerifyLibrary: "); - dmsg += ws2s(filepath); + dmsg += wstring2string(filepath, CP_UTF8); writeLog(L"c:\\tmp\\certifError.log", dmsg.c_str()); } @@ -208,14 +208,14 @@ bool SecurityGuard::verifySignedLibrary(const std::wstring& filepath) if (!result) { - throw string("Checking certificate of ") + ws2s(filepath) + " : " + ws2s(GetLastErrorAsString(GetLastError())); + throw string("Checking certificate of ") + wstring2string(filepath, CP_UTF8) + " : " + wstring2string(GetLastErrorAsString(GetLastError()), CP_UTF8); } // Get signer information size. result = ::CryptMsgGetParam(hMsg, CMSG_SIGNER_INFO_PARAM, 0, NULL, &dwSignerInfo); if (!result) { - throw string("CryptMsgGetParam first call: ") + ws2s(GetLastErrorAsString(GetLastError())); + throw string("CryptMsgGetParam first call: ") + wstring2string(GetLastErrorAsString(GetLastError()), CP_UTF8); } // Get Signer Information. @@ -228,7 +228,7 @@ bool SecurityGuard::verifySignedLibrary(const std::wstring& filepath) result = ::CryptMsgGetParam(hMsg, CMSG_SIGNER_INFO_PARAM, 0, (PVOID)pSignerInfo, &dwSignerInfo); if (!result) { - throw string("CryptMsgGetParam: ") + ws2s(GetLastErrorAsString(GetLastError())); + throw string("CryptMsgGetParam: ") + wstring2string(GetLastErrorAsString(GetLastError()), CP_UTF8); } // Get the signer certificate from temporary certificate store. @@ -238,10 +238,10 @@ bool SecurityGuard::verifySignedLibrary(const std::wstring& filepath) PCCERT_CONTEXT context = ::CertFindCertificateInStore(hStore, X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, 0, CERT_FIND_SUBJECT_CERT, (PVOID)&cert_info, NULL); if (!context) { - throw string("Certificate context: ") + ws2s(GetLastErrorAsString(GetLastError())); + throw string("Certificate context: ") + wstring2string(GetLastErrorAsString(GetLastError()), CP_UTF8); } - // Getting the full subject + // Getting the full subject auto subject_sze = ::CertNameToStr(X509_ASN_ENCODING, &context->pCertInfo->Subject, CERT_X500_NAME_STR, NULL, 0); if (subject_sze <= 1) { @@ -259,13 +259,13 @@ bool SecurityGuard::verifySignedLibrary(const std::wstring& filepath) DWORD key_id_sze = 0; if (!::CertGetCertificateContextProperty(context, CERT_KEY_IDENTIFIER_PROP_ID, NULL, &key_id_sze)) { - throw string("x509 property not found") + ws2s(GetLastErrorAsString(GetLastError())); + throw string("x509 property not found") + wstring2string(GetLastErrorAsString(GetLastError()), CP_UTF8); } std::unique_ptr key_id_buff(new BYTE[key_id_sze]); if (!::CertGetCertificateContextProperty(context, CERT_KEY_IDENTIFIER_PROP_ID, key_id_buff.get(), &key_id_sze)) { - throw string("Getting certificate property problem.") + ws2s(GetLastErrorAsString(GetLastError())); + throw string("Getting certificate property problem.") + wstring2string(GetLastErrorAsString(GetLastError()), CP_UTF8); } wstringstream ss; @@ -277,20 +277,20 @@ bool SecurityGuard::verifySignedLibrary(const std::wstring& filepath) key_id_hex = ss.str(); if (doLogCertifError) - writeLog(L"c:\\tmp\\certifError.log", ws2s(key_id_hex).c_str()); + writeLog(L"c:\\tmp\\certifError.log", wstring2string(key_id_hex, CP_UTF8).c_str()); // Getting the display name auto sze = ::CertGetNameString(context, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL, NULL, 0); if (sze <= 1) { - throw string("Getting data size problem.") + ws2s(GetLastErrorAsString(GetLastError())); + throw string("Getting data size problem.") + wstring2string(GetLastErrorAsString(GetLastError()), CP_UTF8); } // Get display name. std::unique_ptr display_name_buffer(new wchar_t[sze]); if (::CertGetNameString(context, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL, display_name_buffer.get(), sze) <= 1) { - throw string("Cannot get certificate info." + ws2s(GetLastErrorAsString(GetLastError()))); + throw string("Cannot get certificate info." + wstring2string(GetLastErrorAsString(GetLastError()), CP_UTF8)); } display_name = display_name_buffer.get(); diff --git a/PowerEditor/src/MISC/PluginsManager/PluginsManager.h b/PowerEditor/src/MISC/PluginsManager/PluginsManager.h index fbe7344ca..23fdf19bb 100644 --- a/PowerEditor/src/MISC/PluginsManager/PluginsManager.h +++ b/PowerEditor/src/MISC/PluginsManager/PluginsManager.h @@ -147,7 +147,7 @@ private: std::wstring msg = L"An exception occurred due to plugin: "; msg += pluginName; msg += L"\r\n\r\nException reason: "; - msg += s2ws(e.what()); + msg += string2wstring(e.what(), CP_UTF8); ::MessageBox(NULL, msg.c_str(), L"Plugin Exception", MB_OK); } diff --git a/PowerEditor/src/ScintillaComponent/Buffer.cpp b/PowerEditor/src/ScintillaComponent/Buffer.cpp index 930a9f4b2..887741bf8 100644 --- a/PowerEditor/src/ScintillaComponent/Buffer.cpp +++ b/PowerEditor/src/ScintillaComponent/Buffer.cpp @@ -18,7 +18,6 @@ #include #include #include -#include #include #include "Buffer.h" #include "Scintilla.h" @@ -168,8 +167,7 @@ void Buffer::updateTimeStamp() wstring nppIssueLog = nppParam.getUserPath(); pathAppend(nppIssueLog, issueFn); - std::wstring_convert> converter; - std::string msg = converter.to_bytes(_fullPathName); + std::string msg = wstring2string(_fullPathName, CP_UTF8); char buf[1024]; sprintf(buf, " in updateTimeStamp(): timeStampLive (%lu/%lu) < _timeStamp (%lu/%lu)", timeStampLive.dwLowDateTime, timeStampLive.dwHighDateTime, _timeStamp.dwLowDateTime, _timeStamp.dwHighDateTime); msg += buf; @@ -343,8 +341,7 @@ bool Buffer::checkFileState() // returns true if the status has been changed (it wstring nppIssueLog = nppParam.getUserPath(); pathAppend(nppIssueLog, issueFn); - std::wstring_convert> converter; - std::string msg = converter.to_bytes(_fullPathName); + std::string msg = wstring2string(_fullPathName, CP_UTF8); char buf[1024]; sprintf(buf, " in checkFileState(): attributes.ftLastWriteTime (%lu/%lu) < _timeStamp (%lu/%lu)", attributes.ftLastWriteTime.dwLowDateTime, attributes.ftLastWriteTime.dwHighDateTime, _timeStamp.dwLowDateTime, _timeStamp.dwHighDateTime); msg += buf; diff --git a/PowerEditor/src/ScintillaComponent/FindReplaceDlg.cpp b/PowerEditor/src/ScintillaComponent/FindReplaceDlg.cpp index ad75a4b9c..9ce806c61 100644 --- a/PowerEditor/src/ScintillaComponent/FindReplaceDlg.cpp +++ b/PowerEditor/src/ScintillaComponent/FindReplaceDlg.cpp @@ -4154,7 +4154,7 @@ void FindReplaceDlg::setStatusbarMessageWithRegExprErr(ScintillaEditView* pEditV std::wstring result = pNativeSpeaker->getLocalizedStrFromID("find-status-invalid-re", L"Find: Invalid Regular Expression"); string s = msg; - setStatusbarMessage(result, FSNotFound, s2ws(s)); + setStatusbarMessage(result, FSNotFound, string2wstring(s, CP_UTF8)); } wstring FindReplaceDlg::getScopeInfoForStatusBar(FindOption const *pFindOpt) const diff --git a/PowerEditor/src/WinControls/Preference/preferenceDlg.cpp b/PowerEditor/src/WinControls/Preference/preferenceDlg.cpp index 99d21d240..abaa06bc9 100644 --- a/PowerEditor/src/WinControls/Preference/preferenceDlg.cpp +++ b/PowerEditor/src/WinControls/Preference/preferenceDlg.cpp @@ -634,7 +634,7 @@ intptr_t CALLBACK GeneralSubDlg::run_dlgProc(UINT message, WPARAM wParam, LPARAM if (nppParam.getNativeLangA()) // if nativeLangA is not NULL, then we can be sure the default language (English) is not used { string fn = localizationSwitcher.getFileName(); - wstring fnW = s2ws(fn); + wstring fnW = string2wstring(fn, CP_UTF8); lang = localizationSwitcher.getLangFromXmlFileName(fnW.c_str()); } auto index = ::SendDlgItemMessage(_hSelf, IDC_COMBO_LOCALIZATION, CB_FINDSTRINGEXACT, static_cast(-1), reinterpret_cast(lang.c_str())); diff --git a/PowerEditor/visual.net/notepadPlus.Cpp.props b/PowerEditor/visual.net/notepadPlus.Cpp.props index ba6e78585..320ee8dc8 100644 --- a/PowerEditor/visual.net/notepadPlus.Cpp.props +++ b/PowerEditor/visual.net/notepadPlus.Cpp.props @@ -25,7 +25,7 @@ ..\src;..\src\MISC;..\src\MISC\Common;..\src\MISC\Exception;..\src\MISC\PluginsManager;..\src\MISC\Process;..\src\MISC\RegExt;..\src\MISC\md5;..\src\MISC\sha1;..\src\MISC\sha2;..\src\MISC\sha512;..\src\MISC\SysMsg;..\src\ScintillaComponent;..\src\Win32Explr;..\src\WinControls;..\src\WinControls\AboutDlg;..\src\WinControls\AnsiCharPanel;..\src\WinControls\ClipboardHistory;..\src\WinControls\ColourPicker;..\src\WinControls\ContextMenu;..\src\WinControls\DockingWnd;..\src\WinControls\DocumentMap;..\src\WinControls\FileBrowser;..\src\WinControls\FindCharsInRange;..\src\WinControls\FunctionList;..\src\WinControls\Grid;..\src\WinControls\ImageListSet;..\src\WinControls\OpenSaveFileDialog;..\src\WinControls\PluginsAdmin;..\src\WinControls\Preference;..\src\WinControls\ProjectPanel;..\src\WinControls\ReadDirectoryChanges;..\src\WinControls\shortcut;..\src\WinControls\SplitterContainer;..\src\WinControls\StaticDialog;..\src\WinControls\StaticDialog\RunDlg;..\src\WinControls\StatusBar;..\src\WinControls\TabBar;..\src\WinControls\TaskList;..\src\WinControls\ToolBar;..\src\WinControls\ToolTip;..\src\WinControls\TrayIcon;..\src\WinControls\TreeView;..\src\WinControls\VerticalFileSwitcher;..\src\WinControls\WindowsDlg;%(AdditionalIncludeDirectories) - WIN32;_WIN32_WINNT=_WIN32_WINNT_WIN7;NTDDI_VERSION=NTDDI_WIN7;_WINDOWS;OEMRESOURCE;NOMINMAX;_USE_64BIT_TIME_T;TIXML_USE_STL;TIXMLA_USE_STL;_CRT_NONSTDC_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES;_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT;_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING;%(PreprocessorDefinitions) + WIN32;_WIN32_WINNT=_WIN32_WINNT_WIN7;NTDDI_VERSION=NTDDI_WIN7;_WINDOWS;OEMRESOURCE;NOMINMAX;_USE_64BIT_TIME_T;TIXML_USE_STL;TIXMLA_USE_STL;_CRT_NONSTDC_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES;_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT;%(PreprocessorDefinitions) Async Level4 true