Skip to content

Commit

Permalink
Avoid temporary string allocation when (de/en)coding
Browse files Browse the repository at this point in the history
  • Loading branch information
rdipardo committed Dec 28, 2024
1 parent 97d783e commit f07ef73
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
14 changes: 12 additions & 2 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@ Checks: |
bugprone-*,
cppcoreguidelines-*,
-*avoid-c-arrays,
-*avoid-do-while,
-*magic-numbers,
-*avoid-non-const-global-variables,
-*constant-array-index,
-*easily-swappable-parameters,
-*empty-catch,
-*pro-type-reinterpret-cast
-*enum-size,
-*no-int-to-ptr,
-*non-private-member-variables-in-classes,
-*optin.core.EnumCastOutOfRange,
-*pro-bounds-array-to-pointer-decay,
-*pro-type-reinterpret-cast,
-*switch-missing-default-case
CheckOptions:
- key: bugprone-non-zero-enum-to-bool-conversion.EnumIgnoreList
value: 'HtmlTag::SelectionOptions'
WarningsAsErrors: ''
HeaderFilterRegex: ''
AnalyzeTemporaryDtors: false
UseColor : true
FormatStyle: 'none'
12 changes: 6 additions & 6 deletions src/Entities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using namespace HtmlTag;

/////////////////////////////////////////////////////////////////////////////////////////
namespace {
int doEncode(std::wstring &text, EntityList &entities, bool includeLineBreaks);
int doEncode(std::wstring &text, EntityList const &entities, bool includeLineBreaks);
}

// --------------------------------------------------------------------------------------
Expand Down Expand Up @@ -128,7 +128,7 @@ int Entities::decode() {
std::string entityCodeStr;
std::wstring entityCode = target.substr(firstPos, lastPos - firstPos + 1);
TextConv::textToBytes(entityCode.c_str(), entityCodeStr, CP_ACP);
std::string entity = entities[entityCodeStr];
std::string const &entity = entities[entityCodeStr];
if (!entity.empty()) {
codePoint = std::stoi(entity);
isValid = (codePoint != 0);
Expand All @@ -146,7 +146,7 @@ int Entities::decode() {
} else {
*decoded = static_cast<wchar_t>(codePoint);
}
target = target.substr(0, firstPos - 1) + decoded + target.substr(nextIndex);
target.replace(firstPos - 1, std::wstring::npos, decoded + target.substr(nextIndex));
++result;
}

Expand All @@ -165,7 +165,7 @@ int Entities::decode() {

/////////////////////////////////////////////////////////////////////////////////////////
namespace {
int doEncode(std::wstring &text, Entities::EntityList &entities, bool includeLineBreaks) {
int doEncode(std::wstring &text, Entities::EntityList const &entities, bool includeLineBreaks) {
int result = 0;
SciActiveDocument doc = plugin.editor().activeDocument();

Expand All @@ -176,7 +176,7 @@ int doEncode(std::wstring &text, Entities::EntityList &entities, bool includeLin
bool didReplace = false;

try {
for (intptr_t chIndex = text.length() - 1; chIndex >= 0; chIndex--) {
for (intptr_t chIndex = static_cast<intptr_t>(text.length()) - 1; chIndex >= 0; chIndex--) {
size_t startPos = chIndex, endPos = chIndex + 1;
uint32_t charCode = text[chIndex];
std::string entity = entities[std::to_string(charCode)];
Expand All @@ -202,7 +202,7 @@ int doEncode(std::wstring &text, Entities::EntityList &entities, bool includeLin
didReplace = false;

if (didReplace) {
text = text.substr(0, startPos) + L'&' + encodedEntity + L';' + text.substr(endPos);
text.replace(startPos, endPos - startPos, L'&' + encodedEntity + L';');
++result;
}
if (chIndex >= static_cast<intptr_t>(text.length()))
Expand Down
12 changes: 6 additions & 6 deletions src/Unicode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ int Unicode::decode() {
if (doc.getSelectionMode() != smStreamSingle)
return result;

size_t lenPrefix = plugin.options.unicodePrefix.size();
Sci_Position lenPrefix = static_cast<Sci_Position>(plugin.options.unicodePrefix.size());
std::wstring pattern(plugin.options.unicodeRE.size() + 1, L'\0');
TextConv::bytesToText(plugin.options.unicodeRE.c_str(), pattern, CP_ACP);
SciTextRange target(doc, doc.currentSelection().startPos(), doc.currentSelection().endPos());
SciTextRange match{ doc };
wchar_t mbCharBuf[3]{};
std::wstring mbCharBuf(3, L'\0');

doc.sendMessage(SCI_BEGINUNDOACTION);
try {
Expand All @@ -82,7 +82,7 @@ int Unicode::decode() {
head = ((head - 0x10000) >> 10) + 0xD800;
mbCharBuf[0] = static_cast<wchar_t>(head);
mbCharBuf[1] = static_cast<wchar_t>(tail);
match = &std::wstring(mbCharBuf)[0];
match = mbCharBuf;
} else if (head >= 0xD800 && head <= 0xDBFF) {
SciTextRange matchNext{ doc };
doc.find(&pattern[0], matchNext, SCFIND_REGEXP, match.endPos() - lenPrefix,
Expand All @@ -93,7 +93,7 @@ int Unicode::decode() {
mbCharBuf[0] = static_cast<wchar_t>(head);
mbCharBuf[1] = static_cast<wchar_t>(tail);
matchNext = L"";
match = &std::wstring(mbCharBuf)[0];
match = mbCharBuf;

if (result < 1)
doc.currentSelection().startPos(match.startPos());
Expand All @@ -102,7 +102,7 @@ int Unicode::decode() {
} else {
mbCharBuf[0] = static_cast<wchar_t>(head);
mbCharBuf[1] = 0;
match = &std::wstring(mbCharBuf)[0];
match = mbCharBuf;
}

if (result < 1)
Expand Down Expand Up @@ -131,7 +131,7 @@ int doEncode(std::wstring &text, bool multiSel) {
std::wstring prefix(plugin.options.unicodePrefix.size() + 1, L'\0');
TextConv::bytesToText(plugin.options.unicodePrefix.c_str(), prefix, CP_ACP);

for (intptr_t chIndex = text.length() - 1; chIndex >= 0; chIndex--) {
for (intptr_t chIndex = static_cast<intptr_t>(text.length()) - 1; chIndex >= 0; chIndex--) {
uint32_t charCode = text[chIndex];
if (charCode > 127) {
std::wstringstream encoded;
Expand Down

0 comments on commit f07ef73

Please sign in to comment.