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