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>2021-02-25 11:35:01 +0100
committerSebastien Bacher <seb128@ubuntu.com>2021-02-25 11:49:48 +0100
commit77dea84aa6e816f66dc622cec8ae2668eba3c068 (patch)
tree755536a0ed1fb36c2899e9bff9e092c4008a25c4 /shared/nm-glib-aux/nm-str-buf.h
parent47f08826b44eed651b44dc8bacb5b46bc4c52067 (diff)
parentfd80297a396cc4dd7da4da20208fb360aa369aa7 (diff)
Merge branch '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, 56 insertions, 5 deletions
diff --git a/shared/nm-glib-aux/nm-str-buf.h b/shared/nm-glib-aux/nm-str-buf.h
index 062630a5..cb0d3fb1 100644
--- a/shared/nm-glib-aux/nm-str-buf.h
+++ b/shared/nm-glib-aux/nm-str-buf.h
@@ -1,4 +1,4 @@
-/* SPDX-License-Identifier: LGPL-2.1+ */
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
 #ifndef __NM_STR_BUF_H__
 #define __NM_STR_BUF_H__
@@ -31,7 +31,7 @@ typedef struct _NMStrBuf {
 /*****************************************************************************/
 
 static inline void
-_nm_str_buf_assert(NMStrBuf *strbuf)
+_nm_str_buf_assert(const NMStrBuf *strbuf)
 {
     nm_assert(strbuf);
     nm_assert((!!strbuf->_priv_str) == (strbuf->_priv_allocated > 0));
@@ -268,7 +268,34 @@ nm_str_buf_append_required_delimiter(NMStrBuf *strbuf, char delimiter)
 }
 
 static inline void
-nm_str_buf_reset(NMStrBuf *strbuf, const char *str)
+nm_str_buf_append_dirty(NMStrBuf *strbuf, gsize len)
+{
+    _nm_str_buf_assert(strbuf);
+
+    /* this append @len bytes to the buffer, but it does not
+     * initialize them! */
+    if (len > 0) {
+        nm_str_buf_maybe_expand(strbuf, len, FALSE);
+        strbuf->_priv_len += len;
+    }
+}
+
+static inline void
+nm_str_buf_append_c_len(NMStrBuf *strbuf, char ch, gsize len)
+{
+    _nm_str_buf_assert(strbuf);
+
+    if (len > 0) {
+        nm_str_buf_maybe_expand(strbuf, len, FALSE);
+        memset(&strbuf->_priv_str[strbuf->_priv_len], ch, len);
+        strbuf->_priv_len += len;
+    }
+}
+
+/*****************************************************************************/
+
+static inline NMStrBuf *
+nm_str_buf_reset(NMStrBuf *strbuf)
 {
     _nm_str_buf_assert(strbuf);
 
@@ -280,8 +307,7 @@ nm_str_buf_reset(NMStrBuf *strbuf, const char *str)
         strbuf->_priv_len = 0;
     }
 
-    if (str)
-        nm_str_buf_append(strbuf, str);
+    return strbuf;
 }
 
 /*****************************************************************************/
@@ -361,6 +387,31 @@ nm_str_buf_get_str_unsafe(NMStrBuf *strbuf)
     return strbuf->_priv_str;
 }
 
+static inline char *
+nm_str_buf_get_str_at_unsafe(NMStrBuf *strbuf, gsize index)
+{
+    _nm_str_buf_assert(strbuf);
+
+    /* it is acceptable to ask for a pointer at the end of the buffer -- even
+     * if there is no data there. The caller is anyway required to take care
+     * of the length (that's the "unsafe" part), and in that case, the length
+     * is merely zero. */
+    nm_assert(index <= strbuf->allocated);
+
+    if (!strbuf->_priv_str)
+        return NULL;
+
+    return &strbuf->_priv_str[index];
+}
+
+static inline char
+nm_str_buf_get_char(const NMStrBuf *strbuf, gsize index)
+{
+    _nm_str_buf_assert(strbuf);
+    nm_assert(index < strbuf->allocated);
+    return strbuf->_priv_str[index];
+}
+
 /**
  * nm_str_buf_finalize:
  * @strbuf: an initilized #NMStrBuf