Skip to content

Commit e3f5928

Browse files
committed
Improve performance
1 parent 11df5e6 commit e3f5928

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

src/html2md.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Converter::Converter(const string *html, Options *options) : html_(*html) {
9292
if (options)
9393
option = *options;
9494

95+
md_.reserve(html->size() * 0.8);
9596
tags_.reserve(41);
9697

9798
// non-printing tags
@@ -153,6 +154,26 @@ Converter::Converter(const string *html, Options *options) : html_(*html) {
153154
}
154155

155156
void Converter::CleanUpMarkdown() {
157+
std::string buffer;
158+
buffer.reserve(md_.size());
159+
160+
// Replace HTML symbols during the initial pass
161+
for (size_t i = 0; i < md_.size();) {
162+
bool replaced = false;
163+
for (const auto &[symbol, replacement] : htmlSymbolConversions_) {
164+
if (md_.compare(i, symbol.size(), symbol) == 0) {
165+
buffer += replacement;
166+
i += symbol.size();
167+
replaced = true;
168+
break;
169+
}
170+
}
171+
if (!replaced) {
172+
buffer += md_[i++];
173+
}
174+
}
175+
176+
md_ = std::move(buffer);
156177
TidyAllLines(&md_);
157178

158179
ReplaceAll(&md_, " , ", ", ");
@@ -161,12 +182,6 @@ void Converter::CleanUpMarkdown() {
161182
ReplaceAll(&md_, "\n\n", "\n");
162183
ReplaceAll(&md_, "\n*\n", "\n");
163184
ReplaceAll(&md_, "\n. ", ".\n");
164-
165-
// Replace using the conversions map
166-
for (const auto &conversion : htmlSymbolConversions_) {
167-
ReplaceAll(&md_, conversion.first, conversion.second);
168-
}
169-
170185
ReplaceAll(&md_, "\t\t ", "\t\t");
171186
}
172187

0 commit comments

Comments
 (0)