use C++ compiler for va_copy test, at least under IRIX the C99 C compiler has it...
[wxWidgets.git] / src / common / unichar.cpp
CommitLineData
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 38wxUniChar::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
111d9948
VS
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?
47#else
dd0ef332
VS
48 wchar_t buf[2];
49 if ( wxConvLibc.ToWChar(buf, 2, &c, 1) != 2 )
50 return wxT('?'); // FIXME-UTF8: what to use as failure character?
51 return buf[0];
111d9948 52#endif
dd0ef332
VS
53}
54
55/* static */
d1b7ed67 56char wxUniChar::To8bit(wxUniChar::value_type c)
dd0ef332
VS
57{
58 // all supported charsets have the first 128 characters same as ASCII:
59 if ( c < 0x80 )
60 return c;
61
111d9948
VS
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?
65#else
dd0ef332
VS
66 wchar_t in = c;
67 char buf[2];
68 if ( wxConvLibc.FromWChar(buf, 2, &in, 1) != 2 )
69 return '?'; // FIXME-UTF8: what to use as failure character?
70 return buf[0];
111d9948 71#endif
dd0ef332 72}
81727065
VS
73
74
75// ---------------------------------------------------------------------------
76// wxUniCharRef
77// ---------------------------------------------------------------------------
78
79#if wxUSE_UNICODE_UTF8
467175ab
VS
80wxUniChar wxUniCharRef::UniChar() const
81{
82 return wxStringOperations::DecodeChar(m_pos);
83}
84
81727065
VS
85wxUniCharRef& wxUniCharRef::operator=(const wxUniChar& c)
86{
467175ab
VS
87 wxStringOperations::Utf8CharBuffer utf(wxStringOperations::EncodeChar(c));
88 size_t lenOld = wxStringOperations::GetUtf8CharLength(*m_pos);
89 size_t lenNew = wxStringOperations::GetUtf8CharLength(utf[0]);
81727065
VS
90
91 if ( lenNew == lenOld )
92 {
93 iterator pos(m_pos);
94 for ( size_t i = 0; i < lenNew; ++i, ++pos )
95 *pos = utf[i];
96 }
97 else
98 {
99 size_t idx = m_pos - m_str.begin();
100
101 m_str.replace(m_pos, m_pos + lenOld, utf, lenNew);
102
103 // this is needed to keep m_pos valid:
104 m_pos = m_str.begin() + idx;
105 }
106
107 return *this;
108}
109#endif // wxUSE_UNICODE_UTF8