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