Skip to content

Commit

Permalink
Make addHotSpot more efficient.
Browse files Browse the repository at this point in the history
  • Loading branch information
Coises committed Mar 21, 2024
1 parent 0bc28df commit 0ac1812
Showing 1 changed file with 27 additions and 48 deletions.
75 changes: 27 additions & 48 deletions PowerEditor/src/Notepad_plus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3431,56 +3431,35 @@ void Notepad_plus::addHotSpot(ScintillaEditView* view)
LRESULT indicFore = pView->execute(SCI_STYLEGETFORE, STYLE_DEFAULT);
pView->execute(SCI_SETINDICATORVALUE, indicFore);

UINT cp = static_cast<UINT>(pView->execute(SCI_GETCODEPAGE));
char* encodedText = nullptr;
try {
encodedText = new char[endPos - startPos + 1];
}
catch (const std::bad_alloc&)
{
return;
}

pView->getText(encodedText, startPos, endPos);
TCHAR* wideText = nullptr;
try
{
wideText = new TCHAR[endPos - startPos + 1];
}
catch (const std::bad_alloc&)
{
delete[] encodedText;
return;
}

int wideTextLen = MultiByteToWideChar(cp, 0, encodedText, static_cast<int>(endPos - startPos + 1), (LPWSTR) wideText, static_cast<int>(endPos - startPos + 1)) - 1;
delete[] encodedText;
if (wideTextLen > 0)
{
int startWide = 0;
int lenWide = 0;
int startEncoded = 0;
int lenEncoded = 0;
while (true)
{
bool r = isUrl(wideText, wideTextLen, startWide, & lenWide);
if (lenWide <= 0)
break;
assert ((startWide + lenWide) <= wideTextLen);
lenEncoded = WideCharToMultiByte(cp, 0, & wideText [startWide], lenWide, NULL, 0, NULL, NULL);
if (r)
pView->execute(SCI_INDICATORFILLRANGE, startEncoded + startPos, lenEncoded);
else
pView->execute(SCI_INDICATORCLEARRANGE, startEncoded + startPos, lenEncoded);
startWide += lenWide;
startEncoded += lenEncoded;
if ((startWide >= wideTextLen) || ((startEncoded + startPos) >= endPos))
break;
std::string rxURL;
std::wstring userSchemes = (NppParameters::getInstance()).getNppGUI()._uriSchemes;
if (!userSchemes.empty() && userSchemes.find_first_not_of(L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.+-:// ") == std::wstring::npos) {
rxURL.resize(userSchemes.length());
WideCharToMultiByte(0, 0, userSchemes.data(), static_cast<int>(userSchemes.length()),rxURL.data(), static_cast<int>(rxURL.length()), 0, 0);
size_t i = rxURL.find_first_not_of(' ');
if (i != std::wstring::npos) {
size_t j = rxURL.find_last_not_of(' ');
rxURL = rxURL.substr(i, j - i + 1);
for (; i = rxURL.find_first_of(' ', i), i != std::wstring::npos;) rxURL.replace(i, rxURL.find_first_not_of(' ', i) - i, 1, '|');
rxURL += '|';
}
}
rxURL += "ftp://|http://|https://|mailto:|file://";
rxURL = "(?<=<)(?:" + rxURL + ")[^\\r\\n>]*+(?=>)|\\b(?:" + rxURL + ")([0-9A-Z:/?#[\\]@!$&'*+,;=\\-._~%]++|\\((?1)*+\\))++";

pView->execute(SCI_SETSEARCHFLAGS, SCFIND_REGEXP | SCFIND_POSIX);
while (startPos < endPos) {
pView->execute(SCI_SETTARGETRANGE, startPos, endPos);
intptr_t startURL = pView->execute(SCI_SEARCHINTARGET, rxURL.length(), reinterpret_cast<LPARAM>(rxURL.data()));
if (startURL < startPos) {
pView->execute(SCI_INDICATORCLEARRANGE, startPos, endPos - startPos);
break;
}
assert ((startEncoded + startPos) == endPos);
assert (startWide == wideTextLen);
if (startURL > startPos) pView->execute(SCI_INDICATORCLEARRANGE, startPos, startURL - startPos);
intptr_t endURL = pView->execute(SCI_GETTARGETEND);
pView->execute(SCI_INDICATORFILLRANGE, startURL, endURL - startURL);
startPos = endURL;
}
delete[] wideText;
}

bool Notepad_plus::isConditionExprLine(intptr_t lineNumber)
Expand Down

0 comments on commit 0ac1812

Please sign in to comment.