2012-04-29 20:52:25 +02:00
|
|
|
//
|
|
|
|
// UnicodeConverter.cpp
|
|
|
|
//
|
|
|
|
// $Id: //poco/1.4/Foundation/src/UnicodeConverter.cpp#1 $
|
|
|
|
//
|
|
|
|
// Library: Foundation
|
|
|
|
// Package: Text
|
|
|
|
// Module: UnicodeConverter
|
|
|
|
//
|
|
|
|
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
|
|
|
|
// and Contributors.
|
|
|
|
//
|
2014-05-04 21:02:42 +02:00
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
2012-04-29 20:52:25 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#include "Poco/UnicodeConverter.h"
|
|
|
|
#include "Poco/TextConverter.h"
|
|
|
|
#include "Poco/TextIterator.h"
|
|
|
|
#include "Poco/UTF8Encoding.h"
|
|
|
|
#include "Poco/UTF16Encoding.h"
|
2012-07-31 20:37:04 +02:00
|
|
|
#include "Poco/UTF32Encoding.h"
|
2012-08-02 07:01:00 +02:00
|
|
|
#include <cstring>
|
2012-04-29 20:52:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
namespace Poco {
|
|
|
|
|
|
|
|
|
2012-08-02 06:41:24 +02:00
|
|
|
void UnicodeConverter::convert(const std::string& utf8String, UTF32String& utf32String)
|
2012-07-31 20:37:04 +02:00
|
|
|
{
|
|
|
|
utf32String.clear();
|
|
|
|
UTF8Encoding utf8Encoding;
|
|
|
|
TextIterator it(utf8String, utf8Encoding);
|
|
|
|
TextIterator end(utf8String);
|
|
|
|
|
|
|
|
while (it != end)
|
|
|
|
{
|
|
|
|
int cc = *it++;
|
2012-08-02 06:41:24 +02:00
|
|
|
utf32String += (UTF32Char) cc;
|
2012-07-31 20:37:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-02 06:41:24 +02:00
|
|
|
void UnicodeConverter::convert(const char* utf8String, std::size_t length, UTF32String& utf32String)
|
2012-07-31 20:37:04 +02:00
|
|
|
{
|
2012-08-02 06:41:24 +02:00
|
|
|
if (!utf8String || !length)
|
2012-07-31 20:37:04 +02:00
|
|
|
{
|
2012-08-02 06:41:24 +02:00
|
|
|
utf32String.clear();
|
|
|
|
return;
|
2012-07-31 20:37:04 +02:00
|
|
|
}
|
2012-08-02 06:41:24 +02:00
|
|
|
|
|
|
|
convert(std::string(utf8String, utf8String + length), utf32String);
|
2012-07-31 20:37:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-02 06:41:24 +02:00
|
|
|
void UnicodeConverter::convert(const char* utf8String, UTF32String& utf32String)
|
2012-07-31 20:37:04 +02:00
|
|
|
{
|
2014-05-01 05:58:28 +02:00
|
|
|
if (!utf8String || !std::strlen(utf8String))
|
2012-08-02 06:41:24 +02:00
|
|
|
{
|
|
|
|
utf32String.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
convert(utf8String, std::strlen(utf8String), utf32String);
|
2012-07-31 20:37:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-02 06:41:24 +02:00
|
|
|
void UnicodeConverter::convert(const std::string& utf8String, UTF16String& utf16String)
|
2012-04-29 20:52:25 +02:00
|
|
|
{
|
|
|
|
utf16String.clear();
|
|
|
|
UTF8Encoding utf8Encoding;
|
|
|
|
TextIterator it(utf8String, utf8Encoding);
|
|
|
|
TextIterator end(utf8String);
|
|
|
|
while (it != end)
|
|
|
|
{
|
|
|
|
int cc = *it++;
|
|
|
|
if (cc <= 0xffff)
|
|
|
|
{
|
2012-08-02 06:41:24 +02:00
|
|
|
utf16String += (UTF16Char) cc;
|
2012-04-29 20:52:25 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cc -= 0x10000;
|
2012-08-02 06:41:24 +02:00
|
|
|
utf16String += (UTF16Char) ((cc >> 10) & 0x3ff) | 0xd800;
|
|
|
|
utf16String += (UTF16Char) (cc & 0x3ff) | 0xdc00;
|
2012-04-29 20:52:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-02 06:41:24 +02:00
|
|
|
void UnicodeConverter::convert(const char* utf8String, std::size_t length, UTF16String& utf16String)
|
2012-04-29 20:52:25 +02:00
|
|
|
{
|
2012-08-02 06:41:24 +02:00
|
|
|
if (!utf8String || !length)
|
2012-04-29 20:52:25 +02:00
|
|
|
{
|
2012-08-02 06:41:24 +02:00
|
|
|
utf16String.clear();
|
|
|
|
return;
|
|
|
|
}
|
2012-04-29 20:52:25 +02:00
|
|
|
|
2012-08-02 06:41:24 +02:00
|
|
|
convert(std::string(utf8String, utf8String + length), utf16String);
|
|
|
|
}
|
2012-04-29 20:52:25 +02:00
|
|
|
|
|
|
|
|
2012-08-02 06:41:24 +02:00
|
|
|
void UnicodeConverter::convert(const char* utf8String, UTF16String& utf16String)
|
|
|
|
{
|
2014-05-01 05:58:28 +02:00
|
|
|
if (!utf8String || !std::strlen(utf8String))
|
2012-08-02 06:41:24 +02:00
|
|
|
{
|
|
|
|
utf16String.clear();
|
|
|
|
return;
|
2012-04-29 20:52:25 +02:00
|
|
|
}
|
2012-08-02 06:41:24 +02:00
|
|
|
|
|
|
|
convert(std::string(utf8String), utf16String);
|
2012-04-29 20:52:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-02 06:41:24 +02:00
|
|
|
void UnicodeConverter::convert(const UTF16String& utf16String, std::string& utf8String)
|
2012-04-29 20:52:25 +02:00
|
|
|
{
|
2012-08-02 06:41:24 +02:00
|
|
|
utf8String.clear();
|
|
|
|
UTF8Encoding utf8Encoding;
|
|
|
|
UTF16Encoding utf16Encoding;
|
|
|
|
TextConverter converter(utf16Encoding, utf8Encoding);
|
|
|
|
converter.convert(utf16String.data(), (int) utf16String.length() * sizeof(UTF16Char), utf8String);
|
2012-04-29 20:52:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-02 06:41:24 +02:00
|
|
|
void UnicodeConverter::convert(const UTF32String& utf32String, std::string& utf8String)
|
2012-04-29 20:52:25 +02:00
|
|
|
{
|
|
|
|
utf8String.clear();
|
|
|
|
UTF8Encoding utf8Encoding;
|
2012-08-02 06:41:24 +02:00
|
|
|
UTF32Encoding utf32Encoding;
|
|
|
|
TextConverter converter(utf32Encoding, utf8Encoding);
|
|
|
|
converter.convert(utf32String.data(), (int) utf32String.length() * sizeof(UTF32Char), utf8String);
|
2012-04-29 20:52:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-02 06:41:24 +02:00
|
|
|
void UnicodeConverter::convert(const UTF16Char* utf16String, std::size_t length, std::string& utf8String)
|
2012-04-29 20:52:25 +02:00
|
|
|
{
|
|
|
|
utf8String.clear();
|
|
|
|
UTF8Encoding utf8Encoding;
|
2012-08-02 06:41:24 +02:00
|
|
|
UTF16Encoding utf16Encoding;
|
|
|
|
TextConverter converter(utf16Encoding, utf8Encoding);
|
|
|
|
converter.convert(utf16String, (int) length * sizeof(UTF16Char), utf8String);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UnicodeConverter::convert(const UTF32Char* utf32String, std::size_t length, std::string& utf8String)
|
|
|
|
{
|
|
|
|
toUTF8(UTF32String(utf32String, length), utf8String);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UnicodeConverter::convert(const UTF16Char* utf16String, std::string& utf8String)
|
|
|
|
{
|
|
|
|
toUTF8(utf16String, UTFStrlen(utf16String), utf8String);
|
2012-04-29 20:52:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-02 06:41:24 +02:00
|
|
|
void UnicodeConverter::convert(const UTF32Char* utf32String, std::string& utf8String)
|
2012-04-29 20:52:25 +02:00
|
|
|
{
|
2012-08-02 06:41:24 +02:00
|
|
|
toUTF8(utf32String, UTFStrlen(utf32String), utf8String);
|
2012-04-29 20:52:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace Poco
|