]>
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
7 // Copyright: (c) 2007 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ===========================================================================
13 // ===========================================================================
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
23 #include "wx/strconv.h" // wxConvLibc
27 #include "wx/unichar.h"
28 #include "wx/string.h"
30 // ===========================================================================
32 // ===========================================================================
34 // ---------------------------------------------------------------------------
36 // ---------------------------------------------------------------------------
39 wxUniChar::value_type
wxUniChar::FromHi8bit(char c
)
41 #if wxUSE_UTF8_LOCALE_ONLY
42 wxFAIL_MSG( "invalid UTF-8 character" );
45 return wxT('?'); // FIXME-UTF8: what to use as failure character?
51 if ( wxConvLibc
.ToWChar(wbuf
, 2, cbuf
, 2) != 2 )
53 wxFAIL_MSG( "invalid multibyte character" );
54 return wxT('?'); // FIXME-UTF8: what to use as failure character?
61 char wxUniChar::ToHi8bit(wxUniChar::value_type c
)
63 #if wxUSE_UTF8_LOCALE_ONLY
64 wxFAIL_MSG( "character cannot be converted to single UTF-8 byte" );
67 return '?'; // FIXME-UTF8: what to use as failure character?
73 if ( wxConvLibc
.FromWChar(cbuf
, 2, wbuf
, 2) != 2 )
75 wxFAIL_MSG( "character cannot be converted to single byte" );
76 return '?'; // FIXME-UTF8: what to use as failure character?
83 // ---------------------------------------------------------------------------
85 // ---------------------------------------------------------------------------
87 #if wxUSE_UNICODE_UTF8
88 wxUniChar
wxUniCharRef::UniChar() const
90 return wxStringOperations::DecodeChar(m_pos
);
93 wxUniCharRef
& wxUniCharRef::operator=(const wxUniChar
& c
)
95 wxStringOperations::Utf8CharBuffer
utf(wxStringOperations::EncodeChar(c
));
96 size_t lenOld
= wxStringOperations::GetUtf8CharLength(*m_pos
);
97 size_t lenNew
= wxStringOperations::GetUtf8CharLength(utf
[0]);
99 if ( lenNew
== lenOld
)
101 // this is the simpler case: if the new value's UTF-8 code has the
102 // same length, we can just replace it:
105 for ( size_t i
= 0; i
< lenNew
; ++i
, ++pos
)
108 else // length of character encoding in UTF-8 changed
110 // the worse case is when the new value has either longer or shorter
111 // code -- in that case, we have to use wxStringImpl::replace() and
112 // this invalidates all iterators, so we have to update them too:
114 wxStringImpl
& strimpl
= m_str
.m_impl
;
116 int iterDiff
= lenNew
- lenOld
;
117 size_t posIdx
= m_pos
- strimpl
.begin();
119 // compute positions of outstanding iterators for this string after the
120 // replacement is done (there is only a small number of iterators at
121 // any time, so we use an array on the stack to avoid unneeded
123 static const size_t STATIC_SIZE
= 32;
124 size_t indexes_a
[STATIC_SIZE
];
125 size_t *indexes
= indexes_a
;
127 wxStringIteratorNode
*it
;
128 for ( it
= m_str
.m_iterators
.ptr
; it
; it
= it
->m_next
, ++iterNum
)
130 wxASSERT( it
->m_iter
|| it
->m_citer
);
132 if ( iterNum
== STATIC_SIZE
)
134 wxLogTrace( wxT("utf8"), wxT("unexpectedly many iterators") );
136 size_t total
= iterNum
+ 1;
137 for ( wxStringIteratorNode
*it2
= it
; it2
; it2
= it2
->m_next
)
139 indexes
= new size_t[total
];
140 memcpy(indexes
, indexes_a
, sizeof(size_t) * STATIC_SIZE
);
143 size_t idx
= it
->m_iter
144 ? (*it
->m_iter
- strimpl
.begin())
145 : (*it
->m_citer
- strimpl
.begin());
150 indexes
[iterNum
] = idx
;
153 // update the string:
154 strimpl
.replace(m_pos
, m_pos
+ lenOld
, utf
, lenNew
);
156 #if wxUSE_STRING_POS_CACHE
157 m_str
.InvalidateCache();
158 #endif // wxUSE_STRING_POS_CACHE
160 // finally, set the iterators to valid values again (note that this
161 // updates m_pos as well):
163 for ( i
= 0, it
= m_str
.m_iterators
.ptr
; it
; it
= it
->m_next
, ++i
)
165 wxASSERT( i
< iterNum
);
166 wxASSERT( it
->m_iter
|| it
->m_citer
);
169 *it
->m_iter
= strimpl
.begin() + indexes
[i
];
171 *it
->m_citer
= strimpl
.begin() + indexes
[i
];
174 if ( indexes
!= indexes_a
)
180 #endif // wxUSE_UNICODE_UTF8