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 <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-03-15 10:01:36 +01:00 committed by Dirk Hohndel
parent 5afe1a53d8
commit bbacdf94e3

View file

@ -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. // Helper to extract integers from C-style format strings.
// The default returned value, if no digits are found is 0. // The default returned value, if no digits are found, is 0.
static int parse_fmt_int(const char **act) // 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)) if (!isdigit(**act))
return 0; return 0;
int res = 0; int res = 0;
@ -119,8 +124,10 @@ static int parse_fmt_int(const char **act)
return res; 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; const char *act = fmt;
QString ret; QString ret;
for (;;) { for (;;) {
@ -169,24 +176,13 @@ QString vqasprintf_loc(const char *fmt, va_list ap)
} }
// Field width // Field width
int field_width; int field_width = parse_fmt_int(&act, &ap);
if (*act == '*') {
field_width = va_arg(ap, int);
++act;
} else {
field_width = parse_fmt_int(&act);
}
// Precision // Precision
int precision = -1; int precision = -1;
if (*act == '.') { if (*act == '.') {
++act; ++act;
if (*act == '*') { precision = parse_fmt_int(&act, &ap);
precision = va_arg(ap, int);
++act;
} else {
precision = parse_fmt_int(&act);
}
} }
// Length modifier // Length modifier
@ -343,6 +339,7 @@ QString vqasprintf_loc(const char *fmt, va_list ap)
break; break;
} }
} }
va_end(ap);
return ret; return ret;
} }