utils: refactor count_nl from mu-cmd-find

And update users.
Please enter the commit message for your changes. Lines starting
This commit is contained in:
Dirk-Jan C. Binnema
2025-07-27 19:37:48 +03:00
parent 28ea23a2a3
commit 8d3b65c5d8
2 changed files with 17 additions and 9 deletions

View File

@ -1,5 +1,5 @@
/*
** Copyright (C) 2008-2023 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** Copyright (C) 2008-2025 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
@ -22,6 +22,7 @@
#include <initializer_list>
#include <string>
#include <algorithm>
#include <utils/mu-utils.hh>
#include <utils/mu-result.hh>
@ -77,6 +78,21 @@ const char* set_tz(const char* tz);
bool set_en_us_utf8_locale();
/**
* Count new lines in string.
*
* @param s string
*
* @return number of newlines
*/
static inline size_t count_nl(const std::string& s) {
return std::count(s.begin(), s.end(), '\n');
}
/**
* For unit tests, assert two std::string's are equal.
*

View File

@ -533,14 +533,6 @@ Mu::mu_cmd_find(const Store& store, const Options& opts)
static std::string test_mu_home;
auto count_nl(const std::string& s)->size_t {
size_t n{};
for (auto&& c: s)
if (c == '\n')
++n;
return n;
}
static size_t
search_func(const std::string& expr, size_t expected)
{