1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: 32-bit string (UCS-4)
4 // Author: Robert Roebling
5 // Copyright: (c) Robert Roebling
6 // RCS-ID: $Id: tab.h 37400 2006-02-09 00:28:34Z VZ $
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #ifndef _WX_USTRING_H_BASE_
11 #define _WX_USTRING_H_BASE_
14 #include "wx/string.h"
16 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxCharTypeBuffer
<wxChar32
> )
18 #if SIZEOF_WCHAR_T == 2
20 typedef wxWCharBuffer wxU16CharBuffer
;
24 class WXDLLIMPEXP_BASE wxU16CharBuffer
: public wxCharTypeBuffer
<wxChar16
>
27 typedef wxCharTypeBuffer
<wxChar16
> wxCharTypeBufferBase
;
29 wxU16CharBuffer(const wxCharTypeBufferBase
& buf
)
30 : wxCharTypeBufferBase(buf
) {}
32 wxU16CharBuffer(const CharType
*str
= NULL
) : wxCharTypeBufferBase(str
) {}
33 wxU16CharBuffer(size_t len
) : wxCharTypeBufferBase(len
) {}
40 #if SIZEOF_WCHAR_T == 2
42 class WXDLLIMPEXP_BASE wxU32CharBuffer
: public wxCharTypeBuffer
<wxChar32
>
45 typedef wxCharTypeBuffer
<wxChar32
> wxCharTypeBufferBase
;
47 wxU32CharBuffer(const wxCharTypeBufferBase
& buf
)
48 : wxCharTypeBufferBase(buf
) {}
50 wxU32CharBuffer(const CharType
*str
= NULL
) : wxCharTypeBufferBase(str
) {}
51 wxU32CharBuffer(size_t len
) : wxCharTypeBufferBase(len
) {}
56 typedef wxWCharBuffer wxU32CharBuffer
;
60 class WXDLLIMPEXP_BASE wxUString
: public std::basic_string
<wxChar32
>
65 wxUString( const wxChar32
*str
) { assign(str
); }
66 wxUString( const wxUString
&str
) { assign(str
); }
67 wxUString( const wxU32CharBuffer
&buf
) { assign(buf
); }
69 wxUString( const char *str
) { assign(str
); }
70 wxUString( const wxCharBuffer
&buf
) { assign(buf
); }
71 wxUString( const char *str
, const wxMBConv
&conv
) { assign(str
,conv
); }
72 wxUString( const wxCharBuffer
&buf
, const wxMBConv
&conv
) { assign(buf
,conv
); }
74 wxUString( const wxChar16
*str
) { assign(str
); }
75 wxUString( const wxU16CharBuffer
&buf
) { assign(buf
); }
77 wxUString( const wxCStrData
*cstr
) { assign(cstr
); }
78 wxUString( const wxString
&str
) { assign(str
); }
80 wxUString( char ch
) { assign(ch
); }
81 wxUString( wxChar16 ch
) { assign(ch
); }
82 wxUString( wxChar32 ch
) { assign(ch
); }
83 wxUString( wxUniChar ch
) { assign(ch
); }
84 wxUString( wxUniCharRef ch
) { assign(ch
); }
85 wxUString( size_type n
, char ch
) { assign(n
,ch
); }
86 wxUString( size_type n
, wxChar16 ch
) { assign(n
,ch
); }
87 wxUString( size_type n
, wxChar32 ch
) { assign(n
,ch
); }
88 wxUString( size_type n
, wxUniChar ch
) { assign(n
,ch
); }
89 wxUString( size_type n
, wxUniCharRef ch
) { assign(n
,ch
); }
91 // static construction
93 static wxUString
FromAscii( const char *str
, size_type n
)
96 ret
.assignFromAscii( str
, n
);
100 static wxUString
FromAscii( const char *str
)
103 ret
.assignFromAscii( str
);
107 static wxUString
FromUTF8( const char *str
, size_type n
)
110 ret
.assignFromUTF8( str
, n
);
114 static wxUString
FromUTF8( const char *str
)
117 ret
.assignFromUTF8( str
);
121 static wxUString
FromUTF16( const wxChar16
*str
, size_type n
)
124 ret
.assignFromUTF16( str
, n
);
128 static wxUString
FromUTF16( const wxChar16
*str
)
131 ret
.assignFromUTF16( str
);
135 // assign from encoding
137 wxUString
&assignFromAscii( const char *str
);
138 wxUString
&assignFromAscii( const char *str
, size_type n
);
139 wxUString
&assignFromUTF8( const char *str
);
140 wxUString
&assignFromUTF8( const char *str
, size_type n
);
141 wxUString
&assignFromUTF16( const wxChar16
* str
);
142 wxUString
&assignFromUTF16( const wxChar16
* str
, size_type n
);
143 wxUString
&assignFromCString( const char* str
);
144 wxUString
&assignFromCString( const char* str
, const wxMBConv
&conv
);
148 wxCharBuffer
utf8_str() const;
149 wxU16CharBuffer
utf16_str() const;
151 #if SIZEOF_WCHAR_T == 2
152 wxWCharBuffer
wc_str() const
157 wchar_t *wc_str() const
159 return (wchar_t*) c_str();
163 operator wxString() const
165 #if wxUSE_UNICODE_UTF8
166 return wxString::FromUTF8( utf8_str() );
168 #if SIZEOF_WCHAR_T == 2
169 return wxString( utf16_str() );
171 return wxString( c_str() );
176 #if wxUSE_UNICODE_UTF8
177 wxCharBuffer
wx_str()
182 #if SIZEOF_WCHAR_T == 2
183 wxWCharBuffer
wx_str()
197 inline wxUString
&assign( const wxChar32
* str
)
199 std::basic_string
<wxChar32
> *base
= this;
200 return (wxUString
&) base
->assign( str
);
203 inline wxUString
&assign( const wxChar32
* str
, size_type n
)
205 std::basic_string
<wxChar32
> *base
= this;
206 return (wxUString
&) base
->assign( str
, n
);
209 inline wxUString
&assign( const wxUString
&str
)
211 std::basic_string
<wxChar32
> *base
= this;
212 return (wxUString
&) base
->assign( str
);
215 inline wxUString
&assign( const wxUString
&str
, size_type pos
, size_type n
)
217 std::basic_string
<wxChar32
> *base
= this;
218 return (wxUString
&) base
->assign( str
, pos
, n
);
221 inline wxUString
&assign( wxChar32 ch
)
223 std::basic_string
<wxChar32
> *base
= this;
224 return (wxUString
&) base
->assign( (size_type
) 1, ch
);
227 inline wxUString
&assign( size_type n
, wxChar32 ch
)
229 std::basic_string
<wxChar32
> *base
= this;
230 return (wxUString
&) base
->assign( n
, ch
);
233 wxUString
&assign( const wxU32CharBuffer
&buf
)
235 return assign( buf
.data() );
238 wxUString
&assign( const char *str
)
240 return assignFromCString( str
);
243 wxUString
&assign( const wxCharBuffer
&buf
)
245 return assignFromCString( buf
.data() );
248 wxUString
&assign( const char *str
, const wxMBConv
&conv
)
250 return assignFromCString( str
, conv
);
253 wxUString
&assign( const wxCharBuffer
&buf
, const wxMBConv
&conv
)
255 return assignFromCString( buf
.data(), conv
);
258 wxUString
&assign( const wxChar16
*str
)
260 return assignFromUTF16( str
);
263 wxUString
&assign( const wxU16CharBuffer
&buf
)
265 return assignFromUTF16( buf
.data() );
268 wxUString
&assign( const wxCStrData
*cstr
)
270 #if SIZEOF_WCHAR_T == 2
271 return assignFromUTF16( cstr
->AsWChar() );
273 return assign( cstr
->AsWChar() );
277 wxUString
&assign( const wxString
&str
)
279 #if wxUSE_UNICODE_UTF8
280 return assignFromUTF8( str
.wx_str() );
282 #if SIZEOF_WCHAR_T == 2
283 return assignFromUTF16( str
.wc_str() );
285 return assign( wc_str() );
290 wxUString
&assign( char ch
)
295 return assignFromCString( buf
);
298 wxUString
&assign( size_type n
, char ch
)
300 wxCharBuffer
buffer(n
);
301 char *p
= buffer
.data();
303 for (i
= 0; i
< n
; i
++)
308 return assignFromCString( buffer
.data() );
311 wxUString
&assign( wxChar16 ch
)
316 return assignFromUTF16( buf
);
319 wxUString
&assign( size_type n
, wxChar16 ch
)
321 wxU16CharBuffer
buffer(n
);
322 wxChar16
*p
= buffer
.data();
324 for (i
= 0; i
< n
; i
++)
329 return assignFromUTF16( buffer
.data() );
332 wxUString
&assign( wxUniChar ch
)
334 return assign( (const wxChar32
) ch
.GetValue() );
337 wxUString
&assign( size_type n
, wxUniChar ch
)
339 return assign( n
, (const wxChar32
) ch
.GetValue() );
342 wxUString
&assign( wxUniCharRef ch
)
344 return assign( (const wxChar32
) ch
.GetValue() );
347 wxUString
&assign( size_type n
, wxUniCharRef ch
)
349 return assign( n
, (const wxChar32
) ch
.GetValue() );
352 // append [STL overload]
354 inline wxUString
&append( const wxUString
&s
)
356 std::basic_string
<wxChar32
> *base
= this;
357 return (wxUString
&) base
->append( s
);
360 inline wxUString
&append( const wxUString
&s
, size_type pos
, size_type n
)
362 std::basic_string
<wxChar32
> *base
= this;
363 return (wxUString
&) base
->append( s
, pos
, n
);
366 inline wxUString
&append( const wxChar32
* s
)
368 std::basic_string
<wxChar32
> *base
= this;
369 return (wxUString
&) base
->append( s
);
372 inline wxUString
&append( const wxChar32
* s
, size_type n
)
374 std::basic_string
<wxChar32
> *base
= this;
375 return (wxUString
&) base
->append( s
, n
);
378 inline wxUString
&append( size_type n
, wxChar32 c
)
380 std::basic_string
<wxChar32
> *base
= this;
381 return (wxUString
&) base
->append( n
, c
);
384 inline wxUString
&append( wxChar32 c
)
386 std::basic_string
<wxChar32
> *base
= this;
387 return (wxUString
&) base
->append( 1, c
);
390 // append [wx overload]
392 wxUString
&append( const wxU16CharBuffer
&buf
)
394 return append( buf
.data() );
397 wxUString
&append( const wxU32CharBuffer
&buf
)
399 return append( buf
.data() );
402 wxUString
&append( const char *str
)
404 return append( wxUString( str
) );
407 wxUString
&append( const wxCharBuffer
&buf
)
409 return append( wxUString( buf
) );
412 wxUString
&append( const wxChar16
*str
)
414 return append( wxUString( str
) );
417 wxUString
&append( const wxString
&str
)
419 return append( wxUString( str
) );
422 wxUString
&append( const wxCStrData
*cstr
)
424 return append( wxUString( cstr
) );
427 wxUString
&append( char ch
)
432 return append( buf
);
435 wxUString
&append( wxChar16 ch
)
440 return append( buf
);
443 wxUString
&append( wxUniChar ch
)
445 return append( (size_type
) 1, (wxChar32
) ch
.GetValue() );
448 wxUString
&append( wxUniCharRef ch
)
450 return append( (size_type
) 1, (wxChar32
) ch
.GetValue() );
454 // insert [STL overloads]
456 inline wxUString
&insert( size_type pos
, const wxUString
&s
)
458 std::basic_string
<wxChar32
> *base
= this;
459 return (wxUString
&) base
->insert( pos
, s
);
462 inline wxUString
&insert( size_type pos
, const wxUString
&s
, size_type pos1
, size_type n
)
464 std::basic_string
<wxChar32
> *base
= this;
465 return (wxUString
&) base
->insert( pos
, s
, pos1
, n
);
468 inline wxUString
&insert( size_type pos
, const wxChar32
*s
)
470 std::basic_string
<wxChar32
> *base
= this;
471 return (wxUString
&) base
->insert( pos
, s
);
474 inline wxUString
&insert( size_type pos
, const wxChar32
*s
, size_type n
)
476 std::basic_string
<wxChar32
> *base
= this;
477 return (wxUString
&) base
->insert( pos
, s
, n
);
480 inline wxUString
&insert( size_type pos
, size_type n
, wxChar32 c
)
482 std::basic_string
<wxChar32
> *base
= this;
483 return (wxUString
&) base
->insert( pos
, n
, c
);
487 // insert [STL overloads]
489 wxUString
&insert( size_type n
, const char *s
)
491 return insert( n
, wxUString( s
) );
494 wxUString
&insert( size_type n
, const wxChar16
*s
)
496 return insert( n
, wxUString( s
) );
499 wxUString
&insert( size_type n
, const wxCharBuffer
&buf
)
501 return insert( n
, wxUString( buf
) );
504 wxUString
&insert( size_type n
, const wxU16CharBuffer
&buf
)
506 return insert( n
, wxUString( buf
) );
509 wxUString
&insert( size_type n
, const wxU32CharBuffer
&buf
)
511 return insert( n
, buf
.data() );
514 wxUString
&insert( size_type n
, const wxString
&s
)
516 return insert( n
, wxUString( s
) );
519 wxUString
&insert( size_type n
, const wxCStrData
*cstr
)
521 return insert( n
, wxUString( cstr
) );
524 wxUString
&insert( size_type n
, char ch
)
529 return insert( n
, buf
);
532 wxUString
&insert( size_type n
, wchar_t ch
)
537 return insert( n
, buf
);
542 iterator
insert( iterator it
, wxChar32 ch
)
544 std::basic_string
<wxChar32
> *base
= this;
545 return base
->insert( it
, ch
);
548 void insert(iterator it
, const_iterator first
, const_iterator last
)
550 std::basic_string
<wxChar32
> *base
= this;
551 base
->insert( it
, first
, last
);
556 inline wxUString
& operator=(const wxUString
& s
)
557 { return assign( s
); }
558 inline wxUString
& operator=(const wxString
& s
)
559 { return assign( s
); }
560 inline wxUString
& operator=(const wxCStrData
* s
)
561 { return assign( s
); }
562 inline wxUString
& operator=(const char *s
)
563 { return assign( s
); }
564 inline wxUString
& operator=(const wxChar16
*s
)
565 { return assign( s
); }
566 inline wxUString
& operator=(const wxChar32
*s
)
567 { return assign( s
); }
568 inline wxUString
& operator=(const wxCharBuffer
&s
)
569 { return assign( s
); }
570 inline wxUString
& operator=(const wxU16CharBuffer
&s
)
571 { return assign( s
); }
572 inline wxUString
& operator=(const wxU32CharBuffer
&s
)
573 { return assign( s
); }
574 inline wxUString
& operator=(const char ch
)
575 { return assign( ch
); }
576 inline wxUString
& operator=(const wxChar16 ch
)
577 { return assign( ch
); }
578 inline wxUString
& operator=(const wxChar32 ch
)
579 { return assign( ch
); }
580 inline wxUString
& operator=(const wxUniChar ch
)
581 { return assign( ch
); }
582 inline wxUString
& operator=(const wxUniCharRef ch
)
583 { return assign( ch
); }
586 inline wxUString
& operator+=(const wxUString
& s
)
587 { return append( s
); }
588 inline wxUString
& operator+=(const wxString
& s
)
589 { return append( s
); }
590 inline wxUString
& operator+=(const wxCStrData
* s
)
591 { return append( s
); }
592 inline wxUString
& operator+=(const char *s
)
593 { return append( s
); }
594 inline wxUString
& operator+=(const wxChar16
*s
)
595 { return append( s
); }
596 inline wxUString
& operator+=(const wxChar32
*s
)
597 { return append( s
); }
598 inline wxUString
& operator+=(const wxCharBuffer
&s
)
599 { return append( s
); }
600 inline wxUString
& operator+=(const wxU16CharBuffer
&s
)
601 { return append( s
); }
602 inline wxUString
& operator+=(const wxU32CharBuffer
&s
)
603 { return append( s
); }
604 inline wxUString
& operator+=(const char ch
)
605 { return append( ch
); }
606 inline wxUString
& operator+=(const wxChar16 ch
)
607 { return append( ch
); }
608 inline wxUString
& operator+=(const wxChar32 ch
)
609 { return append( ch
); }
610 inline wxUString
& operator+=(const wxUniChar ch
)
611 { return append( ch
); }
612 inline wxUString
& operator+=(const wxUniCharRef ch
)
613 { return append( ch
); }
617 inline wxUString
operator+(const wxUString
&s1
, const wxUString
&s2
)
618 { wxUString
ret( s1
); ret
.append( s2
); return ret
; }
619 inline wxUString
operator+(const wxUString
&s1
, const char *s2
)
620 { return s1
+ wxUString(s2
); }
621 inline wxUString
operator+(const wxUString
&s1
, const wxString
&s2
)
622 { return s1
+ wxUString(s2
); }
623 inline wxUString
operator+(const wxUString
&s1
, const wxCStrData
*s2
)
624 { return s1
+ wxUString(s2
); }
625 inline wxUString
operator+(const wxUString
&s1
, const wxChar16
* s2
)
626 { return s1
+ wxUString(s2
); }
627 inline wxUString
operator+(const wxUString
&s1
, const wxChar32
*s2
)
628 { return s1
+ wxUString(s2
); }
629 inline wxUString
operator+(const wxUString
&s1
, const wxCharBuffer
&s2
)
630 { return s1
+ wxUString(s2
); }
631 inline wxUString
operator+(const wxUString
&s1
, const wxU16CharBuffer
&s2
)
632 { return s1
+ wxUString(s2
); }
633 inline wxUString
operator+(const wxUString
&s1
, const wxU32CharBuffer
&s2
)
634 { return s1
+ wxUString(s2
); }
635 inline wxUString
operator+(const wxUString
&s1
, char s2
)
636 { return s1
+ wxUString(s2
); }
637 inline wxUString
operator+(const wxUString
&s1
, wxChar32 s2
)
638 { wxUString
ret( s1
); ret
.append( s2
); return ret
; }
639 inline wxUString
operator+(const wxUString
&s1
, wxChar16 s2
)
640 { wxUString
ret( s1
); ret
.append( (wxChar32
) s2
); return ret
; }
641 inline wxUString
operator+(const wxUString
&s1
, wxUniChar s2
)
642 { wxUString
ret( s1
); ret
.append( (wxChar32
) s2
.GetValue() ); return ret
; }
643 inline wxUString
operator+(const wxUString
&s1
, wxUniCharRef s2
)
644 { wxUString
ret( s1
); ret
.append( (wxChar32
) s2
.GetValue() ); return ret
; }
646 inline wxUString
operator+(const char *s1
, const wxUString
&s2
)
647 { return wxUString(s1
) + s2
; }
648 inline wxUString
operator+(const wxString
&s1
, const wxUString
&s2
)
649 { return wxUString(s1
) + s2
; }
650 inline wxUString
operator+(const wxCStrData
*s1
, const wxUString
&s2
)
651 { return wxUString(s1
) + s2
; }
652 inline wxUString
operator+(const wxChar16
* s1
, const wxUString
&s2
)
653 { return wxUString(s1
) + s2
; }
654 inline wxUString
operator+(const wxChar32
*s1
, const wxUString
&s2
)
655 { return wxUString(s1
) + s2
; }
656 inline wxUString
operator+(const wxCharBuffer
&s1
, const wxUString
&s2
)
657 { return wxUString(s1
) + s2
; }
658 inline wxUString
operator+(const wxU16CharBuffer
&s1
, const wxUString
&s2
)
659 { return wxUString(s1
) + s2
; }
660 inline wxUString
operator+(const wxU32CharBuffer
&s1
, const wxUString
&s2
)
661 { return wxUString(s1
) + s2
; }
662 inline wxUString
operator+(char s1
, const wxUString
&s2
)
663 { return wxUString(s1
) + s2
; }
664 inline wxUString
operator+(wxChar32 s1
, const wxUString
&s2
)
665 { return wxUString(s1
) + s2
; }
666 inline wxUString
operator+(wxChar16 s1
, const wxUString
&s2
)
667 { return wxUString(s1
) + s2
; }
668 inline wxUString
operator+(wxUniChar s1
, const wxUString
&s2
)
669 { return wxUString(s1
) + s2
; }
670 inline wxUString
operator+(wxUniCharRef s1
, const wxUString
&s2
)
671 { return wxUString(s1
) + s2
; }
674 inline bool operator==(const wxUString
& s1
, const wxUString
& s2
)
675 { return s1
.compare( s2
) == 0; }
676 inline bool operator!=(const wxUString
& s1
, const wxUString
& s2
)
677 { return s1
.compare( s2
) != 0; }
678 inline bool operator< (const wxUString
& s1
, const wxUString
& s2
)
679 { wxPrintf( "test\n"); return s1
.compare( s2
) < 0; }
680 inline bool operator> (const wxUString
& s1
, const wxUString
& s2
)
681 { return s1
.compare( s2
) > 0; }
682 inline bool operator<=(const wxUString
& s1
, const wxUString
& s2
)
683 { return s1
.compare( s2
) <= 0; }
684 inline bool operator>=(const wxUString
& s1
, const wxUString
& s2
)
685 { return s1
.compare( s2
) >= 0; }
687 #define wxUSTRING_COMP_OPERATORS( T ) \
688 inline bool operator==(const wxUString& s1, T s2) \
689 { return s1.compare( wxUString(s2) ) == 0; } \
690 inline bool operator!=(const wxUString& s1, T s2) \
691 { return s1.compare( wxUString(s2) ) != 0; } \
692 inline bool operator< (const wxUString& s1, T s2) \
693 { return s1.compare( wxUString(s2) ) < 0; } \
694 inline bool operator> (const wxUString& s1, T s2) \
695 { return s1.compare( wxUString(s2) ) > 0; } \
696 inline bool operator<=(const wxUString& s1, T s2) \
697 { return s1.compare( wxUString(s2) ) <= 0; } \
698 inline bool operator>=(const wxUString& s1, T s2) \
699 { return s1.compare( wxUString(s2) ) >= 0; } \
701 inline bool operator==(T s2, const wxUString& s1) \
702 { return s1.compare( wxUString(s2) ) == 0; } \
703 inline bool operator!=(T s2, const wxUString& s1) \
704 { return s1.compare( wxUString(s2) ) != 0; } \
705 inline bool operator< (T s2, const wxUString& s1) \
706 { return s1.compare( wxUString(s2) ) > 0; } \
707 inline bool operator> (T s2, const wxUString& s1) \
708 { return s1.compare( wxUString(s2) ) < 0; } \
709 inline bool operator<=(T s2, const wxUString& s1) \
710 { return s1.compare( wxUString(s2) ) >= 0; } \
711 inline bool operator>=(T s2, const wxUString& s1) \
712 { return s1.compare( wxUString(s2) ) <= 0; }
714 wxUSTRING_COMP_OPERATORS( const wxString
& )
715 wxUSTRING_COMP_OPERATORS( const char * )
716 wxUSTRING_COMP_OPERATORS( const wxChar16
* )
717 wxUSTRING_COMP_OPERATORS( const wxChar32
* )
718 wxUSTRING_COMP_OPERATORS( const wxCharBuffer
& )
719 wxUSTRING_COMP_OPERATORS( const wxU16CharBuffer
& )
720 wxUSTRING_COMP_OPERATORS( const wxU32CharBuffer
& )
721 wxUSTRING_COMP_OPERATORS( const wxCStrData
* )
724 // _WX_USTRING_H_BASE_