summaryrefslogtreecommitdiff
path: root/dlib/dlib.c
diff options
context:
space:
mode:
authorjcid <devnull@localhost>2008-04-07 18:55:14 +0200
committerjcid <devnull@localhost>2008-04-07 18:55:14 +0200
commit9388b5d4464d13117bcaad1fda1a1b7ebd8d0264 (patch)
tree8bb52fd9a34f8acd996eff309635b64594917f12 /dlib/dlib.c
parent56583b97209336f87a46bb1c2bd18a75b9fc588f (diff)
- Added dStr_printable() to dlib.
Diffstat (limited to 'dlib/dlib.c')
-rw-r--r--dlib/dlib.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/dlib/dlib.c b/dlib/dlib.c
index 862e642f..f01314eb 100644
--- a/dlib/dlib.c
+++ b/dlib/dlib.c
@@ -435,6 +435,40 @@ char *dStr_memmem(Dstr *haystack, Dstr *needle)
}
/*
+ * Return a printable representation of the provided Dstr, limited to a length
+ * of roughly maxlen.
+ *
+ * This is NOT threadsafe.
+ */
+const char *dStr_printable(Dstr *in, int maxlen)
+{
+ int i;
+ static const char *const HEX = "0123456789ABCDEF";
+ static Dstr *out = NULL;
+
+ if (in == NULL)
+ return NULL;
+
+ if (out)
+ dStr_truncate(out, 0);
+ else
+ out = dStr_sized_new(in->len);
+
+ for (i = 0; (i < in->len) && (out->len < maxlen); ++i) {
+ if (isprint(in->str[i]) || (in->str[i] == '\n')) {
+ dStr_append_c(out, in->str[i]);
+ } else {
+ dStr_append_l(out, "\\x", 2);
+ dStr_append_c(out, HEX[(in->str[i] >> 4) & 15]);
+ dStr_append_c(out, HEX[in->str[i] & 15]);
+ }
+ }
+ if (out->len >= maxlen)
+ dStr_append(out, "...");
+ return out->str;
+}
+
+/*
*- dList ---------------------------------------------------------------------
*/