Import std::string into epubgrep::search namespace.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
tastytea 2021-05-26 18:02:27 +02:00
parent fc0aa02bc9
commit fe02b155f5
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
1 changed files with 23 additions and 22 deletions

View File

@ -29,6 +29,8 @@
namespace epubgrep::search namespace epubgrep::search
{ {
using std::string;
std::vector<match> search(const fs::path &filepath, std::string_view regex, std::vector<match> search(const fs::path &filepath, std::string_view regex,
const options &opts) const options &opts)
{ {
@ -68,11 +70,11 @@ std::vector<match> search(const fs::path &filepath, std::string_view regex,
cleanup_text(document); cleanup_text(document);
} }
std::string::const_iterator begin{document.begin()}; string::const_iterator begin{document.begin()};
std::string::const_iterator end{document.end()}; string::const_iterator end{document.end()};
boost::match_results<std::string::const_iterator> match_result; boost::match_results<string::const_iterator> match_result;
std::string last_headline; string last_headline;
std::string last_page; string last_page;
while (boost::regex_search(begin, end, match_result, re, while (boost::regex_search(begin, end, match_result, re,
boost::match_default)) boost::match_default))
@ -102,13 +104,13 @@ std::vector<match> search(const fs::path &filepath, std::string_view regex,
return matches; return matches;
} }
void cleanup_text(std::string &text) void cleanup_text(string &text)
{ {
size_t pos{}; size_t pos{};
while ((pos = text.find('<', pos)) != std::string::npos) while ((pos = text.find('<', pos)) != string::npos)
{ {
// Mark headlines. We need them later on. // Mark headlines. We need them later on.
std::string replacement; string replacement;
if (boost::regex_match(text.substr(pos, 3), boost::regex{"<[hH][1-6]"})) if (boost::regex_match(text.substr(pos, 3), boost::regex{"<[hH][1-6]"}))
{ {
replacement = "<H>"; replacement = "<H>";
@ -135,27 +137,26 @@ void cleanup_text(std::string &text)
} }
pos = 0; pos = 0;
while ((pos = text.find('\r', pos)) != std::string::npos) while ((pos = text.find('\r', pos)) != string::npos)
{ {
text.erase(pos, 1); text.erase(pos, 1);
} }
pos = 0; pos = 0;
while ((pos = text.find('\n', pos)) != std::string::npos) while ((pos = text.find('\n', pos)) != string::npos)
{ {
text.replace(pos, 1, " "); text.replace(pos, 1, " ");
} }
pos = 0; pos = 0;
while ((pos = text.find(" ", pos)) != std::string::npos) while ((pos = text.find(" ", pos)) != string::npos)
{ {
text.replace(pos, 2, " "); text.replace(pos, 2, " ");
} }
} }
match_context match_context context(const boost::match_results<string::const_iterator> &match,
context(const boost::match_results<std::string::const_iterator> &match, std::uint64_t words)
std::uint64_t words)
{ {
if (words == 0) if (words == 0)
{ {
@ -174,7 +175,7 @@ context(const boost::match_results<std::string::const_iterator> &match,
if (pos_before != 0) if (pos_before != 0)
{ {
pos_before = prefix.rfind(' ', pos_before); pos_before = prefix.rfind(' ', pos_before);
if (pos_before != std::string::npos) if (pos_before != string::npos)
{ {
--pos_before; --pos_before;
} }
@ -184,10 +185,10 @@ context(const boost::match_results<std::string::const_iterator> &match,
} }
} }
if (pos_after != std::string::npos) if (pos_after != string::npos)
{ {
pos_after = suffix.find(' ', pos_after); pos_after = suffix.find(' ', pos_after);
if (pos_after != std::string::npos) if (pos_after != string::npos)
{ {
++pos_after; ++pos_after;
} }
@ -199,7 +200,7 @@ context(const boost::match_results<std::string::const_iterator> &match,
{ {
pos_before += 2; pos_before += 2;
} }
if (pos_after != std::string::npos) if (pos_after != string::npos)
{ {
pos_after -= 1; pos_after -= 1;
} }
@ -207,25 +208,25 @@ context(const boost::match_results<std::string::const_iterator> &match,
return {prefix.substr(pos_before), suffix.substr(0, pos_after)}; return {prefix.substr(pos_before), suffix.substr(0, pos_after)};
} }
std::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) while ((pos = prefix.rfind("<H>", pos)) != std::string_view::npos)
{ {
pos += 3; pos += 3;
return std::string{prefix.substr(pos, prefix.find('<', pos) - pos)}; return string{prefix.substr(pos, prefix.find('<', pos) - pos)};
} }
return {}; return {};
} }
std::string page(const std::string_view prefix) string page(const std::string_view prefix)
{ {
size_t pos{prefix.length()}; size_t pos{prefix.length()};
while ((pos = prefix.rfind("<PAGE ", pos)) != std::string_view::npos) while ((pos = prefix.rfind("<PAGE ", pos)) != std::string_view::npos)
{ {
pos += 6; pos += 6;
return std::string{prefix.substr(pos, prefix.find('>', pos) - pos)}; return string{prefix.substr(pos, prefix.find('>', pos) - pos)};
} }
return {}; return {};