utils: add Regex::replace + unit tests

This commit is contained in:
Dirk-Jan C. Binnema 2023-01-28 18:46:00 +02:00
parent ba69e2104a
commit daef904ca1
3 changed files with 61 additions and 19 deletions

View File

@ -16,22 +16,22 @@
lib_mu_utils=static_library('mu-utils', [ lib_mu_utils=static_library('mu-utils', [
'mu-command-handler.cc', 'mu-command-handler.cc',
'mu-logger.cc', 'mu-logger.cc',
'mu-option.cc', 'mu-option.cc',
'mu-readline.cc', 'mu-readline.cc',
'mu-sexp.cc', 'mu-sexp.cc',
'mu-test-utils.cc', 'mu-test-utils.cc',
'mu-utils.cc', 'mu-utils-file.cc',
'mu-utils-file.cc'], 'mu-utils.cc'
dependencies: [ ], dependencies: [
glib_dep, glib_dep,
gio_dep, gio_dep,
config_h_dep, config_h_dep,
readline_dep readline_dep
], ], include_directories:
include_directories: include_directories(['.','..']), include_directories(['.','..']),
install: false) install: false)
lib_mu_utils_dep = declare_dependency( lib_mu_utils_dep = declare_dependency(
link_with: lib_mu_utils, link_with: lib_mu_utils,
@ -47,6 +47,12 @@ test('test-sexp',
cpp_args: ['-DBUILD_TESTS'], cpp_args: ['-DBUILD_TESTS'],
dependencies: [glib_dep, lib_mu_utils_dep])) 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', test('test-command-handler',
executable('test-command-handler', 'mu-command-handler.cc', executable('test-command-handler', 'mu-command-handler.cc',
install: false, install: false,

View File

@ -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 ** 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 ** 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_; } operator bool() const noexcept { return !!rx_; }
// No need for a move CTOR, copying is cheap (GRegex is ref-counted)
// No need for a move CTOR, copying is cheap (GRegex is refcounted)
/** /**
* Copy CTOR * Copy CTOR
@ -119,6 +118,25 @@ struct Regex {
return rx_ ? g_regex_match(rx_, str.c_str(), mflags, {}) : false; 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: private:
Regex(const char *ptrn, GRegexCompileFlags cflags, GRegexMatchFlags mflags) { Regex(const char *ptrn, GRegexCompileFlags cflags, GRegexMatchFlags mflags) {
GError *err{}; GError *err{};

View File

@ -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.
**
*/