src/string.cpp

changeset 272
2074672a6554
parent 251
c4b96bc41298
child 273
0a9141118630
equal deleted inserted replaced
271:d5ec224c1879 272:2074672a6554
17 */ 17 */
18 18
19 #include <stdexcept> 19 #include <stdexcept>
20 #include "common.h" 20 #include "common.h"
21 #include "string.h" 21 #include "string.h"
22
23 String::String () {}
24
25 String::String (const char* data) {
26 m_string = data;
27 }
28
29 String::String (const QString data) {
30 m_string = data.toStdString ();
31 }
32
33 String::String (std::string data) {
34 m_string = data;
35 }
22 36
23 str fmt (const char* fmtstr, ...) { 37 str fmt (const char* fmtstr, ...) {
24 va_list va; 38 va_list va;
25 39
26 va_start (va, fmtstr); 40 va_start (va, fmtstr);
57 } while (buf == null); 71 } while (buf == null);
58 72
59 return buf; 73 return buf;
60 } 74 }
61 75
76 void String::append (const char* data) {
77 m_string.append (data);
78 }
79
80 void String::append (const char data) {
81 m_string.push_back (data);
82 }
83
84 void String::append (const String data) {
85 m_string.append (data.chars ());
86 }
87
88 String::it String::begin () {
89 return m_string.begin ();
90 }
91
92 String::c_it String::begin () const {
93 return m_string.cbegin ();
94 }
95
96 const char* String::c () const {
97 return chars ();
98 }
99
100 size_t String::capacity () const {
101 return m_string.capacity ();
102 }
103
104 const char* String::chars () const {
105 return m_string.c_str ();
106 }
107
108 int String::compare (const char* other) const {
109 return m_string.compare (other);
110 }
111
112 int String::compare (String other) const {
113 return m_string.compare (other);
114 }
115
116 String::it String::end () {
117 return m_string.end ();
118 }
119
120 String::c_it String::end () const {
121 return m_string.end ();
122 }
123
124 void String::clear () {
125 m_string.clear ();
126 }
127
128 bool String::empty() const {
129 return m_string.empty ();
130 }
131
132 void String::erase (size_t pos) {
133 m_string.erase (m_string.begin () + pos);
134 }
135
136 void String::insert (size_t pos, char c) {
137 m_string.insert (m_string.begin () + pos, c);
138 }
139
140 size_t String::len () const {
141 return m_string.length ();
142 }
143
144 size_t String::maxSize () const {
145 return m_string.max_size ();
146 }
147
148 void String::resize (size_t n) {
149 m_string.resize (n);
150 }
151
152 void String::shrinkToFit () {
153 m_string.shrink_to_fit ();
154 }
155
156
157
62 void String::trim (short n) { 158 void String::trim (short n) {
63 if (n > 0) 159 if (n > 0)
64 for (short i = 0; i < n; ++i) 160 for (short i = 0; i < n; ++i)
65 m_string.erase (m_string.end () - 1 - i); 161 m_string.erase (m_string.end () - 1 - i);
66 else 162 else
67 for (short i = abs (n) - 1; i >= 0; ++i) 163 for (short i = abs (n) - 1; i >= 0; ++i)
68 m_string.erase (m_string.begin () + i); 164 m_string.erase (m_string.begin () + i);
69 } 165 }
70 166
167 String String::strip (char unwanted) {
168 return strip ({unwanted});
169 }
170
71 String String::strip (std::initializer_list<char> unwanted) { 171 String String::strip (std::initializer_list<char> unwanted) {
72 String copy (m_string); 172 String copy (m_string);
73 173
74 for (char c : unwanted) { 174 for (char c : unwanted) {
75 for (long i = len (); i >= 0; --i) 175 for (long i = len (); i >= 0; --i)
110 vector<String> res; 210 vector<String> res;
111 size_t a = 0; 211 size_t a = 0;
112 212
113 // Find all separators and store the text left to them. 213 // Find all separators and store the text left to them.
114 while (1) { 214 while (1) {
115 size_t b = first (del, a); 215 long b = first (del, a);
116 216
117 if (b == npos) 217 if (b == -1)
118 break; 218 break;
119 219
120 String sub = substr (a, b); 220 String sub = substr (a, b);
121 if (~sub > 0) 221 if (~sub > 0)
122 res.push_back (substr (a, b)); 222 res.push_back (substr (a, b));
130 230
131 return res; 231 return res;
132 } 232 }
133 233
134 void String::replace (const char* a, const char* b) { 234 void String::replace (const char* a, const char* b) {
135 size_t pos; 235 long pos;
136 236
137 while ((pos = first (a)) != npos) 237 while ((pos = first (a)) != -1)
138 m_string = m_string.replace (pos, strlen (a), b); 238 m_string = m_string.replace (pos, strlen (a), b);
139 } 239 }
140 240
141 void String::format (const char* fmtstr, ...) { 241 void String::format (const char* fmtstr, ...) {
142 va_list va; 242 va_list va;
223 } 323 }
224 } 324 }
225 325
226 return -1; 326 return -1;
227 } 327 }
328
329 String String::operator+ (const String data) const {
330 String newstr = *this;
331 newstr += data;
332 return newstr;
333 }
334
335 String String::operator+ (const char* data) const {
336 String newstr = *this;
337 newstr += data;
338 return newstr;
339 }
340
341 String& String::operator+= (const String data) {
342 append (data);
343 return *this;
344 }
345
346 String& String::operator+= (const char* data) {
347 append (data);
348 return *this;
349 }
350
351 String& String::operator+= (const char data) {
352 append (data);
353 return *this;
354 }
355
356 String String::operator+ () const {
357 return upper ();
358 }
359
360 String String::operator- () const {
361 return lower ();
362 }
363
364 String String::operator- (size_t n) const {
365 String newstr = m_string;
366 newstr -= n;
367 return newstr;
368 }
369
370 String& String::operator-= (size_t n) {
371 trim (n);
372 return *this;
373 }
374
375 size_t String::operator~ () const {
376 return len ();
377 }
378
379 vector<String> String::operator/ (String del) const {
380 return split (del);
381 }
382
383 char& String::operator[] (size_t n) {
384 return m_string[n];
385 }
386
387 const char& String::operator[] (size_t n) const {
388 return m_string[n];
389 }
390
391 bool String::operator== (const String other) const {
392 return compare (other) == 0;
393 }
394
395 bool String::operator== (const char* other) const {
396 return compare (other) == 0;
397 }
398
399 bool String::operator!= (const String other) const {
400 return compare (other) != 0;
401 }
402
403 bool String::operator!= (const char* other) const {
404 return compare (other) != 0;
405 }
406
407 bool String::operator! () const {
408 return empty ();
409 }
410
411 String::operator const char* () const {
412 return chars ();
413 }
414
415 String::operator QString () {
416 return chars ();
417 }
418
419 String::operator const QString () const {
420 return chars ();
421 }

mercurial