mirror of https://github.com/djcb/mu.git
utils: add Regex::replace + unit tests
This commit is contained in:
parent
ba69e2104a
commit
daef904ca1
|
@ -22,15 +22,15 @@ lib_mu_utils=static_library('mu-utils', [
|
|||
'mu-readline.cc',
|
||||
'mu-sexp.cc',
|
||||
'mu-test-utils.cc',
|
||||
'mu-utils.cc',
|
||||
'mu-utils-file.cc'],
|
||||
dependencies: [
|
||||
'mu-utils-file.cc',
|
||||
'mu-utils.cc'
|
||||
], dependencies: [
|
||||
glib_dep,
|
||||
gio_dep,
|
||||
config_h_dep,
|
||||
readline_dep
|
||||
],
|
||||
include_directories: include_directories(['.','..']),
|
||||
], include_directories:
|
||||
include_directories(['.','..']),
|
||||
install: false)
|
||||
|
||||
lib_mu_utils_dep = declare_dependency(
|
||||
|
@ -47,6 +47,12 @@ test('test-sexp',
|
|||
cpp_args: ['-DBUILD_TESTS'],
|
||||
dependencies: [glib_dep, lib_mu_utils_dep]))
|
||||
|
||||
test('test-regex',
|
||||
executable('test-regex', 'mu-regex.cc',
|
||||
install: false,
|
||||
cpp_args: ['-DBUILD_TESTS'],
|
||||
dependencies: [glib_dep, lib_mu_utils_dep]))
|
||||
|
||||
test('test-command-handler',
|
||||
executable('test-command-handler', 'mu-command-handler.cc',
|
||||
install: false,
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
** Copyright (C) 2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||
** Copyright (C) 2022-2023 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
|
||||
|
@ -78,8 +78,7 @@ struct Regex {
|
|||
*/
|
||||
operator bool() const noexcept { return !!rx_; }
|
||||
|
||||
|
||||
// No need for a move CTOR, copying is cheap (GRegex is refcounted)
|
||||
// No need for a move CTOR, copying is cheap (GRegex is ref-counted)
|
||||
|
||||
/**
|
||||
* Copy CTOR
|
||||
|
@ -119,6 +118,25 @@ struct Regex {
|
|||
return rx_ ? g_regex_match(rx_, str.c_str(), mflags, {}) : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace all occurences of @this regexp in some string with a
|
||||
* replacement string
|
||||
*
|
||||
* @param str some string
|
||||
* @param repl replacement string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
std::string replace(const std::string& str, const std::string& repl) {
|
||||
char *s{g_regex_replace(rx_, str.c_str(), str.length(), 0,
|
||||
repl.c_str(), G_REGEX_MATCH_DEFAULT, {})};
|
||||
if (!s)
|
||||
throw Err(Error::Code::InvalidArgument, "error in Regex::replace");
|
||||
std::string r{s};
|
||||
g_free(s);
|
||||
return r;
|
||||
}
|
||||
|
||||
private:
|
||||
Regex(const char *ptrn, GRegexCompileFlags cflags, GRegexMatchFlags mflags) {
|
||||
GError *err{};
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
/*
|
||||
** Copyright (C) 2023 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
|
||||
** Free Software Foundation; either version 3, or (at your option) any
|
||||
** later version.
|
||||
**
|
||||
** This program is distributed in the hope that it will be useful,
|
||||
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
** GNU General Public License for more details.
|
||||
**
|
||||
** You should have received a copy of the GNU General Public License
|
||||
** along with this program; if not, write to the Free Software Foundation,
|
||||
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
**
|
||||
*/
|
Loading…
Reference in New Issue