diff --git a/src/libutil/fmt.cc b/src/libutil/fmt.cc index 914fb62b2..3dd93d73e 100644 --- a/src/libutil/fmt.cc +++ b/src/libutil/fmt.cc @@ -1,13 +1,20 @@ +#include "fmt.hh" + #include namespace nix { -std::string hiliteMatches(const std::string &s, std::vector matches, std::string prefix, std::string postfix) { +std::string hiliteMatches( + std::string_view s, + std::vector matches, + std::string_view prefix, + std::string_view postfix) +{ // Avoid copy on zero matches 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(); }); @@ -20,10 +27,10 @@ std::string hiliteMatches(const std::string &s, std::vector matches out.append(s.substr(last_end, m.position() - last_end)); // Merge continous matches ssize_t end = start + m.length(); - while(++it != matches.end() && (*it).position() <= end) { + while (++it != matches.end() && (*it).position() <= end) { auto n = *it; ssize_t nend = start + (n.position() - start + n.length()); - if(nend > end) + if (nend > end) end = nend; } out.append(prefix); @@ -31,6 +38,7 @@ std::string hiliteMatches(const std::string &s, std::vector matches out.append(postfix); last_end = end; } + out.append(s.substr(last_end)); return out; } diff --git a/src/libutil/fmt.hh b/src/libutil/fmt.hh index 1f81bfcfb..a7126fb65 100644 --- a/src/libutil/fmt.hh +++ b/src/libutil/fmt.hh @@ -156,12 +156,15 @@ inline hintformat hintfmt(std::string plain_string) return hintfmt("%s", normaltxt(plain_string)); } -/** - * Highlight all the given matches in the given string `s` by wrapping them - * between `prefix` and `postfix`. - * - * If some matches overlap, then their union will be wrapped rather than the - * individual matches. - */ -std::string hiliteMatches(const std::string &s, std::vector matches, std::string prefix, std::string postfix); +/* Highlight all the given matches in the given string `s` by wrapping + them between `prefix` and `postfix`. + + If some matches overlap, then their union will be wrapped rather + than the individual matches. */ +std::string hiliteMatches( + std::string_view s, + std::vector matches, + std::string_view prefix, + std::string_view postfix); + } diff --git a/src/nix/search.cc b/src/nix/search.cc index 0d10d8c2e..e9307342c 100644 --- a/src/nix/search.cc +++ b/src/nix/search.cc @@ -106,19 +106,19 @@ struct CmdSearch : InstallableCommand, MixJSON for (auto & regex : regexes) { found = false; - auto add_all = [&found](std::sregex_iterator it, std::vector& vec){ + auto addAll = [&found](std::sregex_iterator it, std::vector & vec) { const auto end = std::sregex_iterator(); - while(it != end) { + while (it != end) { vec.push_back(*it++); found = true; } }; - add_all(std::sregex_iterator(attrPath2.begin(), attrPath2.end(), regex), attrPathMatches); - add_all(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(attrPath2.begin(), attrPath2.end(), regex), attrPathMatches); + addAll(std::sregex_iterator(name.name.begin(), name.name.end(), regex), nameMatches); + addAll(std::sregex_iterator(description.begin(), description.end(), regex), descriptionMatches); - if(!found) + if (!found) break; }