summaryrefslogtreecommitdiff
path: root/src/modules/filters/utf8nfkd.cpp
diff options
context:
space:
mode:
authorRoberto C. Sanchez <roberto@connexer.com>2014-03-29 10:53:59 -0400
committerRoberto C. Sanchez <roberto@connexer.com>2014-03-29 10:53:59 -0400
commit03134fa5f6f25d92724ce4c183f9bbe12a9e37dc (patch)
tree847326a4de82f0241ac87cbbc427a1b92a696a02 /src/modules/filters/utf8nfkd.cpp
parentd7469385b05b9510338407fa123e9ad090f80af6 (diff)
Imported Upstream version 1.5.11
Diffstat (limited to 'src/modules/filters/utf8nfkd.cpp')
-rw-r--r--src/modules/filters/utf8nfkd.cpp52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/modules/filters/utf8nfkd.cpp b/src/modules/filters/utf8nfkd.cpp
new file mode 100644
index 0000000..a19d36b
--- /dev/null
+++ b/src/modules/filters/utf8nfkd.cpp
@@ -0,0 +1,52 @@
+/******************************************************************************
+*
+* utf8nfkd - SWFilter descendant to perform NFKD (compatability decomposition
+* normalization) on UTF-8 text
+*/
+
+#ifdef _ICU_
+
+#include <stdlib.h>
+
+#include <utilstr.h>
+
+#include <utf8nfkd.h>
+#include <swbuf.h>
+
+SWORD_NAMESPACE_START
+
+UTF8NFKD::UTF8NFKD() {
+ conv = ucnv_open("UTF-8", &err);
+}
+
+UTF8NFKD::~UTF8NFKD() {
+ ucnv_close(conv);
+}
+
+char UTF8NFKD::processText(SWBuf &text, const SWKey *key, const SWModule *module)
+{
+ if ((unsigned long)key < 2) // hack, we're en(1)/de(0)ciphering
+ return -1;
+
+ int32_t len = 5 + text.length() * 5;
+ source = new UChar[len + 1]; //each char could become a surrogate pair
+
+ // Convert UTF-8 string to UTF-16 (UChars)
+ int32_t ulen = ucnv_toUChars(conv, source, len, text.c_str(), -1, &err);
+ target = new UChar[len + 1];
+
+ //compatability decomposition
+ ulen = unorm_normalize(source, ulen, UNORM_NFKD, 0, target, len, &err);
+
+ text.setSize(len);
+ len = ucnv_fromUChars(conv, text.getRawData(), len, target, ulen, &err);
+ text.setSize(len);
+
+ delete [] source;
+ delete [] target;
+
+ return 0;
+}
+
+SWORD_NAMESPACE_END
+#endif