1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/stringops.h
3 // Purpose: implementation of wxString primitive operations
4 // Author: Vaclav Slavik
8 // Copyright: (c) 2007 REA Elektronik GmbH
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_WXSTRINGOPS_H__
13 #define _WX_WXSTRINGOPS_H__
15 #include "wx/chartype.h"
16 #include "wx/stringimpl.h"
17 #include "wx/unichar.h"
19 // This header contains wxStringOperations "namespace" class that implements
20 // elementary operations on string data as static methods; wxString methods and
21 // iterators are implemented in terms of it. Two implementations are available,
22 // one for UTF-8 encoded char* string and one for "raw" wchar_t* strings (or
23 // char* in ANSI build).
25 // FIXME-UTF8: only wchar after we remove ANSI build
26 #if wxUSE_UNICODE_WCHAR || !wxUSE_UNICODE
27 struct WXDLLIMPEXP_BASE wxStringOperationsWchar
29 // moves the iterator to the next Unicode character
30 static void IncIter(wxStringImpl::iterator
& i
) { ++i
; }
31 static void IncIter(wxStringImpl::const_iterator
& i
) { ++i
; }
33 // moves the iterator to the previous Unicode character
34 static void DecIter(wxStringImpl::iterator
& i
) { --i
; }
35 static void DecIter(wxStringImpl::const_iterator
& i
) { --i
; }
37 // moves the iterator by n Unicode characters
38 static wxStringImpl::iterator
AddToIter(const wxStringImpl::iterator
& i
, int n
)
40 static wxStringImpl::const_iterator
AddToIter(const wxStringImpl::const_iterator
& i
, int n
)
42 static const wxChar
* AddToIter(const wxChar
*i
, int n
)
45 // returns distance of the two iterators in Unicode characters
46 static int DiffIters(const wxStringImpl::iterator
& i1
,
47 const wxStringImpl::iterator
& i2
)
49 static int DiffIters(const wxStringImpl::const_iterator
& i1
,
50 const wxStringImpl::const_iterator
& i2
)
53 // encodes the character to a form used to represent it in internal
54 // representation (returns a string in UTF8 version)
55 static wxChar
EncodeChar(const wxUniChar
& ch
) { return (wxChar
)ch
; }
57 static wxUniChar
DecodeChar(const wxStringImpl::const_iterator
& i
)
60 #endif // wxUSE_UNICODE_WCHAR || !wxUSE_UNICODE
63 #if wxUSE_UNICODE_UTF8
64 struct WXDLLIMPEXP_BASE wxStringOperationsUtf8
66 // checks correctness of UTF-8 sequence
67 static bool IsValidUtf8String(const char *c
);
69 static bool IsValidUtf8LeadByte(unsigned char c
);
72 // table of offsets to skip forward when iterating over UTF-8 sequence
73 static unsigned char ms_utf8IterTable
[256];
76 template<typename Iterator
>
77 static void IncIter(Iterator
& i
)
79 wxASSERT( IsValidUtf8LeadByte(*i
) );
80 i
+= ms_utf8IterTable
[(unsigned char)*i
];
83 template<typename Iterator
>
84 static void DecIter(Iterator
& i
)
86 wxASSERT( IsValidUtf8LeadByte(*i
) );
88 // Non-lead bytes are all in the 0x80..0xBF range (i.e. 10xxxxxx in
89 // binary), so we just have to go back until we hit a byte that is
90 // either < 0x80 (i.e. 0xxxxxxx in binary) or 0xC0..0xFF (11xxxxxx in
91 // binary; this includes some invalid values, but we can ignore it
92 // here, because we assume valid UTF-8 input for the purpose of
93 // efficient implementation).
95 while ( ((*i
) & 0xC0) == 0x80 /* 2 highest bits are '10' */ )
99 template<typename Iterator
>
100 static Iterator
AddToIter(const Iterator
& i
, int n
)
106 for ( int j
= 0; j
< n
; ++j
)
111 for ( int j
= 0; j
> n
; --j
)
118 template<typename Iterator
>
119 static int DiffIters(Iterator i1
, Iterator i2
)
143 // buffer for single UTF-8 character
144 struct Utf8CharBuffer
147 operator const char*() const { return data
; }
150 // encodes the character as UTF-8:
151 static Utf8CharBuffer
EncodeChar(const wxUniChar
& ch
);
153 // returns n copies of ch encoded in UTF-8 string
154 static wxCharBuffer
EncodeNChars(size_t n
, const wxUniChar
& ch
);
156 // returns the length of UTF-8 encoding of the character with lead byte 'c'
157 static size_t GetUtf8CharLength(char c
)
159 wxASSERT( IsValidUtf8LeadByte(c
) );
160 return ms_utf8IterTable
[(unsigned char)c
];
163 // decodes single UTF-8 character from UTF-8 string
164 static wxUniChar
DecodeChar(wxStringImpl::const_iterator i
);
166 #endif // wxUSE_UNICODE_UTF8
169 #if wxUSE_UNICODE_UTF8
170 typedef wxStringOperationsUtf8 wxStringOperations
;
172 typedef wxStringOperationsWchar wxStringOperations
;
175 #endif // _WX_WXSTRINGOPS_H_