From 94564fa914c883c701c08f30a786fb5dcbefc86b Mon Sep 17 00:00:00 2001 From: tastytea Date: Sun, 30 May 2021 21:16:24 +0200 Subject: [PATCH] Strip whitespace from headlines. --- src/search.cpp | 28 ++++++++++++++++++++-------- src/search.hpp | 3 +++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/search.cpp b/src/search.cpp index 6850a26..41e9329 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -194,12 +194,6 @@ match_context context(const boost::match_results &match, auto pos_after{begin_after}; const std::array whitespace{' ', '\n', '\r', '\t'}; - auto is_whitespace{ - [&whitespace](char check) - { - return std::any_of(whitespace.begin(), whitespace.end(), - [&check](const char ws) { return check == ws; }); - }}; while (words != 0) { @@ -251,10 +245,21 @@ match_context context(const boost::match_results &match, string headline(const std::string_view prefix) { size_t pos{prefix.length()}; - while ((pos = prefix.rfind("", pos)) != std::string_view::npos) + if ((pos = prefix.rfind("", pos)) != std::string_view::npos) { pos += 3; - return string{prefix.substr(pos, prefix.find('<', pos) - pos)}; + string result{prefix.substr(pos, prefix.find('<', pos) - pos)}; + + while (is_whitespace(*result.begin())) + { + result.erase(0, 1); + } + while (is_whitespace(*result.rbegin())) + { + result.erase(result.size() - 1); + } + + return result; } return {}; @@ -272,4 +277,11 @@ string page(const std::string_view prefix) return {}; } +bool is_whitespace(const char check) +{ + const std::array whitespace{' ', '\n', '\r', '\t'}; + return std::any_of(whitespace.begin(), whitespace.end(), + [&check](const char ws) { return check == ws; }); +} + } // namespace epubgrep::search diff --git a/src/search.hpp b/src/search.hpp index 0981729..5abd1e3 100644 --- a/src/search.hpp +++ b/src/search.hpp @@ -71,6 +71,9 @@ context(const boost::match_results &match, //! Return current page if possible. [[nodiscard]] std::string page(std::string_view prefix); +//! Return true if check is whitespace. +[[nodiscard]] bool is_whitespace(char check); + } // namespace epubgrep::search #endif // EPUBGREP_SEARCH_HPP