[vte] regex: Use string_view and optional
- From: Christian Persch <chpe src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vte] regex: Use string_view and optional
- Date: Wed, 27 Nov 2019 18:17:40 +0000 (UTC)
commit 9a6dded6e975e11877976ca46713644d4e4adaae
Author: Christian Persch <chpe src gnome org>
Date: Wed Nov 27 19:16:57 2019 +0100
regex: Use string_view and optional
src/regex.cc | 63 +++++++++++++++++++++++++++++----------------------------
src/regex.hh | 9 +++++----
src/vteregex.cc | 3 ++-
3 files changed, 39 insertions(+), 36 deletions(-)
---
diff --git a/src/regex.cc b/src/regex.cc
index cf99a1b1..52e4e0bf 100644
--- a/src/regex.cc
+++ b/src/regex.cc
@@ -179,55 +179,56 @@ Regex::has_compile_flags(uint32_t flags) const noexcept
*
* See man:pcre2api(3) on pcre2_substitute() for more information.
*
- * Returns: (transfer full): the substituted string, or %NULL
- * if an error occurred
+ * Returns: the substituted string, or std::nullopt if an error occurred
*/
-char*
-Regex::substitute(char const* subject,
- char const* replacement,
+std::optional<std::string>
+Regex::substitute(std::string_view const& subject,
+ std::string_view const& replacement,
uint32_t flags,
- GError** error) const noexcept
+ GError** error) const
{
- assert(subject != nullptr);
- assert(replacement != nullptr);
assert (!(flags & PCRE2_SUBSTITUTE_OVERFLOW_LENGTH));
- uint8_t outbuf[2048];
- PCRE2_SIZE outlen = sizeof(outbuf);
- int r = pcre2_substitute_8(code(),
- (PCRE2_SPTR8)subject, PCRE2_ZERO_TERMINATED,
- 0 /* start offset */,
- flags | PCRE2_SUBSTITUTE_OVERFLOW_LENGTH,
- nullptr /* match data */,
- nullptr /* match context */,
- (PCRE2_SPTR8)replacement, PCRE2_ZERO_TERMINATED,
- (PCRE2_UCHAR8*)outbuf, &outlen);
-
+ char outbuf[2048];
+ PCRE2_SIZE outlen = sizeof(outbuf) - 1;
+ auto r = pcre2_substitute_8(code(),
+ (PCRE2_SPTR8)subject.data(), subject.size(),
+ 0 /* start offset */,
+ flags | PCRE2_SUBSTITUTE_OVERFLOW_LENGTH,
+ nullptr /* match data */,
+ nullptr /* match context */,
+ (PCRE2_SPTR8)replacement.data(), replacement.size(),
+ (PCRE2_UCHAR8*)outbuf, &outlen);
+ /* Note that on success, outlen excludes the trailing NUL. */
if (r >= 0)
- return g_strndup((char*)outbuf, outlen);
+ return std::string{outbuf, outlen};
if (r == PCRE2_ERROR_NOMEMORY) {
/* The buffer was not large enough; allocated a buffer of the
- * required size and try again. Note that @outlen as returned
- * from pcre2_substitute_8() above includes the trailing \0.
+ * required size and try again. Note that as opposed to the successful
+ * call to pcre2_substitute_8() above, in the error case outlen *includes*
+ * the trailing NUL.
*/
- uint8_t *outbuf2 = (uint8_t*)g_malloc(outlen);
+ std::string outbuf2;
+ outbuf2.resize(outlen);
+
r = pcre2_substitute_8(code(),
- (PCRE2_SPTR8)subject, PCRE2_ZERO_TERMINATED,
+ (PCRE2_SPTR8)subject.data(), subject.size(),
0 /* start offset */,
flags | PCRE2_SUBSTITUTE_OVERFLOW_LENGTH,
nullptr /* match data */,
nullptr /* match context */,
- (PCRE2_SPTR8)replacement, PCRE2_ZERO_TERMINATED,
- (PCRE2_UCHAR8*)outbuf2, &outlen);
- if (r >= 0)
- return (char*)outbuf2;
-
- g_free(outbuf2);
+ (PCRE2_SPTR8)replacement.data(), replacement.size(),
+ (PCRE2_UCHAR8*)outbuf2.data(), &outlen);
+ if (r >= 0) {
+ /* Note that on success, outlen excludes the trailing NUL. */
+ outbuf2.resize(outlen);
+ return outbuf2;
+ }
}
set_gerror_from_pcre_error(r, error);
- return nullptr;
+ return std::nullopt;
}
} // namespace base
diff --git a/src/regex.hh b/src/regex.hh
index d99bf0e6..8db3e158 100644
--- a/src/regex.hh
+++ b/src/regex.hh
@@ -18,6 +18,7 @@
#pragma once
#include <memory>
+#include <optional>
#include <string>
#include <glib.h>
@@ -71,10 +72,10 @@ public:
bool jited() const noexcept;
- char* substitute(char const* subject,
- char const* replacement,
- uint32_t flags,
- GError** error) const noexcept;
+ std::optional<std::string> substitute(std::string_view const& subject,
+ std::string_view const& replacement,
+ uint32_t flags,
+ GError** error) const;
}; // class Regex
diff --git a/src/vteregex.cc b/src/vteregex.cc
index 4bff1c36..26a55b43 100644
--- a/src/vteregex.cc
+++ b/src/vteregex.cc
@@ -217,5 +217,6 @@ vte_regex_substitute(VteRegex *regex,
g_return_val_if_fail(replacement != nullptr, nullptr);
g_return_val_if_fail (!(flags & PCRE2_SUBSTITUTE_OVERFLOW_LENGTH), nullptr);
- return IMPL(regex)->substitute(subject, replacement, flags, error);
+ auto const r = IMPL(regex)->substitute(subject, replacement, flags, error);
+ return r ? g_strndup(r->c_str(), r->size()) : nullptr;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]