Strip whitespace from headlines.

This commit is contained in:
tastytea 2021-05-30 21:16:24 +02:00
parent da22a54a8a
commit 94564fa914
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
2 changed files with 23 additions and 8 deletions

View File

@ -194,12 +194,6 @@ match_context context(const boost::match_results<string::const_iterator> &match,
auto pos_after{begin_after};
const std::array<char, 4> 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<string::const_iterator> &match,
string headline(const std::string_view prefix)
{
size_t pos{prefix.length()};
while ((pos = prefix.rfind("<H>", pos)) != std::string_view::npos)
if ((pos = prefix.rfind("<H>", 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<char, 4> whitespace{' ', '\n', '\r', '\t'};
return std::any_of(whitespace.begin(), whitespace.end(),
[&check](const char ws) { return check == ws; });
}
} // namespace epubgrep::search

View File

@ -71,6 +71,9 @@ context(const boost::match_results<std::string::const_iterator> &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