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>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
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)
{