Strip whitespace from headlines.
This commit is contained in:
parent
da22a54a8a
commit
94564fa914
|
@ -194,12 +194,6 @@ match_context context(const boost::match_results<string::const_iterator> &match,
|
||||||
auto pos_after{begin_after};
|
auto pos_after{begin_after};
|
||||||
|
|
||||||
const std::array<char, 4> whitespace{' ', '\n', '\r', '\t'};
|
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)
|
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)
|
string headline(const std::string_view prefix)
|
||||||
{
|
{
|
||||||
size_t pos{prefix.length()};
|
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;
|
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 {};
|
return {};
|
||||||
|
@ -272,4 +277,11 @@ string page(const std::string_view prefix)
|
||||||
return {};
|
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
|
} // namespace epubgrep::search
|
||||||
|
|
|
@ -71,6 +71,9 @@ context(const boost::match_results<std::string::const_iterator> &match,
|
||||||
//! Return current page if possible.
|
//! Return current page if possible.
|
||||||
[[nodiscard]] std::string page(std::string_view prefix);
|
[[nodiscard]] std::string page(std::string_view prefix);
|
||||||
|
|
||||||
|
//! Return true if check is whitespace.
|
||||||
|
[[nodiscard]] bool is_whitespace(char check);
|
||||||
|
|
||||||
} // namespace epubgrep::search
|
} // namespace epubgrep::search
|
||||||
|
|
||||||
#endif // EPUBGREP_SEARCH_HPP
|
#endif // EPUBGREP_SEARCH_HPP
|
||||||
|
|
Loading…
Reference in New Issue
Block a user