summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: f578f96)
raw | patch | inline | side by side (parent: f578f96)
author | Sebastian Harl <sh@tokkee.org> | |
Mon, 10 Feb 2014 18:07:49 +0000 (19:07 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Mon, 10 Feb 2014 18:07:49 +0000 (19:07 +0100) |
Instead of calling strbuf_append() for each byte, precompute the string in a
simple for-loop and then append the result to the buffer. This basically
inlines a formatter specific to this use-case and, thus, should be must
faster.
simple for-loop and then append the result to the buffer. This basically
inlines a formatter specific to this use-case and, thus, should be must
faster.
src/core/data.c | patch | blob | history |
diff --git a/src/core/data.c b/src/core/data.c
index 1bb5400028223c89eca2f9a10e7ee3e81cc5cb03..8f4330eabccf173a2ddca0b6721dab2594295f70 100644 (file)
--- a/src/core/data.c
+++ b/src/core/data.c
break;
case SDB_TYPE_BINARY:
{
- size_t i;
- /* TODO: improve this! */
- sdb_strbuf_append(buf, "%s", "\"");
- for (i = 0; i < datum->data.binary.length; ++i)
- sdb_strbuf_append(buf, "\\%x",
- (int)datum->data.binary.datum[i]);
- sdb_strbuf_append(buf, "%s", "\"");
+ char tmp[4 * datum->data.binary.length + 1];
+ size_t i, pos;
+
+ pos = 0;
+ for (i = 0; i < datum->data.binary.length; ++i) {
+ int byte = (int)datum->data.binary.datum[i];
+ char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
+ '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
+
+ tmp[pos] = '\\';
+ ++pos;
+
+ if (byte > 0xf) {
+ tmp[pos] = hex[byte >> 4];
+ ++pos;
+ }
+ tmp[pos] = hex[byte & 0xf];
+ ++pos;
+ }
+ tmp[pos] = '\0';
+ sdb_strbuf_append(buf, "\"%s\"", tmp);
}
break;
default: