src/String.h

changeset 115
9be16e1c1e44
parent 88
5def6ff8b466
equal deleted inserted replaced
114:6cbeb9f8350f 115:9be16e1c1e44
48 using ConstIterator = StringType::const_iterator; 48 using ConstIterator = StringType::const_iterator;
49 49
50 String() {} 50 String() {}
51 51
52 explicit String (char a) : 52 explicit String (char a) :
53 mString (&a) {} 53 m_string (&a) {}
54 54
55 String (const char* data) : 55 String (const char* data) :
56 mString (data) {} 56 m_string (data) {}
57 57
58 String (const StringType& data) : 58 String (const StringType& data) :
59 mString (data) {} 59 m_string (data) {}
60 60
61 void Dump() const; 61 void dump() const;
62 int Compare (const String& other) const; 62 int compare (const String& other) const;
63 bool EndsWith (const String& other); 63 bool endsWith (const String& other);
64 int Count (char needle) const; 64 int count (char needle) const;
65 int FirstIndexOf (const char* c, int a = 0) const; 65 int firstIndexOf (const char* c, int a = 0) const;
66 int LastIndexOf (const char* c, int a = -1) const; 66 int lastIndexOf (const char* c, int a = -1) const;
67 String ToLowercase() const; 67 String toLowercase() const;
68 bool IsNumeric() const; 68 bool isNumeric() const;
69 bool MaskAgainst (const String& pattern) const; 69 bool maskAgainst (const String& pattern) const;
70 int WordPosition (int n) const; 70 int wordPosition (int n) const;
71 void Replace (const char* a, const char* b); 71 void replace (const char* a, const char* b);
72 StringList Split (String del) const; 72 StringList split (const String& del) const;
73 StringList Split (char del) const; 73 StringList split (char del) const;
74 void SPrintf (const char* fmtstr, ...); 74 void sprintf (const char* fmtstr, ...);
75 bool StartsWith (const String& other); 75 bool startsWith (const String& other);
76 String Strip (const List<char>& unwanted); 76 String strip (const List<char>& unwanted);
77 String Mid (long a, long b = -1) const; 77 String mid (long a, long b = -1) const;
78 double ToDouble (bool* ok = nullptr) const; 78 double toDouble (bool* ok = nullptr) const;
79 float ToFloat (bool* ok = nullptr) const; 79 float toFloat (bool* ok = nullptr) const;
80 long ToLong (bool* ok = nullptr, int base = 10) const; 80 long toLong (bool* ok = nullptr, int base = 10) const;
81 void Trim (int n); 81 void trim (int n);
82 String ToUppercase() const; 82 String toUppercase() const;
83 83
84 String operator+ (const String& data) const; 84 String operator+ (const String& data) const;
85 String operator+ (const char* data) const; 85 String operator+ (const char* data) const;
86 86
87 static String FromNumber (int a); 87 static String fromNumber (int a);
88 static String FromNumber (long a); 88 static String fromNumber (long a);
89 89
90 inline bool IsEmpty() const 90 inline bool isEmpty() const
91 { 91 {
92 return mString[0] == '\0'; 92 return m_string[0] == '\0';
93 } 93 }
94 94
95 inline void Append (const char* data) 95 inline void append (const char* data)
96 { 96 {
97 mString.append (data); 97 m_string.append (data);
98 } 98 }
99 99
100 inline void Append (const char data) 100 inline void append (char data)
101 { 101 {
102 mString.push_back (data); 102 m_string.push_back (data);
103 } 103 }
104 104
105 inline void Append (const String& data) 105 inline void append (const String& data)
106 { 106 {
107 mString.append (data.CString()); 107 m_string.append (data.chars());
108 } 108 }
109 109
110 inline Iterator begin() 110 inline Iterator begin()
111 { 111 {
112 return mString.begin(); 112 return m_string.begin();
113 } 113 }
114 114
115 inline ConstIterator begin() const 115 inline ConstIterator begin() const
116 { 116 {
117 return mString.cbegin(); 117 return m_string.cbegin();
118 } 118 }
119 119
120 inline const char* CString() const 120 inline const char* chars() const
121 { 121 {
122 return mString.c_str(); 122 return m_string.c_str();
123 } 123 }
124 124
125 inline const char* c_str() const 125 inline const char* c_str() const
126 { 126 {
127 return mString.c_str(); 127 return m_string.c_str();
128 } 128 }
129 129
130 inline Iterator end() 130 inline Iterator end()
131 { 131 {
132 return mString.end(); 132 return m_string.end();
133 } 133 }
134 134
135 inline ConstIterator end() const 135 inline ConstIterator end() const
136 { 136 {
137 return mString.end(); 137 return m_string.end();
138 } 138 }
139 139
140 inline void Clear() 140 inline void clear()
141 { 141 {
142 mString.clear(); 142 m_string.clear();
143 } 143 }
144 144
145 inline void RemoveAt (int pos) 145 inline void removeAt (int pos)
146 { 146 {
147 mString.erase (mString.begin() + pos); 147 m_string.erase (m_string.begin() + pos);
148 } 148 }
149 149
150 inline void Insert (int pos, char c) 150 inline void insert (int pos, char c)
151 { 151 {
152 mString.insert (mString.begin() + pos, c); 152 m_string.insert (m_string.begin() + pos, c);
153 } 153 }
154 154
155 inline int Length() const 155 inline int length() const
156 { 156 {
157 return mString.length(); 157 return m_string.length();
158 } 158 }
159 159
160 inline void Remove (int pos, int len) 160 inline void remove (int pos, int len)
161 { 161 {
162 mString.replace (pos, len, ""); 162 m_string.replace (pos, len, "");
163 } 163 }
164 164
165 inline void RemoveFromStart (int len) 165 inline void removeFromStart (int len)
166 { 166 {
167 Remove (0, len); 167 remove (0, len);
168 } 168 }
169 169
170 inline void RemoveFromEnd (int len) 170 inline void removeFromEnd (int len)
171 { 171 {
172 Remove (Length() - len, len); 172 remove (length() - len, len);
173 } 173 }
174 174
175 inline void Replace (int pos, int n, const String& a) 175 inline void replace (int pos, int n, const String& a)
176 { 176 {
177 mString.replace (pos, n, a.CString()); 177 m_string.replace (pos, n, a.chars());
178 } 178 }
179 179
180 inline void ShrinkToFit() 180 inline void shrinkToFit()
181 { 181 {
182 mString.shrink_to_fit(); 182 m_string.shrink_to_fit();
183 } 183 }
184 184
185 inline const StringType& STDString() const 185 inline const StringType& stdString() const
186 { 186 {
187 return mString; 187 return m_string;
188 } 188 }
189 189
190 inline String Strip (char unwanted) 190 inline String strip (char unwanted)
191 { 191 {
192 return Strip ({unwanted}); 192 return strip ({unwanted});
193 } 193 }
194 194
195 // ============================================================================= 195 // =============================================================================
196 // 196 //
197 inline String operator+ (int num) const 197 inline String operator+ (int num) const
198 { 198 {
199 return *this + String::FromNumber (num); 199 return *this + String::fromNumber (num);
200 } 200 }
201 201
202 // ============================================================================= 202 // =============================================================================
203 // 203 //
204 inline String& operator+= (const String data) 204 inline String& operator+= (const String data)
205 { 205 {
206 Append (data); 206 append (data);
207 return *this; 207 return *this;
208 } 208 }
209 209
210 // ============================================================================= 210 // =============================================================================
211 // 211 //
212 inline String& operator+= (const char* data) 212 inline String& operator+= (const char* data)
213 { 213 {
214 Append (data); 214 append (data);
215 return *this; 215 return *this;
216 } 216 }
217 217
218 // ============================================================================= 218 // =============================================================================
219 // 219 //
220 inline String& operator+= (int num) 220 inline String& operator+= (int num)
221 { 221 {
222 return operator+= (String::FromNumber (num)); 222 return operator+= (String::fromNumber (num));
223 } 223 }
224 224
225 // ============================================================================= 225 // =============================================================================
226 // 226 //
227 inline void Prepend (String a) 227 inline void prepend (String a)
228 { 228 {
229 mString = (a + mString).STDString(); 229 m_string = (a + m_string).stdString();
230 } 230 }
231 231
232 // ============================================================================= 232 // =============================================================================
233 // 233 //
234 inline String& operator+= (const char data) 234 inline String& operator+= (const char data)
235 { 235 {
236 Append (data); 236 append (data);
237 return *this; 237 return *this;
238 } 238 }
239 239
240 // ============================================================================= 240 // =============================================================================
241 // 241 //
242 inline String operator- (int n) const 242 inline String operator- (int n) const
243 { 243 {
244 String newString = mString; 244 String newString = m_string;
245 newString -= n; 245 newString -= n;
246 return newString; 246 return newString;
247 } 247 }
248 248
249 // ============================================================================= 249 // =============================================================================
250 // 250 //
251 inline String& operator-= (int n) 251 inline String& operator-= (int n)
252 { 252 {
253 Trim (n); 253 trim (n);
254 return *this; 254 return *this;
255 } 255 }
256 256
257 // ============================================================================= 257 // =============================================================================
258 // 258 //
259 inline String operator+() const 259 inline String operator+() const
260 { 260 {
261 return ToUppercase(); 261 return toUppercase();
262 } 262 }
263 263
264 // ============================================================================= 264 // =============================================================================
265 // 265 //
266 inline String operator-() const 266 inline String operator-() const
267 { 267 {
268 return ToLowercase(); 268 return toLowercase();
269 } 269 }
270 270
271 // ============================================================================= 271 // =============================================================================
272 // 272 //
273 inline bool operator== (const String& other) const 273 inline bool operator== (const String& other) const
274 { 274 {
275 return STDString() == other.STDString(); 275 return stdString() == other.stdString();
276 } 276 }
277 277
278 // ============================================================================= 278 // =============================================================================
279 // 279 //
280 inline bool operator== (const char* other) const 280 inline bool operator== (const char* other) const
284 284
285 // ============================================================================= 285 // =============================================================================
286 // 286 //
287 inline bool operator!= (const String& other) const 287 inline bool operator!= (const String& other) const
288 { 288 {
289 return STDString() != other.STDString(); 289 return stdString() != other.stdString();
290 } 290 }
291 291
292 // ============================================================================= 292 // =============================================================================
293 // 293 //
294 inline bool operator!= (const char* other) const 294 inline bool operator!= (const char* other) const
298 298
299 // ============================================================================= 299 // =============================================================================
300 // 300 //
301 inline bool operator> (const String& other) const 301 inline bool operator> (const String& other) const
302 { 302 {
303 return STDString() > other.STDString(); 303 return stdString() > other.stdString();
304 } 304 }
305 305
306 // ============================================================================= 306 // =============================================================================
307 // 307 //
308 inline bool operator< (const String& other) const 308 inline bool operator< (const String& other) const
309 { 309 {
310 return STDString() < other.STDString(); 310 return stdString() < other.stdString();
311 } 311 }
312 312
313 // ============================================================================= 313 // =============================================================================
314 // 314 //
315 inline operator const char*() const 315 inline operator const char*() const
316 { 316 {
317 return CString(); 317 return chars();
318 } 318 }
319 319
320 // ============================================================================= 320 // =============================================================================
321 // 321 //
322 inline operator const StringType&() const 322 inline operator const StringType&() const
323 { 323 {
324 return STDString(); 324 return stdString();
325 } 325 }
326 326
327 // ============================================================================= 327 // =============================================================================
328 // 328 //
329 // Difference between indices @a and @b. @b can be -1, in which 329 // Difference between indices @a and @b. @b can be -1, in which
330 // case it will be length() - 1. 330 // case it will be length() - 1.
331 // 331 //
332 inline int IndexDifference (int a, int b) 332 inline int indexDifference (int a, int b)
333 { 333 {
334 assert (b == -1 || b >= a); 334 assert (b == -1 || b >= a);
335 return (b != -1 ? b - a : Length() - 1 - a); 335 return (b != -1 ? b - a : length() - 1 - a);
336 } 336 }
337 337
338 private: 338 private:
339 StringType mString; 339 StringType m_string;
340 }; 340 };
341 341
342 // ============================================================================= 342 // =============================================================================
343 // 343 //
344 class StringList : public List<String> 344 class StringList : public List<String>
345 { 345 {
346 public: 346 public:
347 StringList() {} 347 StringList() {}
348 StringList (std::initializer_list<String> vals) : 348 StringList (std::initializer_list<String> vals) :
349 List<String> (vals) {} 349 List<String> (vals) {}
350 StringList (const List<String>& a) : List<String> (a.GetDeque()) {} 350 StringList (const List<String>& a) : List<String> (a.deque()) {}
351 StringList (const ListType& a) : List<String> (a) {} 351 StringList (const WrappedList& a) : List<String> (a) {}
352 352
353 String Join (const String& delim); 353 String join (const String& delim);
354 }; 354 };
355 355
356 356
357 // ============================================================================= 357 // =============================================================================
358 // 358 //

mercurial