changeset 569:ccff3eb8dbb6

Add outlen parameter to th_vprintf_altfmt_funcs.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 08 Jan 2020 02:36:42 +0200
parents 2fbe42d957c4
children 262411458cde
files th_printf.c th_string.h
diffstat 2 files changed, 9 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/th_printf.c	Wed Jan 08 02:25:04 2020 +0200
+++ b/th_printf.c	Wed Jan 08 02:36:42 2020 +0200
@@ -126,6 +126,7 @@
         if (*prec > 0)
             (*prec)--;
 
+        *outlen = 1;
         return "0";
     }
     else
@@ -140,7 +141,10 @@
     (void) prec;
 
     if (vret != 0)
+    {
+        *outlen = 2;
         return (*flags & TH_PF_UPCASE) ? "0X" : "0x";
+    }
     else
         return NULL;
 }
@@ -150,7 +154,7 @@
     char *buf, int f_flags, int f_width, int f_prec, int f_len, int vret,
     BOOL f_neg, BOOL f_unsig, th_vprintf_altfmt_func f_alt)
 {
-    int ret = 0, nspacepad, nzeropad;
+    int ret = 0, nspacepad, nzeropad, f_altstr_len = 0;
     char f_sign, *f_altstr;
 
     // Special case for value of 0
@@ -172,7 +176,7 @@
 
     // Get alternative format string, if needed and available
     f_altstr = (f_flags & TH_PF_ALT) && f_alt != NULL ?
-        f_alt(buf, f_len, vret, &f_prec, &f_flags) : NULL;
+        f_alt(buf, f_len, vret, &f_prec, &f_flags, &f_altstr_len) : NULL;
 
     // Are we using a sign prefix?
     f_sign = f_unsig ? 0 : ((f_flags & TH_PF_SIGN) ?
@@ -190,7 +194,7 @@
     // and would benefit from commenting on how it works.
     // Also same goes for the th_vprintf_altfmt_* logic.
     //
-    int nlen = (f_sign ? 1 : 0) + (f_altstr != NULL ? strlen(f_altstr) : 0);
+    int nlen = (f_sign ? 1 : 0) + f_altstr_len;
     int qlen = (f_prec > f_len ? f_prec : f_len) + nlen;
 
     if (f_flags & TH_PF_POINTER && vret == 0)
--- a/th_string.h	Wed Jan 08 02:25:04 2020 +0200
+++ b/th_string.h	Wed Jan 08 02:36:42 2020 +0200
@@ -122,10 +122,10 @@
 /* Internal printf() implementation. NOTICE! This API may be unstable.
  */
 #define TH_VPRINTF_ALTFMT_FUNC(fname) char * fname ( \
-    const char *buf, const size_t len, const int vret, int *prec, int *flags)
+    const char *buf, const size_t len, const int vret, int *prec, int *flags, int *outlen)
 
 typedef char * (*th_vprintf_altfmt_func)(
-    const char *buf, const size_t len, const int vret, int *prec, int *flags);
+    const char *buf, const size_t len, const int vret, int *prec, int *flags, int *outlen);
 
 
 int     th_vprintf_do(th_vprintf_ctx *ctx, th_vprintf_putch vputch, const char *fmt, va_list ap);