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

@ -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{};