]>
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 v
)
64 if ( !GetAsHi8bit(v
, &c
) )
66 wxFAIL_MSG( "character cannot be converted to single byte" );
67 c
= '?'; // FIXME-UTF8: what to use as failure character?
74 bool wxUniChar::GetAsHi8bit(value_type v
, char *c
)
80 if ( wxConvLibc
.FromWChar(cbuf
, 2, wbuf
, 2) != 2 )
87 // ---------------------------------------------------------------------------
89 // ---------------------------------------------------------------------------
91 #if wxUSE_UNICODE_UTF8
92 wxUniChar
wxUniCharRef::UniChar() const
94 return wxStringOperations::DecodeChar(m_pos
);
97 wxUniCharRef
& wxUniCharRef::operator=(const wxUniChar
& c
)
99 wxStringOperations::Utf8CharBuffer
utf(wxStringOperations::EncodeChar(c
));
100 size_t lenOld
= wxStringOperations::GetUtf8CharLength(*m_pos
);
101 size_t lenNew
= wxStringOperations::GetUtf8CharLength(utf
[0]);
103 if ( lenNew
== lenOld
)
105 // this is the simpler case: if the new value's UTF-8 code has the
106 // same length, we can just replace it:
109 for ( size_t i
= 0; i
< lenNew
; ++i
, ++pos
)
112 else // length of character encoding in UTF-8 changed
114 // the worse case is when the new value has either longer or shorter
115 // code -- in that case, we have to use wxStringImpl::replace() and
116 // this invalidates all iterators, so we have to update them too:
118 wxStringImpl
& strimpl
= m_str
.m_impl
;
120 int iterDiff
= lenNew
- lenOld
;
121 size_t posIdx
= m_pos
- strimpl
.begin();
123 // compute positions of outstanding iterators for this string after the
124 // replacement is done (there is only a small number of iterators at
125 // any time, so we use an array on the stack to avoid unneeded
127 static const size_t STATIC_SIZE
= 32;
128 size_t indexes_a
[STATIC_SIZE
];
129 size_t *indexes
= indexes_a
;
131 wxStringIteratorNode
*it
;
132 for ( it
= m_str
.m_iterators
.ptr
; it
; it
= it
->m_next
, ++iterNum
)
134 wxASSERT( it
->m_iter
|| it
->m_citer
);
136 if ( iterNum
== STATIC_SIZE
)
138 wxLogTrace( wxT("utf8"), wxT("unexpectedly many iterators") );
140 size_t total
= iterNum
+ 1;
141 for ( wxStringIteratorNode
*it2
= it
; it2
; it2
= it2
->m_next
)
143 indexes
= new size_t[total
];
144 memcpy(indexes
, indexes_a
, sizeof(size_t) * STATIC_SIZE
);
147 size_t idx
= it
->m_iter
148 ? (*it
->m_iter
- strimpl
.begin())
149 : (*it
->m_citer
- strimpl
.begin());
154 indexes
[iterNum
] = idx
;
157 // update the string:
158 strimpl
.replace(m_pos
, m_pos
+ lenOld
, utf
, lenNew
);
160 #if wxUSE_STRING_POS_CACHE
161 m_str
.InvalidateCache();
162 #endif // wxUSE_STRING_POS_CACHE
164 // finally, set the iterators to valid values again (note that this
165 // updates m_pos as well):
167 for ( i
= 0, it
= m_str
.m_iterators
.ptr
; it
; it
= it
->m_next
, ++i
)
169 wxASSERT( i
< iterNum
);
170 wxASSERT( it
->m_iter
|| it
->m_citer
);
173 *it
->m_iter
= strimpl
.begin() + indexes
[i
];
175 *it
->m_citer
= strimpl
.begin() + indexes
[i
];
178 if ( indexes
!= indexes_a
)
184 #endif // wxUSE_UNICODE_UTF8