hiliteMatches(): Style fixes, pass more stuff by reference

This commit is contained in:
Eelco Dolstra 2022-01-24 14:47:34 +01:00
parent 4530574363
commit 7afbdf2545
3 changed files with 30 additions and 19 deletions

View file

@ -1,11 +1,18 @@
#include "fmt.hh"
#include <regex> #include <regex>
namespace nix { namespace nix {
std::string hiliteMatches(const std::string &s, std::vector<std::smatch> matches, std::string prefix, std::string postfix) { std::string hiliteMatches(
std::string_view s,
std::vector<std::smatch> matches,
std::string_view prefix,
std::string_view postfix)
{
// Avoid copy on zero matches // Avoid copy on zero matches
if (matches.size() == 0) if (matches.size() == 0)
return s; return (std::string) s;
std::sort(matches.begin(), matches.end(), [](const auto & a, const auto & b) { std::sort(matches.begin(), matches.end(), [](const auto & a, const auto & b) {
return a.position() < b.position(); return a.position() < b.position();
@ -31,6 +38,7 @@ std::string hiliteMatches(const std::string &s, std::vector<std::smatch> matches
out.append(postfix); out.append(postfix);
last_end = end; last_end = end;
} }
out.append(s.substr(last_end)); out.append(s.substr(last_end));
return out; return out;
} }

View file

@ -156,12 +156,15 @@ inline hintformat hintfmt(std::string plain_string)
return hintfmt("%s", normaltxt(plain_string)); return hintfmt("%s", normaltxt(plain_string));
} }
/** /* Highlight all the given matches in the given string `s` by wrapping
* Highlight all the given matches in the given string `s` by wrapping them them between `prefix` and `postfix`.
* between `prefix` and `postfix`.
* If some matches overlap, then their union will be wrapped rather
* If some matches overlap, then their union will be wrapped rather than the than the individual matches. */
* individual matches. std::string hiliteMatches(
*/ std::string_view s,
std::string hiliteMatches(const std::string &s, std::vector<std::smatch> matches, std::string prefix, std::string postfix); std::vector<std::smatch> matches,
std::string_view prefix,
std::string_view postfix);
} }

View file

@ -106,7 +106,7 @@ struct CmdSearch : InstallableCommand, MixJSON
for (auto & regex : regexes) { for (auto & regex : regexes) {
found = false; found = false;
auto add_all = [&found](std::sregex_iterator it, std::vector<std::smatch>& vec){ auto addAll = [&found](std::sregex_iterator it, std::vector<std::smatch> & vec) {
const auto end = std::sregex_iterator(); const auto end = std::sregex_iterator();
while (it != end) { while (it != end) {
vec.push_back(*it++); vec.push_back(*it++);
@ -114,9 +114,9 @@ struct CmdSearch : InstallableCommand, MixJSON
} }
}; };
add_all(std::sregex_iterator(attrPath2.begin(), attrPath2.end(), regex), attrPathMatches); addAll(std::sregex_iterator(attrPath2.begin(), attrPath2.end(), regex), attrPathMatches);
add_all(std::sregex_iterator(name.name.begin(), name.name.end(), regex), nameMatches); addAll(std::sregex_iterator(name.name.begin(), name.name.end(), regex), nameMatches);
add_all(std::sregex_iterator(description.begin(), description.end(), regex), descriptionMatches); addAll(std::sregex_iterator(description.begin(), description.end(), regex), descriptionMatches);
if (!found) if (!found)
break; break;