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)
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 wxUString
&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 wxUString
&assign( wxChar32 ch
)
204 std::basic_string
<wxChar32
> *base
= this;
205 return (wxUString
&) base
->assign( (size_type
) 1, ch
);
208 wxUString
&assign( size_type n
, wxChar32 ch
)
210 std::basic_string
<wxChar32
> *base
= this;
211 return (wxUString
&) base
->assign( n
, ch
);
214 wxUString
&assign( const wxScopedU32CharBuffer
&buf
)
216 return assign( buf
.data() );
219 wxUString
&assign( const char *str
)
221 return assignFromCString( str
);
224 wxUString
&assign( const wxScopedCharBuffer
&buf
)
226 return assignFromCString( buf
.data() );
229 wxUString
&assign( const char *str
, const wxMBConv
&conv
)
231 return assignFromCString( str
, conv
);
234 wxUString
&assign( const wxScopedCharBuffer
&buf
, const wxMBConv
&conv
)
236 return assignFromCString( buf
.data(), conv
);
239 wxUString
&assign( const wxChar16
*str
)
241 return assignFromUTF16( str
);
244 wxUString
&assign( const wxScopedU16CharBuffer
&buf
)
246 return assignFromUTF16( buf
.data() );
249 wxUString
&assign( const wxCStrData
*cstr
)
251 #if SIZEOF_WCHAR_T == 2
252 return assignFromUTF16( cstr
->AsWChar() );
254 return assign( cstr
->AsWChar() );
258 wxUString
&assign( const wxString
&str
)
260 #if wxUSE_UNICODE_UTF8
261 return assignFromUTF8( str
.wx_str() );
263 #if SIZEOF_WCHAR_T == 2
264 return assignFromUTF16( str
.wc_str() );
266 return assign( str
.wc_str() );
271 wxUString
&assign( char ch
)
276 return assignFromCString( buf
);
279 wxUString
&assign( size_type n
, char ch
)
281 wxCharBuffer
buffer(n
);
282 char *p
= buffer
.data();
284 for (i
= 0; i
< n
; i
++)
289 return assignFromCString( buffer
.data() );
292 wxUString
&assign( wxChar16 ch
)
297 return assignFromUTF16( buf
);
300 wxUString
&assign( size_type n
, wxChar16 ch
)
302 wxU16CharBuffer
buffer(n
);
303 wxChar16
*p
= buffer
.data();
305 for (i
= 0; i
< n
; i
++)
310 return assignFromUTF16( buffer
.data() );
313 wxUString
&assign( wxUniChar ch
)
315 return assign( (wxChar32
) ch
.GetValue() );
318 wxUString
&assign( size_type n
, wxUniChar ch
)
320 return assign( n
, (wxChar32
) ch
.GetValue() );
323 wxUString
&assign( wxUniCharRef ch
)
325 return assign( (wxChar32
) ch
.GetValue() );
328 wxUString
&assign( size_type n
, wxUniCharRef ch
)
330 return assign( n
, (wxChar32
) ch
.GetValue() );
333 // append [STL overload]
335 wxUString
&append( const wxUString
&s
)
337 std::basic_string
<wxChar32
> *base
= this;
338 return (wxUString
&) base
->append( s
);
341 wxUString
&append( const wxUString
&s
, size_type pos
, size_type n
)
343 std::basic_string
<wxChar32
> *base
= this;
344 return (wxUString
&) base
->append( s
, pos
, n
);
347 wxUString
&append( const wxChar32
* s
)
349 std::basic_string
<wxChar32
> *base
= this;
350 return (wxUString
&) base
->append( s
);
353 wxUString
&append( const wxChar32
* s
, size_type n
)
355 std::basic_string
<wxChar32
> *base
= this;
356 return (wxUString
&) base
->append( s
, n
);
359 wxUString
&append( size_type n
, wxChar32 c
)
361 std::basic_string
<wxChar32
> *base
= this;
362 return (wxUString
&) base
->append( n
, c
);
365 wxUString
&append( wxChar32 c
)
367 std::basic_string
<wxChar32
> *base
= this;
368 return (wxUString
&) base
->append( 1, c
);
371 // append [wx overload]
373 wxUString
&append( const wxScopedU16CharBuffer
&buf
)
375 return append( buf
.data() );
378 wxUString
&append( const wxScopedU32CharBuffer
&buf
)
380 return append( buf
.data() );
383 wxUString
&append( const char *str
)
385 return append( wxUString( str
) );
388 wxUString
&append( const wxScopedCharBuffer
&buf
)
390 return append( wxUString( buf
) );
393 wxUString
&append( const wxChar16
*str
)
395 return append( wxUString( str
) );
398 wxUString
&append( const wxString
&str
)
400 return append( wxUString( str
) );
403 wxUString
&append( const wxCStrData
*cstr
)
405 return append( wxUString( cstr
) );
408 wxUString
&append( char ch
)
413 return append( buf
);
416 wxUString
&append( wxChar16 ch
)
421 return append( buf
);
424 wxUString
&append( wxUniChar ch
)
426 return append( (size_type
) 1, (wxChar32
) ch
.GetValue() );
429 wxUString
&append( wxUniCharRef ch
)
431 return append( (size_type
) 1, (wxChar32
) ch
.GetValue() );
435 // insert [STL overloads]
437 wxUString
&insert( size_type pos
, const wxUString
&s
)
439 std::basic_string
<wxChar32
> *base
= this;
440 return (wxUString
&) base
->insert( pos
, s
);
443 wxUString
&insert( size_type pos
, const wxUString
&s
, size_type pos1
, size_type n
)
445 std::basic_string
<wxChar32
> *base
= this;
446 return (wxUString
&) base
->insert( pos
, s
, pos1
, n
);
449 wxUString
&insert( size_type pos
, const wxChar32
*s
)
451 std::basic_string
<wxChar32
> *base
= this;
452 return (wxUString
&) base
->insert( pos
, s
);
455 wxUString
&insert( size_type pos
, const wxChar32
*s
, size_type n
)
457 std::basic_string
<wxChar32
> *base
= this;
458 return (wxUString
&) base
->insert( pos
, s
, n
);
461 wxUString
&insert( size_type pos
, size_type n
, wxChar32 c
)
463 std::basic_string
<wxChar32
> *base
= this;
464 return (wxUString
&) base
->insert( pos
, n
, c
);
468 // insert [STL overloads]
470 wxUString
&insert( size_type n
, const char *s
)
472 return insert( n
, wxUString( s
) );
475 wxUString
&insert( size_type n
, const wxChar16
*s
)
477 return insert( n
, wxUString( s
) );
480 wxUString
&insert( size_type n
, const wxScopedCharBuffer
&buf
)
482 return insert( n
, wxUString( buf
) );
485 wxUString
&insert( size_type n
, const wxScopedU16CharBuffer
&buf
)
487 return insert( n
, wxUString( buf
) );
490 wxUString
&insert( size_type n
, const wxScopedU32CharBuffer
&buf
)
492 return insert( n
, buf
.data() );
495 wxUString
&insert( size_type n
, const wxString
&s
)
497 return insert( n
, wxUString( s
) );
500 wxUString
&insert( size_type n
, const wxCStrData
*cstr
)
502 return insert( n
, wxUString( cstr
) );
505 wxUString
&insert( size_type n
, char ch
)
510 return insert( n
, buf
);
513 wxUString
&insert( size_type n
, wchar_t ch
)
518 return insert( n
, buf
);
523 iterator
insert( iterator it
, wxChar32 ch
)
525 std::basic_string
<wxChar32
> *base
= this;
526 return base
->insert( it
, ch
);
529 void insert(iterator it
, const_iterator first
, const_iterator last
)
531 std::basic_string
<wxChar32
> *base
= this;
532 base
->insert( it
, first
, last
);
537 wxUString
& operator=(const wxUString
& s
)
538 { return assign( s
); }
539 wxUString
& operator=(const wxString
& s
)
540 { return assign( s
); }
541 wxUString
& operator=(const wxCStrData
* s
)
542 { return assign( s
); }
543 wxUString
& operator=(const char *s
)
544 { return assign( s
); }
545 wxUString
& operator=(const wxChar16
*s
)
546 { return assign( s
); }
547 wxUString
& operator=(const wxChar32
*s
)
548 { return assign( s
); }
549 wxUString
& operator=(const wxScopedCharBuffer
&s
)
550 { return assign( s
); }
551 wxUString
& operator=(const wxScopedU16CharBuffer
&s
)
552 { return assign( s
); }
553 wxUString
& operator=(const wxScopedU32CharBuffer
&s
)
554 { return assign( s
); }
555 wxUString
& operator=(const char ch
)
556 { return assign( ch
); }
557 wxUString
& operator=(const wxChar16 ch
)
558 { return assign( ch
); }
559 wxUString
& operator=(const wxChar32 ch
)
560 { return assign( ch
); }
561 wxUString
& operator=(const wxUniChar ch
)
562 { return assign( ch
); }
563 wxUString
& operator=(const wxUniCharRef ch
)
564 { return assign( ch
); }
567 wxUString
& operator+=(const wxUString
& s
)
568 { return append( s
); }
569 wxUString
& operator+=(const wxString
& s
)
570 { return append( s
); }
571 wxUString
& operator+=(const wxCStrData
* s
)
572 { return append( s
); }
573 wxUString
& operator+=(const char *s
)
574 { return append( s
); }
575 wxUString
& operator+=(const wxChar16
*s
)
576 { return append( s
); }
577 wxUString
& operator+=(const wxChar32
*s
)
578 { return append( s
); }
579 wxUString
& operator+=(const wxScopedCharBuffer
&s
)
580 { return append( s
); }
581 wxUString
& operator+=(const wxScopedU16CharBuffer
&s
)
582 { return append( s
); }
583 wxUString
& operator+=(const wxScopedU32CharBuffer
&s
)
584 { return append( s
); }
585 wxUString
& operator+=(const char ch
)
586 { return append( ch
); }
587 wxUString
& operator+=(const wxChar16 ch
)
588 { return append( ch
); }
589 wxUString
& operator+=(const wxChar32 ch
)
590 { return append( ch
); }
591 wxUString
& operator+=(const wxUniChar ch
)
592 { return append( ch
); }
593 wxUString
& operator+=(const wxUniCharRef ch
)
594 { return append( ch
); }
599 #pragma warning(default:4275)
602 inline wxUString
operator+(const wxUString
&s1
, const wxUString
&s2
)
603 { wxUString
ret( s1
); ret
.append( s2
); return ret
; }
604 inline wxUString
operator+(const wxUString
&s1
, const char *s2
)
605 { return s1
+ wxUString(s2
); }
606 inline wxUString
operator+(const wxUString
&s1
, const wxString
&s2
)
607 { return s1
+ wxUString(s2
); }
608 inline wxUString
operator+(const wxUString
&s1
, const wxCStrData
*s2
)
609 { return s1
+ wxUString(s2
); }
610 inline wxUString
operator+(const wxUString
&s1
, const wxChar16
* s2
)
611 { return s1
+ wxUString(s2
); }
612 inline wxUString
operator+(const wxUString
&s1
, const wxChar32
*s2
)
613 { return s1
+ wxUString(s2
); }
614 inline wxUString
operator+(const wxUString
&s1
, const wxScopedCharBuffer
&s2
)
615 { return s1
+ wxUString(s2
); }
616 inline wxUString
operator+(const wxUString
&s1
, const wxScopedU16CharBuffer
&s2
)
617 { return s1
+ wxUString(s2
); }
618 inline wxUString
operator+(const wxUString
&s1
, const wxScopedU32CharBuffer
&s2
)
619 { return s1
+ wxUString(s2
); }
620 inline wxUString
operator+(const wxUString
&s1
, char s2
)
621 { return s1
+ wxUString(s2
); }
622 inline wxUString
operator+(const wxUString
&s1
, wxChar32 s2
)
623 { wxUString
ret( s1
); ret
.append( s2
); return ret
; }
624 inline wxUString
operator+(const wxUString
&s1
, wxChar16 s2
)
625 { wxUString
ret( s1
); ret
.append( (wxChar32
) s2
); return ret
; }
626 inline wxUString
operator+(const wxUString
&s1
, wxUniChar s2
)
627 { wxUString
ret( s1
); ret
.append( (wxChar32
) s2
.GetValue() ); return ret
; }
628 inline wxUString
operator+(const wxUString
&s1
, wxUniCharRef s2
)
629 { wxUString
ret( s1
); ret
.append( (wxChar32
) s2
.GetValue() ); return ret
; }
631 inline wxUString
operator+(const char *s1
, const wxUString
&s2
)
632 { return wxUString(s1
) + s2
; }
633 inline wxUString
operator+(const wxString
&s1
, const wxUString
&s2
)
634 { return wxUString(s1
) + s2
; }
635 inline wxUString
operator+(const wxCStrData
*s1
, const wxUString
&s2
)
636 { return wxUString(s1
) + s2
; }
637 inline wxUString
operator+(const wxChar16
* s1
, const wxUString
&s2
)
638 { return wxUString(s1
) + s2
; }
639 inline wxUString
operator+(const wxChar32
*s1
, const wxUString
&s2
)
640 { return wxUString(s1
) + s2
; }
641 inline wxUString
operator+(const wxScopedCharBuffer
&s1
, const wxUString
&s2
)
642 { return wxUString(s1
) + s2
; }
643 inline wxUString
operator+(const wxScopedU16CharBuffer
&s1
, const wxUString
&s2
)
644 { return wxUString(s1
) + s2
; }
645 inline wxUString
operator+(const wxScopedU32CharBuffer
&s1
, const wxUString
&s2
)
646 { return wxUString(s1
) + s2
; }
647 inline wxUString
operator+(char s1
, const wxUString
&s2
)
648 { return wxUString(s1
) + s2
; }
649 inline wxUString
operator+(wxChar32 s1
, const wxUString
&s2
)
650 { return wxUString(s1
) + s2
; }
651 inline wxUString
operator+(wxChar16 s1
, const wxUString
&s2
)
652 { return wxUString(s1
) + s2
; }
653 inline wxUString
operator+(wxUniChar s1
, const wxUString
&s2
)
654 { return wxUString(s1
) + s2
; }
655 inline wxUString
operator+(wxUniCharRef s1
, const wxUString
&s2
)
656 { return wxUString(s1
) + s2
; }
659 inline bool operator==(const wxUString
& s1
, const wxUString
& s2
)
660 { return s1
.compare( s2
) == 0; }
661 inline bool operator!=(const wxUString
& s1
, const wxUString
& s2
)
662 { return s1
.compare( s2
) != 0; }
663 inline bool operator< (const wxUString
& s1
, const wxUString
& s2
)
664 { return s1
.compare( s2
) < 0; }
665 inline bool operator> (const wxUString
& s1
, const wxUString
& s2
)
666 { return s1
.compare( s2
) > 0; }
667 inline bool operator<=(const wxUString
& s1
, const wxUString
& s2
)
668 { return s1
.compare( s2
) <= 0; }
669 inline bool operator>=(const wxUString
& s1
, const wxUString
& s2
)
670 { return s1
.compare( s2
) >= 0; }
672 #define wxUSTRING_COMP_OPERATORS( T ) \
673 inline bool operator==(const wxUString& s1, T s2) \
674 { return s1.compare( wxUString(s2) ) == 0; } \
675 inline bool operator!=(const wxUString& s1, T s2) \
676 { return s1.compare( wxUString(s2) ) != 0; } \
677 inline bool operator< (const wxUString& s1, T s2) \
678 { return s1.compare( wxUString(s2) ) < 0; } \
679 inline bool operator> (const wxUString& s1, T s2) \
680 { return s1.compare( wxUString(s2) ) > 0; } \
681 inline bool operator<=(const wxUString& s1, T s2) \
682 { return s1.compare( wxUString(s2) ) <= 0; } \
683 inline bool operator>=(const wxUString& s1, T s2) \
684 { return s1.compare( wxUString(s2) ) >= 0; } \
686 inline bool operator==(T s2, const wxUString& s1) \
687 { return s1.compare( wxUString(s2) ) == 0; } \
688 inline bool operator!=(T s2, const wxUString& s1) \
689 { return s1.compare( wxUString(s2) ) != 0; } \
690 inline bool operator< (T s2, const wxUString& s1) \
691 { return s1.compare( wxUString(s2) ) > 0; } \
692 inline bool operator> (T s2, const wxUString& s1) \
693 { return s1.compare( wxUString(s2) ) < 0; } \
694 inline bool operator<=(T s2, const wxUString& s1) \
695 { return s1.compare( wxUString(s2) ) >= 0; } \
696 inline bool operator>=(T s2, const wxUString& s1) \
697 { return s1.compare( wxUString(s2) ) <= 0; }
699 wxUSTRING_COMP_OPERATORS( const wxString
& )
700 wxUSTRING_COMP_OPERATORS( const char * )
701 wxUSTRING_COMP_OPERATORS( const wxChar16
* )
702 wxUSTRING_COMP_OPERATORS( const wxChar32
* )
703 wxUSTRING_COMP_OPERATORS( const wxScopedCharBuffer
& )
704 wxUSTRING_COMP_OPERATORS( const wxScopedU16CharBuffer
& )
705 wxUSTRING_COMP_OPERATORS( const wxScopedU32CharBuffer
& )
706 wxUSTRING_COMP_OPERATORS( const wxCStrData
* )
708 #endif // _WX_USTRING_H_