Skip to content

Commit

Permalink
Add cmp_read/write_decimal
Browse files Browse the repository at this point in the history
Move cmp_read/write_float/double to the specific API
  • Loading branch information
Charlie Gunyon committed Jun 22, 2015
1 parent 0ab00f6 commit bff9f30
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
31 changes: 30 additions & 1 deletion cmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ THE SOFTWARE.

#include "cmp.h"

static const uint32_t version = 13;
static const uint32_t version = 14;
static const uint32_t mp_version = 5;

enum {
Expand Down Expand Up @@ -419,6 +419,16 @@ bool cmp_write_double(cmp_ctx_t *ctx, double d) {
return ctx->write(ctx, &d, sizeof(double));
}

bool cmp_write_decimal(cmp_ctx_t *ctx, double d) {
float f = (float)d;
float df = (double)f;

if (df == d)
return cmp_write_float(ctx, f);
else
return cmp_write_double(ctx, d);
}

bool cmp_write_nil(cmp_ctx_t *ctx) {
return write_type_marker(ctx, NIL_MARKER);
}
Expand Down Expand Up @@ -1585,6 +1595,25 @@ bool cmp_read_double(cmp_ctx_t *ctx, double *d) {
return true;
}

bool cmp_read_decimal(cmp_ctx_t *ctx, double *d) {
cmp_object_t obj;

if (!cmp_read_object(ctx, &obj))
return false;

switch (obj.type) {
case CMP_TYPE_FLOAT:
*d = (double)obj.as.flt;
return true;
case CMP_TYPE_DOUBLE:
*d = obj.as.dbl;
return true;
default:
ctx->error = INVALID_TYPE_ERROR;
return false;
}
}

bool cmp_read_nil(cmp_ctx_t *ctx) {
cmp_object_t obj;

Expand Down
26 changes: 16 additions & 10 deletions cmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,11 @@ bool cmp_write_integer(cmp_ctx_t *ctx, int64_t d);
/* Writes an unsigned integer to the backend */
bool cmp_write_uinteger(cmp_ctx_t *ctx, uint64_t u);

/* Writes a single-precision float to the backend */
bool cmp_write_float(cmp_ctx_t *ctx, float f);

/* Writes a double-precision float to the backend */
bool cmp_write_double(cmp_ctx_t *ctx, double d);
/*
* Writes a floating-point value (either single or double-precision) to the
* backend
*/
bool cmp_write_decimal(cmp_ctx_t *ctx, double d);

/* Writes NULL to the backend */
bool cmp_write_nil(cmp_ctx_t *ctx);
Expand Down Expand Up @@ -251,11 +251,11 @@ bool cmp_read_ulong(cmp_ctx_t *ctx, uint64_t *u);
/* Reads an unsigned integer */
bool cmp_read_uinteger(cmp_ctx_t *ctx, uint64_t *u);

/* Reads a single-precision float from the backend */
bool cmp_read_float(cmp_ctx_t *ctx, float *f);

/* Reads a double-precision float from the backend */
bool cmp_read_double(cmp_ctx_t *ctx, double *d);
/*
* Reads a floating point value (either single or double-precision) from the
* backend
*/
bool cmp_read_decimal(cmp_ctx_t *ctx, double *d);

/* "Reads" (more like "skips") a NULL value from the backend */
bool cmp_read_nil(cmp_ctx_t *ctx);
Expand Down Expand Up @@ -320,6 +320,9 @@ bool cmp_write_u16(cmp_ctx_t *ctx, uint16_t s);
bool cmp_write_u32(cmp_ctx_t *ctx, uint32_t i);
bool cmp_write_u64(cmp_ctx_t *ctx, uint64_t l);

bool cmp_write_float(cmp_ctx_t *ctx, float f);
bool cmp_write_double(cmp_ctx_t *ctx, double d);

bool cmp_write_fixstr_marker(cmp_ctx_t *ctx, uint8_t size);
bool cmp_write_fixstr(cmp_ctx_t *ctx, const char *data, uint8_t size);
bool cmp_write_str8_marker(cmp_ctx_t *ctx, uint8_t size);
Expand Down Expand Up @@ -380,6 +383,9 @@ bool cmp_read_u16(cmp_ctx_t *ctx, uint16_t *s);
bool cmp_read_u32(cmp_ctx_t *ctx, uint32_t *i);
bool cmp_read_u64(cmp_ctx_t *ctx, uint64_t *l);

bool cmp_read_float(cmp_ctx_t *ctx, float *f);
bool cmp_read_double(cmp_ctx_t *ctx, double *d);

bool cmp_read_fixext1_marker(cmp_ctx_t *ctx, int8_t *type);
bool cmp_read_fixext1(cmp_ctx_t *ctx, int8_t *type, void *data);
bool cmp_read_fixext2_marker(cmp_ctx_t *ctx, int8_t *type);
Expand Down
20 changes: 20 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2406,6 +2406,26 @@ bool run_number_tests(void) {
9
);

test_format(
cmp_write_decimal,
cmp_read_decimal,
flt,
double,
2.0f,
"\xca\x40\x00\x00\x00",
5
);

test_format(
cmp_write_decimal,
cmp_read_decimal,
dbl,
double,
1111111111111111.125000,
"\xcb\x43\x0f\x94\x65\xb8\xab\x8e\x39",
9
);

return true;
}

Expand Down

0 comments on commit bff9f30

Please sign in to comment.