Skip to content

Commit 2f5ba4f

Browse files
ozone10donho
authored andcommitted
Prepare comments for pugixml replacement
Ref: notepad-plus-plus#16175 Close notepad-plus-plus#17428
1 parent 6e5ea55 commit 2f5ba4f

File tree

6 files changed

+130
-127
lines changed

6 files changed

+130
-127
lines changed

PowerEditor/src/Notepad_plus.cpp

Lines changed: 70 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <wininet.h>
2121

2222
#include <ctime>
23+
#include <memory>
2324

2425
#include "NppXml.h"
2526
#include "Notepad_plus_Window.h"
@@ -5286,36 +5287,36 @@ void Notepad_plus::showFunctionNextHint(bool isNext)
52865287
autoC->callTipClick(1);
52875288
}
52885289

5289-
static wstring extractSymbol(wchar_t firstChar, wchar_t secondChar, const wchar_t *str2extract)
5290+
static std::string extractSymbol(char firstChar, char secondChar, const char* str2extract)
52905291
{
52915292
bool found = false;
5292-
const size_t extractedLen = 128;
5293-
wchar_t extracted[extractedLen] = {'\0'};
5293+
static constexpr size_t extractedLen = 128;
5294+
char extracted[extractedLen] = { '\0' };
52945295

5295-
for (size_t i = 0, j = 0, len = lstrlen(str2extract) ; i < len && j < extractedLen - 1; ++i)
5296+
for (size_t i = 0, j = 0, len = std::strlen(str2extract); i < len && j < extractedLen - 1; ++i)
52965297
{
52975298
if (found)
52985299
{
52995300
if (!str2extract[i] || str2extract[i] == ' ')
53005301
{
53015302
extracted[j] = '\0';
5302-
return wstring(extracted);
5303+
return extracted;
53035304
}
53045305
extracted[j++] = str2extract[i];
53055306
}
53065307
else
53075308
{
53085309
if (!str2extract[i])
5309-
return L"";
5310+
return "";
53105311

5311-
if (str2extract[i] == firstChar && str2extract[i+1] == secondChar)
5312+
if (str2extract[i] == firstChar && str2extract[i + 1] == secondChar)
53125313
{
53135314
found = true;
53145315
++i;
53155316
}
53165317
}
53175318
}
5318-
return wstring(extracted);
5319+
return extracted;
53195320
}
53205321

53215322
bool Notepad_plus::doBlockComment(comment_mode currCommentMode)
@@ -5326,13 +5327,13 @@ bool Notepad_plus::doBlockComment(comment_mode currCommentMode)
53265327
return false;
53275328

53285329
//-- BlockToStreamComment:
5329-
const wchar_t *commentStart;
5330-
const wchar_t *commentEnd;
5331-
wstring symbolStart;
5332-
wstring symbolEnd;
5330+
const char* commentStart = nullptr;
5331+
const char* commentEnd = nullptr;
5332+
std::string symbolStart;
5333+
std::string symbolEnd;
53335334

5334-
const wchar_t *commentLineSymbol;
5335-
wstring symbol;
5335+
const char* commentLineSymbol = nullptr;
5336+
std::string symbol;
53365337

53375338
//Single Line Comment/Uncomment/Toggle can have two modes:
53385339
// * a NORMAL MODE which uses a commentLineSymbol to comment/uncomment code per line, and
@@ -5349,12 +5350,14 @@ bool Notepad_plus::doBlockComment(comment_mode currCommentMode)
53495350
if (!userLangContainer)
53505351
return false;
53515352

