summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2018-03-15 10:01:36 +0100
committerGravatar Dirk Hohndel <dirk@hohndel.org>2018-04-09 11:29:43 -0700
commitbbacdf94e325dbc78b4a6d60a297b6e330cc90c0 (patch)
tree64659b49f608d04dba0041628f8b7302a8506df5
parent5afe1a53d8c662f26de048c8d954be323b96026b (diff)
downloadsubsurface-bbacdf94e325dbc78b4a6d60a297b6e330cc90c0.tar.gz
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>
-rw-r--r--core/format.cpp29
1 files changed, 13 insertions, 16 deletions
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;
}