summaryrefslogtreecommitdiff
path: root/test/custom.c
diff options
context:
space:
mode:
authorSteven G. Johnson <stevenj@mit.edu>2016-11-30 10:40:26 -0500
committerGitHub <noreply@github.com>2016-11-30 10:40:26 -0500
commitb4621f43c3b8aaa5636cb129cd0f2e0f8cc81889 (patch)
tree5922e437cb79fa411b49ea2fd9089631db24f6f7 /test/custom.c
parent8da37e28920ba72b81c1d2cd4995647aadcd6db5 (diff)
downloadlibutf8proc-b4621f43c3b8aaa5636cb129cd0f2e0f8cc81889.tar.gz
libutf8proc-b4621f43c3b8aaa5636cb129cd0f2e0f8cc81889.tar.bz2
new utf8proc_map_custom for hooking in user-defined custom mappings (#89)
* new utf8proc_map_custom for hooking in user-defined custom mappings * whoops, add test program * NEWS, version bump for 2.1 * change test functions to static so that gcc doesn't complain about missing prototypes
Diffstat (limited to 'test/custom.c')
-rw-r--r--test/custom.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/test/custom.c b/test/custom.c
new file mode 100644
index 0000000..f85b3cc
--- /dev/null
+++ b/test/custom.c
@@ -0,0 +1,27 @@
+#include "tests.h"
+
+static int thunk_test = 1;
+
+static utf8proc_int32_t custom(utf8proc_int32_t codepoint, void *thunk)
+{
+ check(((int *) thunk) == &thunk_test, "unexpected thunk passed");
+ if (codepoint == 'a')
+ return 'b';
+ if (codepoint == 'S')
+ return 0x00df; /* ß */
+ return codepoint;
+}
+
+int main(void)
+{
+ utf8proc_uint8_t input[] = {0x41,0x61,0x53,0x62,0xef,0xbd,0x81,0x00}; /* "AaSb\uff41" */
+ utf8proc_uint8_t correct[] = {0x61,0x62,0x73,0x73,0x62,0x61,0x00}; /* "abssba" */
+ utf8proc_uint8_t *output;
+ utf8proc_map_custom(input, 0, &output, UTF8PROC_CASEFOLD | UTF8PROC_COMPOSE | UTF8PROC_COMPAT | UTF8PROC_NULLTERM,
+ custom, &thunk_test);
+ printf("mapped \"%s\" -> \"%s\"\n", (char*)input, (char*)output);
+ check(strlen((char*) output) == 6, "incorrect output length");
+ check(!memcmp(correct, output, 7), "incorrect output data");
+ free(output);
+ return 0;
+}