hiliteMatches(): Style fixes, pass more stuff by reference
This commit is contained in:
parent
4530574363
commit
7afbdf2545
|
@ -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();
|
||||
});
|
||||
|
||||
|
@ -20,10 +27,10 @@ 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);
|
||||
|
@ -31,6 +38,7 @@ std::string hiliteMatches(const std::string &s, std::vector<std::smatch> matches
|
|||
out.append(postfix);
|
||||
last_end = end;
|
||||
}
|
||||
|
||||
out.append(s.substr(last_end));
|
||||
return out;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue