patch: add pl_float()
This commit is contained in:
parent
424119c0d6
commit
85ab7c2120
2 changed files with 39 additions and 0 deletions
|
@ -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);
|
||||
|
|
38
src/fmt/pl.c
38
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
|
||||
*
|
||||
|
|
Loading…
Add table
Reference in a new issue