commit: r1680 - in neon/trunk: src test
joe at manyfish.co.uk
joe at manyfish.co.uk
Tue Aug 11 11:50:33 EDT 2009
Author: joe
Date: Tue Aug 11 08:50:33 2009
New Revision: 1680
Modified:
neon/trunk/src/ne_string.c
neon/trunk/src/ne_string.h
neon/trunk/test/string-tests.c
Log:
* src/ne_string.c (qappend_count, quoted_append): Factor out from
ne_buffer_qappend.
(ne_strnqdup): New function.
* src/ne_string.h (ne_strnqdup): New prototype.
* test/string-tests.c (qappend): Test for it.
Modified: neon/trunk/src/ne_string.c
==============================================================================
--- neon/trunk/src/ne_string.c (original)
+++ neon/trunk/src/ne_string.c Tue Aug 11 08:50:33 2009
@@ -276,25 +276,30 @@
static const char hex_chars[16] = "0123456789ABCDEF";
-void ne_buffer_qappend(ne_buffer *buf, const unsigned char *data, size_t len)
+/* Return the expected number of bytes needed to append the string
+ * beginning at byte 's', where 'send' points to the last byte after
+ * 's'. */
+static size_t qappend_count(const unsigned char *s, const unsigned char *send)
{
- const unsigned char *p, *dend = data + len;
- size_t needed;
- char *q, *qs;
-
- /* Determine the expected number of bytes needed to append the
- * string. */
- for (p = data, needed = 0; p < dend; p++) {
- needed += ascii_quote[*p];
+ const unsigned char *p;
+ size_t ret;
+
+ for (p = s, ret = 0; p < send; p++) {
+ ret += ascii_quote[*p];
}
- ne_buffer_grow(buf, buf->used + needed);
+ return ret;
+}
- /* buf->used >= 1, so this is safe. */
- qs = q = buf->data + buf->used - 1;
+/* Append the string 's', up to but not including 'send', to string
+ * 'dest', quoting along the way. Returns pointer to NUL. */
+static char *quoted_append(char *dest, const unsigned char *s,
+ const unsigned char *send)
+{
+ const unsigned char *p;
+ char *q = dest;
- /* Append the string, quoting along the way. */
- for (p = data; p < dend; p++) {
+ for (p = s; p < send; p++) {
if (ascii_quote[*p] == 1) {
*q++ = *p;
}
@@ -309,11 +314,36 @@
/* NUL terminate after the last character */
*q = '\0';
+ return q;
+}
+
+void ne_buffer_qappend(ne_buffer *buf, const unsigned char *data, size_t len)
+{
+ const unsigned char *dend = data + len;
+ char *q, *qs;
+
+ ne_buffer_grow(buf, buf->used + qappend_count(data, dend));
+
+ /* buf->used >= 1, so this is safe. */
+ qs = buf->data + buf->used - 1;
+
+ q = quoted_append(qs, data, dend);
+
/* used already accounts for a NUL, so increment by number of
* characters appended, *before* the NUL. */
buf->used += q - qs;
}
+char *ne_strnqdup(const unsigned char *data, size_t len)
+{
+ const unsigned char *dend = data + len;
+ char *dest = malloc(qappend_count(data, dend) + 1);
+
+ quoted_append(dest, data, dend);
+
+ return dest;
+}
+
static const char b64_alphabet[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
Modified: neon/trunk/src/ne_string.h
==============================================================================
--- neon/trunk/src/ne_string.h (original)
+++ neon/trunk/src/ne_string.h Tue Aug 11 08:50:33 2009
@@ -139,6 +139,11 @@
#define ne_strnzcpy(dest, src, n) do { size_t ne__nm1 = (n) - 1; \
strncpy(dest, src, ne__nm1); dest[ne__nm1] = '\0'; } while (0)
+/* Return a malloc-allocated copy of 'data', of length 'len', with all
+ * non-ASCII bytes, and ASCII control characters escaped. (Note that
+ * the escaping includes the NUL byte). */
+char *ne_strnqdup(const unsigned char *data, size_t len);
+
/* Return malloc-allocated concatenation of all NUL-terminated string
* arguments, up to a terminating NULL pointer. */
char *ne_concat(const char *str, ...)
Modified: neon/trunk/test/string-tests.c
==============================================================================
--- neon/trunk/test/string-tests.c (original)
+++ neon/trunk/test/string-tests.c Tue Aug 11 08:50:33 2009
@@ -628,8 +628,10 @@
for (n = 0; ts[n].in; n++) {
ne_buffer *buf = ne_buffer_create();
+ char *s;
+ const unsigned char *in = (const unsigned char *)ts[n].in;
- ne_buffer_qappend(buf, (const unsigned char *)ts[n].in, ts[n].inlen);
+ ne_buffer_qappend(buf, in, ts[n].inlen);
ONCMP(buf->data, ts[n].out);
@@ -637,6 +639,11 @@
("bad buffer length for '%s': %" NE_FMT_SIZE_T,
ts[n].out, buf->used));
+ s = ne_strnqdup(in, ts[n].inlen);
+
+ ONCMP(s, ts[n].out);
+
+ ne_free(s);
ne_buffer_destroy(buf);
}
More information about the neon-commits
mailing list