From 144eb24b5553547970c7d1333a59eb5304982c19 Mon Sep 17 00:00:00 2001 From: Richard Aas Date: Fri, 26 Jun 2015 08:01:21 +0000 Subject: [PATCH] pl: pl_float() support negative numbers --- src/fmt/pl.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/fmt/pl.c b/src/fmt/pl.c index 30725e0..a35fe77 100644 --- a/src/fmt/pl.c +++ b/src/fmt/pl.c @@ -187,7 +187,9 @@ uint64_t pl_x64(const struct pl *pl) /** - * Convert a pointer-length object to floating point representation + * Convert a pointer-length object to floating point representation. + * Both positive and negative numbers are supported, a string with a + * minus sign ('-') is treated as a negative number. * * @param pl Pointer-length object * @@ -197,6 +199,7 @@ double pl_float(const struct pl *pl) { double v=0, mul=1; const char *p; + bool neg = false; if (!pl || !pl->p) return 0; @@ -215,12 +218,15 @@ double pl_float(const struct pl *pl) v /= mul; mul = 1; } + else if (ch == '-' && p == pl->p) { + neg = true; + } else { return 0; } } - return v; + return neg ? -v : v; }