MyGUI 3.5.3
MyGUI_UString.cpp
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#include "MyGUI_Precompiled.h"
8#include "MyGUI_UString.h"
9
10#if defined(_MSVC_LANG)
11 // MSVC might reports an old __cplusplus value
12 #define UTF_CPP_CPLUSPLUS _MSVC_LANG
13#endif
14#include <utf8.h>
15
16namespace MyGUI
17{
18 namespace
19 {
20
21 // Keep dependency exceptions out of the public API.
22 template<typename Convert>
23 auto convertText(Convert convert)
24 {
25 try
26 {
27 return convert();
28 }
29 catch (const utf8::exception& error)
30 {
31 throw UString::invalid_data(error.what());
32 }
33 }
34
35 void validateCharacter(char32_t character)
36 {
37 char buffer[4];
38 utf8::append(character, buffer);
39 }
40
41 }
42
43 UString::UString(std::size_t count, Char character) :
44 mData(count, static_cast<char32_t>(character))
45 {
46 }
47
48 UString::UString(std::string_view text)
49 {
50 assign(text);
51 }
52
53 UString::UString(utf32string text) :
54 mData(std::move(text))
55 {
56 }
57
58 UString::UString(const std::wstring& text)
59 {
60 static_assert(sizeof(wchar_t) == 2 || sizeof(wchar_t) == 4);
61 mData.reserve(text.size());
62 for (auto iter = text.begin(); iter != text.end();)
63 {
64 char32_t character = U'\uFFFD';
65 if constexpr (sizeof(wchar_t) == 2)
66 {
67 auto next = iter;
68 try
69 {
70 const auto value = utf8::next16(next, text.end());
71 // Some malformed sequences leave next unchanged instead of throwing.
72 if (next != iter)
73 character = value;
74 }
75 catch (const utf8::exception&)
76 {
77 next = iter;
78 }
79 // Replace only the unpaired surrogate; preserve the following character.
80 iter = next == iter ? iter + 1 : next;
81 }
82 else
83 {
84 const auto value = static_cast<char32_t>(*iter++);
85 try
86 {
87 validateCharacter(value);
88 character = value;
89 }
90 catch (const utf8::exception&)
91 {
92 // Native UTF-32 input follows the same replacement policy.
93 }
94 }
95 mData.push_back(character);
96 }
97 }
98
99 UString& UString::assign(std::string_view text)
100 {
101 // An empty string_view may have a null data pointer.
102 if (text.empty())
103 mData.clear();
104 else
105 mData = convertText([text] { return utf8::utf8to32(utf8::replace_invalid(text)); });
106 return *this;
107 }
108
109 const std::string& UString::asUTF8() const
110 {
111 mUTF8 = convertText([this] { return utf8::utf32to8(mData); });
112 return mUTF8;
113 }
114
115 std::wstring UString::asWStr() const
116 {
117 return convertText(
118 [this]
119 {
120 std::wstring result;
121 result.reserve(mData.size());
122 for (auto character : mData)
123 {
124 if constexpr (sizeof(wchar_t) == 2)
125 utf8::append16(character, std::back_inserter(result));
126 else
127 {
128 validateCharacter(character);
129 result.push_back(static_cast<wchar_t>(character));
130 }
131 }
132 return result;
133 });
134 }
135
136} // namespace MyGUI
unsigned int Char
Definition MyGUI_Types.h:49