From daef904ca1e6e7bf53f90dce1450e2d1567dfbd8 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sat, 28 Jan 2023 18:46:00 +0200 Subject: [PATCH] utils: add Regex::replace + unit tests --- lib/utils/meson.build | 38 ++++++++++++++++++++--------------- lib/utils/mu-regex.hh | 24 +++++++++++++++++++--- lib/utils/tests/test-regex.cc | 18 +++++++++++++++++ 3 files changed, 61 insertions(+), 19 deletions(-) create mode 100644 lib/utils/tests/test-regex.cc diff --git a/lib/utils/meson.build b/lib/utils/meson.build index b8e446af..da8bca22 100644 --- a/lib/utils/meson.build +++ b/lib/utils/meson.build @@ -16,22 +16,22 @@ lib_mu_utils=static_library('mu-utils', [ - 'mu-command-handler.cc', - 'mu-logger.cc', - 'mu-option.cc', - 'mu-readline.cc', - 'mu-sexp.cc', - 'mu-test-utils.cc', - 'mu-utils.cc', - 'mu-utils-file.cc'], - dependencies: [ - glib_dep, - gio_dep, - config_h_dep, - readline_dep - ], - include_directories: include_directories(['.','..']), - install: false) + 'mu-command-handler.cc', + 'mu-logger.cc', + 'mu-option.cc', + 'mu-readline.cc', + 'mu-sexp.cc', + 'mu-test-utils.cc', + 'mu-utils-file.cc', + 'mu-utils.cc' +], dependencies: [ + glib_dep, + gio_dep, + config_h_dep, + readline_dep +], include_directories: + include_directories(['.','..']), +install: false) lib_mu_utils_dep = declare_dependency( link_with: lib_mu_utils, @@ -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, diff --git a/lib/utils/mu-regex.hh b/lib/utils/mu-regex.hh index e36e8891..e5519689 100644 --- a/lib/utils/mu-regex.hh +++ b/lib/utils/mu-regex.hh @@ -1,5 +1,5 @@ /* -** Copyright (C) 2022 Dirk-Jan C. Binnema +** Copyright (C) 2022-2023 Dirk-Jan C. Binnema ** ** 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{}; diff --git a/lib/utils/tests/test-regex.cc b/lib/utils/tests/test-regex.cc new file mode 100644 index 00000000..e6bf0836 --- /dev/null +++ b/lib/utils/tests/test-regex.cc @@ -0,0 +1,18 @@ +/* +** Copyright (C) 2023 Dirk-Jan C. Binnema +** +** 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. +** +*/