aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/fmt.cc
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2022-01-24 14:47:34 +0100
committerEelco Dolstra <edolstra@gmail.com>2022-01-24 14:47:34 +0100
commit7afbdf254519e93ecf3730656d52e19a8727772c (patch)
tree710c10e02cfbdc2d6567ce01a55a3ca32b67289e /src/libutil/fmt.cc
parent45305743634e11053b5b9428b7b1d09df2d47856 (diff)
hiliteMatches(): Style fixes, pass more stuff by reference
Diffstat (limited to 'src/libutil/fmt.cc')
-rw-r--r--src/libutil/fmt.cc18
1 files changed, 13 insertions, 5 deletions
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 <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;
}