]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/unichar.cpp | |
3 | // Purpose: wxUniChar and wxUniCharRef classes | |
4 | // Author: Vaclav Slavik | |
5 | // Created: 2007-03-19 | |
6 | // Copyright: (c) 2007 REA Elektronik GmbH | |
7 | // Licence: wxWindows licence | |
8 | /////////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | // =========================================================================== | |
11 | // headers | |
12 | // =========================================================================== | |
13 | ||
14 | // For compilers that support precompilation, includes "wx.h". | |
15 | #include "wx/wxprec.h" | |
16 | ||
17 | #ifdef __BORLANDC__ | |
18 | #pragma hdrstop | |
19 | #endif | |
20 | ||
21 | #ifndef WX_PRECOMP | |
22 | #include "wx/strconv.h" // wxConvLibc | |
23 | #include "wx/log.h" | |
24 | #endif | |
25 | ||
26 | #include "wx/unichar.h" | |
27 | #include "wx/string.h" | |
28 | ||
29 | // =========================================================================== | |
30 | // implementation | |
31 | // =========================================================================== | |
32 | ||
33 | // --------------------------------------------------------------------------- | |
34 | // wxUniChar | |
35 | // --------------------------------------------------------------------------- | |
36 | ||
37 | /* static */ | |
38 | wxUniChar::value_type wxUniChar::FromHi8bit(char c) | |
39 | { | |
40 | #if wxUSE_UTF8_LOCALE_ONLY | |
41 | wxFAIL_MSG( "invalid UTF-8 character" ); | |
42 | wxUnusedVar(c); | |
43 | ||
44 | return wxT('?'); // FIXME-UTF8: what to use as failure character? | |
45 | #else | |
46 | char cbuf[2]; | |
47 | cbuf[0] = c; | |
48 | cbuf[1] = '\0'; | |
49 | wchar_t wbuf[2]; | |
50 | if ( wxConvLibc.ToWChar(wbuf, 2, cbuf, 2) != 2 ) | |
51 | { | |
52 | wxFAIL_MSG( "invalid multibyte character" ); | |
53 | return wxT('?'); // FIXME-UTF8: what to use as failure character? | |
54 | } | |
55 | return wbuf[0]; | |
56 | #endif | |
57 | } | |
58 | ||
59 | /* static */ | |
60 | char wxUniChar::ToHi8bit(wxUniChar::value_type v) | |
61 | { | |
62 | char c; | |
63 | if ( !GetAsHi8bit(v, &c) ) | |
64 | { | |
65 | wxFAIL_MSG( "character cannot be converted to single byte" ); | |
66 | c = '?'; // FIXME-UTF8: what to use as failure character? | |
67 | } | |
68 | ||
69 | return c; | |
70 | } | |
71 | ||
72 | /* static */ | |
73 | bool wxUniChar::GetAsHi8bit(value_type v, char *c) | |
74 | { | |
75 | wchar_t wbuf[2]; | |
76 | wbuf[0] = v; | |
77 | wbuf[1] = L'\0'; | |
78 | char cbuf[2]; | |
79 | if ( wxConvLibc.FromWChar(cbuf, 2, wbuf, 2) != 2 ) | |
80 | return false; | |
81 | ||
82 | *c = cbuf[0]; | |
83 | return true; | |
84 | } | |
85 | ||
86 | // --------------------------------------------------------------------------- | |
87 | // wxUniCharRef | |
88 | // --------------------------------------------------------------------------- | |
89 | ||
90 | #if wxUSE_UNICODE_UTF8 | |
91 | wxUniChar wxUniCharRef::UniChar() const | |
92 | { | |
93 | return wxStringOperations::DecodeChar(m_pos); | |
94 | } | |
95 | ||
96 | wxUniCharRef& wxUniCharRef::operator=(const wxUniChar& c) | |
97 | { | |
98 | wxStringOperations::Utf8CharBuffer utf(wxStringOperations::EncodeChar(c)); | |
99 | size_t lenOld = wxStringOperations::GetUtf8CharLength(*m_pos); | |
100 | size_t lenNew = wxStringOperations::GetUtf8CharLength(utf[0]); | |
101 | ||
102 | if ( lenNew == lenOld ) | |
103 | { | |
104 | // this is the simpler case: if the new value's UTF-8 code has the | |
105 | // same length, we can just replace it: | |
106 | ||
107 | iterator pos(m_pos); | |
108 | for ( size_t i = 0; i < lenNew; ++i, ++pos ) | |
109 | *pos = utf[i]; | |
110 | } | |
111 | else // length of character encoding in UTF-8 changed | |
112 | { | |
113 | // the worse case is when the new value has either longer or shorter | |
114 | // code -- in that case, we have to use wxStringImpl::replace() and | |
115 | // this invalidates all iterators, so we have to update them too: | |
116 | ||
117 | wxStringImpl& strimpl = m_str.m_impl; | |
118 | ||
119 | int iterDiff = lenNew - lenOld; | |
120 | size_t posIdx = m_pos - strimpl.begin(); | |
121 | ||
122 | // compute positions of outstanding iterators for this string after the | |
123 | // replacement is done (there is only a small number of iterators at | |
124 | // any time, so we use an array on the stack to avoid unneeded | |
125 | // allocation): | |
126 | static const size_t STATIC_SIZE = 32; | |
127 | size_t indexes_a[STATIC_SIZE]; | |
128 | size_t *indexes = indexes_a; | |
129 | size_t iterNum = 0; | |
130 | wxStringIteratorNode *it; | |
131 | for ( it = m_str.m_iterators.ptr; it; it = it->m_next, ++iterNum ) | |
132 | { | |
133 | wxASSERT( it->m_iter || it->m_citer ); | |
134 | ||
135 | if ( iterNum == STATIC_SIZE ) | |
136 | { | |
137 | wxLogTrace( wxT("utf8"), wxT("unexpectedly many iterators") ); | |
138 | ||
139 | size_t total = iterNum + 1; | |
140 | for ( wxStringIteratorNode *it2 = it; it2; it2 = it2->m_next ) | |
141 | total++; | |
142 | indexes = new size_t[total]; | |
143 | memcpy(indexes, indexes_a, sizeof(size_t) * STATIC_SIZE); | |
144 | } | |
145 | ||
146 | size_t idx = it->m_iter | |
147 | ? (*it->m_iter - strimpl.begin()) | |
148 | : (*it->m_citer - strimpl.begin()); | |
149 | ||
150 | if ( idx > posIdx ) | |
151 | idx += iterDiff; | |
152 | ||
153 | indexes[iterNum] = idx; | |
154 | } | |
155 | ||
156 | // update the string: | |
157 | strimpl.replace(m_pos, m_pos + lenOld, utf, lenNew); | |
158 | ||
159 | #if wxUSE_STRING_POS_CACHE | |
160 | m_str.InvalidateCache(); | |
161 | #endif // wxUSE_STRING_POS_CACHE | |
162 | ||
163 | // finally, set the iterators to valid values again (note that this | |
164 | // updates m_pos as well): | |
165 | size_t i; | |
166 | for ( i = 0, it = m_str.m_iterators.ptr; it; it = it->m_next, ++i ) | |
167 | { | |
168 | wxASSERT( i < iterNum ); | |
169 | wxASSERT( it->m_iter || it->m_citer ); | |
170 | ||
171 | if ( it->m_iter ) | |
172 | *it->m_iter = strimpl.begin() + indexes[i]; | |
173 | else // it->m_citer | |
174 | *it->m_citer = strimpl.begin() + indexes[i]; | |
175 | } | |
176 | ||
177 | if ( indexes != indexes_a ) | |
178 | delete[] indexes; | |
179 | } | |
180 | ||
181 | return *this; | |
182 | } | |
183 | #endif // wxUSE_UNICODE_UTF8 |