]>
git.saurik.com Git - wxWidgets.git/blob - src/common/unichar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/unichar.cpp
3 // Purpose: wxUniChar and wxUniCharRef classes
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2007 REA Elektronik GmbH
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // ===========================================================================
12 // ===========================================================================
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
22 #include "wx/strconv.h" // wxConvLibc
26 #include "wx/unichar.h"
27 #include "wx/string.h"
29 // ===========================================================================
31 // ===========================================================================
33 // ---------------------------------------------------------------------------
35 // ---------------------------------------------------------------------------
38 wxUniChar::value_type
wxUniChar::FromHi8bit(char c
)
40 #if wxUSE_UTF8_LOCALE_ONLY
41 wxFAIL_MSG( "invalid UTF-8 character" );
44 return wxT('?'); // FIXME-UTF8: what to use as failure character?
50 if ( wxConvLibc
.ToWChar(wbuf
, 2, cbuf
, 2) != 2 )
52 wxFAIL_MSG( "invalid multibyte character" );
53 return wxT('?'); // FIXME-UTF8: what to use as failure character?
60 char wxUniChar::ToHi8bit(wxUniChar::value_type v
)
63 if ( !GetAsHi8bit(v
, &c
) )
65 wxFAIL_MSG( "character cannot be converted to single byte" );
66 c
= '?'; // FIXME-UTF8: what to use as failure character?
73 bool wxUniChar::GetAsHi8bit(value_type v
, char *c
)
79 if ( wxConvLibc
.FromWChar(cbuf
, 2, wbuf
, 2) != 2 )
86 // ---------------------------------------------------------------------------
88 // ---------------------------------------------------------------------------
90 #if wxUSE_UNICODE_UTF8
91 wxUniChar
wxUniCharRef::UniChar() const
93 return wxStringOperations::DecodeChar(m_pos
);
96 wxUniCharRef
& wxUniCharRef::operator=(const wxUniChar
& c
)
98 wxStringOperations::Utf8CharBuffer
utf(wxStringOperations::EncodeChar(c
));
99 size_t lenOld
= wxStringOperations::GetUtf8CharLength(*m_pos
);
100 size_t lenNew
= wxStringOperations::GetUtf8CharLength(utf
[0]);
102 if ( lenNew
== lenOld
)
104 // this is the simpler case: if the new value's UTF-8 code has the
105 // same length, we can just replace it:
108 for ( size_t i
= 0; i
< lenNew
; ++i
, ++pos
)
111 else // length of character encoding in UTF-8 changed
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:
117 wxStringImpl
& strimpl
= m_str
.m_impl
;
119 int iterDiff
= lenNew
- lenOld
;
120 size_t posIdx
= m_pos
- strimpl
.begin();
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
126 static const size_t STATIC_SIZE
= 32;
127 size_t indexes_a
[STATIC_SIZE
];
128 size_t *indexes
= indexes_a
;
130 wxStringIteratorNode
*it
;
131 for ( it
= m_str
.m_iterators
.ptr
; it
; it
= it
->m_next
, ++iterNum
)
133 wxASSERT( it
->m_iter
|| it
->m_citer
);
135 if ( iterNum
== STATIC_SIZE
)
137 wxLogTrace( wxT("utf8"), wxT("unexpectedly many iterators") );
139 size_t total
= iterNum
+ 1;
140 for ( wxStringIteratorNode
*it2
= it
; it2
; it2
= it2
->m_next
)
142 indexes
= new size_t[total
];
143 memcpy(indexes
, indexes_a
, sizeof(size_t) * STATIC_SIZE
);
146 size_t idx
= it
->m_iter
147 ? (*it
->m_iter
- strimpl
.begin())
148 : (*it
->m_citer
- strimpl
.begin());
153 indexes
[iterNum
] = idx
;
156 // update the string:
157 strimpl
.replace(m_pos
, m_pos
+ lenOld
, utf
, lenNew
);
159 #if wxUSE_STRING_POS_CACHE
160 m_str
.InvalidateCache();
161 #endif // wxUSE_STRING_POS_CACHE
163 // finally, set the iterators to valid values again (note that this
164 // updates m_pos as well):
166 for ( i
= 0, it
= m_str
.m_iterators
.ptr
; it
; it
= it
->m_next
, ++i
)
168 wxASSERT( i
< iterNum
);
169 wxASSERT( it
->m_iter
|| it
->m_citer
);
172 *it
->m_iter
= strimpl
.begin() + indexes
[i
];
174 *it
->m_citer
= strimpl
.begin() + indexes
[i
];
177 if ( indexes
!= indexes_a
)
183 #endif // wxUSE_UNICODE_UTF8