1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-30 11:43:15 +02:00

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,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;
}