mirror of
https://github.com/pocoproject/poco.git
synced 2025-01-18 00:15:27 +01:00
Unicode portability improvements (only tested on Windows at this time)
This commit is contained in:
parent
f8bfe8cb0a
commit
416cc7a630
@ -23,6 +23,7 @@ Release 1.5.0 (2012-08-??)
|
||||
- IPAddress force IPv6 always lowercase (RFC 5952)
|
||||
- fixed SF#3538785: SMTPClientSession::sendMessage() should take recipient list
|
||||
- added IPAddress::prefixLength()
|
||||
- UTF portability improvements
|
||||
|
||||
Release 1.4.4 (2012-08-??)
|
||||
==========================
|
||||
|
@ -1024,6 +1024,7 @@
|
||||
<ClInclude Include="include\Poco\Types.h" />
|
||||
<ClInclude Include="include\Poco\UnWindows.h" />
|
||||
<ClInclude Include="include\Poco\UTF32Encoding.h" />
|
||||
<ClInclude Include="include\Poco\UTFString.h" />
|
||||
<ClInclude Include="include\Poco\Version.h" />
|
||||
<ClInclude Include="include\Poco\Void.h" />
|
||||
<ClInclude Include="include\Poco\Base64Decoder.h" />
|
||||
|
@ -1814,6 +1814,9 @@
|
||||
<ClInclude Include="include\Poco\UTF32Encoding.h">
|
||||
<Filter>Text\Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="include\Poco\UTFString.h">
|
||||
<Filter>Text\Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ResourceCompile Include="src\pocomsg.rc">
|
||||
|
88
Foundation/include/Poco/UTFString.h
Normal file
88
Foundation/include/Poco/UTFString.h
Normal file
@ -0,0 +1,88 @@
|
||||
//
|
||||
// Types.h
|
||||
//
|
||||
// $Id: //poco/1.4/Foundation/include/Poco/UTFString.h#2 $
|
||||
//
|
||||
// Library: Foundation
|
||||
// Package: Text
|
||||
// Module: UTFString
|
||||
//
|
||||
// Definitions of strings for UTF encodings.
|
||||
//
|
||||
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#ifndef Foundation_UTFString_INCLUDED
|
||||
#define Foundation_UTFString_INCLUDED
|
||||
|
||||
|
||||
#include "Poco/Foundation.h"
|
||||
#include <string>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
|
||||
|
||||
// UTF string types
|
||||
#ifndef POCO_NO_WSTRING
|
||||
//#if defined(POCO_ENABLE_CPP11) //TODO
|
||||
// typedef char16_t UTF16Char;
|
||||
// typedef std::u16string UTF16String;
|
||||
// typedef char32_t UTF32Char;
|
||||
// typedef std::u32string UTF32String;
|
||||
//#else
|
||||
#if defined(POCO_OS_FAMILY_WINDOWS)
|
||||
typedef wchar_t UTF16Char;
|
||||
typedef std::wstring UTF16String;
|
||||
typedef UInt32 UTF32Char;
|
||||
typedef std::basic_string<UInt32> UTF32String;
|
||||
#elif defined(__SIZEOF_WCHAR_T__) //gcc
|
||||
#if (__SIZEOF_WCHAR_T__ == 2)
|
||||
typedef wchar_t UTF16Char;
|
||||
typedef std::wstring UTF16String;
|
||||
typedef UInt32 UTF32Char;
|
||||
typedef std::basic_string<UInt32> UTF32String;
|
||||
#elif (__SIZEOF_WCHAR_T__ == 4)
|
||||
typedef Poco::Uint16 UTF16Char;
|
||||
typedef std::basic_string<UInt16> UTF16String;
|
||||
typedef UInt32 UTF32Char;
|
||||
typedef std::wstring UTF32String;
|
||||
#endif
|
||||
#else // default to 32-bit wchar_t
|
||||
typedef Poco::Uint16 UTF16Char;
|
||||
typedef std::basic_string<UInt16> UTF16String;
|
||||
typedef UInt32 UTF32Char;
|
||||
typedef std::wstring UTF32String;
|
||||
#endif //POCO_OS_FAMILY_WINDOWS
|
||||
//#endif // POCO_ENABLE_CPP11
|
||||
#endif POCO_NO_WSTRING
|
||||
|
||||
|
||||
} // namespace Poco
|
||||
|
||||
|
||||
#endif // Foundation_UTFString_INCLUDED
|
@ -41,6 +41,7 @@
|
||||
|
||||
|
||||
#include "Poco/Foundation.h"
|
||||
#include "Poco/UTFString.h"
|
||||
|
||||
|
||||
namespace Poco {
|
||||
@ -55,41 +56,87 @@ class Foundation_API UnicodeConverter
|
||||
/// and probably won't be of much use anywhere else ???
|
||||
{
|
||||
public:
|
||||
static void toWideUTF(const std::string& utf8String, std::wstring& utf32String);
|
||||
/// Converts the given UTF-8 encoded string into a native encoded wstring.
|
||||
static void convert(const std::string& utf8String, UTF32String& utf32String);
|
||||
/// Converts the given UTF-8 encoded string into an UTF-32 encoded wide string.
|
||||
|
||||
static void toWideUTF(const char* utf8String, int length, std::wstring& utf32String);
|
||||
/// Converts the given UTF-8 encoded character sequence into a native encoded string.
|
||||
static void convert(const char* utf8String, std::size_t length, UTF32String& utf32String);
|
||||
/// Converts the given UTF-8 encoded character sequence into an UTF-32 encoded wide string.
|
||||
|
||||
static void toWideUTF(const char* utf8String, std::wstring& utf32String);
|
||||
/// Converts the given zero-terminated UTF-8 encoded character sequence into a native encoded wstring.
|
||||
static void convert(const char* utf8String, UTF32String& utf32String);
|
||||
/// Converts the given zero-terminated UTF-8 encoded character sequence into an UTF-32 encoded wide string.
|
||||
|
||||
static void toUTF32(const std::string& utf8String, std::wstring& utf32String);
|
||||
/// Converts the given UTF-8 encoded string into an UTF-32 encoded wstring.
|
||||
static void convert(const std::string& utf8String, UTF16String& utf16String);
|
||||
/// Converts the given UTF-8 encoded string into an UTF-16 encoded wide string.
|
||||
|
||||
static void toUTF32(const char* utf8String, int length, std::wstring& utf32String);
|
||||
/// Converts the given UTF-8 encoded character sequence into an UTF-32 encoded string.
|
||||
static void convert(const char* utf8String, std::size_t length, UTF16String& utf16String);
|
||||
/// Converts the given UTF-8 encoded character sequence into an UTF-16 encoded wide string.
|
||||
|
||||
static void toUTF32(const char* utf8String, std::wstring& utf32String);
|
||||
/// Converts the given zero-terminated UTF-8 encoded character sequence into an UTF-32 encoded wstring.
|
||||
static void convert(const char* utf8String, UTF16String& utf16String);
|
||||
/// Converts the given zero-terminated UTF-8 encoded character sequence into an UTF-16 encoded wide string.
|
||||
|
||||
static void toUTF16(const std::string& utf8String, std::wstring& utf16String);
|
||||
/// Converts the given UTF-8 encoded string into an UTF-16 encoded wstring.
|
||||
static void convert(const UTF16String& utf16String, std::string& utf8String);
|
||||
/// Converts the given UTF-16 encoded wide string into an UTF-8 encoded string.
|
||||
|
||||
static void toUTF16(const char* utf8String, int length, std::wstring& utf16String);
|
||||
/// Converts the given UTF-8 encoded character sequence into an UTF-16 encoded string.
|
||||
static void convert(const UTF32String& utf32String, std::string& utf8String);
|
||||
/// Converts the given UTF-32 encoded wide string into an UTF-8 encoded string.
|
||||
|
||||
static void toUTF16(const char* utf8String, std::wstring& utf16String);
|
||||
/// Converts the given zero-terminated UTF-8 encoded character sequence into an UTF-16 encoded wstring.
|
||||
static void convert(const UTF16Char* utf16String, std::size_t length, std::string& utf8String);
|
||||
/// Converts the given zero-terminated UTF-16 encoded wide character sequence into an UTF-8 encoded string.
|
||||
|
||||
static void toUTF8(const std::wstring& utf16String, std::string& utf8String);
|
||||
/// Converts the given UTF-16 encoded wstring into an UTF-8 encoded string.
|
||||
static void convert(const UTF32Char* utf16String, std::size_t length, std::string& utf8String);
|
||||
/// Converts the given zero-terminated UTF-32 encoded wide character sequence into an UTF-8 encoded string.
|
||||
|
||||
static void toUTF8(const wchar_t* utf16String, int length, std::string& utf8String);
|
||||
/// Converts the given zero-terminated UTF-16 encoded wide character sequence into an UTF-8 encoded wstring.
|
||||
|
||||
static void toUTF8(const wchar_t* utf16String, std::string& utf8String);
|
||||
static void convert(const UTF16Char* utf16String, std::string& utf8String);
|
||||
/// Converts the given UTF-16 encoded zero terminated character sequence into an UTF-8 encoded string.
|
||||
|
||||
static void convert(const UTF32Char* utf32String, std::string& utf8String);
|
||||
/// Converts the given UTF-32 encoded zero terminated character sequence into an UTF-8 encoded string.
|
||||
|
||||
template <typename F, typename T>
|
||||
static void toUTF32(const F& f, T& t)
|
||||
{
|
||||
convert(f, t);
|
||||
}
|
||||
|
||||
template <typename F, typename T>
|
||||
static void toUTF32(const F& f, std::size_t l, T& t)
|
||||
{
|
||||
convert(f, l, t);
|
||||
}
|
||||
|
||||
template <typename F, typename T>
|
||||
static void toUTF16(const F& f, T& t)
|
||||
{
|
||||
convert(f, t);
|
||||
}
|
||||
|
||||
template <typename F, typename T>
|
||||
static void toUTF16(const F& f, std::size_t l, T& t)
|
||||
{
|
||||
convert(f, l, t);
|
||||
}
|
||||
|
||||
template <typename F, typename T>
|
||||
static void toUTF8(const F& f, T& t)
|
||||
{
|
||||
convert(f, t);
|
||||
}
|
||||
|
||||
template <typename F, typename T>
|
||||
static void toUTF8(const F& f, std::size_t l, T& t)
|
||||
{
|
||||
convert(f, l, t);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static size_t UTFStrlen(const T* ptr)
|
||||
/// Returns the length (in characters) of a zero-terminated UTF string.
|
||||
{
|
||||
if (ptr == 0) return 0;
|
||||
const T* p;
|
||||
for (p = ptr; *p; ++p);
|
||||
return p - ptr;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
@ -43,53 +43,12 @@
|
||||
#include "Poco/UTF8Encoding.h"
|
||||
#include "Poco/UTF16Encoding.h"
|
||||
#include "Poco/UTF32Encoding.h"
|
||||
#include <cstring>
|
||||
#include <wchar.h>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
|
||||
|
||||
void UnicodeConverter::toWideUTF(const std::string& utf8String, std::wstring& utfWideString)
|
||||
{
|
||||
if (sizeof(wchar_t) == 2)
|
||||
{
|
||||
toUTF16(utf8String, utfWideString);
|
||||
}
|
||||
else
|
||||
{
|
||||
toUTF32(utf8String, utfWideString);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void UnicodeConverter::toWideUTF(const char* utf8String, int length, std::wstring& utfWideString)
|
||||
{
|
||||
if (sizeof(wchar_t) == 2)
|
||||
{
|
||||
toUTF16(utf8String, utfWideString);
|
||||
}
|
||||
else
|
||||
{
|
||||
toUTF32(utf8String, utfWideString);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void UnicodeConverter::toWideUTF(const char* utf8String, std::wstring& utfWideString)
|
||||
{
|
||||
if (sizeof(wchar_t) == 2)
|
||||
{
|
||||
toUTF16(utf8String, utfWideString);
|
||||
}
|
||||
else
|
||||
{
|
||||
toUTF32(utf8String, utfWideString);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void UnicodeConverter::toUTF32(const std::string& utf8String, std::wstring& utf32String)
|
||||
void UnicodeConverter::convert(const std::string& utf8String, UTF32String& utf32String)
|
||||
{
|
||||
utf32String.clear();
|
||||
UTF8Encoding utf8Encoding;
|
||||
@ -99,64 +58,36 @@ void UnicodeConverter::toUTF32(const std::string& utf8String, std::wstring& utf3
|
||||
while (it != end)
|
||||
{
|
||||
int cc = *it++;
|
||||
utf32String += (wchar_t) cc;
|
||||
utf32String += (UTF32Char) cc;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void UnicodeConverter::toUTF32(const char* utf8String, int length, std::wstring& utf32String)
|
||||
void UnicodeConverter::convert(const char* utf8String, std::size_t length, UTF32String& utf32String)
|
||||
{
|
||||
poco_check_ptr (utf8String);
|
||||
|
||||
utf32String.clear();
|
||||
|
||||
UTF8Encoding utf8Encoding;
|
||||
UTF32Encoding utf32Encoding;
|
||||
const unsigned char* it = (const unsigned char*) utf8String;
|
||||
const unsigned char* end = (const unsigned char*) utf8String + length;
|
||||
|
||||
while (it < end)
|
||||
if (!utf8String || !length)
|
||||
{
|
||||
int n = utf8Encoding.queryConvert(it, 1);
|
||||
int uc;
|
||||
int read = 1;
|
||||
|
||||
while (-1 > n && (end - it) >= -n)
|
||||
{
|
||||
read = -n;
|
||||
n = utf8Encoding.queryConvert(it, read);
|
||||
}
|
||||
|
||||
if (-1 > n)
|
||||
{
|
||||
it = end;
|
||||
}
|
||||
else
|
||||
{
|
||||
it += read;
|
||||
}
|
||||
|
||||
if (-1 >= n)
|
||||
{
|
||||
uc = 0xfffd; // Replacement Character (instead of '?')
|
||||
}
|
||||
else
|
||||
{
|
||||
uc = n;
|
||||
}
|
||||
|
||||
utf32String += (wchar_t) uc;
|
||||
utf32String.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
convert(std::string(utf8String, utf8String + length), utf32String);
|
||||
}
|
||||
|
||||
|
||||
void UnicodeConverter::toUTF32(const char* utf8String, std::wstring& utf32String)
|
||||
void UnicodeConverter::convert(const char* utf8String, UTF32String& utf32String)
|
||||
{
|
||||
toUTF32(utf8String, (int) std::strlen(utf8String), utf32String);
|
||||
if (!utf8String || !strlen(utf8String))
|
||||
{
|
||||
utf32String.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
convert(utf8String, std::strlen(utf8String), utf32String);
|
||||
}
|
||||
|
||||
|
||||
void UnicodeConverter::toUTF16(const std::string& utf8String, std::wstring& utf16String)
|
||||
void UnicodeConverter::convert(const std::string& utf8String, UTF16String& utf16String)
|
||||
{
|
||||
utf16String.clear();
|
||||
UTF8Encoding utf8Encoding;
|
||||
@ -167,120 +98,87 @@ void UnicodeConverter::toUTF16(const std::string& utf8String, std::wstring& utf1
|
||||
int cc = *it++;
|
||||
if (cc <= 0xffff)
|
||||
{
|
||||
utf16String += (wchar_t) cc;
|
||||
utf16String += (UTF16Char) cc;
|
||||
}
|
||||
else
|
||||
{
|
||||
cc -= 0x10000;
|
||||
utf16String += (wchar_t) ((cc >> 10) & 0x3ff) | 0xd800;
|
||||
utf16String += (wchar_t) (cc & 0x3ff) | 0xdc00;
|
||||
utf16String += (UTF16Char) ((cc >> 10) & 0x3ff) | 0xd800;
|
||||
utf16String += (UTF16Char) (cc & 0x3ff) | 0xdc00;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void UnicodeConverter::toUTF16(const char* utf8String, int length, std::wstring& utf16String)
|
||||
void UnicodeConverter::convert(const char* utf8String, std::size_t length, UTF16String& utf16String)
|
||||
{
|
||||
poco_check_ptr (utf8String);
|
||||
if (!utf8String || !length)
|
||||
{
|
||||
utf16String.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
utf16String.clear();
|
||||
convert(std::string(utf8String, utf8String + length), utf16String);
|
||||
}
|
||||
|
||||
|
||||
void UnicodeConverter::convert(const char* utf8String, UTF16String& utf16String)
|
||||
{
|
||||
if (!utf8String || !strlen(utf8String))
|
||||
{
|
||||
utf16String.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
convert(std::string(utf8String), utf16String);
|
||||
}
|
||||
|
||||
|
||||
void UnicodeConverter::convert(const UTF16String& utf16String, std::string& utf8String)
|
||||
{
|
||||
utf8String.clear();
|
||||
UTF8Encoding utf8Encoding;
|
||||
UTF16Encoding utf16Encoding;
|
||||
const unsigned char* it = (const unsigned char*) utf8String;
|
||||
const unsigned char* end = (const unsigned char*) utf8String + length;
|
||||
|
||||
while (it < end)
|
||||
{
|
||||
int n = utf8Encoding.queryConvert(it, 1);
|
||||
int uc;
|
||||
int read = 1;
|
||||
|
||||
while (-1 > n && (end - it) >= -n)
|
||||
{
|
||||
read = -n;
|
||||
n = utf8Encoding.queryConvert(it, read);
|
||||
}
|
||||
|
||||
if (-1 > n)
|
||||
{
|
||||
it = end;
|
||||
}
|
||||
else
|
||||
{
|
||||
it += read;
|
||||
}
|
||||
|
||||
if (-1 >= n)
|
||||
{
|
||||
uc = 0xfffd; // Replacement Character (instead of '?')
|
||||
}
|
||||
else
|
||||
{
|
||||
uc = n;
|
||||
}
|
||||
|
||||
if (uc > 0xffff)
|
||||
{
|
||||
uc -= 0x10000;
|
||||
utf16String += (wchar_t) ((uc >> 10) & 0x3ff) | 0xd800 ;
|
||||
utf16String += (wchar_t) (uc & 0x3ff) | 0xdc00 ;
|
||||
}
|
||||
else
|
||||
{
|
||||
utf16String += (wchar_t) uc;
|
||||
}
|
||||
}
|
||||
TextConverter converter(utf16Encoding, utf8Encoding);
|
||||
converter.convert(utf16String.data(), (int) utf16String.length() * sizeof(UTF16Char), utf8String);
|
||||
}
|
||||
|
||||
|
||||
void UnicodeConverter::toUTF16(const char* utf8String, std::wstring& utf16String)
|
||||
{
|
||||
toUTF16(utf8String, (int) std::strlen(utf8String), utf16String);
|
||||
}
|
||||
|
||||
|
||||
void UnicodeConverter::toUTF8(const std::wstring& utf16String, std::string& utf8String)
|
||||
void UnicodeConverter::convert(const UTF32String& utf32String, std::string& utf8String)
|
||||
{
|
||||
utf8String.clear();
|
||||
UTF8Encoding utf8Encoding;
|
||||
if (sizeof(wchar_t) == 2)
|
||||
{
|
||||
UTF16Encoding utf16Encoding;
|
||||
TextConverter converter(utf16Encoding, utf8Encoding);
|
||||
converter.convert(utf16String.data(), (int) utf16String.length() * sizeof(wchar_t), utf8String);
|
||||
}
|
||||
else
|
||||
{
|
||||
UTF32Encoding utf32Encoding;
|
||||
TextConverter converter(utf32Encoding, utf8Encoding);
|
||||
converter.convert(utf16String.data(), (int) utf16String.length() * sizeof(wchar_t), utf8String);
|
||||
}
|
||||
UTF32Encoding utf32Encoding;
|
||||
TextConverter converter(utf32Encoding, utf8Encoding);
|
||||
converter.convert(utf32String.data(), (int) utf32String.length() * sizeof(UTF32Char), utf8String);
|
||||
}
|
||||
|
||||
|
||||
void UnicodeConverter::toUTF8(const wchar_t* utf16String, int length, std::string& utf8String)
|
||||
void UnicodeConverter::convert(const UTF16Char* utf16String, std::size_t length, std::string& utf8String)
|
||||
{
|
||||
utf8String.clear();
|
||||
UTF8Encoding utf8Encoding;
|
||||
if (sizeof(wchar_t) == 2)
|
||||
{
|
||||
UTF16Encoding utf16Encoding;
|
||||
TextConverter converter(utf16Encoding, utf8Encoding);
|
||||
converter.convert(utf16String, (int) length * sizeof(wchar_t), utf8String);
|
||||
}
|
||||
else
|
||||
{
|
||||
UTF32Encoding utf32Encoding;
|
||||
TextConverter converter(utf32Encoding, utf8Encoding);
|
||||
converter.convert(utf16String, (int) length * sizeof(wchar_t), utf8String);
|
||||
}
|
||||
UTF16Encoding utf16Encoding;
|
||||
TextConverter converter(utf16Encoding, utf8Encoding);
|
||||
converter.convert(utf16String, (int) length * sizeof(UTF16Char), utf8String);
|
||||
}
|
||||
|
||||
|
||||
void UnicodeConverter::toUTF8(const wchar_t* utf16String, std::string& utf8String)
|
||||
void UnicodeConverter::convert(const UTF32Char* utf32String, std::size_t length, std::string& utf8String)
|
||||
{
|
||||
toUTF8(utf16String, (int) wcslen(utf16String), utf8String);
|
||||
toUTF8(UTF32String(utf32String, length), utf8String);
|
||||
}
|
||||
|
||||
|
||||
void UnicodeConverter::convert(const UTF16Char* utf16String, std::string& utf8String)
|
||||
{
|
||||
toUTF8(utf16String, UTFStrlen(utf16String), utf8String);
|
||||
}
|
||||
|
||||
|
||||
void UnicodeConverter::convert(const UTF32Char* utf32String, std::string& utf8String)
|
||||
{
|
||||
toUTF8(utf32String, UTFStrlen(utf32String), utf8String);
|
||||
}
|
||||
|
||||
|
||||
|
@ -34,8 +34,14 @@
|
||||
#include "CppUnit/TestCaller.h"
|
||||
#include "CppUnit/TestSuite.h"
|
||||
#include "Poco/UnicodeConverter.h"
|
||||
#include "Poco/UTFString.h"
|
||||
|
||||
|
||||
using Poco::UnicodeConverter;
|
||||
using Poco::UTF16Char;
|
||||
using Poco::UTF16String;
|
||||
using Poco::UTF32Char;
|
||||
using Poco::UTF32String;
|
||||
|
||||
|
||||
UnicodeConverterTest::UnicodeConverterTest(const std::string& name): CppUnit::TestCase(name)
|
||||
@ -48,141 +54,18 @@ UnicodeConverterTest::~UnicodeConverterTest()
|
||||
}
|
||||
|
||||
|
||||
#ifdef _WINDOWS
|
||||
|
||||
void UnicodeConverterTest::testString16()
|
||||
void UnicodeConverterTest::testUTF16()
|
||||
{
|
||||
const unsigned char supp[] = {0x41, 0x42, 0xf0, 0x90, 0x82, 0xa4, 0xf0, 0xaf, 0xa6, 0xa0, 0xf0, 0xaf, 0xa8, 0x9d, 0x00};
|
||||
std::string text((const char*) supp);
|
||||
|
||||
std::wstring wtext;
|
||||
|
||||
UnicodeConverter::toUTF16 (text, wtext);
|
||||
|
||||
std::string text2;
|
||||
|
||||
UnicodeConverter::toUTF8 (wtext, text2);
|
||||
|
||||
assert (text == text2);
|
||||
|
||||
runTests<UTF16String>();
|
||||
}
|
||||
|
||||
void UnicodeConverterTest::testCharPtrLength16()
|
||||
|
||||
void UnicodeConverterTest::testUTF32()
|
||||
{
|
||||
const unsigned char supp[] = {0x41, 0x42, 0xf0, 0x90, 0x82, 0xa4, 0xf0, 0xaf, 0xa6, 0xa0, 0xf0, 0xaf, 0xa8, 0x9d, 0x00};
|
||||
std::string text((const char*) supp);
|
||||
|
||||
std::wstring wtext;
|
||||
std::string text2;
|
||||
|
||||
UnicodeConverter::toUTF16 ((const char*)supp, 14, wtext);
|
||||
UnicodeConverter::toUTF8 (wtext.c_str (), (int) wtext.size (), text2);
|
||||
|
||||
assert (text == text2);
|
||||
runTests<UTF32String>();
|
||||
}
|
||||
|
||||
void UnicodeConverterTest::testCharPtr16()
|
||||
{
|
||||
const unsigned char supp[] = {0x41, 0x42, 0xf0, 0x90, 0x82, 0xa4, 0xf0, 0xaf, 0xa6, 0xa0, 0xf0, 0xaf, 0xa8, 0x9d, 0x00};
|
||||
std::string text((const char*) supp);
|
||||
|
||||
std::wstring wtext;
|
||||
std::string text2;
|
||||
|
||||
UnicodeConverter::toUTF16 ((const char*)supp, wtext);
|
||||
UnicodeConverter::toUTF8 (wtext.c_str (), text2);
|
||||
|
||||
assert (text == text2);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
void UnicodeConverterTest::testString32()
|
||||
{
|
||||
const unsigned char utf8Chars[] = "ľščťžýáíéúäô ĽŠČŤŽÝÁÍÉÚÄÔ";
|
||||
std::string text((const char*) utf8Chars);
|
||||
|
||||
std::wstring wtext;
|
||||
|
||||
UnicodeConverter::toUTF32 (text, wtext);
|
||||
|
||||
std::string text2;
|
||||
|
||||
UnicodeConverter::toUTF8 (wtext, text2);
|
||||
assert (text == text2);
|
||||
}
|
||||
|
||||
void UnicodeConverterTest::testCharPtrLength32()
|
||||
{
|
||||
const unsigned char supp[] = {0x41, 0x42, 0xf0, 0x90, 0x82, 0xa4, 0xf0, 0xaf, 0xa6, 0xa0, 0xf0, 0xaf, 0xa8, 0x9d, 0x00};
|
||||
std::string text((const char*) supp);
|
||||
|
||||
std::wstring wtext;
|
||||
std::string text2;
|
||||
|
||||
UnicodeConverter::toUTF32 ((const char*)supp, 14, wtext);
|
||||
UnicodeConverter::toUTF8 (wtext.c_str (), (int) wtext.size (), text2);
|
||||
|
||||
assert (text == text2);
|
||||
}
|
||||
|
||||
void UnicodeConverterTest::testCharPtr32()
|
||||
{
|
||||
const unsigned char supp[] = {0x41, 0x42, 0xf0, 0x90, 0x82, 0xa4, 0xf0, 0xaf, 0xa6, 0xa0, 0xf0, 0xaf, 0xa8, 0x9d, 0x00};
|
||||
std::string text((const char*) supp);
|
||||
|
||||
std::wstring wtext;
|
||||
std::string text2;
|
||||
|
||||
UnicodeConverter::toUTF32 ((const char*)supp, wtext);
|
||||
UnicodeConverter::toUTF8 (wtext.c_str (), text2);
|
||||
|
||||
assert (text == text2);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void UnicodeConverterTest::testString()
|
||||
{
|
||||
const unsigned char utf8Chars[] = "ľščťžýáíéúäô ĽŠČŤŽÝÁÍÉÚÄÔ";
|
||||
std::string text((const char*) utf8Chars);
|
||||
|
||||
std::wstring wtext;
|
||||
|
||||
UnicodeConverter::toWideUTF (text, wtext);
|
||||
|
||||
std::string text2;
|
||||
|
||||
UnicodeConverter::toUTF8 (wtext, text2);
|
||||
assert (text == text2);
|
||||
}
|
||||
|
||||
void UnicodeConverterTest::testCharPtrLength()
|
||||
{
|
||||
const unsigned char supp[] = {0x41, 0x42, 0xf0, 0x90, 0x82, 0xa4, 0xf0, 0xaf, 0xa6, 0xa0, 0xf0, 0xaf, 0xa8, 0x9d, 0x00};
|
||||
std::string text((const char*) supp);
|
||||
|
||||
std::wstring wtext;
|
||||
std::string text2;
|
||||
|
||||
UnicodeConverter::toWideUTF ((const char*)supp, 14, wtext);
|
||||
UnicodeConverter::toUTF8 (wtext.c_str (), (int) wtext.size (), text2);
|
||||
|
||||
assert (text == text2);
|
||||
}
|
||||
|
||||
void UnicodeConverterTest::testCharPtr()
|
||||
{
|
||||
const unsigned char supp[] = {0x41, 0x42, 0xf0, 0x90, 0x82, 0xa4, 0xf0, 0xaf, 0xa6, 0xa0, 0xf0, 0xaf, 0xa8, 0x9d, 0x00};
|
||||
std::string text((const char*) supp);
|
||||
|
||||
std::wstring wtext;
|
||||
std::string text2;
|
||||
|
||||
UnicodeConverter::toWideUTF ((const char*)supp, wtext);
|
||||
UnicodeConverter::toUTF8 (wtext.c_str (), text2);
|
||||
|
||||
assert (text == text2);
|
||||
}
|
||||
|
||||
void UnicodeConverterTest::setUp()
|
||||
{
|
||||
@ -198,18 +81,8 @@ CppUnit::Test* UnicodeConverterTest::suite()
|
||||
{
|
||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("UnicodeConverterTest");
|
||||
|
||||
#ifdef _WINDOWS
|
||||
CppUnit_addTest(pSuite, UnicodeConverterTest, testString16);
|
||||
CppUnit_addTest(pSuite, UnicodeConverterTest, testCharPtrLength16);
|
||||
CppUnit_addTest(pSuite, UnicodeConverterTest, testCharPtr16);
|
||||
#else
|
||||
CppUnit_addTest(pSuite, UnicodeConverterTest, testString32);
|
||||
CppUnit_addTest(pSuite, UnicodeConverterTest, testCharPtrLength32);
|
||||
CppUnit_addTest(pSuite, UnicodeConverterTest, testCharPtr32);
|
||||
#endif
|
||||
CppUnit_addTest(pSuite, UnicodeConverterTest, testString);
|
||||
CppUnit_addTest(pSuite, UnicodeConverterTest, testCharPtrLength);
|
||||
CppUnit_addTest(pSuite, UnicodeConverterTest, testCharPtr);
|
||||
CppUnit_addTest(pSuite, UnicodeConverterTest, testUTF16);
|
||||
CppUnit_addTest(pSuite, UnicodeConverterTest, testUTF32);
|
||||
|
||||
return pSuite;
|
||||
}
|
||||
|
@ -46,18 +46,8 @@ public:
|
||||
UnicodeConverterTest(const std::string& name);
|
||||
~UnicodeConverterTest();
|
||||
|
||||
#ifdef _WINDOWS
|
||||
void testString16();
|
||||
void testCharPtrLength16();
|
||||
void testCharPtr16();
|
||||
#else
|
||||
void testString32();
|
||||
void testCharPtrLength32();
|
||||
void testCharPtr32();
|
||||
#endif
|
||||
void testString();
|
||||
void testCharPtrLength();
|
||||
void testCharPtr();
|
||||
void testUTF16();
|
||||
void testUTF32();
|
||||
|
||||
void setUp();
|
||||
void tearDown();
|
||||
@ -65,6 +55,33 @@ public:
|
||||
static CppUnit::Test* suite();
|
||||
|
||||
private:
|
||||
template <typename T>
|
||||
void runTests()
|
||||
{
|
||||
const unsigned char supp[] = {0x41, 0x42, 0xf0, 0x90, 0x82, 0xa4, 0xf0, 0xaf, 0xa6, 0xa0, 0xf0, 0xaf, 0xa8, 0x9d, 0x00};
|
||||
std::string text((const char*) supp);
|
||||
|
||||
// Convert from UTF-8 to wide
|
||||
T wtext, wtext2, wtext3;
|
||||
UnicodeConverter::convert(text, wtext);
|
||||
UnicodeConverter::convert((const char*) supp, strlen((const char*) supp), wtext2);
|
||||
UnicodeConverter::convert((const char*) supp, wtext3);
|
||||
|
||||
std::string text2, text3, text4;
|
||||
|
||||
assert (text != text2);
|
||||
assert (text != text3);
|
||||
assert (text != text4);
|
||||
|
||||
// Convert from wide to UTF-8
|
||||
UnicodeConverter::convert(wtext, text2);
|
||||
UnicodeConverter::convert(wtext2, text3);
|
||||
UnicodeConverter::convert(wtext3, text4);
|
||||
|
||||
assert (text == text2);
|
||||
assert (text == text3);
|
||||
assert (text == text4);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user