/src/poco/Foundation/include/Poco/UTFString.h
Line | Count | Source |
1 | | // |
2 | | // UTFString.h |
3 | | // |
4 | | // Library: Foundation |
5 | | // Package: Text |
6 | | // Module: UTFString |
7 | | // |
8 | | // Definitions of strings for UTF encodings. |
9 | | // |
10 | | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
11 | | // and Contributors. |
12 | | // |
13 | | // SPDX-License-Identifier: BSL-1.0 |
14 | | // |
15 | | |
16 | | |
17 | | #ifndef Foundation_UTFString_INCLUDED |
18 | | #define Foundation_UTFString_INCLUDED |
19 | | |
20 | | |
21 | | #include "Poco/Foundation.h" |
22 | | #include "Poco/Types.h" |
23 | | #include <string> |
24 | | |
25 | | |
26 | | namespace Poco { |
27 | | |
28 | | |
29 | | struct UTF16CharTraits |
30 | | { |
31 | | using u16streampos = std::fpos<std::mbstate_t>; |
32 | | using char_type = UInt16; |
33 | | using int_type = int; |
34 | | using off_type = std::streamoff; |
35 | | using pos_type = u16streampos; |
36 | | using state_type = std::mbstate_t; |
37 | | |
38 | | static void assign(char_type& c1, const char_type& c2) |
39 | 0 | { |
40 | 0 | c1 = c2; |
41 | 0 | } |
42 | | |
43 | | static bool eq(char_type c1, char_type c2) |
44 | 0 | { |
45 | 0 | return c1 == c2; |
46 | 0 | } |
47 | | |
48 | | static bool lt(char_type c1, char_type c2) |
49 | 0 | { |
50 | 0 | return c1 < c2; |
51 | 0 | } |
52 | | |
53 | | static int compare(const char_type* s1, const char_type* s2, std::size_t n) |
54 | 0 | { |
55 | 0 | for (; n; --n, ++s1, ++s2) |
56 | 0 | { |
57 | 0 | if (lt(*s1, *s2)) |
58 | 0 | return -1; |
59 | 0 | if (lt(*s2, *s1)) |
60 | 0 | return 1; |
61 | 0 | } |
62 | 0 | return 0; |
63 | 0 | } |
64 | | |
65 | | static std::size_t length(const char_type* s) |
66 | 0 | { |
67 | 0 | std::size_t len = 0; |
68 | 0 | for (; !eq(*s, char_type(0)); ++s) |
69 | 0 | ++len; |
70 | 0 | return len; |
71 | 0 | } |
72 | | |
73 | | static const char_type* find(const char_type* s, std::size_t n, const char_type& a) |
74 | 0 | { |
75 | 0 | for (; n; --n) |
76 | 0 | { |
77 | 0 | if (eq(*s, a)) |
78 | 0 | return s; |
79 | 0 | ++s; |
80 | 0 | } |
81 | 0 | return nullptr; |
82 | 0 | } |
83 | | |
84 | | static char_type* move(char_type* s1, const char_type* s2, std::size_t n) |
85 | 0 | { |
86 | 0 | char_type* r = s1; |
87 | 0 | if (s1 < s2) |
88 | 0 | { |
89 | 0 | for (; n; --n, ++s1, ++s2) |
90 | 0 | assign(*s1, *s2); |
91 | 0 | } |
92 | 0 | else if (s2 < s1) |
93 | 0 | { |
94 | 0 | s1 += n; |
95 | 0 | s2 += n; |
96 | 0 | for (; n; --n) |
97 | 0 | assign(*--s1, *--s2); |
98 | 0 | } |
99 | 0 | return r; |
100 | 0 | } |
101 | | |
102 | | static char_type* copy(char_type* s1, const char_type* s2, std::size_t n) |
103 | 0 | { |
104 | 0 | poco_assert(s2 < s1 || s2 >= s1 + n); |
105 | 0 | char_type* r = s1; |
106 | 0 | for (; n; --n, ++s1, ++s2) |
107 | 0 | assign(*s1, *s2); |
108 | 0 | return r; |
109 | 0 | } Unexecuted instantiation: Poco::UTF16CharTraits::copy(unsigned short*, unsigned short const*, unsigned long) Unexecuted instantiation: Poco::UTF16CharTraits::copy(unsigned short*, unsigned short const*, unsigned long) |
110 | | |
111 | | static char_type* assign(char_type* s, std::size_t n, char_type a) |
112 | 0 | { |
113 | 0 | char_type* r = s; |
114 | 0 | for (; n; --n, ++s) |
115 | 0 | assign(*s, a); |
116 | 0 | return r; |
117 | 0 | } |
118 | | |
119 | | static int_type not_eof(int_type c) |
120 | 0 | { |
121 | 0 | return eq_int_type(c, eof()) ? ~eof() : c; |
122 | 0 | } |
123 | | |
124 | | static char_type to_char_type(int_type c) |
125 | 0 | { |
126 | 0 | return char_type(c); |
127 | 0 | } |
128 | | |
129 | | static int_type to_int_type(char_type c) |
130 | 0 | { |
131 | 0 | return int_type(c); |
132 | 0 | } |
133 | | |
134 | | static bool eq_int_type(int_type c1, int_type c2) |
135 | 0 | { |
136 | 0 | return c1 == c2; |
137 | 0 | } |
138 | | |
139 | | static int_type eof() |
140 | 0 | { |
141 | 0 | return int_type(0xDFFF); |
142 | 0 | } |
143 | | }; |
144 | | |
145 | | |
146 | | struct UTF32CharTraits |
147 | | { |
148 | | using u32streampos = std::fpos<std::mbstate_t>; |
149 | | using char_type = UInt32; |
150 | | using int_type = int; |
151 | | using off_type = std::streamoff; |
152 | | using pos_type = u32streampos; |
153 | | using state_type = std::mbstate_t; |
154 | | |
155 | | static void assign(char_type& c1, const char_type& c2) |
156 | 0 | { |
157 | 0 | c1 = c2; |
158 | 0 | } |
159 | | |
160 | | static bool eq(char_type c1, char_type c2) |
161 | 0 | { |
162 | 0 | return c1 == c2; |
163 | 0 | } |
164 | | |
165 | | static bool lt(char_type c1, char_type c2) |
166 | 0 | { |
167 | 0 | return c1 < c2; |
168 | 0 | } |
169 | | |
170 | | static int compare(const char_type* s1, const char_type* s2, std::size_t n) |
171 | 0 | { |
172 | 0 | for (; n; --n, ++s1, ++s2) |
173 | 0 | { |
174 | 0 | if (lt(*s1, *s2)) |
175 | 0 | return -1; |
176 | 0 | if (lt(*s2, *s1)) |
177 | 0 | return 1; |
178 | 0 | } |
179 | 0 | return 0; |
180 | 0 | } |
181 | | |
182 | | static std::size_t length(const char_type* s) |
183 | 0 | { |
184 | 0 | std::size_t len = 0; |
185 | 0 | for (; !eq(*s, char_type(0)); ++s) |
186 | 0 | ++len; |
187 | 0 | return len; |
188 | 0 | } |
189 | | |
190 | | static const char_type* find(const char_type* s, std::size_t n, const char_type& a) |
191 | 0 | { |
192 | 0 | for (; n; --n) |
193 | 0 | { |
194 | 0 | if (eq(*s, a)) |
195 | 0 | return s; |
196 | 0 | ++s; |
197 | 0 | } |
198 | 0 | return nullptr; |
199 | 0 | } |
200 | | |
201 | | static char_type* move(char_type* s1, const char_type* s2, std::size_t n) |
202 | 0 | { |
203 | 0 | char_type* r = s1; |
204 | 0 | if (s1 < s2) |
205 | 0 | { |
206 | 0 | for (; n; --n, ++s1, ++s2) |
207 | 0 | assign(*s1, *s2); |
208 | 0 | } |
209 | 0 | else if (s2 < s1) |
210 | 0 | { |
211 | 0 | s1 += n; |
212 | 0 | s2 += n; |
213 | 0 | for (; n; --n) |
214 | 0 | assign(*--s1, *--s2); |
215 | 0 | } |
216 | 0 | return r; |
217 | 0 | } |
218 | | |
219 | | static char_type* copy(char_type* s1, const char_type* s2, std::size_t n) |
220 | 0 | { |
221 | 0 | poco_assert(s2 < s1 || s2 >= s1 + n); |
222 | 0 | char_type* r = s1; |
223 | 0 | for (; n; --n, ++s1, ++s2) |
224 | 0 | assign(*s1, *s2); |
225 | 0 | return r; |
226 | 0 | } |
227 | | |
228 | | static char_type* assign(char_type* s, std::size_t n, char_type a) |
229 | 0 | { |
230 | 0 | char_type* r = s; |
231 | 0 | for (; n; --n, ++s) |
232 | 0 | assign(*s, a); |
233 | 0 | return r; |
234 | 0 | } |
235 | | |
236 | | static int_type not_eof(int_type c) |
237 | 0 | { |
238 | 0 | return eq_int_type(c, eof()) ? ~eof() : c; |
239 | 0 | } |
240 | | |
241 | | static char_type to_char_type(int_type c) |
242 | 0 | { |
243 | 0 | return char_type(c); |
244 | 0 | } |
245 | | |
246 | | static int_type to_int_type(char_type c) |
247 | 0 | { |
248 | 0 | return int_type(c); |
249 | 0 | } |
250 | | |
251 | | static bool eq_int_type(int_type c1, int_type c2) |
252 | 0 | { |
253 | 0 | return c1 == c2; |
254 | 0 | } |
255 | | |
256 | | static int_type eof() |
257 | 0 | { |
258 | 0 | return int_type(0xDFFF); |
259 | 0 | } |
260 | | }; |
261 | | |
262 | | |
263 | | #ifdef POCO_NO_WSTRING |
264 | | using UTF16Char = Poco::UInt16; |
265 | | using UTF16String = std::basic_string<UTF16Char, UTF16CharTraits>; |
266 | | using UTF32Char = UInt32; |
267 | | using UTF32String = std::basic_string<UTF32Char, UTF32CharTraits>; |
268 | | #define POCO_USE_STRING16 |
269 | | #define POCO_USE_STRING32 |
270 | | #else // POCO_NO_WSTRING |
271 | | #if defined(POCO_OS_FAMILY_WINDOWS) |
272 | | using UTF16Char = wchar_t; |
273 | | using UTF16String = std::wstring; |
274 | | using UTF32Char = UInt32; |
275 | | using UTF32String = std::basic_string<UTF32Char, UTF32CharTraits>; |
276 | | #elif defined(__SIZEOF_WCHAR_T__) //gcc |
277 | | #if (__SIZEOF_WCHAR_T__ == 2) |
278 | | using UTF16Char = wchar_t; |
279 | | using UTF16String = std::wstring; |
280 | | using UTF32Char = UInt32; |
281 | | using UTF32String = std::basic_string<UTF32Char, UTF32CharTraits>; |
282 | | #define POCO_USE_STRING32 |
283 | | #elif (__SIZEOF_WCHAR_T__ == 4) |
284 | | using UTF16Char = Poco::UInt16; |
285 | | using UTF16String = std::basic_string<UTF16Char, UTF16CharTraits>; |
286 | | using UTF32Char = wchar_t; |
287 | | using UTF32String = std::wstring; |
288 | | #define POCO_USE_STRING16 |
289 | | #endif |
290 | | #else // default to 32-bit wchar_t |
291 | | using UTF16Char = Poco::UInt16; |
292 | | using UTF16String = std::basic_string<UTF16Char, UTF16CharTraits>; |
293 | | using UTF32Char = wchar_t; |
294 | | using UTF32String = std::wstring; |
295 | | #define POCO_USE_STRING16 |
296 | | #endif //POCO_OS_FAMILY_WINDOWS |
297 | | #endif //POCO_NO_WSTRING |
298 | | |
299 | | |
300 | | } // namespace Poco |
301 | | |
302 | | |
303 | | #if defined(POCO_USE_STRING16) |
304 | | extern template class Foundation_API std::basic_string<Poco::UTF16Char, Poco::UTF16CharTraits>; |
305 | | #endif |
306 | | |
307 | | #if defined(POCO_USE_STRING32) |
308 | | extern template class Foundation_API std::basic_string<Poco::UTF32Char, Poco::UTF32CharTraits>; |
309 | | #endif |
310 | | |
311 | | |
312 | | #endif // Foundation_UTFString_INCLUDED |