Code

data: Format NULL as NULL and never quote it.
authorSebastian Harl <sh@tokkee.org>
Tue, 21 Oct 2014 08:02:07 +0000 (10:02 +0200)
committerSebastian Harl <sh@tokkee.org>
Tue, 21 Oct 2014 08:02:07 +0000 (10:02 +0200)
The idea, previously, was to make NULL values easily identifiable in all
messages but the focus really is on the JSON output which supports a real null
value.

src/core/data.c
t/unit/core/data_test.c

index 1f3aa82a42a641fad531c6b682363c51813af168..2994b5f50d3e18348dd183a553c719322e48002c 100644 (file)
@@ -640,7 +640,7 @@ sdb_data_strlen(const sdb_data_t *datum)
        }
        else if (datum->type == SDB_TYPE_STRING) {
                if (! datum->data.string)
-                       return 8; /* "<NULL>" */
+                       return 6; /* NULL */
                /* in the worst case, each character needs to be escaped */
                return 2 * strlen(datum->data.string) + 2;
        }
@@ -650,13 +650,13 @@ sdb_data_strlen(const sdb_data_t *datum)
        }
        else if (datum->type == SDB_TYPE_BINARY) {
                if (! datum->data.binary.datum)
-                       return 8; /* "<NULL>" */
+                       return 6; /* NULL */
                /* "\xNN" */
                return 4 * datum->data.binary.length + 2;
        }
        else if (datum->type == SDB_TYPE_REGEX) {
                if (! datum->data.re.raw)
-                       return 8; /* "<NULL>" */
+                       return 6; /* NULL */
                /* "/.../" */
                return strlen(datum->data.re.raw) + 4;
        }
@@ -673,6 +673,7 @@ sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
 {
        char tmp[sdb_data_strlen(datum) + 1];
        char *data = NULL;
+       _Bool is_null = 0;
        int ret = -1;
 
        size_t i, pos;
@@ -688,7 +689,7 @@ sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
        }
        else if (datum->type == SDB_TYPE_STRING) {
                if (! datum->data.string)
-                       data = "<NULL>";
+                       is_null = 1;
                else {
                        pos = 0;
                        for (i = 0; i < strlen(datum->data.string); ++i) {
@@ -735,11 +736,11 @@ sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
                        data = tmp;
                }
                else
-                       data = "<NULL>";
+                       is_null = 1;
        }
        else if (datum->type == SDB_TYPE_REGEX) {
                if (! datum->data.re.raw)
-                       data = "<NULL>";
+                       is_null = 1;
                else {
                        snprintf(tmp, sizeof(tmp), "/%s/", datum->data.re.raw);
                        data = tmp;
@@ -751,7 +752,12 @@ sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
                return -1;
        }
 
-       if (data) {
+       if (is_null) {
+               /* never quote NULL */
+               strncpy(buf, "NULL", buflen);
+               ret = (int)SDB_MIN(buflen, 4);
+       }
+       else if (data) {
                if (quoted == SDB_UNQUOTED)
                        ret = snprintf(buf, buflen, "%s", data);
                else if (quoted == SDB_SINGLE_QUOTED)
index 5c10c59f7693c465b49db2ae5544568f8566634b..e8567b1ad61c1f2cb2c1a3cf0f72874a93cdfcc9 100644 (file)
@@ -1285,7 +1285,7 @@ START_TEST(test_format)
                },
                {
                        { SDB_TYPE_STRING, { .string = NULL } },
-                       "\"<NULL>\"",
+                       "NULL",
                },
                {
                        { SDB_TYPE_STRING, { .string = "this is a test" } },
@@ -1301,7 +1301,7 @@ START_TEST(test_format)
                },
                {
                        { SDB_TYPE_BINARY, { .binary = { 0, NULL } } },
-                       "\"<NULL>\"",
+                       "NULL",
                },
                {
                        {