Code

data: Let strlen() and format() support string/decimal/integer arrays.
authorSebastian Harl <sh@tokkee.org>
Mon, 10 Nov 2014 08:33:36 +0000 (09:33 +0100)
committerSebastian Harl <sh@tokkee.org>
Mon, 10 Nov 2014 08:33:36 +0000 (09:33 +0100)
src/core/data.c
t/unit/core/data_test.c

index 53cc88835883db4d895a66953af33948969d3f60..28d4c36ad66cbd92149ef6bea5a1f97d0e635ca4 100644 (file)
@@ -787,9 +787,14 @@ sdb_data_strlen(const sdb_data_t *datum)
                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 */
@@ -804,7 +809,7 @@ sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
 
        size_t i, pos;
 
-       if ((! datum) || (! buf))
+       if ((! datum) || (! buf) || (! buflen))
                return -1;
 
        if (datum->type == SDB_TYPE_INTEGER) {
@@ -873,9 +878,32 @@ sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
                }
        }
        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)
@@ -1591,6 +1591,9 @@ END_TEST
 
 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;
@@ -1638,6 +1641,20 @@ START_TEST(test_format)
                        { 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;