]> git.saurik.com Git - wxWidgets.git/blob - src/common/unichar.cpp
initial version of UTF-8 strings representation (still converting to wchar_t* a lot...
[wxWidgets.git] / src / common / unichar.cpp
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
22 #ifndef WX_PRECOMP
23 #include "wx/strconv.h" // wxConvLibc
24 #endif
25
26 #include "wx/unichar.h"
27
28 // FIXME-UTF8: remove once UTF-8 functions moved outside
29 #include "wx/string.h"
30
31 // ===========================================================================
32 // implementation
33 // ===========================================================================
34
35 // ---------------------------------------------------------------------------
36 // wxUniChar
37 // ---------------------------------------------------------------------------
38
39 /* static */
40 wxUniChar::value_type wxUniChar::From8bit(char c)
41 {
42 // all supported charsets have the first 128 characters same as ASCII:
43 if ( (unsigned char)c < 0x80 )
44 return c;
45
46 wchar_t buf[2];
47 if ( wxConvLibc.ToWChar(buf, 2, &c, 1) != 2 )
48 return wxT('?'); // FIXME-UTF8: what to use as failure character?
49 return buf[0];
50 }
51
52 /* static */
53 char wxUniChar::To8bit(wxUniChar::value_type c)
54 {
55 // all supported charsets have the first 128 characters same as ASCII:
56 if ( c < 0x80 )
57 return c;
58
59 wchar_t in = c;
60 char buf[2];
61 if ( wxConvLibc.FromWChar(buf, 2, &in, 1) != 2 )
62 return '?'; // FIXME-UTF8: what to use as failure character?
63 return buf[0];
64 }
65
66
67 // ---------------------------------------------------------------------------
68 // wxUniCharRef
69 // ---------------------------------------------------------------------------
70
71 #if wxUSE_UNICODE_UTF8
72 wxUniCharRef& wxUniCharRef::operator=(const wxUniChar& c)
73 {
74 wxString::Utf8CharBuffer utf(wxString::EncodeChar(c));
75 size_t lenOld = wxString::GetUtf8CharLength(*m_pos);
76 size_t lenNew = wxString::GetUtf8CharLength(utf[0]);
77
78 if ( lenNew == lenOld )
79 {
80 iterator pos(m_pos);
81 for ( size_t i = 0; i < lenNew; ++i, ++pos )
82 *pos = utf[i];
83 }
84 else
85 {
86 size_t idx = m_pos - m_str.begin();
87
88 m_str.replace(m_pos, m_pos + lenOld, utf, lenNew);
89
90 // this is needed to keep m_pos valid:
91 m_pos = m_str.begin() + idx;
92 }
93
94 return *this;
95 }
96 #endif // wxUSE_UNICODE_UTF8