summaryrefslogtreecommitdiff
path: root/src/modules/filters/utf8nfkd.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules/filters/utf8nfkd.cpp')
-rw-r--r--src/modules/filters/utf8nfkd.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/modules/filters/utf8nfkd.cpp b/src/modules/filters/utf8nfkd.cpp
new file mode 100644
index 0000000..450cbbf
--- /dev/null
+++ b/src/modules/filters/utf8nfkd.cpp
@@ -0,0 +1,46 @@
+/******************************************************************************
+*
+* utf8nfkd - SWFilter decendant to perform NFKD (compatability decomposition
+* normalization) on UTF-8 text
+*/
+
+#ifdef _ICU_
+
+#include <stdlib.h>
+#include <string.h>
+
+#ifdef __GNUC__
+#include <unixstr.h>
+#endif
+
+#include <utf8nfkd.h>
+
+UTF8NFKD::UTF8NFKD() {
+ conv = ucnv_open("UTF-8", &err);
+}
+
+UTF8NFKD::~UTF8NFKD() {
+ ucnv_close(conv);
+}
+
+char UTF8NFKD::ProcessText(char *text, int maxlen, const SWKey *key, const SWModule *module)
+{
+ int32_t len = strlen(text) * 2;
+ source = new UChar[len + 1]; //each char could become a surrogate pair
+
+ // Convert UTF-8 string to UTF-16 (UChars)
+ len = ucnv_toUChars(conv, source, len, text, -1, &err);
+ target = new UChar[len + 1];
+
+ //compatability decomposition
+ unorm_normalize(source, len, UNORM_NFKD, 0, target, len, &err);
+
+ ucnv_fromUChars(conv, text, maxlen, target, -1, &err);
+
+ delete [] source;
+ delete [] target;
+
+ return 0;
+}
+
+#endif