From 26e7f707c516cffd5496e4923f1605cd7c5e5fcc Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 12 Dec 2022 15:54:11 +0100 Subject: [PATCH] glib-aux: add nm_str_buf_dup_str() helper --- src/libnm-glib-aux/nm-str-buf.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/libnm-glib-aux/nm-str-buf.h b/src/libnm-glib-aux/nm-str-buf.h index 162cc52ca7..32ab8da840 100644 --- a/src/libnm-glib-aux/nm-str-buf.h +++ b/src/libnm-glib-aux/nm-str-buf.h @@ -447,6 +447,24 @@ nm_str_buf_get_str(NMStrBuf *strbuf) return strbuf->_priv_str; } +static inline char * +nm_str_buf_dup_str(NMStrBuf *strbuf) +{ + _nm_str_buf_assert(strbuf); + + /* Gives almost the same as g_strdup(nm_str_buf_get_str(strbuf)). The difference + * is: + * - unlike nm_str_buf_get_str(), it does not need to allocate + * one more character to NUL terminate the string. + * - it does not require an additional strlen(), because we + * already know the length. */ + + if (!strbuf->_priv_str) + return NULL; + + return nm_memdup_nul(strbuf->_priv_str, strbuf->_priv_len); +} + /** * nm_str_buf_get_str_unsafe: * @strbuf: the buffer