Commit 48baabeb38c0afc3dc071038662f43419b058cf4

Daniel Stone 2010-06-15T19:30:30

Atoms: Avoid allocations in XkbAtomText() XkbAtomGetString() returns a freshly-allocated string, whereas XkbAtomText() returns the same in a temporary buffer. XkbAtomText used to call XkbAtomGetString() and then free the result, which seems quite spectacularly pointless when you think about it. Shuffle the atom code around so we don't have to allocate for XkbAtomText(). This changes semantics slightly wrt non-printable characters, but I haven't been able to see any effect so far. And it may well be ever so slightly quicker. Signed-off-by: Daniel Stone <daniel@fooishbar.org>

diff --git a/src/atom.c b/src/atom.c
index 127716f..9ca307c 100644
--- a/src/atom.c
+++ b/src/atom.c
@@ -104,8 +104,8 @@ XkbcInitAtoms(void)
     nodeTable[None] = NULL;
 }
 
-char *
-XkbcAtomGetString(Atom atom)
+static char *
+_XkbcAtomGetString(Atom atom)
 {
     NodePtr node;
 
@@ -113,7 +113,31 @@ XkbcAtomGetString(Atom atom)
         return NULL;
     if (!(node = nodeTable[atom]))
         return NULL;
-    return strdup(node->string);
+    return node->string;
+}
+
+char *
+XkbcAtomGetString(Atom atom)
+{
+    char *ret = _XkbcAtomGetString(atom);
+    return ret ? strdup(ret) : NULL;
+}
+
+char *
+XkbcAtomText(Atom atom)
+{
+    char *tmp, *ret;
+
+    tmp = _XkbcAtomGetString(atom);
+    if (!tmp)
+        return "";
+
+    ret = tbGetBuffer(strlen(tmp) + 1);
+    if (!ret)
+        return "";
+
+    strcpy(ret, tmp);
+    return ret;
 }
 
 static Atom
diff --git a/src/text.c b/src/text.c
index c5804f6..0c641a5 100644
--- a/src/text.c
+++ b/src/text.c
@@ -56,104 +56,6 @@ tbGetBuffer(unsigned int size)
     return rtrn;
 }
 
-static char *
-XkbStringText(char *str)
-{
-    char *buf, *in, *out;
-    int len;
-    Bool ok;
-
-    if (!str) {
-        buf = tbGetBuffer(2);
-        buf[0] = '\0';
-        return buf;
-    }
-
-    /* Find if there are any non-printable characters */
-    for (ok = True, len = 0, in = str; *in != '\0'; in++, len++) {
-        if (isprint(*in))
-            continue;
-
-        ok = False;
-        switch (*in) {
-        case '\n': case '\t': case '\v':
-        case '\b': case '\r': case '\f':
-            len++;
-            break;
-        default:
-            /* octal: \0ooo */
-            len += 4;
-            break;
-        }
-    }
-
-    if (ok)
-        return str;
-
-    /* Cleanup non-printable characters */
-    buf = tbGetBuffer(len + 1);
-    for (in = str, out = buf; *in != '\0'; in++) {
-        if (isprint(*in)) {
-            *out++ = *in;
-            continue;
-        }
-
-        *out++ = '\\';
-        switch (*in) {
-        case '\n':
-            *out++ = 'n';
-            break;
-        case '\t':
-            *out++ = 't';
-            break;
-        case '\v':
-            *out++ = 'v';
-            break;
-        case '\b':
-            *out++ = 'b';
-            break;
-        case '\r':
-            *out++ = 'r';
-            break;
-        case '\f':
-            *out++ = 'f';
-            break;
-        default:
-            *out++ = '0';
-            snprintf(out, 3, "%o", *in);
-            while (*out != '\0')
-                out++;
-        }
-    }
-
-    *out++ = '\0';
-    return buf;
-}
-
-char *
-XkbcAtomText(Atom atm)
-{
-    char *tmp, *rtrn;
-    int len;
-
-    tmp = XkbcAtomGetString(atm);
-
-    if (!tmp)
-        return "";
-
-    len = strlen(tmp) + 1;
-    if (len >= BUFFER_SIZE)
-        len = BUFFER_SIZE - 2;
-
-    rtrn = tbGetBuffer(len);
-    strncpy(rtrn, tmp, len);
-    rtrn[len] = '\0';
-
-    _XkbFree(tmp);
-
-    return XkbStringText(rtrn);
-}
-
 char *
 XkbcVModIndexText(XkbcDescPtr xkb, unsigned ndx)
 {