summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 1830b62)
raw | patch | inline | side by side (parent: 1830b62)
author | Sebastian Harl <sh@tokkee.org> | |
Thu, 20 Feb 2014 21:24:48 +0000 (22:24 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Thu, 20 Feb 2014 21:24:48 +0000 (22:24 +0100) |
This function provides a worst-case estimate of the number of bytes required
to format a datum as a string.
to format a datum as a string.
src/core/data.c | patch | blob | history | |
src/include/core/data.h | patch | blob | history |
diff --git a/src/core/data.c b/src/core/data.c
index 54b6d282e6640d755e38457e4dbd497bb995fd32..4c89b43dd179363aa7644a8a35dda5206739d10d 100644 (file)
--- a/src/core/data.c
+++ b/src/core/data.c
}
} /* sdb_data_free_datum */
+size_t
+sdb_data_strlen(sdb_data_t *datum)
+{
+ if (! datum)
+ return 0;
+
+ switch (datum->type) {
+ case SDB_TYPE_INTEGER:
+ /* log(64) */
+ return 20;
+ case SDB_TYPE_DECIMAL:
+ /* XXX: -0xN.NNNNNNp+NNN */
+ return 42;
+ case SDB_TYPE_STRING:
+ if (! datum->data.string)
+ return 6; /* "NULL" */
+ /* in the worst case, each character needs to be escaped */
+ return 2 * strlen(datum->data.string) + 2;
+ case SDB_TYPE_DATETIME:
+ /* "YYYY-MM-DD HH:MM:SS +zzzz" */
+ return 27;
+ case SDB_TYPE_BINARY:
+ /* "\xNN" */
+ return 4 * datum->data.binary.length + 2;
+ }
+ return 0;
+} /* sdb_data_strlen */
+
int
sdb_data_format(sdb_data_t *datum, sdb_strbuf_t *buf)
{
index c7c51b830c3761f91bb0ff55736a26087fb560e1..54a2ed2d0d703bec5e227dfaafa53ad136753781 100644 (file)
--- a/src/include/core/data.h
+++ b/src/include/core/data.h
void
sdb_data_free_datum(sdb_data_t *datum);
+/*
+ * sdb_data_strlen:
+ * Returns a (worst-case) estimate for the number of bytes required to format
+ * the datum as a string. Does not take the terminating null byte into
+ * account.
+ */
+size_t
+sdb_data_strlen(sdb_data_t *datum);
+
/*
* sdb_data_format:
* Append the specified datum to the specified string buffer using a default