5352-
symbol = extractSymbol('0', '0', userLangContainer->_keywordLists[SCE_USER_KWLIST_COMMENTS]);
5353+
const std::string kws = wstring2string(userLangContainer->_keywordLists[SCE_USER_KWLIST_COMMENTS]);
5354+
5355+
symbol = extractSymbol('0', '0', kws.c_str());
53535356
commentLineSymbol = symbol.c_str();
53545357
// BlockToStreamComment: Needed to decide, if stream-comment can be called below!
5355-
symbolStart = extractSymbol('0', '3', userLangContainer->_keywordLists[SCE_USER_KWLIST_COMMENTS]);
5358+
symbolStart = extractSymbol('0', '3', kws.c_str());
53565359
commentStart = symbolStart.c_str();
5357-
symbolEnd = extractSymbol('0', '4', userLangContainer->_keywordLists[SCE_USER_KWLIST_COMMENTS]);
5360+
symbolEnd = extractSymbol('0', '4', kws.c_str());
53585361
commentEnd = symbolEnd.c_str();
53595362
}
53605363
else
@@ -5397,16 +5400,16 @@ bool Notepad_plus::doBlockComment(comment_mode currCommentMode)
53975400
}
53985401

53995402
//For Single Line NORMAL MODE
5400-
wstring comment;
5403+
std::string comment;
54015404
size_t comment_length = 0;
54025405

54035406
//For Single Line ADVANCED MODE
5404-
wstring advCommentStart;
5405-
wstring advCommentEnd;
5407+
std::string advCommentStart;
5408+
std::string advCommentEnd;
54065409
size_t advCommentStart_length = 0;
54075410
size_t advCommentEnd_length = 0;
54085411

5409-
const wchar_t aSpace[] { L" " };
5412+
const char aSpace[]{ " " };
54105413

54115414
//Only values that have passed through will be assigned, to be sure they are valid!
54125415
if (!isSingleLineAdvancedMode)
@@ -5463,20 +5466,19 @@ bool Notepad_plus::doBlockComment(comment_mode currCommentMode)
54635466
lineIndent = lineStart;
54645467

54655468
size_t linebufferSize = lineEnd - lineIndent + 1;
5466-
wchar_t* linebuf = new wchar_t[linebufferSize];
5469+
auto linebuf = std::make_unique<char[]>(linebufferSize);
54675470

5468-
_pEditView->getGenericText(linebuf, linebufferSize, lineIndent, lineEnd);
5471+
_pEditView->getGenericText(linebuf.get(), linebufferSize, lineIndent, lineEnd);
54695472

5470-
wstring linebufStr = linebuf;
5471-
delete [] linebuf;
5473+
std::string linebufStr = linebuf.get();
54725474

