From a03f8957aa98942a7b58602e0fbd1db536b89ce6 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Wed, 17 Sep 2014 14:26:51 +0200 Subject: [PATCH] Rewrote empty line stripping in manual display The original code was incorrect: it used erase() on the end() iterator. Also, it only checked for an initially empty page, but could not deal with a page containing only empty lines, which will become an empty page during the empty line removal. The check for a line being empty is now done by searching the line for non-whitespace characters instead of by trimming it. This avoids creating new string objects that are only used to check whether they are empty. --- src/textmanualdialog.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/textmanualdialog.cpp b/src/textmanualdialog.cpp index 934952e..9f748e6 100644 --- a/src/textmanualdialog.cpp +++ b/src/textmanualdialog.cpp @@ -54,16 +54,18 @@ TextManualDialog::TextManualDialog(GMenu2X *gmenu2x, const string &title, const pages.push_back(mp); } - //delete first and last blank lines from each page - for (uint page=0; page 0) { - //first lines - while (trim(pages[page].text[0]).empty()) - pages[page].text.erase(pages[page].text.begin()); - //last lines - while (trim(pages[page].text[pages[page].text.size()-1]).empty()) - pages[page].text.erase(pages[page].text.end()); - } + //delete blank lines at the start and end of each page + for (auto& page : pages) { + //start lines + auto it = page.text.begin(); + while (it != page.text.end() && + it->find_first_not_of(" \t\r") == string::npos) ++it; + page.text.erase(page.text.begin(), it); + //end lines + it = page.text.end(); + while (it != page.text.begin() && + (it - 1)->find_first_not_of(" \t\r") == string::npos) --it; + page.text.erase(it, page.text.end()); } }