2 * This is free and unencumbered software released into the public domain.
4 * Anyone is free to copy, modify, publish, use, compile, sell, or
5 * distribute this software, either in source code form or as a compiled
6 * binary, for any purpose, commercial or non-commercial, and by any
9 * In jurisdictions that recognize copyright laws, the author or authors
10 * of this software dedicate any and all copyright interest in the
11 * software to the public domain. We make this dedication for the benefit
12 * of the public at large and to the detriment of our heirs and
13 * successors. We intend this dedication to be an overt act of
14 * relinquishment in perpetuity of all present and future rights to this
15 * software under copyright law.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
25 * For more information, please refer to <https://unlicense.org>
28 /* https://git.datadissipation.net */
32 #ifdef ASPRINTF_INCLUDE_LIBC
39 #define HAVE_ASPRINTF 1
40 int i_vasprintf_(char **restrict strp
, const char *restrict fmt
, va_list ap
);
41 int i_asprintf_(char **restrict strp
, const char *restrict fmt
, ...);
43 * Credit for a big part of the macro madness:
45 * https://gustedt.wordpress.com/2010/06/08/detect-empty-macro-arguments/
49 * This isn't strict C99, because C99 requires at least one argument in "..."
50 * for macros. Still, I haven't encountered a compiler that won't preprocess
51 * this right (out of the popular ones, MSVC wasn't tested)
53 #define vasprintf(s, f, a) i_vasprintf_(s, f, a)
54 #define I_COND_COMMA_(...) ,
55 #define I_PICK_ARG_(_1, _2, ARG, ...) ARG
56 #define I_HAS_COMMA_(...) I_PICK_ARG_(__VA_ARGS__, 1, 0)
58 #define I_ISEMPTY_(...) \
60 /* test if there is just one argument, eventually an empty \
62 I_HAS_COMMA_(__VA_ARGS__), \
63 /* test if I_COND_COMMA_ together with the argument \
65 I_HAS_COMMA_(I_COND_COMMA_ __VA_ARGS__), \
66 /* test if the argument together with a parenthesis \
68 I_HAS_COMMA_(__VA_ARGS__ (/*empty*/)), \
69 /* test if placing it between I_COND_COMMA_ and the \
70 parenthesis adds a comma */ \
71 I_HAS_COMMA_(I_COND_COMMA_ __VA_ARGS__ (/*empty*/)) \
74 #define I_PASTE5_(_0, _1, _2, _3, _4) _0 ## _1 ## _2 ## _3 ## _4
75 #define II_ISEMPTY_(_0, _1, _2, _3)\
76 I_HAS_COMMA_(I_PASTE5_(I_IS_EMPTY_CASE_, _0, _1, _2, _3))
77 #define I_IS_EMPTY_CASE_0001 ,
79 #define II_PASTE_(a) I_EMPTY_##a
80 #define I_PASTE_(a) II_PASTE_(a)
82 #define I_GET_ARG_(...) I_PICK_ARG_(__VA_ARGS__)
83 #define asprintf(s, f, ...)\
84 i_asprintf_(s, f I_PASTE_(I_ISEMPTY_(I_GET_ARG_(, , __VA_ARGS__))) __VA_ARGS__)
86 #define I_EMPTY_0 I_COND_COMMA_()
87 #endif /* !HAVE_ASPRINTF */
89 #ifdef ASPRINTF_IMPLEMENTATION
91 i_vasprintf_(char **restrict strp
, const char *restrict fmt
, va_list ap
)
97 size
= vsnprintf(NULL
, 0, fmt
, tp
);
103 *strp
= malloc(size
+ 1);
108 ret
= vsnprintf(*strp
, size
+ 1, fmt
, ap
);
118 i_asprintf_(char **restrict strp
, const char *restrict fmt
, ...)
124 ret
= i_vasprintf_(strp
, fmt
, ap
);
129 #endif /* ASPRINTF_IMPLEMENTATION */
130 #endif /* !ASPRINTF_H_ */