@@ -12,6 +12,7 @@ in the source distribution for its full text.
1212#include <assert.h>
1313#include <ctype.h>
1414#include <limits.h> // IWYU pragma: keep
15+ #include <stdarg.h>
1516#include <stdint.h>
1617#include <stdlib.h>
1718#include <string.h>
@@ -297,3 +298,45 @@ size_t RichString_appendnAscii(RichString* this, int attrs, const char* data, si
297298size_t RichString_writeAscii (RichString * this , int attrs , const char * data ) {
298299 return RichString_writeFromAscii (this , attrs , data , 0 , strlen (data ));
299300}
301+
302+ ATTR_FORMAT (printf , 5 , 0 ) ATTR_NONNULL_N (1 , 3 , 5 )
303+ static size_t RichString_appendvnFormatAscii (RichString * this , int attrs , char * buf , size_t len , const char * fmt , va_list vl ) {
304+ // The temporary "buf" does not need to be NUL-terminated.
305+ int ret = vsnprintf (buf , len , fmt , vl );
306+ if (ret < 0 || (unsigned int )ret > len ) {
307+ fail ();
308+ }
309+
310+ return RichString_appendnAscii (this , attrs , buf , (unsigned int )ret );
311+ }
312+
313+ size_t RichString_appendnFormatAscii (RichString * this , int attrs , char * buf , size_t len , const char * fmt , ...) {
314+ va_list vl ;
315+ va_start (vl , fmt );
316+ size_t ret = RichString_appendvnFormatAscii (this , attrs , buf , len , fmt , vl );
317+ va_end (vl );
318+
319+ return ret ;
320+ }
321+
322+ ATTR_FORMAT (printf , 3 , 0 ) ATTR_NONNULL_N (1 , 3 )
323+ static size_t RichString_appendvFormatAscii (RichString * this , int attrs , const char * fmt , va_list vl ) {
324+ char * buf ;
325+ int ret = vasprintf (& buf , fmt , vl );
326+ if (ret < 0 || !buf ) {
327+ fail ();
328+ }
329+
330+ size_t len = RichString_appendnAscii (this , attrs , buf , (unsigned int )ret );
331+ free (buf );
332+ return len ;
333+ }
334+
335+ size_t RichString_appendFormatAscii (RichString * this , int attrs , const char * fmt , ...) {
336+ va_list vl ;
337+ va_start (vl , fmt );
338+ size_t len = RichString_appendvFormatAscii (this , attrs , fmt , vl );
339+ va_end (vl );
340+
341+ return len ;
342+ }
0 commit comments