| 1 | // | 
|---|
| 2 | // UnicodeConverter.cpp | 
|---|
| 3 | // | 
|---|
| 4 | // Library: Foundation | 
|---|
| 5 | // Package: Text | 
|---|
| 6 | // Module:  UnicodeConverter | 
|---|
| 7 | // | 
|---|
| 8 | // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. | 
|---|
| 9 | // and Contributors. | 
|---|
| 10 | // | 
|---|
| 11 | // SPDX-License-Identifier:	BSL-1.0 | 
|---|
| 12 | // | 
|---|
| 13 |  | 
|---|
| 14 |  | 
|---|
| 15 | #include "Poco/UnicodeConverter.h" | 
|---|
| 16 | #include "Poco/TextConverter.h" | 
|---|
| 17 | #include "Poco/TextIterator.h" | 
|---|
| 18 | #include "Poco/UTF8Encoding.h" | 
|---|
| 19 | #include "Poco/UTF16Encoding.h" | 
|---|
| 20 | #include "Poco/UTF32Encoding.h" | 
|---|
| 21 | #include <cstring> | 
|---|
| 22 |  | 
|---|
| 23 |  | 
|---|
| 24 | #if !defined(POCO_NO_WSTRING) | 
|---|
| 25 |  | 
|---|
| 26 |  | 
|---|
| 27 | namespace Poco { | 
|---|
| 28 |  | 
|---|
| 29 |  | 
|---|
| 30 | void UnicodeConverter::convert(const std::string& utf8String, UTF32String& utf32String) | 
|---|
| 31 | { | 
|---|
| 32 | utf32String.clear(); | 
|---|
| 33 | UTF8Encoding utf8Encoding; | 
|---|
| 34 | TextIterator it(utf8String, utf8Encoding); | 
|---|
| 35 | TextIterator end(utf8String); | 
|---|
| 36 |  | 
|---|
| 37 | while (it != end) | 
|---|
| 38 | { | 
|---|
| 39 | int cc = *it++; | 
|---|
| 40 | utf32String += (UTF32Char) cc; | 
|---|
| 41 | } | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 |  | 
|---|
| 45 | void UnicodeConverter::convert(const char* utf8String, std::size_t length, UTF32String& utf32String) | 
|---|
| 46 | { | 
|---|
| 47 | if (!utf8String || !length) | 
|---|
| 48 | { | 
|---|
| 49 | utf32String.clear(); | 
|---|
| 50 | return; | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | convert(std::string(utf8String, utf8String + length), utf32String); | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 |  | 
|---|
| 57 | void UnicodeConverter::convert(const char* utf8String, UTF32String& utf32String) | 
|---|
| 58 | { | 
|---|
| 59 | if (!utf8String || !std::strlen(utf8String)) | 
|---|
| 60 | { | 
|---|
| 61 | utf32String.clear(); | 
|---|
| 62 | return; | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | convert(utf8String, std::strlen(utf8String), utf32String); | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 |  | 
|---|
| 69 | void UnicodeConverter::convert(const std::string& utf8String, UTF16String& utf16String) | 
|---|
| 70 | { | 
|---|
| 71 | utf16String.clear(); | 
|---|
| 72 | UTF8Encoding utf8Encoding; | 
|---|
| 73 | TextIterator it(utf8String, utf8Encoding); | 
|---|
| 74 | TextIterator end(utf8String); | 
|---|
| 75 | while (it != end) | 
|---|
| 76 | { | 
|---|
| 77 | int cc = *it++; | 
|---|
| 78 | if (cc <= 0xffff) | 
|---|
| 79 | { | 
|---|
| 80 | utf16String += (UTF16Char) cc; | 
|---|
| 81 | } | 
|---|
| 82 | else | 
|---|
| 83 | { | 
|---|
| 84 | cc -= 0x10000; | 
|---|
| 85 | utf16String += (UTF16Char) ((cc >> 10) & 0x3ff) | 0xd800; | 
|---|
| 86 | utf16String += (UTF16Char) (cc & 0x3ff) | 0xdc00; | 
|---|
| 87 | } | 
|---|
| 88 | } | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 |  | 
|---|
| 92 | void UnicodeConverter::convert(const char* utf8String,  std::size_t length, UTF16String& utf16String) | 
|---|
| 93 | { | 
|---|
| 94 | if (!utf8String || !length) | 
|---|
| 95 | { | 
|---|
| 96 | utf16String.clear(); | 
|---|
| 97 | return; | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | convert(std::string(utf8String, utf8String + length), utf16String); | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 |  | 
|---|
| 104 | void UnicodeConverter::convert(const char* utf8String, UTF16String& utf16String) | 
|---|
| 105 | { | 
|---|
| 106 | if (!utf8String || !std::strlen(utf8String)) | 
|---|
| 107 | { | 
|---|
| 108 | utf16String.clear(); | 
|---|
| 109 | return; | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | convert(std::string(utf8String), utf16String); | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 |  | 
|---|
| 116 | void UnicodeConverter::convert(const UTF16String& utf16String, std::string& utf8String) | 
|---|
| 117 | { | 
|---|
| 118 | utf8String.clear(); | 
|---|
| 119 | UTF8Encoding utf8Encoding; | 
|---|
| 120 | UTF16Encoding utf16Encoding; | 
|---|
| 121 | TextConverter converter(utf16Encoding, utf8Encoding); | 
|---|
| 122 | converter.convert(utf16String.data(), (int) utf16String.length() * sizeof(UTF16Char), utf8String); | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 |  | 
|---|
| 126 | void UnicodeConverter::convert(const UTF32String& utf32String, std::string& utf8String) | 
|---|
| 127 | { | 
|---|
| 128 | utf8String.clear(); | 
|---|
| 129 | UTF8Encoding utf8Encoding; | 
|---|
| 130 | UTF32Encoding utf32Encoding; | 
|---|
| 131 | TextConverter converter(utf32Encoding, utf8Encoding); | 
|---|
| 132 | converter.convert(utf32String.data(), (int) utf32String.length() * sizeof(UTF32Char), utf8String); | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 |  | 
|---|
| 136 | void UnicodeConverter::convert(const UTF16Char* utf16String,  std::size_t length, std::string& utf8String) | 
|---|
| 137 | { | 
|---|
| 138 | utf8String.clear(); | 
|---|
| 139 | UTF8Encoding utf8Encoding; | 
|---|
| 140 | UTF16Encoding utf16Encoding; | 
|---|
| 141 | TextConverter converter(utf16Encoding, utf8Encoding); | 
|---|
| 142 | converter.convert(utf16String, (int) length * sizeof(UTF16Char), utf8String); | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 |  | 
|---|
| 146 | void UnicodeConverter::convert(const UTF32Char* utf32String,  std::size_t length, std::string& utf8String) | 
|---|
| 147 | { | 
|---|
| 148 | toUTF8(UTF32String(utf32String, length), utf8String); | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 |  | 
|---|
| 152 | void UnicodeConverter::convert(const UTF16Char* utf16String, std::string& utf8String) | 
|---|
| 153 | { | 
|---|
| 154 | toUTF8(utf16String, UTFStrlen(utf16String), utf8String); | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 |  | 
|---|
| 158 | void UnicodeConverter::convert(const UTF32Char* utf32String, std::string& utf8String) | 
|---|
| 159 | { | 
|---|
| 160 | toUTF8(utf32String, UTFStrlen(utf32String), utf8String); | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 |  | 
|---|
| 164 | } // namespace Poco | 
|---|
| 165 |  | 
|---|
| 166 |  | 
|---|
| 167 | #endif // POCO_OS_FAMILY_WINDOWS && !POCO_NO_WSTRING | 
|---|
| 168 |  | 
|---|