]> git.ozlabs.org Git - ccan/commitdiff
Add xstring module - bounded string builder with three valued comparator
authorDan Good <dan@dancancode.com>
Thu, 10 Dec 2015 06:05:22 +0000 (06:05 +0000)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 10 Dec 2015 23:14:00 +0000 (09:44 +1030)
Makefile-ccan
ccan/xstring/LICENSE [new symlink]
ccan/xstring/_info [new file with mode: 0644]
ccan/xstring/test/run.c [new file with mode: 0644]
ccan/xstring/test/run2.c [new file with mode: 0644]
ccan/xstring/xstring.c [new file with mode: 0644]
ccan/xstring/xstring.h [new file with mode: 0644]
licenses/APACHE-2 [new file with mode: 0644]

index c21c953f11f812be503056d3acf74f441f394f86..8eac2d73c70586954aa5bed9c0ac86b42abf1423 100644 (file)
@@ -122,7 +122,8 @@ MODS_WITH_SRC := aga \
        time \
        timer \
        ttxml \
-       wwviaudio
+       wwviaudio \
+       xstring
 
 MODS:=$(MODS_WITH_SRC) $(MODS_NO_SRC)
 
diff --git a/ccan/xstring/LICENSE b/ccan/xstring/LICENSE
new file mode 120000 (symlink)
index 0000000..4f8ee74
--- /dev/null
@@ -0,0 +1 @@
+../../licenses/APACHE-2
\ No newline at end of file
diff --git a/ccan/xstring/_info b/ccan/xstring/_info
new file mode 100644 (file)
index 0000000..28928f1
--- /dev/null
@@ -0,0 +1,102 @@
+#include "config.h"
+#include <stdio.h>
+#include <string.h>
+
+/**
+ * xstring - bounded string builder with three valued comparator
+ *
+ * xstring handles string concatenation to a buffer, flagging truncation.
+ * Additions can be transactional, where the addition wholly succeeds or the buffer is reterminated at its prior length.
+ * Once flagged, no further additions are allowed.
+ * Clearing reuses the buffer from the beginning.
+ *
+ * When comparing two xstrings, the truncation state is taken into account.
+ * If either is truncated, an equality test returns unknown (-1).
+ * Testing if x contains y returns unknown (-1) if y is truncated.
+ * If x is whole, the test returns true (1) or false (0) appropriately.
+ * If x is truncated, the test returns true (1) if the known portion of x contains y, and unknown (-1) otherwise.
+ *
+ * The structure members are intended for direct access.
+ *
+ * Example:
+ *     // demo - break long lines
+ *     #include <string.h>
+ *     #include <stdio.h>
+ *     #include <unistd.h>
+ *     #include <err.h>
+ *     #include <fcntl.h>
+ *     #include <sys/types.h>
+ *     #include <sys/stat.h>
+ *     #include <sys/mman.h>
+ *
+ *     #include "ccan/xstring/xstring.h"
+ *
+ *     #define MAXLEN 80
+ *     #define MARGIN 10
+ *     int main(int argc, char *argv[])
+ *     {
+ *             char buf[BUFSIZ];
+ *             xstring _x, *x = &_x;
+ *             struct stat sb;
+ *             int fd, span, lnlen;
+ *             char *map, *p, *q;
+ *
+ *             xstrInit(x, buf, sizeof(buf), 0);
+ *             assert(sizeof(buf) > MAXLEN);
+ *
+ *             if (argc != 2) return 1;
+ *
+ *             if ((fd = open(argv[1], O_RDONLY)) == -1)
+ *                     err(1, "open");
+ *             if (fstat(fd, &sb) == -1)
+ *                     err(1, "stat");
+ *             if ((map = mmap(NULL, sb.st_size + 1, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0)) == MAP_FAILED)
+ *                     err(1, "mmap");
+ *             map[sb.st_size] = '\n';
+ *
+ *             for (p = map, lnlen = 0; p < map + sb.st_size; ) {
+ *                     span = strcspn(p, " \t\n") + 1;
+ *
+ *                     if (lnlen + span > MAXLEN) {
+ *                             span = MAXLEN - lnlen;
+ *                             xstrAddSubsT(x, p, span, "\n", 1, NULL);
+ *                     }
+ *                     else
+ *                             xstrAddSubT(x, p, span);
+ *
+ *                     if (x->truncated) {
+ *                             fputs(x->str, stdout);
+ *                             xstrClear(x);
+ *                             continue; // p unchanged
+ *                     }
+ *
+ *                     q = x->str + x->len - 1;
+ *                     if (lnlen + MARGIN > MAXLEN)
+ *                             *q = '\n';
+ *
+ *                     lnlen = *q == '\n' ? 0 : lnlen + span;
+ *                     p += span;
+ *             }
+ *
+ *             if (x->len)
+ *                     fputs(x->str, stdout);
+ *
+ *             return 0;
+ *     }
+ *
+ * License: APACHE-2
+ * Author: Dan Good <dan@dancancode.com>
+ */
+int main(int argc, char *argv[])
+{
+       /* Expect exactly one argument */
+       if (argc != 2)
+               return 1;
+
+       if (strcmp(argv[1], "depends") == 0) {
+               printf("ccan/failtest\n");
+               return 0;
+       }
+
+       return 1;
+}
diff --git a/ccan/xstring/test/run.c b/ccan/xstring/test/run.c
new file mode 100644 (file)
index 0000000..9f24d9a
--- /dev/null
@@ -0,0 +1,147 @@
+#include <ccan/xstring/xstring.h>
+/* Include the C files directly. */
+#include <ccan/xstring/xstring.c>
+#include <ccan/tap/tap.h>
+
+int main(void)
+{
+       plan_tests(70);
+
+       { //12
+               char buf[4] = { 'a', 'a', 'a', 'a' };
+               xstring a;
+
+               ok1(xstrInit(&a, buf, sizeof(buf), 0) == 0);
+               ok1(a.str == buf && a.len == 0 && a.cap == sizeof(buf) && a.truncated == 0 && a.str[0] == '\0');
+
+               buf[0] = 'a';
+               ok1(xstrInit(&a, buf, sizeof(buf), 1) == -1);
+               ok1(a.str == buf && a.len == 3 && a.cap == sizeof(buf) && a.truncated == -1 && strcmp(a.str, "aaa") == 0);
+
+               ok1(xstrInit(&a, buf, sizeof(buf), 1) == 0);
+               ok1(a.str == buf && a.len == 3 && a.cap == sizeof(buf) && a.truncated == 0 && strcmp(a.str, "aaa") == 0);
+
+               xstrClear(&a);
+               ok1(xstrAddChar(&a, '1') == 0 && a.truncated == 0 && strcmp(a.str, "1") == 0);
+               ok1(xstrAddChar(&a, '2') == 0 && a.truncated == 0 && strcmp(a.str, "12") == 0);
+               ok1(xstrAddChar(&a, '3') == 0 && a.truncated == 0 && strcmp(a.str, "123") == 0);
+               ok1(xstrAddChar(&a, '\0') == 0 && a.truncated == 0 && strcmp(a.str, "123") == 0);
+               ok1(xstrAddChar(&a, '4') == -1 && a.truncated == -1 && strcmp(a.str, "123") == 0);
+               ok1(xstrAddChar(&a, '5') == -1 && a.truncated == -1 && strcmp(a.str, "123") == 0);
+       }
+
+       { //21
+               char buf[10];
+               xstring _x, *x = &_x;
+
+               ok1(xstrInit(x, buf, sizeof(buf), 0) == 0);
+               ok1(x->str == buf && x->len == 0 && x->cap == sizeof(buf) && x->truncated == 0 && *x->str == '\0');
+
+               ok1(xstrAdd(x, "") == 0 && x->len == 0 && x->truncated == 0 && *x->str == '\0');
+               ok1(xstrAdd(x, NULL) == 0 && x->len == 0 && x->truncated == 0 && *x->str == '\0');
+
+               ok1(xstrAdd(x, "foo") == 0 && x->len == 3 && x->truncated == 0 && strcmp(x->str, "foo") == 0);
+               ok1(xstrAdd(x, "bar") == 0 && x->len == 6 && x->truncated == 0 && strcmp(x->str, "foobar") == 0);
+               ok1(xstrAdd(x, "baz") == 0 && x->len == 9 && x->truncated == 0 && strcmp(x->str, "foobarbaz") == 0);
+               ok1(xstrAdd(x, "oof") == -1 && x->len == 9 && x->truncated == -1 && strcmp(x->str, "foobarbaz") == 0);
+               ok1(xstrAdd(x, "rab") == -1 && x->len == 9 && x->truncated == -1 && strcmp(x->str, "foobarbaz") == 0);
+
+               xstrClear(x);
+               ok1(x->str == buf && x->len == 0 && x->cap == sizeof(buf) && x->truncated == 0 && *x->str == '\0');
+               ok1(xstrAdd(x, "foobarbazoof") == -1 && x->len == 9 && x->truncated == -1 && strcmp(x->str, "foobarbaz") == 0);
+
+               xstrClear(x);
+               ok1(xstrAdd(x, "foo") == 0 && x->len == 3 && x->truncated == 0 && strcmp(x->str, "foo") == 0);
+               ok1(xstrAddT(x, "foobarbazoof") == -1 && x->len == 3 && x->truncated == -1 && strcmp(x->str, "foo") == 0);
+               xstrClear(x);
+               ok1(xstrAddT(&_x, "foobarbazoof") == -1 && x->len == 0 && x->truncated == -1 && *x->str == '\0');
+
+               xstrClear(x);
+               ok1(xstrCat(x, "foo", "bar", "baz", NULL) == 0 && x->len == 9 && x->truncated == 0 && strcmp(x->str, "foobarbaz") == 0);
+               xstrClear(x);
+               ok1(xstrCat(x, "foo", "bar", "baz", "oof", NULL) == -1 && x->len == 9 && x->truncated == -1 && strcmp(x->str, "foobarbaz") == 0);
+               xstrClear(x);
+               ok1(xstrCatT(x, "foo", "bar", "baz", "oof", NULL) == -1 && x->len == 0 && x->truncated == -1 && *x->str == '\0');
+               xstrClear(x);
+               ok1(xstrCatT(&_x, "foo", "bar", "baz", "oof", NULL) == -1 && x->len == 0 && x->truncated == -1 && *x->str == '\0');
+
+               xstrClear(x);
+               ok1(xstrJoin(x, ",", "fo", "ba", "ba", NULL) == 0 && x->len == 8 && x->truncated == 0 && strcmp(x->str, "fo,ba,ba") == 0);
+               xstrClear(x);
+               ok1(xstrJoin(x, ",", "foobarbaz", "oof", NULL) == -1 && x->len == 9 && x->truncated == -1 && strcmp(x->str, "foobarbaz") == 0);
+               xstrClear(x);
+               ok1(xstrJoin(x, ",", "foobarba", "oof", NULL) == -1 && x->len == 9 && x->truncated == -1 && strcmp(x->str, "foobarba,") == 0);
+       }
+
+       { //9
+               char buf[10];
+               xstring _x, *x = &_x;
+
+               ok1(xstrInit(x, buf, sizeof(buf), 0) == 0);
+               ok1(xstrAddSub(x, NULL, 0) == 0 && x->len == 0 && x->truncated == 0 && *x->str == '\0');
+               ok1(xstrAddSub(x, "foo", 0) == 0 && x->len == 0 && x->truncated == 0 && *x->str == '\0');
+               ok1(xstrAddSub(x, "foobar", 3) == 0 && x->len == 3 && x->truncated == 0 && strcmp(x->str, "foo") == 0);
+               ok1(xstrAddSub(x, "foobarbaz", 7) == -1 && x->len == 3 && x->truncated == -1 && strcmp(x->str, "foo") == 0);
+
+               xstrClear(x);
+               ok1(xstrAddSubs(x, "aa", 1, "bbb", 2, NULL) == 0 && x->len == 3 && x->truncated == 0 && strcmp(x->str, "abb") == 0);
+               ok1(xstrAddSubs(x, "cccccccc", 7, NULL) == -1 && x->len == 3 && x->truncated == -1 && strcmp(x->str, "abb") == 0);
+               xstrClear(x);
+               ok1(xstrAddSubsT(x, "aa", 1, "bbb", 2, NULL) == 0 && x->len == 3 && x->truncated == 0 && strcmp(x->str, "abb") == 0);
+               ok1(xstrAddSubsT(x, "cccccccc", 7, NULL) == -1 && x->len == 3 && x->truncated == -1 && strcmp(x->str, "abb") == 0);
+       }
+
+       { //28
+               char a[10], b[10];
+               xstring _x, *x = &_x;
+               xstring _y, *y = &_y;
+
+               xstrInit(x, a, sizeof(a), 0);
+               xstrInit(y, b, sizeof(b), 0);
+               xstrAdd(x, "foobarbaz");
+               xstrAdd(y, "foobarbaz");
+               ok1(xstrEq3(x, y) == 1);
+               ok1(xstrEq(x, y) == 1);
+               ok1(xstrContains3(x, y, 1) == 1);
+               ok1(xstrContains(x, y, 1) == 1);
+               xstrAddChar(x, 'x');
+               ok1(xstrEq3(x, y) == -1);
+               ok1(xstrEq(x, y) == 0);
+               ok1(xstrContains3(x, y, 1) == 1);
+               ok1(xstrContains(x, y, 1) == 1);
+               xstrClear(x);
+               xstrAdd(x, "foobarbaz");
+               xstrAddChar(y, 'x');
+               ok1(xstrEq3(x, y) == -1);
+               ok1(xstrEq(x, y) == 0);
+               ok1(xstrContains3(x, y, 1) == -1);
+               ok1(xstrContains(x, y, 1) == 0);
+               xstrClear(y);
+               xstrAdd(y, "Foobarbaz");
+               ok1(xstrEq3(x, y) == 0);
+               ok1(xstrEq(x, y) == 0);
+
+               xstrClear(x);
+               xstrClear(y);
+               xstrAdd(x, "foo");
+               xstrAdd(y, "foobarbaz");
+               ok1(xstrContains3(x, y, 1) == 0);
+               ok1(xstrContains(x, y, 1) == 0);
+               ok1(xstrContains3(y, x, 1) == 1);
+               ok1(xstrContains(y, x, 1) == 1);
+               ok1(xstrContains3(y, x, 0) == 1);
+               ok1(xstrContains(y, x, 0) == 1);
+               ok1(xstrContains3(y, x, -1) == 0);
+               ok1(xstrContains(y, x, -1) == 0);
+               xstrClear(x);
+               xstrAdd(x, "baz");
+               ok1(xstrContains3(y, x, -1) == 1);
+               ok1(xstrContains(y, x, -1) == 1);
+               ok1(xstrContains3(y, x, 0) == 1);
+               ok1(xstrContains(y, x, 0) == 1);
+               ok1(xstrContains3(y, x, 1) == 0);
+               ok1(xstrContains(y, x, 1) == 0);
+       }
+
+       return exit_status();
+}
diff --git a/ccan/xstring/test/run2.c b/ccan/xstring/test/run2.c
new file mode 100644 (file)
index 0000000..6bd8f0f
--- /dev/null
@@ -0,0 +1,38 @@
+#include <ccan/failtest/failtest_override.h>
+#include <ccan/failtest/failtest.h>
+#include <ccan/xstring/xstring.h>
+/* Include the C files directly. */
+#include <ccan/xstring/xstring.c>
+#include <ccan/tap/tap.h>
+
+unsigned last_fail_line;
+
+enum failtest_result once_only(struct tlist_calls *history)
+{
+       const struct failtest_call *tail = tlist_tail(history, list);
+
+       if (tail->line == last_fail_line)
+               return FAIL_DONT_FAIL;
+
+       last_fail_line = tail->line;
+       return FAIL_OK;
+}
+
+int main(int argc, char *argv[])
+{
+       failtest_init(argc, argv);
+       failtest_hook = once_only;
+       plan_tests(3);
+
+       xstring *x;
+
+       ok1((x = xstrNew(100)) != NULL); // fail first malloc
+       if (x) xstrFree(x);
+
+       ok1((x = xstrNew(100)) != NULL); // fail second malloc
+       if (x) xstrFree(x);
+
+       ok1((x = xstrNew(0)) == NULL && errno == EINVAL);
+
+       failtest_exit(exit_status());
+}
diff --git a/ccan/xstring/xstring.c b/ccan/xstring/xstring.c
new file mode 100644 (file)
index 0000000..cb2e77f
--- /dev/null
@@ -0,0 +1,207 @@
+#include <errno.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#define _XOPEN_SOURCE 700
+#include <string.h>
+#include "xstring.h"
+
+xstring *xstrNew(const size_t size)
+{
+       char *str;
+       xstring *x;
+
+       if (size < 1) {
+               errno = EINVAL;
+               return NULL;
+       }
+
+       str = malloc(size);
+       if (!str)
+               return NULL;
+
+       x = malloc(sizeof(struct xstring));
+       if (!x) {
+               free(str);
+               return NULL;
+       }
+
+       xstrInit(x, str, size, 0);
+       return x;
+}
+
+int xstrInit(xstring *x, char *str, const size_t size, int keep)
+{
+       assert(x && str && size > 0);
+       memset(x, 0, sizeof(*x));
+
+       x->str = str;
+       x->cap = size;
+
+       if (keep) {
+               x->len = strnlen(str, x->cap);
+               if (x->cap == x->len) {
+                       x->truncated = -1;
+                       x->len--;
+               }
+       }
+
+       *(x->str + x->len) = '\0';
+
+       return x->truncated;
+}
+
+int xstrAddChar(xstring *x, const char c)
+{
+       assert(x);
+
+       if (x->truncated || c == '\0')
+               return x->truncated;
+
+       if (x->len + 1 < x->cap) {
+               char *p = x->str + x->len;
+               *p++ = c;
+               *p = '\0';
+               x->len++;
+       }
+       else
+               x->truncated = -1;
+
+       return x->truncated;
+}
+
+int xstrAdd(xstring *x, const char *src)
+{
+       char *p, *q, *s;
+
+       assert(x);
+
+       if (x->truncated || !src || *src == '\0')
+               return x->truncated;
+
+       for (s = (char *)src,
+            p = x->str + x->len,
+            q = x->str + x->cap - 1;
+            *s != '\0' && p < q; p++, s++) {
+               *p = *s;
+       }
+
+       *p = '\0';
+       x->len = (size_t) (p - x->str);
+
+       if (*s != '\0')
+               x->truncated = -1;
+
+       return x->truncated;
+}
+
+int xstrAddSub(xstring *x, const char *src, size_t len)
+{
+       assert(x);
+
+       if (x->truncated || !src || len == 0)
+               return x->truncated;
+
+       if (x->len + len + 1 > x->cap)
+               return x->truncated = -1;
+
+       memcpy(x->str + x->len, src, len);
+
+       x->len += len;
+       *(x->str + x->len) = '\0';
+
+       return x->truncated;
+}
+
+int xstrCat(xstring *x, ...)
+{
+       va_list ap;
+       char *s;
+
+       assert(x);
+       va_start(ap, x);
+       while ((s = va_arg(ap, char *)) != NULL) {
+               if (xstrAdd(x, s) == -1)
+                       break;
+       }
+       va_end(ap);
+       return x->truncated;
+}
+
+int xstrJoin(xstring *x, const char *sep, ...)
+{
+       va_list ap;
+       char *s;
+       int i;
+
+       assert(x && sep);
+       va_start(ap, sep);
+       for (i = 0; (s = va_arg(ap, char *)) != NULL; i++) {
+               if (i && xstrAdd(x, sep) == -1)
+                       break;
+               if (xstrAdd(x, s) == -1)
+                       break;
+       }
+       va_end(ap);
+       return x->truncated;
+}
+
+int xstrAddSubs(xstring *x, ...)
+{
+       va_list ap;
+       char *s;
+
+       assert(x);
+       va_start(ap, x);
+       while ((s = va_arg(ap, char *)) != NULL) {
+               size_t n = va_arg(ap, size_t);
+               if (xstrAddSub(x, s, n) == -1)
+                       break;
+       }
+       va_end(ap);
+       return x->truncated;
+}
+
+int xstrEq3(xstring *x, xstring *y)
+{
+       assert(x && y);
+
+       if (x->truncated || y->truncated)
+               return unknown;
+
+       return x->len == y->len && xstrContains3(x, y, 1);
+}
+
+/* Does the first string contain the second */
+int xstrContains3(xstring *x, xstring *y, int where)
+{
+       int b;
+
+       assert(x && y && where >= -1 && where <= 1);
+
+       if (y->truncated)
+               return unknown;
+
+       if (x->len < y->len)
+               return 0;
+
+       switch (where) {
+       case -1:
+               b = strncmp(x->str + x->len - y->len, y->str, y->len) == 0;
+               break;
+       case 0:
+               b = strstr(x->str, y->str) != NULL;
+               break;
+       case 1:
+               b = strncmp(x->str, y->str, y->len) == 0;
+               break;
+       }
+
+       return b ? 1 : x->truncated ? unknown : 0;
+}
+
+void xstrFree(xstring *x)
+{
+       assert(x);
+       free(x->str);
+       free(x);
+}
diff --git a/ccan/xstring/xstring.h b/ccan/xstring/xstring.h
new file mode 100644 (file)
index 0000000..4be6b44
--- /dev/null
@@ -0,0 +1,236 @@
+#ifndef _XSTRING_H
+#define _XSTRING_H 1
+
+#include <assert.h>
+#include <sys/types.h>
+
+/**
+ * struct xstring - string metadata
+ * @str: pointer to buf
+ * @len: current length of buf contents
+ * @cap: maximum capacity of buf
+ * @truncated: -1 indicates truncation
+ */
+typedef struct xstring {
+       char *str;
+       size_t len;
+       size_t cap;
+       int truncated;
+} xstring;
+
+/**
+ * xstrNew - mallocs and inits
+ * @size: size of buffer to allocate
+ *
+ * mallocs both buffer and struct, calls xstrInit
+ *
+ * Return: ptr to xstring or NULL
+ */
+xstring *xstrNew(const size_t size);
+
+/**
+ * xstrInit - initialize an xstring struct
+ * @x: pointer to xstring
+ * @str: buffer to manage
+ * @size: size of str
+ * @keep: if !0, keep existing contents of str;
+ *        otherwise, str[0] = '\0'
+ *
+ * Return: x->truncated
+ */
+int xstrInit(xstring *x, char *str, const size_t size, int keep);
+
+/**
+ * xstrClear - clear xstring
+ * @x: pointer to xstring
+ *
+ * This sets x->len and x->truncated to 0 and str[0] to '\0';
+ *
+ * Return: x->truncated (always 0)
+ */
+#define xstrClear(x) (xstrInit((x), (x)->str, (x)->cap, 0))
+
+/**
+ * xstrAddChar - add a single character
+ * @x: pointer to xstring
+ * @c: character to append
+ *
+ * Return: x->truncated
+ */
+int xstrAddChar(xstring *x, const char c);
+
+/**
+ * xstrAdd - append a string
+ * @x: pointer to xstring
+ * @src: string to append
+ *
+ * Append as much from src as fits - if not all, flag truncation.
+ *
+ * Return: x->truncated
+ */
+int xstrAdd(xstring *x, const char *src);
+
+/**
+ * xstrAddSub - append a substring
+ * @x: pointer to xstring
+ * @src: string to append
+ * @len: length of substring
+ *
+ * Append substring and '\0' if len fits, otherwise flag truncation.
+ *
+ * Return: x->truncated
+ */
+int xstrAddSub(xstring *x, const char *src, size_t len);
+
+/** xstrCat - append multiple strings
+ * @x: pointer to xstring
+ * @...: one or more strings followed by NULL
+ *
+ * Run xstrAdd for each string.
+ *
+ * Return: x->truncated
+ */
+int xstrCat(xstring *x, ...);
+
+/** xstrJoin - append multiple strings joined by sep
+ * @x: pointer to xstring
+ * @sep: separator string
+ * @...: one or more strings followed by NULL
+ *
+ * Run xstrAdd for each string and append sep between each pair.
+ *
+ * Return: x->truncated
+ */
+int xstrJoin(xstring *x, const char *sep, ...);
+
+/**
+ * xstrAddSubs - append multiple substrings
+ * @x: pointer to xstring
+ * @...: one or more pairs of string and length followed by NULL
+ *
+ * Run xstrAddSub for each pair of string and length.
+ *
+ * Return: x->truncated
+ */
+int xstrAddSubs(xstring *x, ...);
+
+#define transact(x, y)  ({ \
+       size_t last; \
+       int ret; \
+       assert((x)); \
+       last = (x)->len; \
+       if ((ret = (y)) == -1) { \
+               (x)->len = last; \
+               *((x)->str + (x)->len) = '\0'; \
+       } \
+       ret; \
+})
+
+/**
+ * xstrAddT - append a string as a transaction
+ * @x: pointer to xstring
+ * @src: string to append
+ *
+ * Run xstrAdd. Reterminate at inital length if truncation occurs.
+ *
+ * Return: x->truncated
+ */
+#define xstrAddT(x, y)        transact(x, xstrAdd((x), y))
+
+/** xstrCatT - append multiple strings as one transaction
+ * @x: pointer to xstring
+ * @...: one or more strings followed by NULL
+ *
+ * Run xstrCat. Reterminate at inital length if truncation occurs.
+ *
+ * Return: x->truncated
+ */
+#define xstrCatT(x, y...)     transact(x, xstrCat((x) , ## y))
+
+/** xstrJoinT - append multiple strings joined by sep as one transaction
+ * @x: pointer to xstring
+ * @sep: separator string
+ * @...: one or more strings followed by NULL
+ *
+ * Run xstrJoin. Reterminate at inital length if truncation occurs.
+ *
+ * Return: x->truncated
+ */
+#define xstrJoinT(x, y...)    transact(x, xstrJoin((x) , ## y))
+
+/**
+ * xstrAddSubsT - append multiple substrings as one transaction
+ * @x: pointer to xstring
+ * @...: one or more pairs of string and length followed by NULL
+ *
+ * Run xstrAddSubs. Reterminate at inital length if truncation occurs.
+ *
+ * Return: x->truncated
+ */
+#define xstrAddSubsT(x, y...) transact(x, xstrAddSubs((x) , ## y))
+
+/**
+ * xstrAddSubT - same as xstrAddSub
+ */
+// addsub is already transactional
+#define xstrAddSubT xstrAddSub
+
+#define unknown -1
+
+/**
+ * xstrEq3 - test if two strings are equal
+ * @x: pointer to xstring
+ * @y: pointer to xstring
+ *
+ * Return true (1) if the strings held by x and y match and no truncation has occurred.
+ * Return unknown (-1) if either is flagged as truncated.
+ * Return false (0) otherwise.
+ *
+ * Return: -1, 0, or 1
+ */
+int xstrEq3(xstring *x, xstring *y);
+
+/**
+ * xstrEq - test if two strings are equal
+ * @x: pointer to xstring
+ * @y: pointer to xstring
+ *
+ * Same as xstrEq3, but return false (0) for unknown (-1).
+ *
+ * Return: 0 or 1
+ */
+#define xstrEq(x, y) (xstrEq3((x), (y)) == 1)
+
+/**
+ * xstrContains3 - test if first string contains second
+ * @x: pointer to xstring
+ * @y: pointer to xstring
+ * @where: -1 (ends), 0 (anywhere), 1 (begins)
+ *
+ * If neither x nor y are truncated, returns true (1) or false (0).
+ * If y is truncated, return unknown (-1).
+ * If x is truncated, return true (1) if known portion of x contains y, unknown (-1) otherwise.
+ *
+ * Return: -1, 0, or 1
+ */
+int xstrContains3(xstring *x, xstring *y, int where);
+
+/**
+ * xstrContains3 - test if first string contains second
+ * @x: pointer to xstring
+ * @y: pointer to xstring
+ * @where: -1 (ends), 0 (anywhere), 1 (begins)
+ *
+ * Same as xstrContains3, but return false (0) for unknown (-1).
+ *
+ * Return: 0 or 1
+ */
+#define xstrContains(x, y, w) (xstrContains3((x), (y), (w)) == 1)
+
+/**
+ * xstrFree - free malloced memory
+ * @x: pointer to xstring
+ */
+void xstrFree(xstring *x);
+
+#endif
diff --git a/licenses/APACHE-2 b/licenses/APACHE-2
new file mode 100644 (file)
index 0000000..d645695
--- /dev/null
@@ -0,0 +1,202 @@
+
+                                 Apache License
+                           Version 2.0, January 2004
+                        http://www.apache.org/licenses/
+
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+   1. Definitions.
+
+      "License" shall mean the terms and conditions for use, reproduction,
+      and distribution as defined by Sections 1 through 9 of this document.
+
+      "Licensor" shall mean the copyright owner or entity authorized by
+      the copyright owner that is granting the License.
+
+      "Legal Entity" shall mean the union of the acting entity and all
+      other entities that control, are controlled by, or are under common
+      control with that entity. For the purposes of this definition,
+      "control" means (i) the power, direct or indirect, to cause the
+      direction or management of such entity, whether by contract or
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
+      outstanding shares, or (iii) beneficial ownership of such entity.
+
+      "You" (or "Your") shall mean an individual or Legal Entity
+      exercising permissions granted by this License.
+
+      "Source" form shall mean the preferred form for making modifications,
+      including but not limited to software source code, documentation
+      source, and configuration files.
+
+      "Object" form shall mean any form resulting from mechanical
+      transformation or translation of a Source form, including but
+      not limited to compiled object code, generated documentation,
+      and conversions to other media types.
+
+      "Work" shall mean the work of authorship, whether in Source or
+      Object form, made available under the License, as indicated by a
+      copyright notice that is included in or attached to the work
+      (an example is provided in the Appendix below).
+
+      "Derivative Works" shall mean any work, whether in Source or Object
+      form, that is based on (or derived from) the Work and for which the
+      editorial revisions, annotations, elaborations, or other modifications
+      represent, as a whole, an original work of authorship. For the purposes
+      of this License, Derivative Works shall not include works that remain
+      separable from, or merely link (or bind by name) to the interfaces of,
+      the Work and Derivative Works thereof.
+
+      "Contribution" shall mean any work of authorship, including
+      the original version of the Work and any modifications or additions
+      to that Work or Derivative Works thereof, that is intentionally
+      submitted to Licensor for inclusion in the Work by the copyright owner
+      or by an individual or Legal Entity authorized to submit on behalf of
+      the copyright owner. For the purposes of this definition, "submitted"
+      means any form of electronic, verbal, or written communication sent
+      to the Licensor or its representatives, including but not limited to
+      communication on electronic mailing lists, source code control systems,
+      and issue tracking systems that are managed by, or on behalf of, the
+      Licensor for the purpose of discussing and improving the Work, but
+      excluding communication that is conspicuously marked or otherwise
+      designated in writing by the copyright owner as "Not a Contribution."
+
+      "Contributor" shall mean Licensor and any individual or Legal Entity
+      on behalf of whom a Contribution has been received by Licensor and
+      subsequently incorporated within the Work.
+
+   2. Grant of Copyright License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      copyright license to reproduce, prepare Derivative Works of,
+      publicly display, publicly perform, sublicense, and distribute the
+      Work and such Derivative Works in Source or Object form.
+
+   3. Grant of Patent License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      (except as stated in this section) patent license to make, have made,
+      use, offer to sell, sell, import, and otherwise transfer the Work,
+      where such license applies only to those patent claims licensable
+      by such Contributor that are necessarily infringed by their
+      Contribution(s) alone or by combination of their Contribution(s)
+      with the Work to which such Contribution(s) was submitted. If You
+      institute patent litigation against any entity (including a
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
+      or a Contribution incorporated within the Work constitutes direct
+      or contributory patent infringement, then any patent licenses
+      granted to You under this License for that Work shall terminate
+      as of the date such litigation is filed.
+
+   4. Redistribution. You may reproduce and distribute copies of the
+      Work or Derivative Works thereof in any medium, with or without
+      modifications, and in Source or Object form, provided that You
+      meet the following conditions:
+
+      (a) You must give any other recipients of the Work or
+          Derivative Works a copy of this License; and
+
+      (b) You must cause any modified files to carry prominent notices
+          stating that You changed the files; and
+
+      (c) You must retain, in the Source form of any Derivative Works
+          that You distribute, all copyright, patent, trademark, and
+          attribution notices from the Source form of the Work,
+          excluding those notices that do not pertain to any part of
+          the Derivative Works; and
+
+      (d) If the Work includes a "NOTICE" text file as part of its
+          distribution, then any Derivative Works that You distribute must
+          include a readable copy of the attribution notices contained
+          within such NOTICE file, excluding those notices that do not
+          pertain to any part of the Derivative Works, in at least one
+          of the following places: within a NOTICE text file distributed
+          as part of the Derivative Works; within the Source form or
+          documentation, if provided along with the Derivative Works; or,
+          within a display generated by the Derivative Works, if and
+          wherever such third-party notices normally appear. The contents
+          of the NOTICE file are for informational purposes only and
+          do not modify the License. You may add Your own attribution
+          notices within Derivative Works that You distribute, alongside
+          or as an addendum to the NOTICE text from the Work, provided
+          that such additional attribution notices cannot be construed
+          as modifying the License.
+
+      You may add Your own copyright statement to Your modifications and
+      may provide additional or different license terms and conditions
+      for use, reproduction, or distribution of Your modifications, or
+      for any such Derivative Works as a whole, provided Your use,
+      reproduction, and distribution of the Work otherwise complies with
+      the conditions stated in this License.
+
+   5. Submission of Contributions. Unless You explicitly state otherwise,
+      any Contribution intentionally submitted for inclusion in the Work
+      by You to the Licensor shall be under the terms and conditions of
+      this License, without any additional terms or conditions.
+      Notwithstanding the above, nothing herein shall supersede or modify
+      the terms of any separate license agreement you may have executed
+      with Licensor regarding such Contributions.
+
+   6. Trademarks. This License does not grant permission to use the trade
+      names, trademarks, service marks, or product names of the Licensor,
+      except as required for reasonable and customary use in describing the
+      origin of the Work and reproducing the content of the NOTICE file.
+
+   7. Disclaimer of Warranty. Unless required by applicable law or
+      agreed to in writing, Licensor provides the Work (and each
+      Contributor provides its Contributions) on an "AS IS" BASIS,
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+      implied, including, without limitation, any warranties or conditions
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+      PARTICULAR PURPOSE. You are solely responsible for determining the
+      appropriateness of using or redistributing the Work and assume any
+      risks associated with Your exercise of permissions under this License.
+
+   8. Limitation of Liability. In no event and under no legal theory,
+      whether in tort (including negligence), contract, or otherwise,
+      unless required by applicable law (such as deliberate and grossly
+      negligent acts) or agreed to in writing, shall any Contributor be
+      liable to You for damages, including any direct, indirect, special,
+      incidental, or consequential damages of any character arising as a
+      result of this License or out of the use or inability to use the
+      Work (including but not limited to damages for loss of goodwill,
+      work stoppage, computer failure or malfunction, or any and all
+      other commercial damages or losses), even if such Contributor
+      has been advised of the possibility of such damages.
+
+   9. Accepting Warranty or Additional Liability. While redistributing
+      the Work or Derivative Works thereof, You may choose to offer,
+      and charge a fee for, acceptance of support, warranty, indemnity,
+      or other liability obligations and/or rights consistent with this
+      License. However, in accepting such obligations, You may act only
+      on Your own behalf and on Your sole responsibility, not on behalf
+      of any other Contributor, and only if You agree to indemnify,
+      defend, and hold each Contributor harmless for any liability
+      incurred by, or claims asserted against, such Contributor by reason
+      of your accepting any such warranty or additional liability.
+
+   END OF TERMS AND CONDITIONS
+
+   APPENDIX: How to apply the Apache License to your work.
+
+      To apply the Apache License to your work, attach the following
+      boilerplate notice, with the fields enclosed by brackets "[]"
+      replaced with your own identifying information. (Don't include
+      the brackets!)  The text should be enclosed in the appropriate
+      comment syntax for the file format. We also recommend that a
+      file or class name and description of purpose be included on the
+      same "printed page" as the copyright notice for easier
+      identification within third-party archives.
+
+   Copyright [yyyy] [name of copyright owner]
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.