sources/mystring.h

changeset 69
eb4c25284a19
parent 66
bd28a5730fd0
child 73
07dda51a7a8e
equal deleted inserted replaced
68:202e74157de5 69:eb4c25284a19
55 m_string (data) {} 55 m_string (data) {}
56 56
57 String (const Vector<char>& data) : 57 String (const Vector<char>& data) :
58 m_string (data.data(), data.size()) {} 58 m_string (data.data(), data.size()) {}
59 59
60 inline METHOD append (const char* data) -> void; 60 using Iterator = std::string::iterator;
61 inline METHOD append (char data) -> void; 61 using ConstIterator = std::string::const_iterator;
62 inline METHOD append (const String& data) -> void;
63 inline METHOD begin() -> std::string::iterator;
64 inline METHOD begin() const -> std::string::const_iterator;
65 inline METHOD clear() -> void;
66 METHOD compare (const String& other) const -> int;
67 METHOD count (char needle) const -> int;
68 inline METHOD chars() const -> const char*;
69 inline METHOD end() -> std::string::iterator;
70 inline METHOD end() const -> std::string::const_iterator;
71 METHOD ends_with (const String& other) -> bool;
72 METHOD find (const char* c, int a = 0) const -> int;
73 METHOD to_lowercase() const -> String;
74 inline METHOD index_difference (int a, int b) -> int;
75 inline METHOD insert (int pos, char c) -> void;
76 inline METHOD insert (int pos, const char* c) -> void;
77 inline METHOD is_empty() const -> bool;
78 METHOD is_numeric() const -> bool;
79 METHOD find_last (const char* c, int a = -1) const -> int;
80 inline METHOD length() const -> int;
81 METHOD mask_against (const String& pattern) const -> bool;
82 METHOD md5() const -> String;
83 METHOD mid (long a, long b = -1) const -> String;
84 inline METHOD modify_index (int& a) -> void;
85 METHOD normalize (int (*filter)(int) = &std::isspace) -> void;
86 inline METHOD prepend (String a) -> void;
87 inline METHOD remove (int pos, int len) -> void;
88 inline METHOD remove_at (int pos) -> void;
89 inline METHOD remove_from_end (int len) -> void;
90 inline METHOD remove_from_start (int len) -> void;
91 METHOD replace (const char* a, const char* b) -> void;
92 inline METHOD replace (int pos, int n, const String& a) -> void;
93 inline METHOD shrink_to_fit() -> void;
94 METHOD split (const String& del) const -> StringList;
95 METHOD split (char del) const -> StringList;
96 METHOD sprintf (const char* fmtstr, ...) -> void;
97 METHOD starts_with (const String& other) -> bool;
98 inline METHOD std_string() const -> const std::string&;
99 inline METHOD strip (char unwanted) -> String;
100 METHOD strip (const List<char>& unwanted) -> String;
101 METHOD to_double (bool* ok = nullptr) const -> double;
102 METHOD to_float (bool* ok = nullptr) const -> float;
103 METHOD to_int (bool* ok = nullptr, int base = 10) const -> long;
104 METHOD trim (int n) -> void;
105 METHOD to_uppercase() const -> String;
106 METHOD word_position (int n) const -> int;
107 62
108 static METHOD from_number (short int a) -> String; 63 ConstIterator begin() const { return m_string.cbegin(); }
109 static METHOD from_number (int a) -> String; 64 int compare (const String &other) const;
110 static METHOD from_number (long int a) -> String; 65 int count (char needle) const;
111 static METHOD from_number (unsigned short int a) -> String; 66 const char* chars() const { return m_string.c_str(); }
112 static METHOD from_number (unsigned int a) -> String; 67 ConstIterator end() const { return m_string.end(); }
113 static METHOD from_number (unsigned long int a) -> String; 68 int find (const char*c, int a = 0) const;
114 static METHOD from_number (double a) -> String; 69 bool is_empty() const { return m_string[0] == '\0'; }
70 bool is_numeric() const;
71 int find_last (const char*c, int a) const;
72 int length() const { return m_string.length(); }
73 bool mask_against (const String &pattern) const;
74 String md5() const;
75 String mid (long a, long b) const;
76 StringList split (const String &del) const;
77 StringList split (char del) const;
78 const std::string& std_string() const { return m_string; }
79 double to_double (bool* ok = nullptr) const;
80 float to_float (bool* ok = nullptr) const;
81 long to_int (bool* ok = nullptr, int base = 10) const;
82 String to_lowercase() const;
83 String to_uppercase() const;
84 int word_position (int n) const;
115 85
116 METHOD operator+ (const String& data) const -> String; 86 void append (const char* data) { m_string.append (data); }
117 METHOD operator+ (const char* data) const -> String; 87 void append (char data) { m_string.push_back (data); }
118 inline METHOD operator+ (int num) const -> String; 88 void append (const String& data) { m_string.append (data.chars()); }
119 inline METHOD operator+= (const String& data) -> String&; 89 Iterator begin() { return m_string.begin(); }
120 inline METHOD operator+= (const char* data) -> String&; 90 void clear() { m_string.clear(); }
121 inline METHOD operator+= (int num) -> String&; 91 Iterator end() { return m_string.end(); }
122 inline METHOD operator+= (char data) -> String&; 92 bool ends_with (const String &other);
123 inline METHOD operator== (const String& other) const -> bool; 93 int index_difference (int a, int b) { modify_index (a); modify_index (b); return b - a; }
124 inline METHOD operator== (const char* other) const -> bool; 94 void insert (int pos, char c) { m_string.insert (m_string.begin() + pos, c); }
125 inline METHOD operator!= (const String& other) const -> bool; 95 void insert (int pos, const char*c) { m_string.insert (pos, c); }
126 inline METHOD operator!= (const char* other) const -> bool; 96 void modify_index (int &a) { if (a < 0) { a = length() - a; } }
127 inline METHOD operator> (const String& other) const -> bool; 97 void normalize (int (*filter)(int) = &std::isspace);
128 inline METHOD operator< (const String& other) const -> bool; 98 void prepend (String a) { m_string = (a + m_string).std_string(); }
129 inline operator const char*() const; 99 void remove (int pos, int len) { m_string.replace (pos, len, ""); }
130 inline operator const std::string&() const; 100 void remove_at (int pos) { m_string.erase (m_string.begin() + pos); }
101 void remove_from_end (int len) { remove (length() - len, len); }
102 void remove_from_start (int len) { remove (0, len); }
103 void replace (const char* a, const char* b);
104 void replace (int pos, int n, const String &a) { m_string.replace (pos, n, a.chars()); }
105 void shrink_to_fit() { m_string.shrink_to_fit(); }
106 void sprintf (const char* fmtstr, ...);
107 bool starts_with (const String &other);
108 String strip (char unwanted) { return strip ({unwanted}); }
109 String strip (const List<char> &unwanted);
110 void trim (int n);
111
112 static String from_number (short int a);
113 static String from_number (int a);
114 static String from_number (long int a);
115 static String from_number (unsigned short int a);
116 static String from_number (unsigned int a);
117 static String from_number (unsigned long int a);
118 static String from_number (double a);
119
120 String operator+ (const String& data) const;
121 String operator+ (const char* data) const;
122 String operator+ (int num) const { return *this + String::from_number (num); }
123 String& operator+= (const String& data) { append (data); return *this; }
124 String& operator+= (const char* data) { append (data); return *this; }
125 String& operator+= (int num) { return operator+= (String::from_number (num)); }
126 String& operator+= (char data) { append (data); return *this; }
127 bool operator== (const String& other) const { return std_string() == other.std_string(); }
128 bool operator== (const char* other) const { return m_string == other; }
129 bool operator!= (const String& other) const { return std_string() != other.std_string(); }
130 bool operator!= (const char* other) const { return m_string != other; }
131 bool operator> (const String& other) const { return std_string() > other.std_string(); }
132 bool operator< (const String& other) const { return std_string() < other.std_string(); }
133 bool operator>= (const String& other) const { return std_string() >= other.std_string(); }
134 bool operator<= (const String& other) const { return std_string() <= other.std_string(); }
135 operator const char*() const { return chars(); }
136 operator const std::string&() const { return std_string(); }
131 137
132 private: 138 private:
133 std::string m_string; 139 std::string m_string;
134 }; 140 };
135 141
136 // ------------------------------------------------------------------------------------------------- 142 // -------------------------------------------------------------------------------------------------
137 // 143 //
138 class StringList : public List<String> 144 class StringList : public List<String>
139 { 145 {
140 public: 146 public:
141 template<typename... Args> 147 using Super = List<String>;
142 StringList (Args... args) : 148 using Super::Super;
143 List<String> (args...) {}
144 149
145 METHOD join (const String& delim) -> String; 150 StringList() {}
151
152 StringList (const Super& other) :
153 Super (other) {}
154
155 String join (const String& delim);
146 }; 156 };
147 157
148 // ------------------------------------------------------------------------------------------------- 158 // -------------------------------------------------------------------------------------------------
149 // 159 //
150 inline FUNCTION operator== (const char* a, const String& b) -> bool; 160 inline bool operator== (const char* a, const String& b)
151 inline FUNCTION operator+ (const char* a, const String& b) -> String;
152
153 // -------------------------------------------------------------------------------------------------
154 //
155 inline METHOD
156 String::is_empty() const -> bool
157 {
158 return m_string[0] == '\0';
159 }
160
161 // -------------------------------------------------------------------------------------------------
162 //
163 inline METHOD
164 String::append (const char* data) -> void
165 {
166 m_string.append (data);
167 }
168
169 // -------------------------------------------------------------------------------------------------
170 //
171 inline METHOD
172 String::append (char data) -> void
173 {
174 m_string.push_back (data);
175 }
176
177 // -------------------------------------------------------------------------------------------------
178 //
179 inline METHOD
180 String::append (const String& data) -> void
181 {
182 m_string.append (data.chars());
183 }
184
185 // -------------------------------------------------------------------------------------------------
186 //
187 inline METHOD
188 String::begin() -> std::string::iterator
189 {
190 return m_string.begin();
191 }
192
193 // -------------------------------------------------------------------------------------------------
194 //
195 inline METHOD
196 String::begin() const -> std::string::const_iterator
197 {
198 return m_string.cbegin();
199 }
200
201 // -------------------------------------------------------------------------------------------------
202 //
203 inline const char* String::chars() const
204 {
205 return m_string.c_str();
206 }
207
208 // -------------------------------------------------------------------------------------------------
209 //
210 inline METHOD
211 String::end() -> std::string::iterator
212 {
213 return m_string.end();
214 }
215
216 // -------------------------------------------------------------------------------------------------
217 //
218 inline METHOD
219 String::end() const -> std::string::const_iterator
220 {
221 return m_string.end();
222 }
223
224 // -------------------------------------------------------------------------------------------------
225 //
226 inline METHOD
227 String::clear() -> void
228 {
229 m_string.clear();
230 }
231
232 // -------------------------------------------------------------------------------------------------
233 //
234 inline METHOD
235 String::remove_at (int pos) -> void
236 {
237 m_string.erase (m_string.begin() + pos);
238 }
239
240 // -------------------------------------------------------------------------------------------------
241 //
242 inline METHOD
243 String::insert (int pos, char c) -> void
244 {
245 m_string.insert (m_string.begin() + pos, c);
246 }
247
248 // -------------------------------------------------------------------------------------------------
249 //
250 inline METHOD
251 String::insert (int pos, const char* c) -> void
252 {
253 m_string.insert (pos, c);
254 }
255
256 // -------------------------------------------------------------------------------------------------
257 //
258 inline METHOD
259 String::length() const -> int
260 {
261 return m_string.length();
262 }
263
264 // -------------------------------------------------------------------------------------------------
265 //
266 inline METHOD
267 String::remove (int pos, int len) -> void
268 {
269 m_string.replace (pos, len, "");
270 }
271
272 // -------------------------------------------------------------------------------------------------
273 //
274 inline METHOD
275 String::remove_from_start (int len) -> void
276 {
277 remove (0, len);
278 }
279
280 // -------------------------------------------------------------------------------------------------
281 //
282 inline METHOD
283 String::remove_from_end (int len) -> void
284 {
285 remove (length() - len, len);
286 }
287
288 // -------------------------------------------------------------------------------------------------
289 //
290 inline METHOD
291 String::replace (int pos, int n, const String& a) -> void
292 {
293 m_string.replace (pos, n, a.chars());
294 }
295
296 // -------------------------------------------------------------------------------------------------
297 //
298 inline METHOD
299 String::shrink_to_fit() -> void
300 {
301 m_string.shrink_to_fit();
302 }
303
304 // -------------------------------------------------------------------------------------------------
305 //
306 inline const std::string& String::std_string() const
307 {
308 return m_string;
309 }
310
311 // -------------------------------------------------------------------------------------------------
312 //
313 inline METHOD
314 String::strip (char unwanted) -> String
315 {
316 return strip ({unwanted});
317 }
318
319 // -------------------------------------------------------------------------------------------------
320 //
321 inline METHOD
322 String::operator+ (int num) const -> String
323 {
324 return *this + String::from_number (num);
325 }
326
327 // -------------------------------------------------------------------------------------------------
328 //
329 inline METHOD
330 String::operator+= (const String& data) -> String&
331 {
332 append (data);
333 return *this;
334 }
335
336 // -------------------------------------------------------------------------------------------------
337 //
338 inline METHOD
339 String::operator+= (const char* data) -> String&
340 {
341 append (data);
342 return *this;
343 }
344
345 // -------------------------------------------------------------------------------------------------
346 //
347 inline METHOD
348 String::operator+= (int num) -> String&
349 {
350 return operator+= (String::from_number (num));
351 }
352
353 // -------------------------------------------------------------------------------------------------
354 //
355 inline METHOD
356 String::prepend (String a) -> void
357 {
358 m_string = (a + m_string).std_string();
359 }
360
361 // -------------------------------------------------------------------------------------------------
362
363 inline METHOD
364 String::operator+= (char data) -> String&
365 {
366 append (data);
367 return *this;
368 }
369
370 // -------------------------------------------------------------------------------------------------
371 //
372 inline METHOD
373 String::operator== (const String& other) const -> bool
374 {
375 return std_string() == other.std_string();
376 }
377
378 // -------------------------------------------------------------------------------------------------
379 //
380 inline METHOD
381 String::operator== (const char* other) const -> bool
382 {
383 return operator== (String (other));
384 }
385
386 // -------------------------------------------------------------------------------------------------
387 //
388 inline METHOD
389 String::operator!= (const String& other) const -> bool
390 {
391 return std_string() != other.std_string();
392 }
393
394 // -------------------------------------------------------------------------------------------------
395 //
396 inline METHOD
397 String::operator!= (const char* other) const -> bool
398 {
399 return operator!= (String (other));
400 }
401
402 // -------------------------------------------------------------------------------------------------
403 //
404 inline METHOD
405 String::operator> (const String& other) const -> bool
406 {
407 return std_string() > other.std_string();
408 }
409
410 // -------------------------------------------------------------------------------------------------
411 //
412 inline METHOD
413 String::operator< (const String& other) const -> bool
414 {
415 return std_string() < other.std_string();
416 }
417
418 // -------------------------------------------------------------------------------------------------
419 //
420 inline String::operator const char*() const
421 {
422 return chars();
423 }
424
425 // -------------------------------------------------------------------------------------------------
426 //
427 inline String::operator const std::string&() const
428 {
429 return std_string();
430 }
431
432 // -------------------------------------------------------------------------------------------------
433 //
434 inline METHOD
435 String::modify_index (int& a) -> void
436 {
437 if (a < 0)
438 a = length() - a;
439 }
440
441 // -------------------------------------------------------------------------------------------------
442 //
443 inline METHOD
444 String::index_difference (int a, int b) -> int
445 {
446 modify_index (a);
447 modify_index (b);
448 return b - a;
449 }
450
451 // -------------------------------------------------------------------------------------------------
452 //
453 inline FUNCTION
454 operator== (const char* a, const String& b) -> bool
455 { 161 {
456 return b == a; 162 return b == a;
457 } 163 }
458 164
459 // ------------------------------------------------------------------------------------------------- 165 // -------------------------------------------------------------------------------------------------
460 // 166 //
461 inline FUNCTION 167 inline String operator+ (const char* a, const String& b)
462 operator+ (const char* a, const String& b) -> String
463 { 168 {
464 return String (a) + b; 169 return String (a) + b;
465 } 170 }

mercurial