1 // Copyright 2009 The Archiveopteryx Developers <info@aox.org>
2
3 #ifndef USTRING_H
4 #define USTRING_H
5
6 #include "global.h"
7
8 class EString;
9
10
11 class UStringData
12 : public Garbage
13 {
14 private:
UStringData()15 UStringData(): str( 0 ), len( 0 ), max( 0 ) {
16 setFirstNonPointer( &len );
17 }
18 UStringData( int );
19
20 friend class UString;
21 friend bool operator==( const class UString &, const class UString & );
22 friend bool operator==( const UString &, const char * );
23 void * operator new( size_t, uint );
new(size_t s)24 void * operator new( size_t s ) { return Garbage::operator new( s); }
25
26 uint * str;
27 uint len;
28 uint max;
29 };
30
31
32 class UString
33 : public Garbage
34 {
35 public:
36 UString();
37 UString( const UString & );
38 ~UString();
39
40 UString & operator=( const UString & );
41 UString & operator+=( const UString & str );
42
43 void operator delete( void * );
44
45 // const, returns zero when used beyond the end
46 uint operator[]( uint i ) const {
47 if ( !d || i >= d->len )
48 return 0;
49 return d->str[i];
50 }
51
isEmpty()52 bool isEmpty() const { return !d || d->len == 0; }
length()53 uint length() const { return d ? d->len : 0; }
54
55 void append( const UString & );
56 void append( const uint );
57 void append( const char * );
58
59 void reserve( uint );
60 void truncate( uint = 0 );
61
62 bool isAscii() const;
63 EString ascii() const;
64 EString utf8() const;
65
66 int find( char, int=0 ) const;
67 int find( const UString &, int=0 ) const;
68 bool contains( const UString & ) const;
69 bool contains( const char ) const;
70 bool contains( const char * ) const;
71
72 UString section( const char *, uint ) const;
73 UString section( const UString &, uint ) const;
74
75 UString mid( uint, uint = UINT_MAX ) const;
76 uint number( bool *, uint = 10 ) const;
77 UString simplified() const;
78 UString trimmed() const;
79
data()80 inline const uint * data() const { return d ? (const uint*)d->str : 0; }
81
82 UString titlecased() const;
83
detach()84 inline void detach() { if ( !modifiable() ) reserve( length() ); }
85
modifiable()86 bool modifiable() const { return d && d->max > 0; }
87
88 bool operator<( const UString & ) const;
89 bool operator>( const UString & ) const;
90 bool operator<=( const UString & ) const;
91 bool operator>=( const UString & ) const;
92
93 int compare( const UString & ) const;
94
95 bool startsWith( const EString & ) const;
96 bool startsWith( const UString & ) const;
97 bool startsWith( const char * ) const;
98 bool endsWith( const EString & ) const;
99 bool endsWith( const UString & ) const;
100 bool endsWith( const char * ) const;
101
102 static bool isDigit( uint );
103 static bool isLetter( uint );
104 static bool isSpace( uint );
105
106 private:
107 void reserve2( uint );
108
109
110 private:
111 class UStringData * d;
112 };
113
114
115 inline bool operator==( const UString & s1, const UString & s2 )
116 {
117 if ( s1.length() != s2.length() )
118 return false;
119 uint i = 0;
120 while ( i < s1.length() ) {
121 if ( s1[i] != s2[i] )
122 return false;
123 i++;
124 }
125 return true;
126 }
127
128
129 inline bool operator!=( const UString & s1, const UString & s2 )
130 {
131 return !( s1 == s2 );
132 }
133
134
135 inline bool operator==( const UString & s1, const char * s2 )
136 {
137 if ( !s2 )
138 return false;
139 uint i = 0;
140 while ( i < s1.length() ) {
141 if ( s2[i] < 32 || s2[i] >= 127 || s1[i] != s2[i] )
142 return false;
143 i++;
144 }
145 if ( s2[i] )
146 return false;
147 return true;
148 }
149
150
151 inline bool operator!=( const UString & s1, const char * s2 )
152 {
153 return !( s1 == s2 );
154 }
155
156
157 extern const UString operator+( const UString & a, const UString & b );
158 extern const UString operator+( const UString & a, const char * b );
159 extern const UString operator+( const char * a, const UString & b );
160 extern const UString operator+=( const UString & a, const UString & b );
161
us(const char * s)162 inline UString us( const char * s )
163 {
164 UString u;
165 u.append( s );
166 return u;
167 }
168
169
170 #endif
171