From 85ab7c2120ba637747e860a6903ecbc8d6d2c66d Mon Sep 17 00:00:00 2001 From: "Alfred E. Heggestad" Date: Wed, 16 Mar 2011 21:57:52 +0000 Subject: [PATCH] patch: add pl_float() --- include/re_fmt.h | 1 + src/fmt/pl.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/include/re_fmt.h b/include/re_fmt.h index 54a274d..afc13c4 100644 --- a/include/re_fmt.h +++ b/include/re_fmt.h @@ -32,6 +32,7 @@ uint32_t pl_u32(const struct pl *pl); uint32_t pl_x32(const struct pl *pl); uint64_t pl_u64(const struct pl *pl); uint64_t pl_x64(const struct pl *pl); +double pl_float(const struct pl *pl); bool pl_isset(const struct pl *pl); int pl_strcpy(const struct pl *pl, char *str, size_t size); int pl_strdup(char **dst, const struct pl *src); diff --git a/src/fmt/pl.c b/src/fmt/pl.c index 33ee83e..30725e0 100644 --- a/src/fmt/pl.c +++ b/src/fmt/pl.c @@ -186,6 +186,44 @@ uint64_t pl_x64(const struct pl *pl) } +/** + * Convert a pointer-length object to floating point representation + * + * @param pl Pointer-length object + * + * @return Double value + */ +double pl_float(const struct pl *pl) +{ + double v=0, mul=1; + const char *p; + + if (!pl || !pl->p) + return 0; + + p = &pl->p[pl->l]; + + while (p > pl->p) { + + const char ch = *--p; + + if ('0' <= ch && ch <= '9') { + v += mul * (ch - '0'); + mul *= 10; + } + else if (ch == '.') { + v /= mul; + mul = 1; + } + else { + return 0; + } + } + + return v; +} + + /** * Check if pointer-length object is set *