summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 0ec2844)
raw | patch | inline | side by side (parent: 0ec2844)
author | Sebastian Harl <sh@tokkee.org> | |
Mon, 10 Nov 2014 08:33:36 +0000 (09:33 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Mon, 10 Nov 2014 08:33:36 +0000 (09:33 +0100) |
src/core/data.c | patch | blob | history | |
t/unit/core/data_test.c | patch | blob | history |
diff --git a/src/core/data.c b/src/core/data.c
index 53cc88835883db4d895a66953af33948969d3f60..28d4c36ad66cbd92149ef6bea5a1f97d0e635ca4 100644 (file)
--- a/src/core/data.c
+++ b/src/core/data.c
return strlen(datum->data.re.raw) + 4;
}
else if (datum->type & SDB_TYPE_ARRAY) {
- /* TODO */
- errno = ENOTSUP;
- return 0;
+ size_t len = 2; /* [] */
+ size_t i;
+ for (i = 0; i < datum->data.array.length; ++i) {
+ sdb_data_t v = SDB_DATA_INIT;
+ sdb_data_array_get(datum, i, &v);
+ len += sdb_data_strlen(&v) + 1;
+ }
+ return len;
}
return 0;
} /* sdb_data_strlen */
size_t i, pos;
- if ((! datum) || (! buf))
+ if ((! datum) || (! buf) || (! buflen))
return -1;
if (datum->type == SDB_TYPE_INTEGER) {
}
}
else if (datum->type & SDB_TYPE_ARRAY) {
- /* TODO */
- errno = ENOTSUP;
- return -1;
+ ret = 1;
+ buf[0] = '[';
+ for (i = 0; i < datum->data.array.length; ++i) {
+ sdb_data_t v = SDB_DATA_INIT;
+ int n;
+ if ((size_t)ret >= buflen - 1)
+ break;
+
+ if (ret > 1) {
+ buf[ret] = ',';
+ buf[ret + 1] = ' ';
+ ret += 2;
+ }
+
+ sdb_data_array_get(datum, i, &v);
+ n = sdb_data_format(&v, buf + ret, buflen - ret, quoted);
+ if (n > 0)
+ ret += n;
+ else
+ break;
+ }
+ if ((size_t)ret < buflen - 1) {
+ buf[ret] = ']';
+ buf[ret + 1] = '\0';
+ ++ret;
+ }
}
if (is_null) {
index 704ebdd48b43ae58cf38b62c8d18fb4c0e1b781c..2da33a19833b5d1204aa39ce2834511cb5e13031 100644 (file)
--- a/t/unit/core/data_test.c
+++ b/t/unit/core/data_test.c
START_TEST(test_format)
{
+ int64_t int_values[] = { 47, 11, 23 };
+ char *string_values[] = { "foo", "bar", "qux", "baz" };
+
struct {
sdb_data_t datum;
const char *expected;
{ SDB_TYPE_REGEX, { .re = { "some regex", empty_re } } },
"\"/some regex/\"",
},
+ {
+ {
+ SDB_TYPE_INTEGER | SDB_TYPE_ARRAY,
+ { .array = { SDB_STATIC_ARRAY_LEN(int_values), int_values } },
+ },
+ "[47, 11, 23]",
+ },
+ {
+ {
+ SDB_TYPE_STRING | SDB_TYPE_ARRAY,
+ { .array = { SDB_STATIC_ARRAY_LEN(string_values), string_values } },
+ },
+ "[\"foo\", \"bar\", \"qux\", \"baz\"]",
+ },
};
size_t i;