Skip to content

Commit

Permalink
hiliteMatches(): Style fixes, pass more stuff by reference
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Jan 24, 2022
1 parent 4530574 commit 7afbdf2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
18 changes: 13 additions & 5 deletions src/libutil/fmt.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
#include "fmt.hh"

#include <regex>

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
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();
});

Expand All @@ -20,17 +27,18 @@ std::string hiliteMatches(const std::string &s, std::vector<std::smatch> 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);
out.append(s.substr(start, end - start));
out.append(postfix);
last_end = end;
}

out.append(s.substr(last_end));
return out;
}
Expand Down
19 changes: 11 additions & 8 deletions src/libutil/fmt.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::smatch> 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<std::smatch> matches,
std::string_view prefix,
std::string_view postfix);

}
12 changes: 6 additions & 6 deletions src/nix/search.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,19 @@ struct CmdSearch : InstallableCommand, MixJSON

for (auto & regex : regexes) {
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();
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;
}

Expand Down

0 comments on commit 7afbdf2

Please sign in to comment.