54735475
if (currCommentMode != cm_comment) // uncomment/toggle
54745476
{
54755477
if (!isSingleLineAdvancedMode)
54765478
{
54775479
// In order to do get case insensitive comparison use strnicmp() instead case-sensitive comparison.
54785480
// Case insensitive comparison is needed e.g. for "REM" and "rem" in Batchfiles.
5479-
if (wcsnicmp(linebufStr.c_str(), comment.c_str(), !(buf->getLangType() == L_BAANC) ? comment_length - 1 : comment_length) == 0)
5481+
if (::_strnicmp(linebufStr.c_str(), comment.c_str(), (buf->getLangType() != L_BAANC) ? comment_length - 1 : comment_length) == 0)
54805482
{
54815483
size_t len = linebufStr[comment_length - 1] == aSpace[0] ? comment_length : !(buf->getLangType() == L_BAANC) ? comment_length - 1 : comment_length;
54825484

@@ -5512,8 +5514,8 @@ bool Notepad_plus::doBlockComment(comment_mode currCommentMode)
55125514
}
55135515
else // isSingleLineAdvancedMode
55145516
{
5515-
if ((wcsnicmp(linebufStr.c_str(), advCommentStart.c_str(), advCommentStart_length - 1) == 0) &&
5516-
(wcsnicmp(linebufStr.substr(linebufStr.length() - advCommentEnd_length + 1, advCommentEnd_length - 1).c_str(), advCommentEnd.substr(1, advCommentEnd_length - 1).c_str(), advCommentEnd_length - 1) == 0))
5517+
if ((::_strnicmp(linebufStr.c_str(), advCommentStart.c_str(), advCommentStart_length - 1) == 0)
5518+
&& (::_strnicmp(linebufStr.substr(linebufStr.length() - advCommentEnd_length + 1, advCommentEnd_length - 1).c_str(), advCommentEnd.substr(1, advCommentEnd_length - 1).c_str(), advCommentEnd_length - 1) == 0))
55175519
{
55185520
size_t startLen = linebufStr[advCommentStart_length - 1] == aSpace[0] ? advCommentStart_length : advCommentStart_length - 1;
55195521
size_t endLen = linebufStr[linebufStr.length() - advCommentEnd_length] == aSpace[0] ? advCommentEnd_length : advCommentEnd_length - 1;
@@ -5626,15 +5628,15 @@ bool Notepad_plus::doBlockComment(comment_mode currCommentMode)
56265628

56275629
bool Notepad_plus::doStreamComment()
56285630
{
5629-
const wchar_t *commentStart;
5630-
const wchar_t *commentEnd;
5631+
const char* commentStart = nullptr;
5632+
const char* commentEnd = nullptr;
56315633

5632-
wstring symbolStart;
5633-
wstring symbolEnd;
5634+
std::string symbolStart;
5635+
std::string symbolEnd;
56345636

56355637
// BlockToStreamComment:
5636-
const wchar_t *commentLineSymbol;
5637-
wstring symbol;
5638+
const char* commentLineSymbol = nullptr;
5639+
std::string symbol;
56385640

56395641
Buffer * buf = _pEditView->getCurrentBuffer();
56405642
// Avoid side-effects (e.g. cursor moves number of comment-characters) when file is read-only.
@@ -5648,13 +5650,15 @@ bool Notepad_plus::doStreamComment()
56485650
if (!userLangContainer)
56495651
return false;
56505652

5653+
const std::string kws = wstring2string(userLangContainer->_keywordLists[SCE_USER_KWLIST_COMMENTS]);
5654+
56515655
// BlockToStreamComment: Next two lines needed to decide, if block-comment can be called below!
5652-
symbol = extractSymbol('0', '0', userLangContainer->_keywordLists[SCE_USER_KWLIST_COMMENTS]);
5656+
symbol = extractSymbol('0', '0', kws.c_str());
56535657
commentLineSymbol = symbol.c_str();
56545658

5655-
symbolStart = extractSymbol('0', '3', userLangContainer->_keywordLists[SCE_USER_KWLIST_COMMENTS]);
5659+
symbolStart = extractSymbol('0', '3', kws.c_str());
56565660
commentStart = symbolStart.c_str();
5657-
symbolEnd = extractSymbol('0', '4', userLangContainer->_keywordLists[SCE_USER_KWLIST_COMMENTS]);
5661+
symbolEnd = extractSymbol('0', '4', kws.c_str());
56585662
commentEnd = symbolEnd.c_str();
56595663
}
56605664
else
@@ -5674,9 +5678,9 @@ bool Notepad_plus::doStreamComment()
56745678
return false;
56755679
}
56765680

5677-
wstring start_comment(commentStart);
5678-
wstring end_comment(commentEnd);
5679-
wstring white_space(L" ");
5681+
std::string start_comment(commentStart);
5682+
std::string end_comment(commentEnd);
5683+
std::string white_space(" ");
56805684

56815685
start_comment += white_space;
56825686
white_space += end_comment;
@@ -8752,16 +8756,16 @@ DWORD WINAPI Notepad_plus::backupDocument(void * /*param*/)
87528756
//-- undoStreamComment: New function to undo stream comment around or within selection end-points.
87538757
bool Notepad_plus::undoStreamComment(bool tryBlockComment)
87548758
{
8755-
const wchar_t* commentStart;
8756-
const wchar_t* commentEnd;
8757-
const wchar_t* commentLineSymbol;
8759+
const char* commentStart = nullptr;
8760+
const char* commentEnd = nullptr;
8761+
const char* commentLineSymbol = nullptr;
87588762

8759-
wstring symbolStart;
8760-
wstring symbolEnd;
8761-
wstring symbol;
8763+
std::string symbolStart;
8764+
std::string symbolEnd;
8765+
std::string symbol;
87628766

8763-
const int charbufLen = 10;
8764-
wchar_t charbuf[charbufLen]{};
8767+
static constexpr int charbufLen = 10;
8768+
char charbuf[charbufLen]{};
87658769

87668770
bool retVal = false;
87678771

@@ -8775,11 +8779,13 @@ bool Notepad_plus::undoStreamComment(bool tryBlockComment)
87758779
if (!userLangContainer)
87768780
return false;
87778781

8778-
symbol = extractSymbol('0', '0', userLangContainer->_keywordLists[SCE_USER_KWLIST_COMMENTS]);
8782+
const std::string kws = wstring2string(userLangContainer->_keywordLists[SCE_USER_KWLIST_COMMENTS]);
8783+
8784+
symbol = extractSymbol('0', '0', kws.c_str());
87798785
commentLineSymbol = symbol.c_str();
8780-
symbolStart = extractSymbol('0', '3', userLangContainer->_keywordLists[SCE_USER_KWLIST_COMMENTS]);
8786+
symbolStart = extractSymbol('0', '3', kws.c_str());
87818787
commentStart = symbolStart.c_str();
8782-
symbolEnd = extractSymbol('0', '4', userLangContainer->_keywordLists[SCE_USER_KWLIST_COMMENTS]);
8788+
symbolEnd = extractSymbol('0', '4', kws.c_str());
87838789
commentEnd = symbolEnd.c_str();
87848790
}
87858791
else
@@ -8799,9 +8805,9 @@ bool Notepad_plus::undoStreamComment(bool tryBlockComment)
87998805
return false;
88008806
}
88018807

8802-
wstring start_comment(commentStart);
8803-
wstring end_comment(commentEnd);
8804-
wstring white_space(L" ");
8808+
std::string start_comment(commentStart);
8809+
std::string end_comment(commentEnd);
8810+
std::string white_space(" ");
88058811
size_t start_comment_length = start_comment.length();
88068812
size_t end_comment_length = end_comment.length();
88078813

@@ -8837,14 +8843,14 @@ bool Notepad_plus::undoStreamComment(bool tryBlockComment)
88378843
//-- When searching upwards the start-position for searching must be moved one after the current position
88388844
// to find a search-string just starting before the current position!
88398845
//-- Direction DIR_UP ---
8840-
posStartCommentBefore[iSelStart] = _pEditView->searchInTarget(start_comment.c_str(), start_comment_length, selectionStart, 0);
8846+
posStartCommentBefore[iSelStart] = _pEditView->searchInTarget(start_comment, selectionStart, 0);
88418847
(posStartCommentBefore[iSelStart] == -1 ? blnStartCommentBefore[iSelStart] = false : blnStartCommentBefore[iSelStart] = true);
8842-
posEndCommentBefore[iSelStart] = _pEditView->searchInTarget(end_comment.c_str(), end_comment_length, selectionStart, 0);
8848+
posEndCommentBefore[iSelStart] = _pEditView->searchInTarget(end_comment, selectionStart, 0);
88438849
(posEndCommentBefore[iSelStart] == -1 ? blnEndCommentBefore[iSelStart] = false : blnEndCommentBefore[iSelStart] = true);
88448850
//-- Direction DIR_DOWN ---
8845-
posStartCommentAfter[iSelStart] = _pEditView->searchInTarget(start_comment.c_str(), start_comment_length, selectionStart, docLength);
8851+
posStartCommentAfter[iSelStart] = _pEditView->searchInTarget(start_comment, selectionStart, docLength);
88468852
(posStartCommentAfter[iSelStart] == -1 ? blnStartCommentAfter[iSelStart] = false : blnStartCommentAfter[iSelStart] = true);
8847-
posEndCommentAfter[iSelStart] = _pEditView->searchInTarget(end_comment.c_str(), end_comment_length, selectionStart, docLength);
8853+
posEndCommentAfter[iSelStart] = _pEditView->searchInTarget(end_comment, selectionStart, docLength);
88488854
(posEndCommentAfter[iSelStart] == -1 ? blnEndCommentAfter[iSelStart] = false : blnEndCommentAfter[iSelStart] = true);
88498855

88508856
//-- Check, if selectionStart or selectionEnd is within a stream comment -----
@@ -8862,14 +8868,14 @@ bool Notepad_plus::undoStreamComment(bool tryBlockComment)
88628868
{
88638869
//-- Find all start- and end-comments before and after the selectionEnd position.
88648870
//-- Direction DIR_UP ---
8865-
posStartCommentBefore[iSelEnd] = _pEditView->searchInTarget(start_comment.c_str(), start_comment_length, selectionEnd, 0);
8871+
posStartCommentBefore[iSelEnd] = _pEditView->searchInTarget(start_comment, selectionEnd, 0);
88668872
(posStartCommentBefore[iSelEnd] == -1 ? blnStartCommentBefore[iSelEnd] = false : blnStartCommentBefore[iSelEnd] = true);
8867-
posEndCommentBefore[iSelEnd] = _pEditView->searchInTarget(end_comment.c_str(), end_comment_length, selectionEnd, 0);
8873+
posEndCommentBefore[iSelEnd] = _pEditView->searchInTarget(end_comment, selectionEnd, 0);
88688874
(posEndCommentBefore[iSelEnd] == -1 ? blnEndCommentBefore[iSelEnd] = false : blnEndCommentBefore[iSelEnd] = true);
88698875
//-- Direction DIR_DOWN ---
8870-
posStartCommentAfter[iSelEnd] = _pEditView->searchInTarget(start_comment.c_str(), start_comment_length, selectionEnd, docLength);
8876+
posStartCommentAfter[iSelEnd] = _pEditView->searchInTarget(start_comment, selectionEnd, docLength);
88718877
(posStartCommentAfter[iSelEnd] == -1 ? blnStartCommentAfter[iSelEnd] = false : blnStartCommentAfter[iSelEnd] = true);
8872-
posEndCommentAfter[iSelEnd] = _pEditView->searchInTarget(end_comment.c_str(), end_comment_length, selectionEnd, docLength);
8878+
posEndCommentAfter[iSelEnd] = _pEditView->searchInTarget(end_comment, selectionEnd, docLength);
88738879
(posEndCommentAfter[iSelEnd] == -1 ? blnEndCommentAfter[iSelEnd] = false : blnEndCommentAfter[iSelEnd] = true);
88748880

88758881
if ((blnStartCommentBefore[iSelEnd] && blnEndCommentAfter[iSelEnd])
@@ -8904,7 +8910,7 @@ bool Notepad_plus::undoStreamComment(bool tryBlockComment)
89048910
//-- First delete end-comment, so that posStartCommentBefore does not change!
89058911
//-- Get character before end-comment to decide, if there is a white character before the end-comment, which will be removed too!
89068912
_pEditView->getGenericText(charbuf, charbufLen, posEndComment - 1, posEndComment);
8907-
if (wcsnicmp(charbuf, white_space.c_str(), white_space.length()) == 0)
8913+
if (::_strnicmp(charbuf, white_space.c_str(), white_space.length()) == 0)
89088914
{
89098915
endCommentLength += 1;
89108916
posEndComment -= 1;
@@ -8916,7 +8922,7 @@ bool Notepad_plus::undoStreamComment(bool tryBlockComment)
89168922

89178923
//-- Get character after start-comment to decide, if there is a white character after the start-comment, which will be removed too!
89188924
_pEditView->getGenericText(charbuf, charbufLen, posStartComment + startCommentLength, posStartComment + startCommentLength + 1);
8919-
if (wcsnicmp(charbuf, white_space.c_str(), white_space.length()) == 0)
8925+
if (::_strnicmp(charbuf, white_space.c_str(), white_space.length()) == 0)
89208926
startCommentLength += 1;
89218927

89228928
//-- Delete starting stream-comment string ---------

PowerEditor/src/Parameters.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@
4545
#include "Common.h"
4646
#include "ContextMenu.h"
4747
#include "Notepad_plus_Window.h"
48+
#include "Notepad_plus_msgs.h"
4849
#include "NppConstants.h"
4950
#include "NppDarkMode.h"
5051
#include "NppXml.h"
5152
#include "ScintillaEditView.h"
5253
#include "TabBar.h"
54+
#include "ToolBar.h"
5355
#include "UserDefineDialog.h"
5456
#include "WordStyleDlg.h"
5557
#include "keys.h"
@@ -3800,7 +3802,7 @@ void NppParameters::feedScintKeys(const NppXml::Element& element)
38003802

38013803
bool NppParameters::getInternalCommandShortcuts(const NppXml::Element& element, CommandShortcut& cs, std::string* folderName)
38023804
{
3803-
if (!element) return false;
3805+
assert(element && "Element in NppParameters::getInternalCommandShortcuts is null node.");
38043806

38053807
const char* name = NppXml::attribute(element, "name", "");
38063808

@@ -3830,7 +3832,7 @@ bool NppParameters::getInternalCommandShortcuts(const NppXml::Element& element,
38303832

38313833
bool NppParameters::getShortcuts(const NppXml::Element& element, Shortcut& sc, std::string* folderName)
38323834
{
3833-
if (!element) return false;
3835+
assert(element && "Element in NppParameters::getShortcuts is null node.");
38343836

38353837
const char* name = NppXml::attribute(element, "name", "");
38363838

@@ -3979,7 +3981,7 @@ LangType NppParameters::getLangFromExt(const wchar_t *ext)
39793981
Lang *l = getLangFromIndex(i--);
39803982
const wchar_t *defList = l->getDefaultExtList();
39813983

3982-
if (defList && isInList(ext, defList))
3984+
if (defList[0] && isInList(ext, defList))
39833985
return l->getLangID();
39843986
}
39853987
return L_TEXT;
@@ -5524,16 +5526,23 @@ void NppParameters::feedKeyWordsParameters(TiXmlNode* node)
55245526
const wchar_t* name = element->Attribute(L"name");
55255527
if (name)
55265528
{
5527-
_langList[_nbLang] = std::make_unique<Lang>(getLangIDFromStr(name), name);
5528-
_langList[_nbLang]->setDefaultExtList(element->Attribute(L"ext"));
5529-
_langList[_nbLang]->setCommentLineSymbol(element->Attribute(L"commentLine"));
5530-
_langList[_nbLang]->setCommentStart(element->Attribute(L"commentStart"));
5531-
_langList[_nbLang]->setCommentEnd(element->Attribute(L"commentEnd"));
5529+
const wchar_t* ext = element->Attribute(L"ext");
55325530

5533-
int tabSettings;
5531+
const wchar_t* commentLine = element->Attribute(L"commentLine");
5532+
const wchar_t* commentStart = element->Attribute(L"commentStart");
5533+
const wchar_t* commentEnd = element->Attribute(L"commentEnd");
5534+
5535+
const std::string cmtLine = commentLine ? wstring2string(commentLine) : "";
5536+
const std::string cmtStart = commentStart ? wstring2string(commentStart) : "";
5537+
const std::string cmtEnd = commentEnd ? wstring2string(commentEnd) : "";
5538+
5539+
int tabSettings = -1;
55345540
const wchar_t* tsVal = element->Attribute(L"tabSettings", &tabSettings);
55355541
const wchar_t* buVal = element->Attribute(L"backspaceUnindent");
5536-
_langList[_nbLang]->setTabInfo(tsVal ? tabSettings : -1, buVal && !lstrcmp(buVal, L"yes"));
5542+
5543+
_langList[_nbLang] = std::make_unique<Lang>(getLangIDFromStr(name), name, ext ? ext : L"",
5544+
cmtLine.c_str(), cmtStart.c_str(), cmtEnd.c_str(),
5545+
tsVal ? tabSettings : -1, buVal && (std::wcscmp(buVal, L"yes") == 0));
55375546

55385547
for (TiXmlNode* kwNode = langNode->FirstChildElement(L"Keywords");
55395548
kwNode;
@@ -6333,7 +6342,7 @@ void NppParameters::feedGUIParameters(TiXmlNode *node)
63336342
newFormat = EolType::unix;
63346343
break;
63356344
default:
6336-
assert(false and "invalid buffer format - fallback to default");
6345+
assert(false && "invalid buffer format - fallback to default");
63376346
}
63386347
_nppGUI._newDocDefaultSettings._format = newFormat;
63396348
}

0 commit comments

Comments
 (0)