Line data Source code
1 : /* Copyright (C) 2020 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : /*
19 : A basic_string derivative that works with uint16_t as its underlying char
20 : type.
21 : */
22 : #ifndef INCLUDED_UTF16STRING
23 : #define INCLUDED_UTF16STRING
24 :
25 : // On Linux, wchar_t is 32-bit, so define a new version of it.
26 : // We now use this code on Windows as well, because wchar_t is a
27 : // native type and distinct from utf16_t.
28 : #include <algorithm>
29 : #include <cstring>
30 : #include <memory>
31 : #include <string>
32 :
33 :
34 : typedef uint16_t utf16_t;
35 :
36 : // jw: this was originally defined in the std namespace, which is at
37 : // least frowned upon if not illegal. giving it a new name and passing it
38 : // as a template parameter is the "correct" and safe way.
39 :
40 : struct utf16_traits
41 : {
42 : typedef utf16_t char_type;
43 : typedef int int_type;
44 : typedef std::streampos pos_type;
45 : typedef std::streamoff off_type;
46 : typedef std::mbstate_t state_type;
47 :
48 0 : static void assign(char_type& c1, const char_type& c2)
49 195303 : { c1 = c2; }
50 :
51 : static bool eq(const char_type& c1, const char_type& c2)
52 : { return c1 == c2; }
53 :
54 : static bool lt(const char_type& c1, const char_type& c2)
55 : { return c1 < c2; }
56 :
57 : static int compare(const char_type* s1, const char_type* s2, size_t n)
58 : {
59 : return memcmp(s1, s2, n*sizeof(char_type));
60 : }
61 :
62 : static size_t length(const char_type* s)
63 : {
64 : const char_type* end=s;
65 : while (*end) end++;
66 : return (size_t)(end-s);
67 : }
68 :
69 : static const char_type* find(const char_type* s, size_t n, const char_type& a)
70 : {
71 : const char_type *end = s+n;
72 : const char_type *res = std::find(s, end, a);
73 : return (res != end)?res:NULL;
74 : }
75 :
76 : static char_type* move(char_type* s1, const char_type* s2, size_t n)
77 : {
78 0 : return (char_type *)memmove(s1, s2, n*sizeof(char_type));
79 : }
80 :
81 : static char_type* copy(char_type* s1, const char_type* s2, size_t n)
82 : {
83 0 : return (char_type *)memcpy(s1, s2, n*sizeof(char_type));
84 : }
85 :
86 : static char_type* assign(char_type* s, size_t n, char_type a)
87 : {
88 24 : while (n--)
89 : {
90 18 : s[n]=a;
91 : }
92 : return s;
93 : }
94 :
95 : static char_type to_char_type(const int_type& c)
96 : { return (char_type)c; }
97 :
98 : static int_type to_int_type(const char_type& c)
99 : { return (int_type)c; }
100 :
101 : static bool eq_int_type(const int_type& c1, const int_type& c2)
102 : { return c1 == c2; }
103 :
104 : static int_type eof()
105 : { return -1; }
106 :
107 : static int_type not_eof(const int_type& c)
108 : { return (c == -1) ? 0 : c; }
109 : };
110 :
111 : typedef std::basic_string<utf16_t, utf16_traits> utf16string;
112 : typedef std::basic_stringstream<utf16_t, utf16_traits> utf16stringstream;
113 :
114 : #endif
|