bug-fix: sort comparator should be irreflexive

This commit is contained in:
Le Tan 2018-01-29 19:31:53 +08:00
parent 77c664b773
commit 17b5ac5f66
2 changed files with 11 additions and 42 deletions

View File

@ -228,7 +228,7 @@ static bool compHLUnit(const HLUnit &p_a, const HLUnit &p_b)
if (p_a.start < p_b.start) {
return true;
} else if (p_a.start == p_b.start) {
return p_a.length >= p_b.length;
return p_a.length > p_b.length;
} else {
return false;
}
@ -308,15 +308,14 @@ void HGMarkdownHighlighter::initHtmlCommentRegionsFromResult()
void HGMarkdownHighlighter::initImageRegionsFromResult()
{
if (!result) {
// From Qt5.7, the capacity is preserved.
m_imageRegions.clear();
if (!result) {
emit imageLinksUpdated(m_imageRegions);
return;
}
int idx = 0;
int oriSize = m_imageRegions.size();
pmh_element *elem = result[pmh_IMAGE];
while (elem != NULL) {
if (elem->end <= elem->pos) {
@ -324,25 +323,10 @@ void HGMarkdownHighlighter::initImageRegionsFromResult()
continue;
}
if (idx < oriSize) {
// Try to reuse the original element.
VElementRegion &reg = m_imageRegions[idx];
if ((int)elem->pos != reg.m_startPos || (int)elem->end != reg.m_endPos) {
reg.m_startPos = (int)elem->pos;
reg.m_endPos = (int)elem->end;
}
} else {
m_imageRegions.push_back(VElementRegion(elem->pos, elem->end));
}
++idx;
elem = elem->next;
}
if (idx < oriSize) {
m_imageRegions.resize(idx);
}
qDebug() << "highlighter: parse" << m_imageRegions.size() << "image regions";
emit imageLinksUpdated(m_imageRegions);
@ -350,17 +334,15 @@ void HGMarkdownHighlighter::initImageRegionsFromResult()
void HGMarkdownHighlighter::initHeaderRegionsFromResult()
{
// From Qt5.7, the capacity is preserved.
m_headerBlocks.clear();
m_headerRegions.clear();
if (!result) {
// From Qt5.7, the capacity is preserved.
m_headerRegions.clear();
emit headersUpdated(m_headerRegions);
return;
}
int idx = 0;
int oriSize = m_headerRegions.size();
pmh_element_type hx[6] = {pmh_H1, pmh_H2, pmh_H3, pmh_H4, pmh_H5, pmh_H6};
for (int i = 0; i < 6; ++i) {
pmh_element *elem = result[hx[i]];
@ -371,16 +353,7 @@ void HGMarkdownHighlighter::initHeaderRegionsFromResult()
continue;
}
if (idx < oriSize) {
// Try to reuse the original element.
VElementRegion &reg = m_headerRegions[idx];
if ((int)elem->pos != reg.m_startPos || (int)elem->end != reg.m_endPos) {
reg.m_startPos = (int)elem->pos;
reg.m_endPos = (int)elem->end;
}
} else {
m_headerRegions.push_back(VElementRegion(elem->pos, elem->end));
}
QTextBlock block = document->findBlock(elem->pos);
if (block.isValid()) {
@ -388,15 +361,10 @@ void HGMarkdownHighlighter::initHeaderRegionsFromResult()
m_headerBlocks.insert(block.blockNumber(), HeaderBlockInfo(i, elem->end - elem->pos - 1));
}
++idx;
elem = elem->next;
}
}
if (idx < oriSize) {
m_headerRegions.resize(idx);
}
std::sort(m_headerRegions.begin(), m_headerRegions.end());
qDebug() << "highlighter: parse" << m_headerRegions.size() << "header regions";
@ -714,7 +682,7 @@ static bool compHLUnitStyle(const HLUnitStyle &a, const HLUnitStyle &b)
if (a.start < b.start) {
return true;
} else if (a.start == b.start) {
return a.length >= b.length;
return a.length > b.length;
} else {
return false;
}

View File

@ -101,7 +101,8 @@ struct VElementRegion
if (m_startPos < p_other.m_startPos) {
return true;
} else if (m_startPos == p_other.m_startPos) {
return m_endPos <= p_other.m_endPos;
// If a < b is true, then b < a must be false.
return m_endPos < p_other.m_endPos;
} else {
return false;
}