]>
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" );
43 return wxT('?'); // FIXME-UTF8: what to use as failure character?
46 if ( wxConvLibc
.ToWChar(buf
, 2, &c
, 1) != 2 )
48 wxFAIL_MSG( "invalid multibyte character" );
49 return wxT('?'); // FIXME-UTF8: what to use as failure character?
56 char wxUniChar::ToHi8bit(wxUniChar::value_type c
)
58 #if wxUSE_UTF8_LOCALE_ONLY
59 wxFAIL_MSG( "character cannot be converted to single UTF-8 byte" );
60 return '?'; // FIXME-UTF8: what to use as failure character?
64 if ( wxConvLibc
.FromWChar(buf
, 2, &in
, 1) != 2 )
66 wxFAIL_MSG( "character cannot be converted to single byte" );
67 return '?'; // FIXME-UTF8: what to use as failure character?
74 // ---------------------------------------------------------------------------
76 // ---------------------------------------------------------------------------
78 #if wxUSE_UNICODE_UTF8
79 wxUniChar
wxUniCharRef::UniChar() const
81 return wxStringOperations::DecodeChar(m_pos
);
84 wxUniCharRef
& wxUniCharRef::operator=(const wxUniChar
& c
)
86 wxStringOperations::Utf8CharBuffer
utf(wxStringOperations::EncodeChar(c
));
87 size_t lenOld
= wxStringOperations::GetUtf8CharLength(*m_pos
);
88 size_t lenNew
= wxStringOperations::GetUtf8CharLength(utf
[0]);
90 if ( lenNew
== lenOld
)
92 // this is the simpler case: if the new value's UTF-8 code has the
93 // same length, we can just replace it:
96 for ( size_t i
= 0; i
< lenNew
; ++i
, ++pos
)
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:
105 wxString
& str
= *wx_const_cast(wxString
*, m_node
.m_str
);
106 wxStringImpl
& strimpl
= str
.m_impl
;
108 int iterDiff
= lenNew
- lenOld
;
109 size_t posIdx
= m_pos
- strimpl
.begin();
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
115 static const size_t STATIC_SIZE
= 32;
116 size_t indexes_a
[STATIC_SIZE
];
117 size_t *indexes
= indexes_a
;
119 wxStringIteratorNode
*it
;
120 for ( it
= str
.m_iterators
.ptr
; it
; it
= it
->m_next
, ++iterNum
)
122 wxASSERT( it
->m_iter
|| it
->m_citer
);
124 if ( iterNum
== STATIC_SIZE
)
126 wxLogTrace( _T("utf8"), _T("unexpectedly many iterators") );
128 size_t total
= iterNum
+ 1;
129 for ( wxStringIteratorNode
*it2
= it
; it2
; it2
= it2
->m_next
)
131 indexes
= new size_t[total
];
132 memcpy(indexes
, indexes_a
, sizeof(size_t) * STATIC_SIZE
);
135 size_t idx
= it
->m_iter
136 ? (*it
->m_iter
- strimpl
.begin())
137 : (*it
->m_citer
- strimpl
.begin());
142 indexes
[iterNum
] = idx
;
145 // update the string:
146 strimpl
.replace(m_pos
, m_pos
+ lenOld
, utf
, lenNew
);
148 // finally, set the iterators to valid values again (note that this
149 // updates m_pos as well):
151 for ( i
= 0, it
= str
.m_iterators
.ptr
; it
; it
= it
->m_next
, ++i
)
153 wxASSERT( i
< iterNum
);
154 wxASSERT( it
->m_iter
|| it
->m_citer
);
157 *it
->m_iter
= strimpl
.begin() + indexes
[i
];
159 *it
->m_citer
= strimpl
.begin() + indexes
[i
];
162 if ( indexes
!= indexes_a
)
168 #endif // wxUSE_UNICODE_UTF8