[Mesa-dev] [PATCH v3 1/3] util: introduce the util_strnappend function (v2)

Silvestrs Timofejevs silvestrs.timofejevs at imgtec.com
Fri Jan 11 16:32:31 UTC 2019


This function is similar to strncat, but unlike strncat it allows to
concatenate the buffer with a formatted string. The alternative would
be to have an intermediate string that is formated first, and then
appended via strncat.

v2:
   revert accidentally introduced blank line removal

Signed-off-by: Silvestrs Timofejevs <silvestrs.timofejevs at imgtec.com>
Reviewed-by: Eric Engestrom <eric.engestrom at intel.com>
---
 src/util/u_string.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/src/util/u_string.h b/src/util/u_string.h
index e408146..2036815 100644
--- a/src/util/u_string.h
+++ b/src/util/u_string.h
@@ -223,6 +223,21 @@ util_strstr(const char *haystack, const char *needle)
 
 #endif
 
+/* Append a formatted string to the buffer, up to the buffer size */
+static inline void
+util_strnappend(char *const buf, const int buf_size, const char *fmt, ...)
+{
+   int max_allowed;
+   va_list args;
+   size_t buf_len = strlen(buf);
+
+   max_allowed = buf_size - buf_len;
+   assert(max_allowed >= 0);
+
+   va_start(args, fmt);
+   (void) util_vsnprintf(&buf[buf_len], max_allowed, fmt, args);
+   va_end(args);
+}
 
 #ifdef __cplusplus
 }
-- 
2.7.4



More information about the mesa-dev mailing list