From bbacdf94e325dbc78b4a6d60a297b6e330cc90c0 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Thu, 15 Mar 2018 10:01:36 +0100 Subject: Cleanup: Slightly shorten code in vqasprintf_loc() Move duplicate code, which reads '*' arguments from va_list into parse_fmt_int() function. To pass pointers-to-va_list, the va_list has to be copied first. Signed-off-by: Berthold Stoeger --- core/format.cpp | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) (limited to 'core') diff --git a/core/format.cpp b/core/format.cpp index 5793bff02..a540f4c21 100644 --- a/core/format.cpp +++ b/core/format.cpp @@ -106,9 +106,14 @@ static QString fmt_float(double d, char type, vasprintf_flags flags, int field_w } // Helper to extract integers from C-style format strings. -// The default returned value, if no digits are found is 0. -static int parse_fmt_int(const char **act) +// The default returned value, if no digits are found, is 0. +// A '*' means fetch from varargs as int. +static int parse_fmt_int(const char **act, va_list *ap) { + if (**act == '*') { + ++(*act); + return va_arg(*ap, int); + } if (!isdigit(**act)) return 0; int res = 0; @@ -119,8 +124,10 @@ static int parse_fmt_int(const char **act) return res; } -QString vqasprintf_loc(const char *fmt, va_list ap) +QString vqasprintf_loc(const char *fmt, va_list ap_in) { + va_list ap; + va_copy(ap, ap_in); // Allows us to pass as pointer to helper functions const char *act = fmt; QString ret; for (;;) { @@ -169,24 +176,13 @@ QString vqasprintf_loc(const char *fmt, va_list ap) } // Field width - int field_width; - if (*act == '*') { - field_width = va_arg(ap, int); - ++act; - } else { - field_width = parse_fmt_int(&act); - } + int field_width = parse_fmt_int(&act, &ap); // Precision int precision = -1; if (*act == '.') { ++act; - if (*act == '*') { - precision = va_arg(ap, int); - ++act; - } else { - precision = parse_fmt_int(&act); - } + precision = parse_fmt_int(&act, &ap); } // Length modifier @@ -343,6 +339,7 @@ QString vqasprintf_loc(const char *fmt, va_list ap) break; } } + va_end(ap); return ret; } -- cgit v1.2.3-70-g09d2