]>
Commit | Line | Data |
---|---|---|
dd0ef332 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/common/unichar.cpp | |
3 | // Purpose: wxUniChar and wxUniCharRef classes | |
4 | // Author: Vaclav Slavik | |
5 | // Created: 2007-03-19 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2007 REA Elektronik GmbH | |
8 | // Licence: wxWindows licence | |
9 | /////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // =========================================================================== | |
12 | // headers | |
13 | // =========================================================================== | |
14 | ||
15 | // For compilers that support precompilation, includes "wx.h". | |
16 | #include "wx/wxprec.h" | |
17 | ||
18 | #ifdef __BORLANDC__ | |
19 | #pragma hdrstop | |
20 | #endif | |
21 | ||
0cb6a6e4 VZ |
22 | #ifndef WX_PRECOMP |
23 | #include "wx/strconv.h" // wxConvLibc | |
24 | #endif | |
25 | ||
dd0ef332 | 26 | #include "wx/unichar.h" |
467175ab | 27 | #include "wx/stringops.h" |
81727065 | 28 | |
dd0ef332 VS |
29 | // =========================================================================== |
30 | // implementation | |
31 | // =========================================================================== | |
32 | ||
81727065 VS |
33 | // --------------------------------------------------------------------------- |
34 | // wxUniChar | |
35 | // --------------------------------------------------------------------------- | |
36 | ||
dd0ef332 | 37 | /* static */ |
d1b7ed67 | 38 | wxUniChar::value_type wxUniChar::From8bit(char c) |
dd0ef332 VS |
39 | { |
40 | // all supported charsets have the first 128 characters same as ASCII: | |
41 | if ( (unsigned char)c < 0x80 ) | |
42 | return c; | |
43 | ||
44 | wchar_t buf[2]; | |
45 | if ( wxConvLibc.ToWChar(buf, 2, &c, 1) != 2 ) | |
46 | return wxT('?'); // FIXME-UTF8: what to use as failure character? | |
47 | return buf[0]; | |
48 | } | |
49 | ||
50 | /* static */ | |
d1b7ed67 | 51 | char wxUniChar::To8bit(wxUniChar::value_type c) |
dd0ef332 VS |
52 | { |
53 | // all supported charsets have the first 128 characters same as ASCII: | |
54 | if ( c < 0x80 ) | |
55 | return c; | |
56 | ||
57 | wchar_t in = c; | |
58 | char buf[2]; | |
59 | if ( wxConvLibc.FromWChar(buf, 2, &in, 1) != 2 ) | |
60 | return '?'; // FIXME-UTF8: what to use as failure character? | |
61 | return buf[0]; | |
62 | } | |
81727065 VS |
63 | |
64 | ||
65 | // --------------------------------------------------------------------------- | |
66 | // wxUniCharRef | |
67 | // --------------------------------------------------------------------------- | |
68 | ||
69 | #if wxUSE_UNICODE_UTF8 | |
467175ab VS |
70 | wxUniChar wxUniCharRef::UniChar() const |
71 | { | |
72 | return wxStringOperations::DecodeChar(m_pos); | |
73 | } | |
74 | ||
81727065 VS |
75 | wxUniCharRef& wxUniCharRef::operator=(const wxUniChar& c) |
76 | { | |
467175ab VS |
77 | wxStringOperations::Utf8CharBuffer utf(wxStringOperations::EncodeChar(c)); |
78 | size_t lenOld = wxStringOperations::GetUtf8CharLength(*m_pos); | |
79 | size_t lenNew = wxStringOperations::GetUtf8CharLength(utf[0]); | |
81727065 VS |
80 | |
81 | if ( lenNew == lenOld ) | |
82 | { | |
83 | iterator pos(m_pos); | |
84 | for ( size_t i = 0; i < lenNew; ++i, ++pos ) | |
85 | *pos = utf[i]; | |
86 | } | |
87 | else | |
88 | { | |
89 | size_t idx = m_pos - m_str.begin(); | |
90 | ||
91 | m_str.replace(m_pos, m_pos + lenOld, utf, lenNew); | |
92 | ||
93 | // this is needed to keep m_pos valid: | |
94 | m_pos = m_str.begin() + idx; | |
95 | } | |
96 | ||
97 | return *this; | |
98 | } | |
99 | #endif // wxUSE_UNICODE_UTF8 |