Skip to content

Commit

Permalink
Fix small(4bit) memory leak when using options
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-gromeyer committed Jan 19, 2023
1 parent 7e0c133 commit 8dbb7ff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
12 changes: 9 additions & 3 deletions include/html2md.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ struct Options {
* Default is true.
*/
bool includeTitle = true;

inline bool operator==(html2md::Options o) const {
return splitLines == o.splitLines && unorderedList == o.unorderedList &&
orderedList == o.orderedList && includeTitle == o.includeTitle;
};
};

/*!
Expand Down Expand Up @@ -324,7 +329,7 @@ class Converter {
std::string md_;
size_t md_len_ = 0;

struct Options *option{};
Options option;

// Tag: base class for tag types
struct Tag {
Expand Down Expand Up @@ -487,7 +492,7 @@ class Converter {

std::map<std::string, std::shared_ptr<Tag>> tags_;

explicit Converter(std::string *html, struct Options *options = nullptr);
explicit Converter(std::string *html, struct Options *options);

void PrepareHtml();

Expand Down Expand Up @@ -561,13 +566,14 @@ class Converter {
// meta: not ignored to tolerate if closing is omitted
}

// Here we can improve the performance
[[nodiscard]] bool IsInIgnoredTag() const {
auto len = dom_tags_.size();

for (auto i = 0; i < len; ++i) {
std::string tag = dom_tags_[i];

if (tag == kTagTitle && !option->includeTitle)
if (tag == kTagTitle && !option.includeTitle)
return true;

if (tag == kTagPre || tag == kTagTitle)
Expand Down
12 changes: 8 additions & 4 deletions src/html2md.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ static string Repeat(const string &str, size_t amount) {
namespace html2md {

Converter::Converter(string *html, Options *options) : html_(*html) {
options ? option = options : option = new struct Options();
if (options)
option = *options;

PrepareHtml();

Expand Down Expand Up @@ -543,7 +544,7 @@ bool Converter::ParseCharInTagContent(char ch) {

if (chars_in_curr_line_ > 80 && !is_in_table_ && !is_in_list_ &&
current_tag_ != kTagImg && current_tag_ != kTagAnchor &&
option->splitLines) {
option.splitLines) {
if (ch == ' ') { // If the next char is - it will become a list
md_ += '\n';
++md_len_;
Expand All @@ -564,6 +565,9 @@ bool Converter::ReplacePreviousSpaceInLineByNewline() {
UpdateMdLen();
auto offset = md_len_ - 1;

if (md_len_ == 0)
return true;

do {
if (md_[offset] == '\n')
return false;
Expand Down Expand Up @@ -754,14 +758,14 @@ void Converter::TagListItem::OnHasLeftOpeningTag(Converter *c) {
return;

if (!c->is_in_ordered_list_) {
c->appendToMd(string({c->option->unorderedList, ' '}));
c->appendToMd(string({c->option.unorderedList, ' '}));
return;
}

++c->index_ol;

string num = std::to_string(c->index_ol);
num.append({c->option->orderedList, ' '});
num.append({c->option.orderedList, ' '});
c->appendToMd(num);
}

Expand Down

0 comments on commit 8dbb7ff

Please sign in to comment.