sources/mystring.h

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

mercurial