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 wxScopedU32CharBuffer
&buf
) { assign(buf
); }
50 wxUString( const char *str
) { assign(str
); }
51 wxUString( const wxScopedCharBuffer
&buf
) { assign(buf
); }
52 wxUString( const char *str
, const wxMBConv
&conv
) { assign(str
,conv
); }
53 wxUString( const wxScopedCharBuffer
&buf
, const wxMBConv
&conv
) { assign(buf
,conv
); }
55 wxUString( const wxChar16
*str
) { assign(str
); }
56 wxUString( const wxScopedU16CharBuffer
&buf
) { assign(buf
); }
58 wxUString( const wxCStrData
*cstr
) { assign(cstr
); }
59 wxUString( const wxString
&str
) { assign(str
); }
61 wxUString( char ch
) { assign(ch
); }
62 wxUString( wxChar16 ch
) { assign(ch
); }
63 wxUString( wxChar32 ch
) { assign(ch
); }
64 wxUString( wxUniChar ch
) { assign(ch
); }
65 wxUString( wxUniCharRef ch
) { assign(ch
); }
66 wxUString( size_type n
, char ch
) { assign(n
,ch
); }
67 wxUString( size_type n
, wxChar16 ch
) { assign(n
,ch
); }
68 wxUString( size_type n
, wxChar32 ch
) { assign(n
,ch
); }
69 wxUString( size_type n
, wxUniChar ch
) { assign(n
,ch
); }
70 wxUString( size_type n
, wxUniCharRef ch
) { assign(n
,ch
); }
72 // static construction
74 static wxUString
FromAscii( const char *str
, size_type n
)
77 ret
.assignFromAscii( str
, n
);
81 static wxUString
FromAscii( const char *str
)
84 ret
.assignFromAscii( str
);
88 static wxUString
FromUTF8( const char *str
, size_type n
)
91 ret
.assignFromUTF8( str
, n
);
95 static wxUString
FromUTF8( const char *str
)
98 ret
.assignFromUTF8( str
);
102 static wxUString
FromUTF16( const wxChar16
*str
, size_type n
)
105 ret
.assignFromUTF16( str
, n
);
109 static wxUString
FromUTF16( const wxChar16
*str
)
112 ret
.assignFromUTF16( str
);
116 // assign from encoding
118 wxUString
&assignFromAscii( const char *str
);
119 wxUString
&assignFromAscii( const char *str
, size_type n
);
120 wxUString
&assignFromUTF8( const char *str
);
121 wxUString
&assignFromUTF8( const char *str
, size_type n
);
122 wxUString
&assignFromUTF16( const wxChar16
* str
);
123 wxUString
&assignFromUTF16( const wxChar16
* str
, size_type n
);
124 wxUString
&assignFromCString( const char* str
);
125 wxUString
&assignFromCString( const char* str
, const wxMBConv
&conv
);
129 wxScopedCharBuffer
utf8_str() const;
130 wxScopedU16CharBuffer
utf16_str() const;
132 #if SIZEOF_WCHAR_T == 2
133 wxScopedWCharBuffer
wc_str() const
138 wchar_t *wc_str() const
140 return (wchar_t*) c_str();
144 operator wxString() const
146 #if wxUSE_UNICODE_UTF8
147 return wxString::FromUTF8( utf8_str() );
149 #if SIZEOF_WCHAR_T == 2
150 return wxString( utf16_str() );
152 return wxString( c_str() );
157 #if wxUSE_UNICODE_UTF8
158 wxScopedCharBuffer
wx_str()
163 #if SIZEOF_WCHAR_T == 2
164 wxScopedWCharBuffer
wx_str()
169 const wchar_t* wx_str()
178 wxUString
&assign( const wxChar32
* str
)
180 std::basic_string
<wxChar32
> *base
= this;
181 return (wxUString
&) base
->assign( str
);
184 wxUString
&assign( const wxChar32
* str
, size_type n
)
186 std::basic_string
<wxChar32
> *base
= this;
187 return (wxUString
&) base
->assign( str
, n
);
190 wxUString
&assign( const wxUString
&str
)
192 std::basic_string
<wxChar32
> *base
= this;
193 return (wxUString
&) base
->assign( str
);
196 wxUString
&assign( const wxUString
&str
, size_type pos
, size_type n
)
198 std::basic_string
<wxChar32
> *base
= this;
199 return (wxUString
&) base
->assign( str
, pos
, n
);
202 // FIXME-VC6: VC 6.0 stl does not support all types of assign functions
204 wxUString
&assign( wxChar32 ch
)
208 std::basic_string
<wxChar32
> *base
= this;
209 return (wxUString
&)base
->assign(chh
);
212 wxUString
&assign( size_type n
, wxChar32 ch
)
214 wxU32CharBuffer
buffer(n
);
215 wxChar32
*p
= buffer
.data();
217 for (i
= 0; i
< n
; i
++)
223 std::basic_string
<wxChar32
> *base
= this;
224 return (wxUString
&)base
->assign(buffer
.data());
227 wxUString
&assign( wxChar32 ch
)
229 std::basic_string
<wxChar32
> *base
= this;
230 return (wxUString
&) base
->assign( (size_type
) 1, ch
);
233 wxUString
&assign( size_type n
, wxChar32 ch
)
235 std::basic_string
<wxChar32
> *base
= this;
236 return (wxUString
&) base
->assign( n
, ch
);
238 #endif // __VISUALC6__
240 wxUString
&assign( const wxScopedU32CharBuffer
&buf
)
242 return assign( buf
.data() );
245 wxUString
&assign( const char *str
)
247 return assignFromCString( str
);
250 wxUString
&assign( const wxScopedCharBuffer
&buf
)
252 return assignFromCString( buf
.data() );
255 wxUString
&assign( const char *str
, const wxMBConv
&conv
)
257 return assignFromCString( str
, conv
);
260 wxUString
&assign( const wxScopedCharBuffer
&buf
, const wxMBConv
&conv
)
262 return assignFromCString( buf
.data(), conv
);
265 wxUString
&assign( const wxChar16
*str
)
267 return assignFromUTF16( str
);
270 wxUString
&assign( const wxScopedU16CharBuffer
&buf
)
272 return assignFromUTF16( buf
.data() );
275 wxUString
&assign( const wxCStrData
*cstr
)
277 #if SIZEOF_WCHAR_T == 2
278 return assignFromUTF16( cstr
->AsWChar() );
280 return assign( cstr
->AsWChar() );
284 wxUString
&assign( const wxString
&str
)
286 #if wxUSE_UNICODE_UTF8
287 return assignFromUTF8( str
.wx_str() );
289 #if SIZEOF_WCHAR_T == 2
290 return assignFromUTF16( str
.wc_str() );
292 return assign( str
.wc_str() );
297 wxUString
&assign( char ch
)
302 return assignFromCString( buf
);
305 wxUString
&assign( size_type n
, char ch
)
307 wxCharBuffer
buffer(n
);
308 char *p
= buffer
.data();
310 for (i
= 0; i
< n
; i
++)
315 return assignFromCString( buffer
.data() );
318 wxUString
&assign( wxChar16 ch
)
323 return assignFromUTF16( buf
);
326 wxUString
&assign( size_type n
, wxChar16 ch
)
328 wxU16CharBuffer
buffer(n
);
329 wxChar16
*p
= buffer
.data();
331 for (i
= 0; i
< n
; i
++)
336 return assignFromUTF16( buffer
.data() );
339 wxUString
&assign( wxUniChar ch
)
341 return assign( (wxChar32
) ch
.GetValue() );
344 wxUString
&assign( size_type n
, wxUniChar ch
)
346 return assign( n
, (wxChar32
) ch
.GetValue() );
349 wxUString
&assign( wxUniCharRef ch
)
351 return assign( (wxChar32
) ch
.GetValue() );
354 wxUString
&assign( size_type n
, wxUniCharRef ch
)
356 return assign( n
, (wxChar32
) ch
.GetValue() );
359 // append [STL overload]
361 wxUString
&append( const wxUString
&s
)
363 std::basic_string
<wxChar32
> *base
= this;
364 return (wxUString
&) base
->append( s
);
367 wxUString
&append( const wxUString
&s
, size_type pos
, size_type n
)
369 std::basic_string
<wxChar32
> *base
= this;
370 return (wxUString
&) base
->append( s
, pos
, n
);
373 wxUString
&append( const wxChar32
* s
)
375 std::basic_string
<wxChar32
> *base
= this;
376 return (wxUString
&) base
->append( s
);
379 wxUString
&append( const wxChar32
* s
, size_type n
)
381 std::basic_string
<wxChar32
> *base
= this;
382 return (wxUString
&) base
->append( s
, n
);
385 // FIXME-VC6: VC 6.0 stl does not support all types of append functions
387 wxUString
&append( size_type n
, wxChar32 c
)
389 wxU32CharBuffer
buffer(n
);
390 wxChar32
*p
= buffer
.data();
392 for (i
= 0; i
< n
; i
++)
398 std::basic_string
<wxChar32
> *base
= this;
399 return (wxUString
&) base
->append(buffer
.data());
402 wxUString
&append( size_type n
, wxChar32 c
)
404 std::basic_string
<wxChar32
> *base
= this;
405 return (wxUString
&) base
->append( n
, c
);
407 #endif // __VISUALC6__
409 wxUString
&append( wxChar32 c
)
411 std::basic_string
<wxChar32
> *base
= this;
412 return (wxUString
&) base
->append( 1, c
);
415 // append [wx overload]
417 wxUString
&append( const wxScopedU16CharBuffer
&buf
)
419 return append( buf
.data() );
422 wxUString
&append( const wxScopedU32CharBuffer
&buf
)
424 return append( buf
.data() );
427 wxUString
&append( const char *str
)
429 return append( wxUString( str
) );
432 wxUString
&append( const wxScopedCharBuffer
&buf
)
434 return append( wxUString( buf
) );
437 wxUString
&append( const wxChar16
*str
)
439 return append( wxUString( str
) );
442 wxUString
&append( const wxString
&str
)
444 return append( wxUString( str
) );
447 wxUString
&append( const wxCStrData
*cstr
)
449 return append( wxUString( cstr
) );
452 wxUString
&append( char ch
)
457 return append( buf
);
460 wxUString
&append( wxChar16 ch
)
465 return append( buf
);
468 wxUString
&append( wxUniChar ch
)
470 return append( (size_type
) 1, (wxChar32
) ch
.GetValue() );
473 wxUString
&append( wxUniCharRef ch
)
475 return append( (size_type
) 1, (wxChar32
) ch
.GetValue() );
479 // insert [STL overloads]
481 wxUString
&insert( size_type pos
, const wxUString
&s
)
483 std::basic_string
<wxChar32
> *base
= this;
484 return (wxUString
&) base
->insert( pos
, s
);
487 wxUString
&insert( size_type pos
, const wxUString
&s
, size_type pos1
, size_type n
)
489 std::basic_string
<wxChar32
> *base
= this;
490 return (wxUString
&) base
->insert( pos
, s
, pos1
, n
);
493 wxUString
&insert( size_type pos
, const wxChar32
*s
)
495 std::basic_string
<wxChar32
> *base
= this;
496 return (wxUString
&) base
->insert( pos
, s
);
499 wxUString
&insert( size_type pos
, const wxChar32
*s
, size_type n
)
501 std::basic_string
<wxChar32
> *base
= this;
502 return (wxUString
&) base
->insert( pos
, s
, n
);
505 wxUString
&insert( size_type pos
, size_type n
, wxChar32 c
)
507 std::basic_string
<wxChar32
> *base
= this;
508 return (wxUString
&) base
->insert( pos
, n
, c
);
512 // insert [STL overloads]
514 wxUString
&insert( size_type n
, const char *s
)
516 return insert( n
, wxUString( s
) );
519 wxUString
&insert( size_type n
, const wxChar16
*s
)
521 return insert( n
, wxUString( s
) );
524 wxUString
&insert( size_type n
, const wxScopedCharBuffer
&buf
)
526 return insert( n
, wxUString( buf
) );
529 wxUString
&insert( size_type n
, const wxScopedU16CharBuffer
&buf
)
531 return insert( n
, wxUString( buf
) );
534 wxUString
&insert( size_type n
, const wxScopedU32CharBuffer
&buf
)
536 return insert( n
, buf
.data() );
539 wxUString
&insert( size_type n
, const wxString
&s
)
541 return insert( n
, wxUString( s
) );
544 wxUString
&insert( size_type n
, const wxCStrData
*cstr
)
546 return insert( n
, wxUString( cstr
) );
549 wxUString
&insert( size_type n
, char ch
)
554 return insert( n
, buf
);
557 wxUString
&insert( size_type n
, wchar_t ch
)
562 return insert( n
, buf
);
567 iterator
insert( iterator it
, wxChar32 ch
)
569 std::basic_string
<wxChar32
> *base
= this;
570 return base
->insert( it
, ch
);
573 void insert(iterator it
, const_iterator first
, const_iterator last
)
575 std::basic_string
<wxChar32
> *base
= this;
576 base
->insert( it
, first
, last
);
581 wxUString
& operator=(const wxString
& s
)
582 { return assign( s
); }
583 wxUString
& operator=(const wxCStrData
* s
)
584 { return assign( s
); }
585 wxUString
& operator=(const char *s
)
586 { return assign( s
); }
587 wxUString
& operator=(const wxChar16
*s
)
588 { return assign( s
); }
589 wxUString
& operator=(const wxChar32
*s
)
590 { return assign( s
); }
591 wxUString
& operator=(const wxScopedCharBuffer
&s
)
592 { return assign( s
); }
593 wxUString
& operator=(const wxScopedU16CharBuffer
&s
)
594 { return assign( s
); }
595 wxUString
& operator=(const wxScopedU32CharBuffer
&s
)
596 { return assign( s
); }
597 wxUString
& operator=(const char ch
)
598 { return assign( ch
); }
599 wxUString
& operator=(const wxChar16 ch
)
600 { return assign( ch
); }
601 wxUString
& operator=(const wxChar32 ch
)
602 { return assign( ch
); }
603 wxUString
& operator=(const wxUniChar ch
)
604 { return assign( ch
); }
605 wxUString
& operator=(const wxUniCharRef ch
)
606 { return assign( ch
); }
609 wxUString
& operator+=(const wxUString
& s
)
610 { return append( s
); }
611 wxUString
& operator+=(const wxString
& s
)
612 { return append( s
); }
613 wxUString
& operator+=(const wxCStrData
* s
)
614 { return append( s
); }
615 wxUString
& operator+=(const char *s
)
616 { return append( s
); }
617 wxUString
& operator+=(const wxChar16
*s
)
618 { return append( s
); }
619 wxUString
& operator+=(const wxChar32
*s
)
620 { return append( s
); }
621 wxUString
& operator+=(const wxScopedCharBuffer
&s
)
622 { return append( s
); }
623 wxUString
& operator+=(const wxScopedU16CharBuffer
&s
)
624 { return append( s
); }
625 wxUString
& operator+=(const wxScopedU32CharBuffer
&s
)
626 { return append( s
); }
627 wxUString
& operator+=(const char ch
)
628 { return append( ch
); }
629 wxUString
& operator+=(const wxChar16 ch
)
630 { return append( ch
); }
631 wxUString
& operator+=(const wxChar32 ch
)
632 { return append( ch
); }
633 wxUString
& operator+=(const wxUniChar ch
)
634 { return append( ch
); }
635 wxUString
& operator+=(const wxUniCharRef ch
)
636 { return append( ch
); }
644 inline wxUString
operator+(const wxUString
&s1
, const wxUString
&s2
)
645 { wxUString
ret( s1
); ret
.append( s2
); return ret
; }
646 inline wxUString
operator+(const wxUString
&s1
, const char *s2
)
647 { return s1
+ wxUString(s2
); }
648 inline wxUString
operator+(const wxUString
&s1
, const wxString
&s2
)
649 { return s1
+ wxUString(s2
); }
650 inline wxUString
operator+(const wxUString
&s1
, const wxCStrData
*s2
)
651 { return s1
+ wxUString(s2
); }
652 inline wxUString
operator+(const wxUString
&s1
, const wxChar16
* s2
)
653 { return s1
+ wxUString(s2
); }
654 inline wxUString
operator+(const wxUString
&s1
, const wxChar32
*s2
)
655 { return s1
+ wxUString(s2
); }
656 inline wxUString
operator+(const wxUString
&s1
, const wxScopedCharBuffer
&s2
)
657 { return s1
+ wxUString(s2
); }
658 inline wxUString
operator+(const wxUString
&s1
, const wxScopedU16CharBuffer
&s2
)
659 { return s1
+ wxUString(s2
); }
660 inline wxUString
operator+(const wxUString
&s1
, const wxScopedU32CharBuffer
&s2
)
661 { return s1
+ wxUString(s2
); }
662 inline wxUString
operator+(const wxUString
&s1
, char s2
)
663 { return s1
+ wxUString(s2
); }
664 inline wxUString
operator+(const wxUString
&s1
, wxChar32 s2
)
665 { wxUString
ret( s1
); ret
.append( s2
); return ret
; }
666 inline wxUString
operator+(const wxUString
&s1
, wxChar16 s2
)
667 { wxUString
ret( s1
); ret
.append( (wxChar32
) s2
); return ret
; }
668 inline wxUString
operator+(const wxUString
&s1
, wxUniChar s2
)
669 { wxUString
ret( s1
); ret
.append( (wxChar32
) s2
.GetValue() ); return ret
; }
670 inline wxUString
operator+(const wxUString
&s1
, wxUniCharRef s2
)
671 { wxUString
ret( s1
); ret
.append( (wxChar32
) s2
.GetValue() ); return ret
; }
673 inline wxUString
operator+(const char *s1
, const wxUString
&s2
)
674 { return wxUString(s1
) + s2
; }
675 inline wxUString
operator+(const wxString
&s1
, const wxUString
&s2
)
676 { return wxUString(s1
) + s2
; }
677 inline wxUString
operator+(const wxCStrData
*s1
, const wxUString
&s2
)
678 { return wxUString(s1
) + s2
; }
679 inline wxUString
operator+(const wxChar16
* s1
, const wxUString
&s2
)
680 { return wxUString(s1
) + s2
; }
681 inline wxUString
operator+(const wxChar32
*s1
, const wxUString
&s2
)
682 { return wxUString(s1
) + s2
; }
683 inline wxUString
operator+(const wxScopedCharBuffer
&s1
, const wxUString
&s2
)
684 { return wxUString(s1
) + s2
; }
685 inline wxUString
operator+(const wxScopedU16CharBuffer
&s1
, const wxUString
&s2
)
686 { return wxUString(s1
) + s2
; }
687 inline wxUString
operator+(const wxScopedU32CharBuffer
&s1
, const wxUString
&s2
)
688 { return wxUString(s1
) + s2
; }
689 inline wxUString
operator+(char s1
, const wxUString
&s2
)
690 { return wxUString(s1
) + s2
; }
691 inline wxUString
operator+(wxChar32 s1
, const wxUString
&s2
)
692 { return wxUString(s1
) + s2
; }
693 inline wxUString
operator+(wxChar16 s1
, const wxUString
&s2
)
694 { return wxUString(s1
) + s2
; }
695 inline wxUString
operator+(wxUniChar s1
, const wxUString
&s2
)
696 { return wxUString(s1
) + s2
; }
697 inline wxUString
operator+(wxUniCharRef s1
, const wxUString
&s2
)
698 { return wxUString(s1
) + s2
; }
701 inline bool operator==(const wxUString
& s1
, const wxUString
& s2
)
702 { return s1
.compare( s2
) == 0; }
703 inline bool operator!=(const wxUString
& s1
, const wxUString
& s2
)
704 { return s1
.compare( s2
) != 0; }
705 inline bool operator< (const wxUString
& s1
, const wxUString
& s2
)
706 { return s1
.compare( s2
) < 0; }
707 inline bool operator> (const wxUString
& s1
, const wxUString
& s2
)
708 { return s1
.compare( s2
) > 0; }
709 inline bool operator<=(const wxUString
& s1
, const wxUString
& s2
)
710 { return s1
.compare( s2
) <= 0; }
711 inline bool operator>=(const wxUString
& s1
, const wxUString
& s2
)
712 { return s1
.compare( s2
) >= 0; }
714 #define wxUSTRING_COMP_OPERATORS( T ) \
715 inline bool operator==(const wxUString& s1, T s2) \
716 { return s1.compare( wxUString(s2) ) == 0; } \
717 inline bool operator!=(const wxUString& s1, T s2) \
718 { return s1.compare( wxUString(s2) ) != 0; } \
719 inline bool operator< (const wxUString& s1, T s2) \
720 { return s1.compare( wxUString(s2) ) < 0; } \
721 inline bool operator> (const wxUString& s1, T s2) \
722 { return s1.compare( wxUString(s2) ) > 0; } \
723 inline bool operator<=(const wxUString& s1, T s2) \
724 { return s1.compare( wxUString(s2) ) <= 0; } \
725 inline bool operator>=(const wxUString& s1, T s2) \
726 { return s1.compare( wxUString(s2) ) >= 0; } \
728 inline bool operator==(T s2, const wxUString& s1) \
729 { return s1.compare( wxUString(s2) ) == 0; } \
730 inline bool operator!=(T s2, const wxUString& s1) \
731 { return s1.compare( wxUString(s2) ) != 0; } \
732 inline bool operator< (T s2, const wxUString& s1) \
733 { return s1.compare( wxUString(s2) ) > 0; } \
734 inline bool operator> (T s2, const wxUString& s1) \
735 { return s1.compare( wxUString(s2) ) < 0; } \
736 inline bool operator<=(T s2, const wxUString& s1) \
737 { return s1.compare( wxUString(s2) ) >= 0; } \
738 inline bool operator>=(T s2, const wxUString& s1) \
739 { return s1.compare( wxUString(s2) ) <= 0; }
741 wxUSTRING_COMP_OPERATORS( const wxString
& )
742 wxUSTRING_COMP_OPERATORS( const char * )
743 wxUSTRING_COMP_OPERATORS( const wxChar16
* )
744 wxUSTRING_COMP_OPERATORS( const wxChar32
* )
745 wxUSTRING_COMP_OPERATORS( const wxScopedCharBuffer
& )
746 wxUSTRING_COMP_OPERATORS( const wxScopedU16CharBuffer
& )
747 wxUSTRING_COMP_OPERATORS( const wxScopedU32CharBuffer
& )
748 wxUSTRING_COMP_OPERATORS( const wxCStrData
* )
750 #endif // _WX_USTRING_H_