]>
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
26 #include "wx/unichar.h"
27 #include "wx/stringops.h"
29 // ===========================================================================
31 // ===========================================================================
33 // ---------------------------------------------------------------------------
35 // ---------------------------------------------------------------------------
38 wxUniChar::value_type
wxUniChar::From8bit(char c
)
40 // all supported charsets have the first 128 characters same as ASCII:
41 if ( (unsigned char)c
< 0x80 )
44 #if wxUSE_UTF8_LOCALE_ONLY
45 wxFAIL_MSG( _T("invalid UTF-8 character") );
46 return wxT('?'); // FIXME-UTF8: what to use as failure character?
49 if ( wxConvLibc
.ToWChar(buf
, 2, &c
, 1) != 2 )
50 return wxT('?'); // FIXME-UTF8: what to use as failure character?
56 char wxUniChar::To8bit(wxUniChar::value_type c
)
58 // all supported charsets have the first 128 characters same as ASCII:
62 #if wxUSE_UTF8_LOCALE_ONLY
63 wxFAIL_MSG( _T("character cannot be converted to single UTF-8 byte") );
64 return '?'; // FIXME-UTF8: what to use as failure character?
68 if ( wxConvLibc
.FromWChar(buf
, 2, &in
, 1) != 2 )
69 return '?'; // FIXME-UTF8: what to use as failure character?
75 // ---------------------------------------------------------------------------
77 // ---------------------------------------------------------------------------
79 #if wxUSE_UNICODE_UTF8
80 wxUniChar
wxUniCharRef::UniChar() const
82 return wxStringOperations::DecodeChar(m_pos
);
85 wxUniCharRef
& wxUniCharRef::operator=(const wxUniChar
& c
)
87 wxStringOperations::Utf8CharBuffer
utf(wxStringOperations::EncodeChar(c
));
88 size_t lenOld
= wxStringOperations::GetUtf8CharLength(*m_pos
);
89 size_t lenNew
= wxStringOperations::GetUtf8CharLength(utf
[0]);
91 if ( lenNew
== lenOld
)
94 for ( size_t i
= 0; i
< lenNew
; ++i
, ++pos
)
99 size_t idx
= m_pos
- m_str
.begin();
101 m_str
.replace(m_pos
, m_pos
+ lenOld
, utf
, lenNew
);
103 // this is needed to keep m_pos valid:
104 m_pos
= m_str
.begin() + idx
;
109 #endif // wxUSE_UNICODE_UTF8