about summary refs log tree commit diff
path: root/shared/nm-glib-aux/nm-str-buf.h
diff options
context:
space:
mode:
authorSebastien Bacher <seb128@ubuntu.com>2020-07-02 11:35:14 +0200
committerSebastien Bacher <seb128@ubuntu.com>2020-07-02 11:52:25 +0200
commitc6c4e39b012d544c1f1ed816686c8ae1599da793 (patch)
tree094d5e07c1d135d0ee67d99e051fd6a9a367a6a4 /shared/nm-glib-aux/nm-str-buf.h
parent7368e45ff5b3b8b328d55408bcd403e4d6c564d0 (diff)
parentacd7b014c72673f5b0c0b91309e27028886da167 (diff)
Merge remote-tracking branch 'salsa/debian/master' into ubuntu/master
Diffstat (limited to 'shared/nm-glib-aux/nm-str-buf.h')
-rw-r--r--shared/nm-glib-aux/nm-str-buf.h61
1 files changed, 45 insertions, 16 deletions
diff --git a/shared/nm-glib-aux/nm-str-buf.h b/shared/nm-glib-aux/nm-str-buf.h
index 8c73bfaf..da082abe 100644
--- a/shared/nm-glib-aux/nm-str-buf.h
+++ b/shared/nm-glib-aux/nm-str-buf.h
@@ -35,24 +35,30 @@ static inline void
 _nm_str_buf_assert (NMStrBuf *strbuf)
 {
 	nm_assert (strbuf);
-	nm_assert (strbuf->_priv_str);
-	nm_assert (strbuf->_priv_allocated > 0);
+	nm_assert ((!!strbuf->_priv_str) == (strbuf->_priv_allocated > 0));
 	nm_assert (strbuf->_priv_len <= strbuf->_priv_allocated);
 }
 
+static inline NMStrBuf
+NM_STR_BUF_INIT (gsize allocated, gboolean do_bzero_mem)
+{
+	NMStrBuf strbuf = {
+		._priv_str          = allocated ? g_malloc (allocated) : NULL,
+		._priv_allocated    = allocated,
+		._priv_len          = 0,
+		._priv_do_bzero_mem = do_bzero_mem,
+	};
+
+	return strbuf;
+}
+
 static inline void
 nm_str_buf_init (NMStrBuf *strbuf,
                  gsize len,
                  bool do_bzero_mem)
 {
 	nm_assert (strbuf);
-	nm_assert (len > 0);
-
-	strbuf->_priv_str          = g_malloc (len);
-	strbuf->_priv_allocated    = len;
-	strbuf->_priv_len          = 0;
-	strbuf->_priv_do_bzero_mem = do_bzero_mem;
-
+	*strbuf = NM_STR_BUF_INIT (len, do_bzero_mem);
 	_nm_str_buf_assert (strbuf);
 }
 
@@ -66,9 +72,6 @@ nm_str_buf_maybe_expand (NMStrBuf *strbuf,
                          gboolean reserve_exact)
 {
 	_nm_str_buf_assert (strbuf);
-
-	/* currently we always require to reserve a non-zero number of bytes. */
-	nm_assert (reserve > 0);
 	nm_assert (strbuf->_priv_len < G_MAXSIZE - reserve);
 
 	/* @reserve is the extra space that we require. */
@@ -164,6 +167,19 @@ nm_str_buf_erase (NMStrBuf *strbuf,
 /*****************************************************************************/
 
 static inline void
+nm_str_buf_append_c_repeated (NMStrBuf *strbuf,
+                              char ch,
+                              guint len)
+{
+	if (len > 0) {
+		nm_str_buf_maybe_expand (strbuf, len + 1, FALSE);
+		do {
+			strbuf->_priv_str[strbuf->_priv_len++] = ch;
+		} while (--len > 0);
+	}
+}
+
+static inline void
 nm_str_buf_append_c (NMStrBuf *strbuf,
                      char ch)
 {
@@ -263,10 +279,16 @@ nm_str_buf_is_initalized (NMStrBuf *strbuf)
  *   is of length "strbuf->len", which may be larger if the
  *   returned string contains NUL characters (binary). The terminating
  *   NUL character is always present after "strbuf->len" characters.
+ *   If currently no buffer is allocated, this will return %NULL.
  */
-static inline const char *
+static inline char *
 nm_str_buf_get_str (NMStrBuf *strbuf)
 {
+	_nm_str_buf_assert (strbuf);
+
+	if (!strbuf->_priv_str)
+		return NULL;
+
 	nm_str_buf_maybe_expand (strbuf, 1, FALSE);
 	strbuf->_priv_str[strbuf->_priv_len] = '\0';
 	return strbuf->_priv_str;
@@ -288,16 +310,23 @@ nm_str_buf_get_str_unsafe (NMStrBuf *strbuf)
  * Returns: (transfer full): the string of the buffer
  *   which must be freed by the caller. The @strbuf
  *   is afterwards in undefined state, though it can be
- *   reused after nm_str_buf_init(). */
+ *   reused after nm_str_buf_init().
+ *   Note that if no string is allocated yet (after nm_str_buf_init() with
+ *   length zero), this will return %NULL. */
 static inline char *
 nm_str_buf_finalize (NMStrBuf *strbuf,
                      gsize *out_len)
 {
-	nm_str_buf_maybe_expand (strbuf, 1, TRUE);
-	strbuf->_priv_str[strbuf->_priv_len] = '\0';
+	_nm_str_buf_assert (strbuf);
 
 	NM_SET_OUT (out_len, strbuf->_priv_len);
 
+	if (!strbuf->_priv_str)
+		return NULL;
+
+	nm_str_buf_maybe_expand (strbuf, 1, TRUE);
+	strbuf->_priv_str[strbuf->_priv_len] = '\0';
+
 	/* the buffer is in invalid state afterwards, however, we clear it
 	 * so far, that nm_auto_str_buf and nm_str_buf_destroy() is happy.  */
 	return g_steal_pointer (&strbuf->_priv_str);