forked from lix-project/lix
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>
|
#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();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -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));
|
out.append(s.substr(last_end, m.position() - last_end));
|
||||||
// Merge continous matches
|
// Merge continous matches
|
||||||
ssize_t end = start + m.length();
|
ssize_t end = start + m.length();
|
||||||
while(++it != matches.end() && (*it).position() <= end) {
|
while (++it != matches.end() && (*it).position() <= end) {
|
||||||
auto n = *it;
|
auto n = *it;
|
||||||
ssize_t nend = start + (n.position() - start + n.length());
|
ssize_t nend = start + (n.position() - start + n.length());
|
||||||
if(nend > end)
|
if (nend > end)
|
||||||
end = nend;
|
end = nend;
|
||||||
}
|
}
|
||||||
out.append(prefix);
|
out.append(prefix);
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -106,19 +106,19 @@ 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++);
|
||||||
found = true;
|
found = true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue