summaryrefslogtreecommitdiff
path: root/test/tests.c
diff options
context:
space:
mode:
authorSteven G. Johnson <stevenj@mit.edu>2015-10-31 18:13:25 -0400
committerSteven G. Johnson <stevenj@mit.edu>2015-10-31 18:13:25 -0400
commita5c9de204717d619ddb2fa4eb994542bb3cfc157 (patch)
treedcb694a5b28a802e119c46f7bf8c2a62b45a462d /test/tests.c
parentf67f1a72f7bbaac8dbf08a183193ac82641cd34b (diff)
parentb10b64dc10fc4c5ca236e85a620ee78e503e201c (diff)
downloadlibutf8proc-a5c9de204717d619ddb2fa4eb994542bb3cfc157.tar.gz
libutf8proc-a5c9de204717d619ddb2fa4eb994542bb3cfc157.tar.bz2
Merge pull request #58 from petercolberg/master
Fix build warnings
Diffstat (limited to 'test/tests.c')
-rw-r--r--test/tests.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/test/tests.c b/test/tests.c
new file mode 100644
index 0000000..0fb0da3
--- /dev/null
+++ b/test/tests.c
@@ -0,0 +1,46 @@
+/* Common functions for our test programs. */
+
+#include "tests.h"
+
+size_t lineno = 0;
+
+void check(int cond, const char *format, ...)
+{
+ if (!cond) {
+ va_list args;
+ fprintf(stderr, "line %zd: ", lineno);
+ va_start(args, format);
+ vfprintf(stderr, format, args);
+ va_end(args);
+ fprintf(stderr, "\n");
+ exit(1);
+ }
+}
+
+size_t skipspaces(const char *buf, size_t i)
+{
+ while (isspace(buf[i])) ++i;
+ return i;
+}
+
+/* if buf points to a sequence of codepoints encoded as hexadecimal strings,
+ separated by whitespace, and terminated by any character not in
+ [0-9a-fA-F] or whitespace, then stores the corresponding utf8 string
+ in dest, returning the number of bytes read from buf */
+size_t encode(char *dest, const char *buf)
+{
+ size_t i = 0, j, d = 0;
+ for (;;) {
+ int c;
+ i = skipspaces(buf, i);
+ for (j=i; buf[j] && strchr("0123456789abcdef", tolower(buf[j])); ++j)
+ ; /* find end of hex input */
+ if (j == i) { /* no codepoint found */
+ dest[d] = 0; /* NUL-terminate destination string */
+ return i + 1;
+ }
+ check(sscanf(buf + i, "%x", (unsigned int *)&c) == 1, "invalid hex input %s", buf+i);
+ i = j; /* skip to char after hex input */
+ d += utf8proc_encode_char(c, (utf8proc_uint8_t *) (dest + d));
+ }
+}