3 // Purpose: 32-bit string (UCS-4)
4 // Author: Robert Roebling
5 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #ifndef _WX_USTRING_H_
11 #define _WX_USTRING_H_
14 #include "wx/string.h"
18 #if SIZEOF_WCHAR_T == 2
19 typedef wxWCharBuffer wxU16CharBuffer
;
20 typedef wxScopedWCharBuffer wxScopedU16CharBuffer
;
22 typedef wxCharTypeBuffer
<wxChar16
> wxU16CharBuffer
;
23 typedef wxScopedCharTypeBuffer
<wxChar16
> wxScopedU16CharBuffer
;
26 #if SIZEOF_WCHAR_T == 4
27 typedef wxWCharBuffer wxU32CharBuffer
;
28 typedef wxScopedWCharBuffer wxScopedU32CharBuffer
;
30 typedef wxCharTypeBuffer
<wxChar32
> wxU32CharBuffer
;
31 typedef wxScopedCharTypeBuffer
<wxChar32
> wxScopedU32CharBuffer
;
35 // "non dll-interface class 'std::basic_string<wxChar32>' used as base
36 // interface for dll-interface class 'wxString'" -- this is OK in our case
37 // (and warning is unavoidable anyhow)
39 #pragma warning(disable:4275)
42 class WXDLLIMPEXP_BASE wxUString
: public std::basic_string
<wxChar32
>
47 wxUString( const wxChar32
*str
) { assign(str
); }
48 wxUString( const wxUString
&str
) { assign(str
); }
49 wxUString( const wxScopedU32CharBuffer
&buf
) { assign(buf
); }
51 wxUString( const char *str
) { assign(str
); }
52 wxUString( const wxScopedCharBuffer
&buf
) { assign(buf
); }
53 wxUString( const char *str
, const wxMBConv
&conv
) { assign(str
,conv
); }
54 wxUString( const wxScopedCharBuffer
&buf
, const wxMBConv
&conv
) { assign(buf
,conv
); }
56 wxUString( const wxChar16
*str
) { assign(str
); }
57 wxUString( const wxScopedU16CharBuffer
&buf
) { assign(buf
); }
59 wxUString( const wxCStrData
*cstr
) { assign(cstr
); }
60 wxUString( const wxString
&str
) { assign(str
); }
62 wxUString( char ch
) { assign(ch
); }
63 wxUString( wxChar16 ch
) { assign(ch
); }
64 wxUString( wxChar32 ch
) { assign(ch
); }
65 wxUString( wxUniChar ch
) { assign(ch
); }
66 wxUString( wxUniCharRef ch
) { assign(ch
); }
67 wxUString( size_type n
, char ch
) { assign(n
,ch
); }
68 wxUString( size_type n
, wxChar16 ch
) { assign(n
,ch
); }
69 wxUString( size_type n
, wxChar32 ch
) { assign(n
,ch
); }
70 wxUString( size_type n
, wxUniChar ch
) { assign(n
,ch
); }
71 wxUString( size_type n
, wxUniCharRef ch
) { assign(n
,ch
); }
73 // static construction
75 static wxUString
FromAscii( const char *str
, size_type n
)
78 ret
.assignFromAscii( str
, n
);
82 static wxUString
FromAscii( const char *str
)
85 ret
.assignFromAscii( str
);
89 static wxUString
FromUTF8( const char *str
, size_type n
)
92 ret
.assignFromUTF8( str
, n
);
96 static wxUString
FromUTF8( const char *str
)
99 ret
.assignFromUTF8( str
);
103 static wxUString
FromUTF16( const wxChar16
*str
, size_type n
)
106 ret
.assignFromUTF16( str
, n
);
110 static wxUString
FromUTF16( const wxChar16
*str
)
113 ret
.assignFromUTF16( str
);
117 // assign from encoding
119 wxUString
&assignFromAscii( const char *str
);
120 wxUString
&assignFromAscii( const char *str
, size_type n
);
121 wxUString
&assignFromUTF8( const char *str
);
122 wxUString
&assignFromUTF8( const char *str
, size_type n
);
123 wxUString
&assignFromUTF16( const wxChar16
* str
);
124 wxUString
&assignFromUTF16( const wxChar16
* str
, size_type n
);
125 wxUString
&assignFromCString( const char* str
);
126 wxUString
&assignFromCString( const char* str
, const wxMBConv
&conv
);
130 wxScopedCharBuffer
utf8_str() const;
131 wxScopedU16CharBuffer
utf16_str() const;
133 #if SIZEOF_WCHAR_T == 2
134 wxScopedWCharBuffer
wc_str() const
139 wchar_t *wc_str() const
141 return (wchar_t*) c_str();
145 operator wxString() const
147 #if wxUSE_UNICODE_UTF8
148 return wxString::FromUTF8( utf8_str() );
150 #if SIZEOF_WCHAR_T == 2
151 return wxString( utf16_str() );
153 return wxString( c_str() );
158 #if wxUSE_UNICODE_UTF8
159 wxScopedCharBuffer
wx_str()
164 #if SIZEOF_WCHAR_T == 2
165 wxScopedWCharBuffer
wx_str()
170 const wchar_t* wx_str()
179 wxUString
&assign( const wxChar32
* str
)
181 std::basic_string
<wxChar32
> *base
= this;
182 return (wxUString
&) base
->assign( str
);
185 wxUString
&assign( const wxChar32
* str
, size_type n
)
187 std::basic_string
<wxChar32
> *base
= this;
188 return (wxUString
&) base
->assign( str
, n
);
191 wxUString
&assign( const wxUString
&str
)
193 std::basic_string
<wxChar32
> *base
= this;
194 return (wxUString
&) base
->assign( str
);
197 wxUString
&assign( const wxUString
&str
, size_type pos
, size_type n
)
199 std::basic_string
<wxChar32
> *base
= this;
200 return (wxUString
&) base
->assign( str
, pos
, n
);
203 // FIXME-VC6: VC 6.0 stl does not support all types of assign functions
205 wxUString
&assign( wxChar32 ch
)
209 std::basic_string
<wxChar32
> *base
= this;
210 return (wxUString
&)base
->assign(chh
);
213 wxUString
&assign( size_type n
, wxChar32 ch
)
215 wxU32CharBuffer
buffer(n
);
216 wxChar32
*p
= buffer
.data();
218 for (i
= 0; i
< n
; i
++)
224 std::basic_string
<wxChar32
> *base
= this;
225 return (wxUString
&)base
->assign(buffer
.data());
228 wxUString
&assign( wxChar32 ch
)
230 std::basic_string
<wxChar32
> *base
= this;
231 return (wxUString
&) base
->assign( (size_type
) 1, ch
);
234 wxUString
&assign( size_type n
, wxChar32 ch
)
236 std::basic_string
<wxChar32
> *base
= this;
237 return (wxUString
&) base
->assign( n
, ch
);
239 #endif // __VISUALC6__
241 wxUString
&assign( const wxScopedU32CharBuffer
&buf
)
243 return assign( buf
.data() );
246 wxUString
&assign( const char *str
)
248 return assignFromCString( str
);
251 wxUString
&assign( const wxScopedCharBuffer
&buf
)
253 return assignFromCString( buf
.data() );
256 wxUString
&assign( const char *str
, const wxMBConv
&conv
)
258 return assignFromCString( str
, conv
);
261 wxUString
&assign( const wxScopedCharBuffer
&buf
, const wxMBConv
&conv
)
263 return assignFromCString( buf
.data(), conv
);
266 wxUString
&assign( const wxChar16
*str
)
268 return assignFromUTF16( str
);
271 wxUString
&assign( const wxScopedU16CharBuffer
&buf
)
273 return assignFromUTF16( buf
.data() );
276 wxUString
&assign( const wxCStrData
*cstr
)
278 #if SIZEOF_WCHAR_T == 2
279 return assignFromUTF16( cstr
->AsWChar() );
281 return assign( cstr
->AsWChar() );
285 wxUString
&assign( const wxString
&str
)
287 #if wxUSE_UNICODE_UTF8
288 return assignFromUTF8( str
.wx_str() );
290 #if SIZEOF_WCHAR_T == 2
291 return assignFromUTF16( str
.wc_str() );
293 return assign( str
.wc_str() );
298 wxUString
&assign( char ch
)
303 return assignFromCString( buf
);
306 wxUString
&assign( size_type n
, char ch
)
308 wxCharBuffer
buffer(n
);
309 char *p
= buffer
.data();
311 for (i
= 0; i
< n
; i
++)
316 return assignFromCString( buffer
.data() );
319 wxUString
&assign( wxChar16 ch
)
324 return assignFromUTF16( buf
);
327 wxUString
&assign( size_type n
, wxChar16 ch
)
329 wxU16CharBuffer
buffer(n
);
330 wxChar16
*p
= buffer
.data();
332 for (i
= 0; i
< n
; i
++)
337 return assignFromUTF16( buffer
.data() );
340 wxUString
&assign( wxUniChar ch
)
342 return assign( (wxChar32
) ch
.GetValue() );
345 wxUString
&assign( size_type n
, wxUniChar ch
)
347 return assign( n
, (wxChar32
) ch
.GetValue() );
350 wxUString
&assign( wxUniCharRef ch
)
352 return assign( (wxChar32
) ch
.GetValue() );
355 wxUString
&assign( size_type n
, wxUniCharRef ch
)
357 return assign( n
, (wxChar32
) ch
.GetValue() );
360 // append [STL overload]
362 wxUString
&append( const wxUString
&s
)
364 std::basic_string
<wxChar32
> *base
= this;
365 return (wxUString
&) base
->append( s
);
368 wxUString
&append( const wxUString
&s
, size_type pos
, size_type n
)
370 std::basic_string
<wxChar32
> *base
= this;
371 return (wxUString
&) base
->append( s
, pos
, n
);
374 wxUString
&append( const wxChar32
* s
)
376 std::basic_string
<wxChar32
> *base
= this;
377 return (wxUString
&) base
->append( s
);
380 wxUString
&append( const wxChar32
* s
, size_type n
)
382 std::basic_string
<wxChar32
> *base
= this;
383 return (wxUString
&) base
->append( s
, n
);
386 // FIXME-VC6: VC 6.0 stl does not support all types of append functions
388 wxUString
&append( size_type n
, wxChar32 c
)
390 wxU32CharBuffer
buffer(n
);
391 wxChar32
*p
= buffer
.data();
393 for (i
= 0; i
< n
; i
++)
399 std::basic_string
<wxChar32
> *base
= this;
400 return (wxUString
&) base
->append(buffer
.data());
403 wxUString
&append( size_type n
, wxChar32 c
)
405 std::basic_string
<wxChar32
> *base
= this;
406 return (wxUString
&) base
->append( n
, c
);
408 #endif // __VISUALC6__
410 wxUString
&append( wxChar32 c
)
412 std::basic_string
<wxChar32
> *base
= this;
413 return (wxUString
&) base
->append( 1, c
);
416 // append [wx overload]
418 wxUString
&append( const wxScopedU16CharBuffer
&buf
)
420 return append( buf
.data() );
423 wxUString
&append( const wxScopedU32CharBuffer
&buf
)
425 return append( buf
.data() );
428 wxUString
&append( const char *str
)
430 return append( wxUString( str
) );
433 wxUString
&append( const wxScopedCharBuffer
&buf
)
435 return append( wxUString( buf
) );
438 wxUString
&append( const wxChar16
*str
)
440 return append( wxUString( str
) );
443 wxUString
&append( const wxString
&str
)
445 return append( wxUString( str
) );
448 wxUString
&append( const wxCStrData
*cstr
)
450 return append( wxUString( cstr
) );
453 wxUString
&append( char ch
)
458 return append( buf
);
461 wxUString
&append( wxChar16 ch
)
466 return append( buf
);
469 wxUString
&append( wxUniChar ch
)
471 return append( (size_type
) 1, (wxChar32
) ch
.GetValue() );
474 wxUString
&append( wxUniCharRef ch
)
476 return append( (size_type
) 1, (wxChar32
) ch
.GetValue() );
480 // insert [STL overloads]
482 wxUString
&insert( size_type pos
, const wxUString
&s
)
484 std::basic_string
<wxChar32
> *base
= this;
485 return (wxUString
&) base
->insert( pos
, s
);
488 wxUString
&insert( size_type pos
, const wxUString
&s
, size_type pos1
, size_type n
)
490 std::basic_string
<wxChar32
> *base
= this;
491 return (wxUString
&) base
->insert( pos
, s
, pos1
, n
);
494 wxUString
&insert( size_type pos
, const wxChar32
*s
)
496 std::basic_string
<wxChar32
> *base
= this;
497 return (wxUString
&) base
->insert( pos
, s
);
500 wxUString
&insert( size_type pos
, const wxChar32
*s
, size_type n
)
502 std::basic_string
<wxChar32
> *base
= this;
503 return (wxUString
&) base
->insert( pos
, s
, n
);
506 wxUString
&insert( size_type pos
, size_type n
, wxChar32 c
)
508 std::basic_string
<wxChar32
> *base
= this;
509 return (wxUString
&) base
->insert( pos
, n
, c
);
513 // insert [STL overloads]
515 wxUString
&insert( size_type n
, const char *s
)
517 return insert( n
, wxUString( s
) );
520 wxUString
&insert( size_type n
, const wxChar16
*s
)
522 return insert( n
, wxUString( s
) );
525 wxUString
&insert( size_type n
, const wxScopedCharBuffer
&buf
)
527 return insert( n
, wxUString( buf
) );
530 wxUString
&insert( size_type n
, const wxScopedU16CharBuffer
&buf
)
532 return insert( n
, wxUString( buf
) );
535 wxUString
&insert( size_type n
, const wxScopedU32CharBuffer
&buf
)
537 return insert( n
, buf
.data() );
540 wxUString
&insert( size_type n
, const wxString
&s
)
542 return insert( n
, wxUString( s
) );
545 wxUString
&insert( size_type n
, const wxCStrData
*cstr
)
547 return insert( n
, wxUString( cstr
) );
550 wxUString
&insert( size_type n
, char ch
)
555 return insert( n
, buf
);
558 wxUString
&insert( size_type n
, wchar_t ch
)
563 return insert( n
, buf
);
568 iterator
insert( iterator it
, wxChar32 ch
)
570 std::basic_string
<wxChar32
> *base
= this;
571 return base
->insert( it
, ch
);
574 void insert(iterator it
, const_iterator first
, const_iterator last
)
576 std::basic_string
<wxChar32
> *base
= this;
577 base
->insert( it
, first
, last
);
582 wxUString
& operator=(const wxUString
& s
)
583 { return assign( s
); }
584 wxUString
& operator=(const wxString
& s
)
585 { return assign( s
); }
586 wxUString
& operator=(const wxCStrData
* s
)
587 { return assign( s
); }
588 wxUString
& operator=(const char *s
)
589 { return assign( s
); }
590 wxUString
& operator=(const wxChar16
*s
)
591 { return assign( s
); }
592 wxUString
& operator=(const wxChar32
*s
)
593 { return assign( s
); }
594 wxUString
& operator=(const wxScopedCharBuffer
&s
)
595 { return assign( s
); }
596 wxUString
& operator=(const wxScopedU16CharBuffer
&s
)
597 { return assign( s
); }
598 wxUString
& operator=(const wxScopedU32CharBuffer
&s
)
599 { return assign( s
); }
600 wxUString
& operator=(const char ch
)
601 { return assign( ch
); }
602 wxUString
& operator=(const wxChar16 ch
)
603 { return assign( ch
); }
604 wxUString
& operator=(const wxChar32 ch
)
605 { return assign( ch
); }
606 wxUString
& operator=(const wxUniChar ch
)
607 { return assign( ch
); }
608 wxUString
& operator=(const wxUniCharRef ch
)
609 { return assign( ch
); }
612 wxUString
& operator+=(const wxUString
& s
)
613 { return append( s
); }
614 wxUString
& operator+=(const wxString
& s
)
615 { return append( s
); }
616 wxUString
& operator+=(const wxCStrData
* s
)
617 { return append( s
); }
618 wxUString
& operator+=(const char *s
)
619 { return append( s
); }
620 wxUString
& operator+=(const wxChar16
*s
)
621 { return append( s
); }
622 wxUString
& operator+=(const wxChar32
*s
)
623 { return append( s
); }
624 wxUString
& operator+=(const wxScopedCharBuffer
&s
)
625 { return append( s
); }
626 wxUString
& operator+=(const wxScopedU16CharBuffer
&s
)
627 { return append( s
); }
628 wxUString
& operator+=(const wxScopedU32CharBuffer
&s
)
629 { return append( s
); }
630 wxUString
& operator+=(const char ch
)
631 { return append( ch
); }
632 wxUString
& operator+=(const wxChar16 ch
)
633 { return append( ch
); }
634 wxUString
& operator+=(const wxChar32 ch
)
635 { return append( ch
); }
636 wxUString
& operator+=(const wxUniChar ch
)
637 { return append( ch
); }
638 wxUString
& operator+=(const wxUniCharRef ch
)
639 { return append( ch
); }
647 inline wxUString
operator+(const wxUString
&s1
, const wxUString
&s2
)
648 { wxUString
ret( s1
); ret
.append( s2
); return ret
; }
649 inline wxUString
operator+(const wxUString
&s1
, const char *s2
)
650 { return s1
+ wxUString(s2
); }
651 inline wxUString
operator+(const wxUString
&s1
, const wxString
&s2
)
652 { return s1
+ wxUString(s2
); }
653 inline wxUString
operator+(const wxUString
&s1
, const wxCStrData
*s2
)
654 { return s1
+ wxUString(s2
); }
655 inline wxUString
operator+(const wxUString
&s1
, const wxChar16
* s2
)
656 { return s1
+ wxUString(s2
); }
657 inline wxUString
operator+(const wxUString
&s1
, const wxChar32
*s2
)
658 { return s1
+ wxUString(s2
); }
659 inline wxUString
operator+(const wxUString
&s1
, const wxScopedCharBuffer
&s2
)
660 { return s1
+ wxUString(s2
); }
661 inline wxUString
operator+(const wxUString
&s1
, const wxScopedU16CharBuffer
&s2
)
662 { return s1
+ wxUString(s2
); }
663 inline wxUString
operator+(const wxUString
&s1
, const wxScopedU32CharBuffer
&s2
)
664 { return s1
+ wxUString(s2
); }
665 inline wxUString
operator+(const wxUString
&s1
, char s2
)
666 { return s1
+ wxUString(s2
); }
667 inline wxUString
operator+(const wxUString
&s1
, wxChar32 s2
)
668 { wxUString
ret( s1
); ret
.append( s2
); return ret
; }
669 inline wxUString
operator+(const wxUString
&s1
, wxChar16 s2
)
670 { wxUString
ret( s1
); ret
.append( (wxChar32
) s2
); return ret
; }
671 inline wxUString
operator+(const wxUString
&s1
, wxUniChar s2
)
672 { wxUString
ret( s1
); ret
.append( (wxChar32
) s2
.GetValue() ); return ret
; }
673 inline wxUString
operator+(const wxUString
&s1
, wxUniCharRef s2
)
674 { wxUString
ret( s1
); ret
.append( (wxChar32
) s2
.GetValue() ); return ret
; }
676 inline wxUString
operator+(const char *s1
, const wxUString
&s2
)
677 { return wxUString(s1
) + s2
; }
678 inline wxUString
operator+(const wxString
&s1
, const wxUString
&s2
)
679 { return wxUString(s1
) + s2
; }
680 inline wxUString
operator+(const wxCStrData
*s1
, const wxUString
&s2
)
681 { return wxUString(s1
) + s2
; }
682 inline wxUString
operator+(const wxChar16
* s1
, const wxUString
&s2
)
683 { return wxUString(s1
) + s2
; }
684 inline wxUString
operator+(const wxChar32
*s1
, const wxUString
&s2
)
685 { return wxUString(s1
) + s2
; }
686 inline wxUString
operator+(const wxScopedCharBuffer
&s1
, const wxUString
&s2
)
687 { return wxUString(s1
) + s2
; }
688 inline wxUString
operator+(const wxScopedU16CharBuffer
&s1
, const wxUString
&s2
)
689 { return wxUString(s1
) + s2
; }
690 inline wxUString
operator+(const wxScopedU32CharBuffer
&s1
, const wxUString
&s2
)
691 { return wxUString(s1
) + s2
; }
692 inline wxUString
operator+(char s1
, const wxUString
&s2
)
693 { return wxUString(s1
) + s2
; }
694 inline wxUString
operator+(wxChar32 s1
, const wxUString
&s2
)
695 { return wxUString(s1
) + s2
; }
696 inline wxUString
operator+(wxChar16 s1
, const wxUString
&s2
)
697 { return wxUString(s1
) + s2
; }
698 inline wxUString
operator+(wxUniChar s1
, const wxUString
&s2
)
699 { return wxUString(s1
) + s2
; }
700 inline wxUString
operator+(wxUniCharRef s1
, const wxUString
&s2
)
701 { return wxUString(s1
) + s2
; }
704 inline bool operator==(const wxUString
& s1
, const wxUString
& s2
)
705 { return s1
.compare( s2
) == 0; }
706 inline bool operator!=(const wxUString
& s1
, const wxUString
& s2
)
707 { return s1
.compare( s2
) != 0; }
708 inline bool operator< (const wxUString
& s1
, const wxUString
& s2
)
709 { return s1
.compare( s2
) < 0; }
710 inline bool operator> (const wxUString
& s1
, const wxUString
& s2
)
711 { return s1
.compare( s2
) > 0; }
712 inline bool operator<=(const wxUString
& s1
, const wxUString
& s2
)
713 { return s1
.compare( s2
) <= 0; }
714 inline bool operator>=(const wxUString
& s1
, const wxUString
& s2
)
715 { return s1
.compare( s2
) >= 0; }
717 #define wxUSTRING_COMP_OPERATORS( T ) \
718 inline bool operator==(const wxUString& s1, T s2) \
719 { return s1.compare( wxUString(s2) ) == 0; } \
720 inline bool operator!=(const wxUString& s1, T s2) \
721 { return s1.compare( wxUString(s2) ) != 0; } \
722 inline bool operator< (const wxUString& s1, T s2) \
723 { return s1.compare( wxUString(s2) ) < 0; } \
724 inline bool operator> (const wxUString& s1, T s2) \
725 { return s1.compare( wxUString(s2) ) > 0; } \
726 inline bool operator<=(const wxUString& s1, T s2) \
727 { return s1.compare( wxUString(s2) ) <= 0; } \
728 inline bool operator>=(const wxUString& s1, T s2) \
729 { return s1.compare( wxUString(s2) ) >= 0; } \
731 inline bool operator==(T s2, const wxUString& s1) \
732 { return s1.compare( wxUString(s2) ) == 0; } \
733 inline bool operator!=(T s2, const wxUString& s1) \
734 { return s1.compare( wxUString(s2) ) != 0; } \
735 inline bool operator< (T s2, const wxUString& s1) \
736 { return s1.compare( wxUString(s2) ) > 0; } \
737 inline bool operator> (T s2, const wxUString& s1) \
738 { return s1.compare( wxUString(s2) ) < 0; } \
739 inline bool operator<=(T s2, const wxUString& s1) \
740 { return s1.compare( wxUString(s2) ) >= 0; } \
741 inline bool operator>=(T s2, const wxUString& s1) \
742 { return s1.compare( wxUString(s2) ) <= 0; }
744 wxUSTRING_COMP_OPERATORS( const wxString
& )
745 wxUSTRING_COMP_OPERATORS( const char * )
746 wxUSTRING_COMP_OPERATORS( const wxChar16
* )
747 wxUSTRING_COMP_OPERATORS( const wxChar32
* )
748 wxUSTRING_COMP_OPERATORS( const wxScopedCharBuffer
& )
749 wxUSTRING_COMP_OPERATORS( const wxScopedU16CharBuffer
& )
750 wxUSTRING_COMP_OPERATORS( const wxScopedU32CharBuffer
& )
751 wxUSTRING_COMP_OPERATORS( const wxCStrData
* )
753 #endif // _WX_USTRING_H_