ucx/cx/printf.h

changeset 11
0aa8cbd7912e
parent 0
1a157da63d7c
child 16
04c9f8d8f03b
--- a/ucx/cx/printf.h	Fri Jan 03 21:40:57 2025 +0100
+++ b/ucx/cx/printf.h	Sat Jan 04 13:03:01 2025 +0100
@@ -40,6 +40,14 @@
 #include "string.h"
 #include <stdarg.h>
 
+/**
+ * Attribute for printf-like functions.
+ * @param fmt_idx index of the format string parameter
+ * @param arg_idx index of the first formatting argument
+ */
+#define cx_attr_printf(fmt_idx, arg_idx) \
+    __attribute__((__format__(printf, fmt_idx, arg_idx)))
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -48,7 +56,7 @@
 /**
  * The maximum string length that fits into stack memory.
  */
-extern unsigned const cx_printf_sbo_size;
+extern const unsigned cx_printf_sbo_size;
 
 /**
  * A \c fprintf like function which writes the output to a stream by
@@ -60,11 +68,13 @@
  * @param ... additional arguments
  * @return the total number of bytes written
  */
-__attribute__((__nonnull__(1, 2, 3), __format__(printf, 3, 4)))
+cx_attr_nonnull_arg(1, 2, 3)
+cx_attr_printf(3, 4)
+cx_attr_cstr_arg(3)
 int cx_fprintf(
         void *stream,
         cx_write_func wfc,
-        char const *fmt,
+        const char *fmt,
         ...
 );
 
@@ -79,11 +89,12 @@
  * @return the total number of bytes written
  * @see cx_fprintf()
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
+cx_attr_cstr_arg(3)
 int cx_vfprintf(
         void *stream,
         cx_write_func wfc,
-        char const *fmt,
+        const char *fmt,
         va_list ap
 );
 
@@ -99,10 +110,12 @@
  * @return the formatted string
  * @see cx_strfree_a()
  */
-__attribute__((__nonnull__(1, 2), __format__(printf, 2, 3)))
+cx_attr_nonnull_arg(1, 2)
+cx_attr_printf(2, 3)
+cx_attr_cstr_arg(2)
 cxmutstr cx_asprintf_a(
-        CxAllocator const *allocator,
-        char const *fmt,
+        const CxAllocator *allocator,
+        const char *fmt,
         ...
 );
 
@@ -132,10 +145,11 @@
  * @return the formatted string
  * @see cx_asprintf_a()
  */
-__attribute__((__nonnull__))
+cx_attr_nonnull
+cx_attr_cstr_arg(2)
 cxmutstr cx_vasprintf_a(
-        CxAllocator const *allocator,
-        char const *fmt,
+        const CxAllocator *allocator,
+        const char *fmt,
         va_list ap
 );
 
@@ -196,8 +210,16 @@
  * @param ... additional arguments
  * @return the length of produced string
  */
-__attribute__((__nonnull__(1, 2, 3, 4), __format__(printf, 4, 5)))
-int cx_sprintf_a(CxAllocator *alloc, char **str, size_t *len, const char *fmt, ... );
+cx_attr_nonnull_arg(1, 2, 3, 4)
+cx_attr_printf(4, 5)
+cx_attr_cstr_arg(4)
+int cx_sprintf_a(
+        CxAllocator *alloc,
+        char **str,
+        size_t *len,
+        const char *fmt,
+        ...
+);
 
 
 /**
@@ -231,8 +253,15 @@
  * @param ap argument list
  * @return the length of produced string
  */
-__attribute__((__nonnull__))
-int cx_vsprintf_a(CxAllocator *alloc, char **str, size_t *len, const char *fmt, va_list ap);
+cx_attr_nonnull
+cx_attr_cstr_arg(4)
+int cx_vsprintf_a(
+        CxAllocator *alloc,
+        char **str,
+        size_t *len,
+        const char *fmt,
+        va_list ap
+);
 
 
 /**
@@ -279,7 +308,17 @@
  * @return the length of produced string
  */
 __attribute__((__nonnull__(1, 2, 4, 5), __format__(printf, 5, 6)))
-int cx_sprintf_sa(CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, ... );
+cx_attr_nonnull_arg(1, 2, 4, 5)
+cx_attr_printf(5, 6)
+cx_attr_cstr_arg(5)
+int cx_sprintf_sa(
+        CxAllocator *alloc,
+        char *buf,
+        size_t *len,
+        char **str,
+        const char *fmt,
+        ...
+);
 
 /**
  * An \c sprintf like function which allocates a new string when the buffer is not large enough.
@@ -324,8 +363,16 @@
  * @param ap argument list
  * @return the length of produced string
  */
-__attribute__((__nonnull__))
-int cx_vsprintf_sa(CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, va_list ap);
+cx_attr_nonnull
+cx_attr_cstr_arg(5)
+int cx_vsprintf_sa(
+        CxAllocator *alloc,
+        char *buf,
+        size_t *len,
+        char **str,
+        const char *fmt,
+        va_list ap
+);
 
 
 #ifdef __cplusplus

mercurial