summaryrefslogtreecommitdiff
path: root/src/modules/filters/utf8nfc.cpp
diff options
context:
space:
mode:
authorRoberto C. Sanchez <roberto@connexer.com>2014-03-29 10:53:33 -0400
committerRoberto C. Sanchez <roberto@connexer.com>2014-03-29 10:53:33 -0400
commit8d3fc864d094eeadc721f8e93436b37a5fab173e (patch)
tree05e201c67dca55b4ccdf90ad479a25d95e3b1e63 /src/modules/filters/utf8nfc.cpp
Imported Upstream version 1.5.3
Diffstat (limited to 'src/modules/filters/utf8nfc.cpp')
-rw-r--r--src/modules/filters/utf8nfc.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/modules/filters/utf8nfc.cpp b/src/modules/filters/utf8nfc.cpp
new file mode 100644
index 0000000..df9e090
--- /dev/null
+++ b/src/modules/filters/utf8nfc.cpp
@@ -0,0 +1,46 @@
+/******************************************************************************
+*
+* utf8nfc - SWFilter decendant to perform NFC (canonical composition
+* normalization) on UTF-8 text
+*/
+
+#ifdef _ICU_
+
+#include <stdlib.h>
+#include <string.h>
+
+#ifdef __GNUC__
+#include <unixstr.h>
+#endif
+
+#include <utf8nfc.h>
+
+UTF8NFC::UTF8NFC() {
+ conv = ucnv_open("UTF-8", &err);
+}
+
+UTF8NFC::~UTF8NFC() {
+ ucnv_close(conv);
+}
+
+char UTF8NFC::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];
+
+ //canonical composition
+ unorm_normalize(source, len, UNORM_NFC, 0, target, len, &err);
+
+ ucnv_fromUChars(conv, text, maxlen, target, -1, &err);
+
+ delete [] source;
+ delete [] target;
+
+ return 0;
+}
+
+#endif