3 // Purpose: 32-bit string (UCS-4)
4 // Author: Robert Roebling
5 // Copyright: (c) Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
10 #define _WX_USTRING_H_
13 #include "wx/string.h"
17 #if SIZEOF_WCHAR_T == 2
18 typedef wxWCharBuffer wxU16CharBuffer
;
19 typedef wxScopedWCharBuffer wxScopedU16CharBuffer
;
21 typedef wxCharTypeBuffer
<wxChar16
> wxU16CharBuffer
;
22 typedef wxScopedCharTypeBuffer
<wxChar16
> wxScopedU16CharBuffer
;
25 #if SIZEOF_WCHAR_T == 4
26 typedef wxWCharBuffer wxU32CharBuffer
;
27 typedef wxScopedWCharBuffer wxScopedU32CharBuffer
;
29 typedef wxCharTypeBuffer
<wxChar32
> wxU32CharBuffer
;
30 typedef wxScopedCharTypeBuffer
<wxChar32
> wxScopedU32CharBuffer
;
34 // "non dll-interface class 'std::basic_string<wxChar32>' used as base
35 // interface for dll-interface class 'wxString'" -- this is OK in our case
36 // (and warning is unavoidable anyhow)
38 #pragma warning(disable:4275)
41 class WXDLLIMPEXP_BASE wxUString
: public std::basic_string
<wxChar32
>
46 wxUString( const wxChar32
*str
) { assign(str
); }
47 wxUString( const wxScopedU32CharBuffer
&buf
) { assign(buf
); }
49 wxUString( const char *str
) { assign(str
); }
50 wxUString( const wxScopedCharBuffer
&buf
) { assign(buf
); }
51 wxUString( const char *str
, const wxMBConv
&conv
) { assign(str
,conv
); }
52 wxUString( const wxScopedCharBuffer
&buf
, const wxMBConv
&conv
) { assign(buf
,conv
); }
54 wxUString( const wxChar16
*str
) { assign(str
); }
55 wxUString( const wxScopedU16CharBuffer
&buf
) { assign(buf
); }
57 wxUString( const wxCStrData
*cstr
) { assign(cstr
); }
58 wxUString( const wxString
&str
) { assign(str
); }
60 wxUString( char ch
) { assign(ch
); }
61 wxUString( wxChar16 ch
) { assign(ch
); }
62 wxUString( wxChar32 ch
) { assign(ch
); }
63 wxUString( wxUniChar ch
) { assign(ch
); }
64 wxUString( wxUniCharRef ch
) { assign(ch
); }
65 wxUString( size_type n
, char ch
) { assign(n
,ch
); }
66 wxUString( size_type n
, wxChar16 ch
) { assign(n
,ch
); }
67 wxUString( size_type n
, wxChar32 ch
) { assign(n
,ch
); }
68 wxUString( size_type n
, wxUniChar ch
) { assign(n
,ch
); }
69 wxUString( size_type n
, wxUniCharRef ch
) { assign(n
,ch
); }
71 // static construction
73 static wxUString
FromAscii( const char *str
, size_type n
)
76 ret
.assignFromAscii( str
, n
);
80 static wxUString
FromAscii( const char *str
)
83 ret
.assignFromAscii( str
);
87 static wxUString
FromUTF8( const char *str
, size_type n
)
90 ret
.assignFromUTF8( str
, n
);
94 static wxUString
FromUTF8( const char *str
)
97 ret
.assignFromUTF8( str
);
101 static wxUString
FromUTF16( const wxChar16
*str
, size_type n
)
104 ret
.assignFromUTF16( str
, n
);
108 static wxUString
FromUTF16( const wxChar16
*str
)
111 ret
.assignFromUTF16( str
);
115 // assign from encoding
117 wxUString
&assignFromAscii( const char *str
);
118 wxUString
&assignFromAscii( const char *str
, size_type n
);
119 wxUString
&assignFromUTF8( const char *str
);
120 wxUString
&assignFromUTF8( const char *str
, size_type n
);
121 wxUString
&assignFromUTF16( const wxChar16
* str
);
122 wxUString
&assignFromUTF16( const wxChar16
* str
, size_type n
);
123 wxUString
&assignFromCString( const char* str
);
124 wxUString
&assignFromCString( const char* str
, const wxMBConv
&conv
);
128 wxScopedCharBuffer
utf8_str() const;
129 wxScopedU16CharBuffer
utf16_str() const;
131 #if SIZEOF_WCHAR_T == 2
132 wxScopedWCharBuffer
wc_str() const
137 wchar_t *wc_str() const
139 return (wchar_t*) c_str();
143 operator wxString() const
145 #if wxUSE_UNICODE_UTF8
146 return wxString::FromUTF8( utf8_str() );
148 #if SIZEOF_WCHAR_T == 2
149 return wxString( utf16_str() );
151 return wxString( c_str() );
156 #if wxUSE_UNICODE_UTF8
157 wxScopedCharBuffer
wx_str()
162 #if SIZEOF_WCHAR_T == 2
163 wxScopedWCharBuffer
wx_str()
168 const wchar_t* wx_str()
177 wxUString
&assign( const wxChar32
* str
)
179 std::basic_string
<wxChar32
> *base
= this;
180 return (wxUString
&) base
->assign( str
);
183 wxUString
&assign( const wxChar32
* str
, size_type n
)
185 std::basic_string
<wxChar32
> *base
= this;
186 return (wxUString
&) base
->assign( str
, n
);
189 wxUString
&assign( const wxUString
&str
)
191 std::basic_string
<wxChar32
> *base
= this;
192 return (wxUString
&) base
->assign( str
);
195 wxUString
&assign( const wxUString
&str
, size_type pos
, size_type n
)
197 std::basic_string
<wxChar32
> *base
= this;
198 return (wxUString
&) base
->assign( str
, pos
, n
);
201 // FIXME-VC6: VC 6.0 stl does not support all types of assign functions
203 wxUString
&assign( wxChar32 ch
)
207 std::basic_string
<wxChar32
> *base
= this;
208 return (wxUString
&)base
->assign(chh
);
211 wxUString
&assign( size_type n
, wxChar32 ch
)
213 wxU32CharBuffer
buffer(n
);
214 wxChar32
*p
= buffer
.data();
216 for (i
= 0; i
< n
; i
++)
222 std::basic_string
<wxChar32
> *base
= this;
223 return (wxUString
&)base
->assign(buffer
.data());
226 wxUString
&assign( wxChar32 ch
)
228 std::basic_string
<wxChar32
> *base
= this;
229 return (wxUString
&) base
->assign( (size_type
) 1, ch
);
232 wxUString
&assign( size_type n
, wxChar32 ch
)
234 std::basic_string
<wxChar32
> *base
= this;
235 return (wxUString
&) base
->assign( n
, ch
);
237 #endif // __VISUALC6__
239 wxUString
&assign( const wxScopedU32CharBuffer
&buf
)
241 return assign( buf
.data() );
244 wxUString
&assign( const char *str
)
246 return assignFromCString( str
);
249 wxUString
&assign( const wxScopedCharBuffer
&buf
)
251 return assignFromCString( buf
.data() );
254 wxUString
&assign( const char *str
, const wxMBConv
&conv
)
256 return assignFromCString( str
, conv
);
259 wxUString
&assign( const wxScopedCharBuffer
&buf
, const wxMBConv
&conv
)
261 return assignFromCString( buf
.data(), conv
);
264 wxUString
&assign( const wxChar16
*str
)
266 return assignFromUTF16( str
);
269 wxUString
&assign( const wxScopedU16CharBuffer
&buf
)
271 return assignFromUTF16( buf
.data() );
274 wxUString
&assign( const wxCStrData
*cstr
)
276 #if SIZEOF_WCHAR_T == 2
277 return assignFromUTF16( cstr
->AsWChar() );
279 return assign( cstr
->AsWChar() );
283 wxUString
&assign( const wxString
&str
)
285 #if wxUSE_UNICODE_UTF8
286 return assignFromUTF8( str
.wx_str() );
288 #if SIZEOF_WCHAR_T == 2
289 return assignFromUTF16( str
.wc_str() );
291 return assign( str
.wc_str() );
296 wxUString
&assign( char ch
)
301 return assignFromCString( buf
);
304 wxUString
&assign( size_type n
, char ch
)
306 wxCharBuffer
buffer(n
);
307 char *p
= buffer
.data();
309 for (i
= 0; i
< n
; i
++)
314 return assignFromCString( buffer
.data() );
317 wxUString
&assign( wxChar16 ch
)
322 return assignFromUTF16( buf
);
325 wxUString
&assign( size_type n
, wxChar16 ch
)
327 wxU16CharBuffer
buffer(n
);
328 wxChar16
*p
= buffer
.data();
330 for (i
= 0; i
< n
; i
++)
335 return assignFromUTF16( buffer
.data() );
338 wxUString
&assign( wxUniChar ch
)
340 return assign( (wxChar32
) ch
.GetValue() );
343 wxUString
&assign( size_type n
, wxUniChar ch
)
345 return assign( n
, (wxChar32
) ch
.GetValue() );
348 wxUString
&assign( wxUniCharRef ch
)
350 return assign( (wxChar32
) ch
.GetValue() );
353 wxUString
&assign( size_type n
, wxUniCharRef ch
)
355 return assign( n
, (wxChar32
) ch
.GetValue() );
358 // append [STL overload]
360 wxUString
&append( const wxUString
&s
)
362 std::basic_string
<wxChar32
> *base
= this;
363 return (wxUString
&) base
->append( s
);
366 wxUString
&append( const wxUString
&s
, size_type pos
, size_type n
)
368 std::basic_string
<wxChar32
> *base
= this;
369 return (wxUString
&) base
->append( s
, pos
, n
);
372 wxUString
&append( const wxChar32
* s
)
374 std::basic_string
<wxChar32
> *base
= this;
375 return (wxUString
&) base
->append( s
);
378 wxUString
&append( const wxChar32
* s
, size_type n
)
380 std::basic_string
<wxChar32
> *base
= this;
381 return (wxUString
&) base
->append( s
, n
);
384 // FIXME-VC6: VC 6.0 stl does not support all types of append functions
386 wxUString
&append( size_type n
, wxChar32 c
)
388 wxU32CharBuffer
buffer(n
);
389 wxChar32
*p
= buffer
.data();
391 for (i
= 0; i
< n
; i
++)
397 std::basic_string
<wxChar32
> *base
= this;
398 return (wxUString
&) base
->append(buffer
.data());
401 wxUString
&append( size_type n
, wxChar32 c
)
403 std::basic_string
<wxChar32
> *base
= this;
404 return (wxUString
&) base
->append( n
, c
);
406 #endif // __VISUALC6__
408 wxUString
&append( wxChar32 c
)
410 std::basic_string
<wxChar32
> *base
= this;
411 return (wxUString
&) base
->append( 1, c
);
414 // append [wx overload]
416 wxUString
&append( const wxScopedU16CharBuffer
&buf
)
418 return append( buf
.data() );
421 wxUString
&append( const wxScopedU32CharBuffer
&buf
)
423 return append( buf
.data() );
426 wxUString
&append( const char *str
)
428 return append( wxUString( str
) );
431 wxUString
&append( const wxScopedCharBuffer
&buf
)
433 return append( wxUString( buf
) );
436 wxUString
&append( const wxChar16
*str
)
438 return append( wxUString( str
) );
441 wxUString
&append( const wxString
&str
)
443 return append( wxUString( str
) );
446 wxUString
&append( const wxCStrData
*cstr
)
448 return append( wxUString( cstr
) );
451 wxUString
&append( char ch
)
456 return append( buf
);
459 wxUString
&append( wxChar16 ch
)
464 return append( buf
);
467 wxUString
&append( wxUniChar ch
)
469 return append( (size_type
) 1, (wxChar32
) ch
.GetValue() );
472 wxUString
&append( wxUniCharRef ch
)
474 return append( (size_type
) 1, (wxChar32
) ch
.GetValue() );
478 // insert [STL overloads]
480 wxUString
&insert( size_type pos
, const wxUString
&s
)
482 std::basic_string
<wxChar32
> *base
= this;
483 return (wxUString
&) base
->insert( pos
, s
);
486 wxUString
&insert( size_type pos
, const wxUString
&s
, size_type pos1
, size_type n
)
488 std::basic_string
<wxChar32
> *base
= this;
489 return (wxUString
&) base
->insert( pos
, s
, pos1
, n
);
492 wxUString
&insert( size_type pos
, const wxChar32
*s
)
494 std::basic_string
<wxChar32
> *base
= this;
495 return (wxUString
&) base
->insert( pos
, s
);
498 wxUString
&insert( size_type pos
, const wxChar32
*s
, size_type n
)
500 std::basic_string
<wxChar32
> *base
= this;
501 return (wxUString
&) base
->insert( pos
, s
, n
);
504 wxUString
&insert( size_type pos
, size_type n
, wxChar32 c
)
506 std::basic_string
<wxChar32
> *base
= this;
507 return (wxUString
&) base
->insert( pos
, n
, c
);
511 // insert [STL overloads]
513 wxUString
&insert( size_type n
, const char *s
)
515 return insert( n
, wxUString( s
) );
518 wxUString
&insert( size_type n
, const wxChar16
*s
)
520 return insert( n
, wxUString( s
) );
523 wxUString
&insert( size_type n
, const wxScopedCharBuffer
&buf
)
525 return insert( n
, wxUString( buf
) );
528 wxUString
&insert( size_type n
, const wxScopedU16CharBuffer
&buf
)
530 return insert( n
, wxUString( buf
) );
533 wxUString
&insert( size_type n
, const wxScopedU32CharBuffer
&buf
)
535 return insert( n
, buf
.data() );
538 wxUString
&insert( size_type n
, const wxString
&s
)
540 return insert( n
, wxUString( s
) );
543 wxUString
&insert( size_type n
, const wxCStrData
*cstr
)
545 return insert( n
, wxUString( cstr
) );
548 wxUString
&insert( size_type n
, char ch
)
553 return insert( n
, buf
);
556 wxUString
&insert( size_type n
, wchar_t ch
)
561 return insert( n
, buf
);
566 iterator
insert( iterator it
, wxChar32 ch
)
568 std::basic_string
<wxChar32
> *base
= this;
569 return base
->insert( it
, ch
);
572 void insert(iterator it
, const_iterator first
, const_iterator last
)
574 std::basic_string
<wxChar32
> *base
= this;
575 base
->insert( it
, first
, last
);
580 wxUString
& operator=(const wxString
& s
)
581 { return assign( s
); }
582 wxUString
& operator=(const wxCStrData
* s
)
583 { return assign( s
); }
584 wxUString
& operator=(const char *s
)
585 { return assign( s
); }
586 wxUString
& operator=(const wxChar16
*s
)
587 { return assign( s
); }
588 wxUString
& operator=(const wxChar32
*s
)
589 { return assign( s
); }
590 wxUString
& operator=(const wxScopedCharBuffer
&s
)
591 { return assign( s
); }
592 wxUString
& operator=(const wxScopedU16CharBuffer
&s
)
593 { return assign( s
); }
594 wxUString
& operator=(const wxScopedU32CharBuffer
&s
)
595 { return assign( s
); }
596 wxUString
& operator=(const char ch
)
597 { return assign( ch
); }
598 wxUString
& operator=(const wxChar16 ch
)
599 { return assign( ch
); }
600 wxUString
& operator=(const wxChar32 ch
)
601 { return assign( ch
); }
602 wxUString
& operator=(const wxUniChar ch
)
603 { return assign( ch
); }
604 wxUString
& operator=(const wxUniCharRef ch
)
605 { return assign( ch
); }
608 wxUString
& operator+=(const wxUString
& s
)
609 { return append( s
); }
610 wxUString
& operator+=(const wxString
& s
)
611 { return append( s
); }
612 wxUString
& operator+=(const wxCStrData
* s
)
613 { return append( s
); }
614 wxUString
& operator+=(const char *s
)
615 { return append( s
); }
616 wxUString
& operator+=(const wxChar16
*s
)
617 { return append( s
); }
618 wxUString
& operator+=(const wxChar32
*s
)
619 { return append( s
); }
620 wxUString
& operator+=(const wxScopedCharBuffer
&s
)
621 { return append( s
); }
622 wxUString
& operator+=(const wxScopedU16CharBuffer
&s
)
623 { return append( s
); }
624 wxUString
& operator+=(const wxScopedU32CharBuffer
&s
)
625 { return append( s
); }
626 wxUString
& operator+=(const char ch
)
627 { return append( ch
); }
628 wxUString
& operator+=(const wxChar16 ch
)
629 { return append( ch
); }
630 wxUString
& operator+=(const wxChar32 ch
)
631 { return append( ch
); }
632 wxUString
& operator+=(const wxUniChar ch
)
633 { return append( ch
); }
634 wxUString
& operator+=(const wxUniCharRef ch
)
635 { return append( ch
); }
643 inline wxUString
operator+(const wxUString
&s1
, const wxUString
&s2
)
644 { wxUString
ret( s1
); ret
.append( s2
); return ret
; }
645 inline wxUString
operator+(const wxUString
&s1
, const char *s2
)
646 { return s1
+ wxUString(s2
); }
647 inline wxUString
operator+(const wxUString
&s1
, const wxString
&s2
)
648 { return s1
+ wxUString(s2
); }
649 inline wxUString
operator+(const wxUString
&s1
, const wxCStrData
*s2
)
650 { return s1
+ wxUString(s2
); }
651 inline wxUString
operator+(const wxUString
&s1
, const wxChar16
* s2
)
652 { return s1
+ wxUString(s2
); }
653 inline wxUString
operator+(const wxUString
&s1
, const wxChar32
*s2
)
654 { return s1
+ wxUString(s2
); }
655 inline wxUString
operator+(const wxUString
&s1
, const wxScopedCharBuffer
&s2
)
656 { return s1
+ wxUString(s2
); }
657 inline wxUString
operator+(const wxUString
&s1
, const wxScopedU16CharBuffer
&s2
)
658 { return s1
+ wxUString(s2
); }
659 inline wxUString
operator+(const wxUString
&s1
, const wxScopedU32CharBuffer
&s2
)
660 { return s1
+ wxUString(s2
); }
661 inline wxUString
operator+(const wxUString
&s1
, char s2
)
662 { return s1
+ wxUString(s2
); }
663 inline wxUString
operator+(const wxUString
&s1
, wxChar32 s2
)
664 { wxUString
ret( s1
); ret
.append( s2
); return ret
; }
665 inline wxUString
operator+(const wxUString
&s1
, wxChar16 s2
)
666 { wxUString
ret( s1
); ret
.append( (wxChar32
) s2
); return ret
; }
667 inline wxUString
operator+(const wxUString
&s1
, wxUniChar s2
)
668 { wxUString
ret( s1
); ret
.append( (wxChar32
) s2
.GetValue() ); return ret
; }
669 inline wxUString
operator+(const wxUString
&s1
, wxUniCharRef s2
)
670 { wxUString
ret( s1
); ret
.append( (wxChar32
) s2
.GetValue() ); return ret
; }
672 inline wxUString
operator+(const char *s1
, const wxUString
&s2
)
673 { return wxUString(s1
) + s2
; }
674 inline wxUString
operator+(const wxString
&s1
, const wxUString
&s2
)
675 { return wxUString(s1
) + s2
; }
676 inline wxUString
operator+(const wxCStrData
*s1
, const wxUString
&s2
)
677 { return wxUString(s1
) + s2
; }
678 inline wxUString
operator+(const wxChar16
* s1
, const wxUString
&s2
)
679 { return wxUString(s1
) + s2
; }
680 inline wxUString
operator+(const wxChar32
*s1
, const wxUString
&s2
)
681 { return wxUString(s1
) + s2
; }
682 inline wxUString
operator+(const wxScopedCharBuffer
&s1
, const wxUString
&s2
)
683 { return wxUString(s1
) + s2
; }
684 inline wxUString
operator+(const wxScopedU16CharBuffer
&s1
, const wxUString
&s2
)
685 { return wxUString(s1
) + s2
; }
686 inline wxUString
operator+(const wxScopedU32CharBuffer
&s1
, const wxUString
&s2
)
687 { return wxUString(s1
) + s2
; }
688 inline wxUString
operator+(char s1
, const wxUString
&s2
)
689 { return wxUString(s1
) + s2
; }
690 inline wxUString
operator+(wxChar32 s1
, const wxUString
&s2
)
691 { return wxUString(s1
) + s2
; }
692 inline wxUString
operator+(wxChar16 s1
, const wxUString
&s2
)
693 { return wxUString(s1
) + s2
; }
694 inline wxUString
operator+(wxUniChar s1
, const wxUString
&s2
)
695 { return wxUString(s1
) + s2
; }
696 inline wxUString
operator+(wxUniCharRef s1
, const wxUString
&s2
)
697 { return wxUString(s1
) + s2
; }
700 inline bool operator==(const wxUString
& s1
, const wxUString
& s2
)
701 { return s1
.compare( s2
) == 0; }
702 inline bool operator!=(const wxUString
& s1
, const wxUString
& s2
)
703 { return s1
.compare( s2
) != 0; }
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; }
713 #define wxUSTRING_COMP_OPERATORS( T ) \
714 inline bool operator==(const wxUString& s1, T s2) \
715 { return s1.compare( wxUString(s2) ) == 0; } \
716 inline bool operator!=(const wxUString& s1, T s2) \
717 { return s1.compare( wxUString(s2) ) != 0; } \
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; } \
727 inline bool operator==(T s2, const wxUString& s1) \
728 { return s1.compare( wxUString(s2) ) == 0; } \
729 inline bool operator!=(T s2, const wxUString& s1) \
730 { 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; }
740 wxUSTRING_COMP_OPERATORS( const wxString
& )
741 wxUSTRING_COMP_OPERATORS( const char * )
742 wxUSTRING_COMP_OPERATORS( const wxChar16
* )
743 wxUSTRING_COMP_OPERATORS( const wxChar32
* )
744 wxUSTRING_COMP_OPERATORS( const wxScopedCharBuffer
& )
745 wxUSTRING_COMP_OPERATORS( const wxScopedU16CharBuffer
& )
746 wxUSTRING_COMP_OPERATORS( const wxScopedU32CharBuffer
& )
747 wxUSTRING_COMP_OPERATORS( const wxCStrData
* )
749 #endif // _WX_USTRING_H_