diff options
author | Steven G. Johnson <stevenj@mit.edu> | 2015-03-06 17:36:08 -0500 |
---|---|---|
committer | Steven G. Johnson <stevenj@mit.edu> | 2015-03-06 17:36:08 -0500 |
commit | 90721f2d39b0cdd5d22409f1bf4f6ce4b7382944 (patch) | |
tree | 2b3ec29840c95bc551d86d40902120db5eb1da12 /test/tests.h | |
parent | 10f7e2ed5a7f3d05cbbc45f457be12456e6969d3 (diff) | |
download | libutf8proc-90721f2d39b0cdd5d22409f1bf4f6ce4b7382944.tar.gz libutf8proc-90721f2d39b0cdd5d22409f1bf4f6ce4b7382944.tar.bz2 |
directory cleanup: move tests and data into subdirectories
Diffstat (limited to 'test/tests.h')
-rw-r--r-- | test/tests.h | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/test/tests.h b/test/tests.h new file mode 100644 index 0000000..d4897f8 --- /dev/null +++ b/test/tests.h @@ -0,0 +1,53 @@ +/* Common functions and includes for our test programs. */ + +#include <stdio.h> +#include <stdlib.h> +#include <ctype.h> +#include <string.h> +#include <stdarg.h> + +#include "../utf8proc.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; + do { + 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", &c) == 1, "invalid hex input %s", buf+i); + i = j; /* skip to char after hex input */ + d += utf8proc_encode_char(c, (uint8_t *) (dest + d)); + } while (1); +} + |