qnum: add uint type

In order to store integer values between INT64_MAX and UINT64_MAX, add
a uint64_t internal representation.

Backports commit 61a8f418b26a2d974e38e4ae55020aca8d402d88 from qemu
This commit is contained in:
Marc-André Lureau 2018-03-03 18:37:54 -05:00 committed by Lioncash
parent a57d8a5b50
commit 6ca6050206
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7
2 changed files with 71 additions and 0 deletions

View file

@ -19,6 +19,7 @@
typedef enum { typedef enum {
QNUM_I64, QNUM_I64,
QNUM_U64,
QNUM_DOUBLE QNUM_DOUBLE
} QNumKind; } QNumKind;
@ -27,15 +28,21 @@ typedef struct QNum {
QNumKind kind; QNumKind kind;
union { union {
int64_t i64; int64_t i64;
uint64_t u64;
double dbl; double dbl;
} u; } u;
} QNum; } QNum;
QNum *qnum_from_int(int64_t value); QNum *qnum_from_int(int64_t value);
QNum *qnum_from_uint(uint64_t value);
QNum *qnum_from_double(double value); QNum *qnum_from_double(double value);
bool qnum_get_try_int(const QNum *qn, int64_t *val); bool qnum_get_try_int(const QNum *qn, int64_t *val);
int64_t qnum_get_int(const QNum *qn); int64_t qnum_get_int(const QNum *qn);
bool qnum_get_try_uint(const QNum *qn, uint64_t *val);
uint64_t qnum_get_uint(const QNum *qn);
double qnum_get_double(QNum *qn); double qnum_get_double(QNum *qn);
char *qnum_to_string(QNum *qn); char *qnum_to_string(QNum *qn);

View file

@ -34,6 +34,22 @@ QNum *qnum_from_int(int64_t value)
return qn; return qn;
} }
/**
* qnum_from_uint(): Create a new QNum from an uint64_t
*
* Return strong reference.
*/
QNum *qnum_from_uint(uint64_t value)
{
QNum *qn = g_new(QNum, 1);
qobject_init(QOBJECT(qn), QTYPE_QNUM);
qn->kind = QNUM_U64;
qn->u.u64 = value;
return qn;
}
/** /**
* qnum_from_double(): Create a new QNum from a double * qnum_from_double(): Create a new QNum from a double
* *
@ -61,6 +77,12 @@ bool qnum_get_try_int(const QNum *qn, int64_t *val)
case QNUM_I64: case QNUM_I64:
*val = qn->u.i64; *val = qn->u.i64;
return true; return true;
case QNUM_U64:
if (qn->u.u64 > INT64_MAX) {
return false;
}
*val = qn->u.u64;
return true;
case QNUM_DOUBLE: case QNUM_DOUBLE:
return false; return false;
} }
@ -82,6 +104,44 @@ int64_t qnum_get_int(const QNum *qn)
return val; return val;
} }
/**
* qnum_get_uint(): Get an unsigned integer from the number
*
* Return true on success.
*/
bool qnum_get_try_uint(const QNum *qn, uint64_t *val)
{
switch (qn->kind) {
case QNUM_I64:
if (qn->u.i64 < 0) {
return false;
}
*val = qn->u.i64;
return true;
case QNUM_U64:
*val = qn->u.u64;
return true;
case QNUM_DOUBLE:
return false;
}
assert(0);
return false;
}
/**
* qnum_get_uint(): Get an unsigned integer from the number
*
* assert() on failure.
*/
uint64_t qnum_get_uint(const QNum *qn)
{
uint64_t val;
bool success = qnum_get_try_uint(qn, &val);
assert(success);
return val;
}
/** /**
* qnum_get_double(): Get a float representation of the number * qnum_get_double(): Get a float representation of the number
* *
@ -92,6 +152,8 @@ double qnum_get_double(QNum *qn)
switch (qn->kind) { switch (qn->kind) {
case QNUM_I64: case QNUM_I64:
return qn->u.i64; return qn->u.i64;
case QNUM_U64:
return qn->u.u64;
case QNUM_DOUBLE: case QNUM_DOUBLE:
return qn->u.dbl; return qn->u.dbl;
} }
@ -108,6 +170,8 @@ char *qnum_to_string(QNum *qn)
switch (qn->kind) { switch (qn->kind) {
case QNUM_I64: case QNUM_I64:
return g_strdup_printf("%" PRId64, qn->u.i64); return g_strdup_printf("%" PRId64, qn->u.i64);
case QNUM_U64:
return g_strdup_printf("%" PRIu64, qn->u.u64);
case QNUM_DOUBLE: case QNUM_DOUBLE:
/* FIXME: snprintf() is locale dependent; but JSON requires /* FIXME: snprintf() is locale dependent; but JSON requires
* numbers to be formatted as if in the C locale. Dependence * numbers to be formatted as if in the C locale. Dependence