[HTML Rendering] Do not do division by zero.

We must correctly handle the case if resultStart is equal to resultEnd.
This commit is contained in:
Matthieu Gautier 2019-09-09 14:38:47 +02:00
parent d372cea146
commit fdc291b7c2
1 changed files with 17 additions and 17 deletions

View File

@ -96,27 +96,29 @@ std::string SearchRenderer::getHtml()
auto resultEnd = mp_searcher->getResultEnd(); auto resultEnd = mp_searcher->getResultEnd();
auto resultCountPerPage = resultEnd - resultStart; auto resultCountPerPage = resultEnd - resultStart;
auto estimatedResultCount = mp_searcher->getEstimatedResultCount(); auto estimatedResultCount = mp_searcher->getEstimatedResultCount();
auto currentPage = 0U;
unsigned int pageStart auto pageStart = 0U;
= resultStart / resultCountPerPage >= 5 auto pageEnd = 0U;
? resultStart / resultCountPerPage - 4 auto lastPageStart = 0U;
: 0; if (resultCountPerPage) {
unsigned int pageCount currentPage = resultStart/resultCountPerPage;
= estimatedResultCount / resultCountPerPage + 1 - pageStart; pageStart = currentPage > 4 ? currentPage-4 : 0;
pageEnd = currentPage + 5;
if (pageCount > 10) { if (pageEnd > estimatedResultCount / resultCountPerPage) {
pageCount = 10; pageEnd = estimatedResultCount / resultCountPerPage;
} else if (pageCount == 1) { }
pageCount = 0; if (estimatedResultCount > resultCountPerPage) {
lastPageStart = round(estimatedResultCount/resultCountPerPage) * resultCountPerPage;
}
} }
for (unsigned int i = pageStart; i < pageStart + pageCount; i++) { for (unsigned int i = pageStart; i < pageEnd; i++) {
kainjow::mustache::data page; kainjow::mustache::data page;
page.set("label", to_string(i + 1)); page.set("label", to_string(i + 1));
page.set("start", to_string(i * resultCountPerPage)); page.set("start", to_string(i * resultCountPerPage));
page.set("end", to_string((i + 1) * resultCountPerPage)); page.set("end", to_string((i + 1) * resultCountPerPage));
if (i * resultCountPerPage == resultStart) { if (i == currentPage) {
page.set("selected", true); page.set("selected", true);
} }
pages.push_back(page); pages.push_back(page);
@ -135,9 +137,7 @@ std::string SearchRenderer::getHtml()
allData.set("resultStart", to_string(resultStart + 1)); allData.set("resultStart", to_string(resultStart + 1));
allData.set("resultEnd", to_string(min(resultEnd, estimatedResultCount))); allData.set("resultEnd", to_string(min(resultEnd, estimatedResultCount)));
allData.set("resultRange", to_string(resultCountPerPage)); allData.set("resultRange", to_string(resultCountPerPage));
allData.set("resultLastPageStart", to_string(estimatedResultCount > resultCountPerPage allData.set("resultLastPageStart", to_string(lastPageStart));
? round(estimatedResultCount / resultCountPerPage) * resultCountPerPage
: 0));
allData.set("lastResult", to_string(estimatedResultCount)); allData.set("lastResult", to_string(estimatedResultCount));
allData.set("protocolPrefix", this->protocolPrefix); allData.set("protocolPrefix", this->protocolPrefix);
allData.set("searchProtocolPrefix", this->searchProtocolPrefix); allData.set("searchProtocolPrefix", this->searchProtocolPrefix);