diff options
author | Remko Tronçon <git@el-tramo.be> | 2012-06-05 10:16:50 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2012-06-05 10:16:50 (GMT) |
commit | f727641c659b0c372fb0372b588b59c7dd65e61a (patch) | |
tree | 6ac488d24b650852003a155f1387065bd9eda6ea | |
parent | e43999d275ff27970ba973edd2be68fb6b998aaf (diff) | |
download | swift-f727641c659b0c372fb0372b588b59c7dd65e61a.zip swift-f727641c659b0c372fb0372b588b59c7dd65e61a.tar.bz2 |
Use faster conversion functions in ICU.
-rw-r--r-- | Swiften/IDN/ICUConverter.h | 30 |
1 files changed, 13 insertions, 17 deletions
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; }; } |