From f727641c659b0c372fb0372b588b59c7dd65e61a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Tue, 5 Jun 2012 12:16:50 +0200
Subject: Use faster conversion functions in ICU.


diff --git a/Swiften/IDN/ICUConverter.h b/Swiften/IDN/ICUConverter.h
index 6ed760a..c476b54 100644
--- a/Swiften/IDN/ICUConverter.h
+++ b/Swiften/IDN/ICUConverter.h
@@ -8,11 +8,10 @@
 
 #pragma GCC diagnostic ignored "-Wold-style-cast"
 
-#include <unicode/ucnv.h>
+#include <unicode/ustring.h>
 #include <cassert>
 #include <string>
 #include <vector>
-#include <boost/shared_ptr.hpp>
 #include <Swiften/Base/ByteArray.h>
 #include <Swiften/Base/SafeByteArray.h>
 
@@ -22,24 +21,21 @@ namespace Swift {
 		public:
 			typedef std::vector<UChar, SafeAllocator<UChar> > ICUString;
 
-			ICUConverter() {
-				UErrorCode status = U_ZERO_ERROR;
-				icuConverter.reset(ucnv_open("utf-8", &status), ucnv_close);
-				assert(U_SUCCESS(status));
-			}
-
 			template<typename T>
 			ICUString convertToICUString(const T& s) {
 				ICUString result;
 				result.resize(s.size());
 				UErrorCode status = U_ZERO_ERROR;
-				int icuResultLength = ucnv_toUChars(icuConverter.get(), vecptr(result), result.size(), toConstCharArray(s), s.size(), &status);
+				int icuResultLength = result.size();
+				u_strFromUTF8Lenient(vecptr(result), result.size(), &icuResultLength, toConstCharArray(s), s.size(), &status);
 				if (status == U_BUFFER_OVERFLOW_ERROR) {
 					status = U_ZERO_ERROR;
 					result.resize(icuResultLength);
-					icuResultLength = ucnv_toUChars(icuConverter.get(), vecptr(result), result.size(), toConstCharArray(s), s.size(), &status);
+					u_strFromUTF8Lenient(vecptr(result), result.size(), &icuResultLength, toConstCharArray(s), s.size(), &status);
+				}
+				if (U_FAILURE(status)) {
+					return ICUString();
 				}
-				assert(U_SUCCESS(status));
 				result.resize(icuResultLength);
 				return result;
 			}
@@ -52,13 +48,16 @@ namespace Swift {
 				std::vector<char, SafeAllocator<char> > result;
 				result.resize(input.size());
 				UErrorCode status = U_ZERO_ERROR;
-				int inputLength = ucnv_fromUChars(icuConverter.get(), vecptr(result), result.size(), vecptr(input), input.size(), &status);
+				int inputLength = result.size();
+				u_strToUTF8(vecptr(result), result.size(), &inputLength, vecptr(input), input.size(), &status);
 				if (status == U_BUFFER_OVERFLOW_ERROR) {
 					status = U_ZERO_ERROR;
 					result.resize(inputLength);
-					inputLength = ucnv_fromUChars(icuConverter.get(), vecptr(result), result.size(), vecptr(input), input.size(), &status);
+					u_strToUTF8(vecptr(result), result.size(), &inputLength, vecptr(input), input.size(), &status);
+				}
+				if (U_FAILURE(status)) {
+					return std::vector<char, SafeAllocator<char> >();
 				}
-				assert(U_SUCCESS(status));
 
 				result.resize(inputLength + 1);
 				result[result.size() - 1] = '\0';
@@ -73,9 +72,6 @@ namespace Swift {
 			static const char* toConstCharArray(const std::vector<unsigned char, SafeAllocator<unsigned char> >& input) {
 				return reinterpret_cast<const char*>(vecptr(input));
 			}
-
-		private:
-			boost::shared_ptr<UConverter> icuConverter;
 	};
 
 }
-- 
cgit v0.10.2-6-g49f6