src/string.cpp

changeset 128
186ebeea2a85
parent 119
bdf8d46c145f
child 133
dbbdb870c835
equal deleted inserted replaced
127:ed4289183de6 128:186ebeea2a85
32 32
33 // ============================================================================= 33 // =============================================================================
34 // 34 //
35 int String::compare (const String& other) const 35 int String::compare (const String& other) const
36 { 36 {
37 return m_string.compare (other.stdString()); 37 return _string.compare (other.stdString());
38 } 38 }
39 39
40 // ============================================================================= 40 // =============================================================================
41 // 41 //
42 void String::trim (int n) 42 void String::trim (int n)
43 { 43 {
44 if (n > 0) 44 if (n > 0)
45 m_string = mid (0, length() - n).stdString(); 45 _string = mid (0, length() - n).stdString();
46 else 46 else
47 m_string = mid (n, -1).stdString(); 47 _string = mid (n, -1).stdString();
48 } 48 }
49 49
50 // ============================================================================= 50 // =============================================================================
51 // 51 //
52 String String::strip (const List< char >& unwanted) 52 String String::strip (const List<char>& unwanted)
53 { 53 {
54 String copy (m_string); 54 String copy (_string);
55 55
56 for (char c : unwanted) 56 for (char c : unwanted)
57 for (int i = 0; i < copy.length(); ++i) 57 {
58 if (copy[i] == c) 58 int pos = 0;
59 copy.removeAt (i); 59 while ((pos = copy.firstIndexOf (String (c))) != -1)
60 60 copy.removeAt (pos--);
61 /* 61 }
62 int pos = 0;
63 while ((pos = copy.First (c)) != -1)
64 copy.RemoveAt (pos--);
65 */
66 62
67 return copy; 63 return copy;
68 } 64 }
69 65
70 // ============================================================================= 66 // =============================================================================
71 // 67 //
72 String String::toUppercase() const 68 String String::toUppercase() const
73 { 69 {
74 String newstr = m_string; 70 String newstr (_string);
75 71
76 for (char& c : newstr) 72 for (char& c : newstr)
73 {
77 if (c >= 'a' && c <= 'z') 74 if (c >= 'a' && c <= 'z')
78 c -= 'a' - 'A'; 75 c -= 'a' - 'A';
76 }
79 77
80 return newstr; 78 return newstr;
81 } 79 }
82 80
83 // ============================================================================= 81 // =============================================================================
84 // 82 //
85 String String::toLowercase() const 83 String String::toLowercase() const
86 { 84 {
87 String newstr = m_string; 85 String newstr (_string);
88 86
89 for (char & c : newstr) 87 for (char& c : newstr)
88 {
90 if (c >= 'A' && c <= 'Z') 89 if (c >= 'A' && c <= 'Z')
91 c += 'a' - 'A'; 90 c += 'a' - 'A';
91 }
92 92
93 return newstr; 93 return newstr;
94 } 94 }
95 95
96 // ============================================================================= 96 // =============================================================================
105 // ============================================================================= 105 // =============================================================================
106 // 106 //
107 StringList String::split (const String& del) const 107 StringList String::split (const String& del) const
108 { 108 {
109 StringList res; 109 StringList res;
110 long a = 0; 110 int a = 0;
111 int b;
111 112
112 // Find all separators and store the text left to them. 113 // Find all separators and store the text left to them.
113 for (;;) 114 while ((b = firstIndexOf (del, a)) != -1)
114 { 115 {
115 long b = firstIndexOf (del, a);
116
117 if (b == -1)
118 break;
119
120 String sub = mid (a, b); 116 String sub = mid (a, b);
121 117
122 if (sub.length() > 0) 118 if (sub.length() > 0)
123 res.append (mid (a, b)); 119 res << sub;
124 120
125 a = b + del.length(); 121 a = b + del.length();
126 } 122 }
127 123
128 // Add the string at the right of the last separator 124 // Add the string at the right of the last separator
137 void String::replace (const char* a, const char* b) 133 void String::replace (const char* a, const char* b)
138 { 134 {
139 long pos; 135 long pos;
140 136
141 while ((pos = firstIndexOf (a)) != -1) 137 while ((pos = firstIndexOf (a)) != -1)
142 m_string = m_string.replace (pos, strlen (a), b); 138 _string = _string.replace (pos, strlen (a), b);
143 } 139 }
144 140
145 // ============================================================================= 141 // =============================================================================
146 // 142 //
147 int String::count (char needle) const 143 int String::count (char needle) const
148 { 144 {
149 int needles = 0; 145 int needles = 0;
150 146
151 for (const char & c : m_string) 147 for (const char & c : _string)
152 if (c == needle) 148 if (c == needle)
153 needles++; 149 needles++;
154 150
155 return needles; 151 return needles;
156 } 152 }
172 a = b; 168 a = b;
173 b = c; 169 b = c;
174 } 170 }
175 171
176 char* newstr = new char[b - a + 1]; 172 char* newstr = new char[b - a + 1];
177 strncpy (newstr, m_string.c_str() + a, b - a); 173 strncpy (newstr, _string.c_str() + a, b - a);
178 newstr[b - a] = '\0'; 174 newstr[b - a] = '\0';
179 175
180 String other (newstr); 176 String other (newstr);
181 delete[] newstr; 177 delete[] newstr;
182 return other; 178 return other;
188 { 184 {
189 int count = 0; 185 int count = 0;
190 186
191 for (int i = 0; i < length(); ++i) 187 for (int i = 0; i < length(); ++i)
192 { 188 {
193 if (m_string[i] != ' ') 189 if (_string[i] != ' ')
194 continue; 190 continue;
195 191
196 if (++count < n) 192 if (++count < n)
197 continue; 193 continue;
198 194
204 200
205 // ============================================================================= 201 // =============================================================================
206 // 202 //
207 int String::firstIndexOf (const char* c, int a) const 203 int String::firstIndexOf (const char* c, int a) const
208 { 204 {
209 for (; a < length(); a++) 205 int pos = _string.find (c, a);
210 if (m_string[a] == c[0] && strncmp (m_string.c_str() + a, c, strlen (c)) == 0) 206
211 return a; 207 if (pos == (int) std::string::npos)
212 208 return -1;
213 return -1; 209
210 return pos;
214 } 211 }
215 212
216 // ============================================================================= 213 // =============================================================================
217 // 214 //
218 int String::lastIndexOf (const char* c, int a) const 215 int String::lastIndexOf (const char* c, int a) const
219 { 216 {
220 if (a == -1 || a >= length()) 217 if (a == -1 || a >= length())
221 a = length() - 1; 218 a = length() - 1;
222 219
223 for (; a > 0; a--) 220 for (; a > 0; a--)
224 if (m_string[a] == c[0] && strncmp (m_string.c_str() + a, c, strlen (c)) == 0) 221 if (_string[a] == c[0] && strncmp (_string.c_str() + a, c, strlen (c)) == 0)
225 return a; 222 return a;
226 223
227 return -1; 224 return -1;
228 } 225 }
229 226
230 // ============================================================================= 227 // =============================================================================
231 // 228 //
232 void String::dump() const 229 void String::dump() const
233 { 230 {
234 print ("`%1`:\n", chars()); 231 print ("`%1`:\n", c_str());
235 int i = 0; 232 int i = 0;
236 233
237 for (char u : m_string) 234 for (char u : _string)
238 print ("\t%1. [%d2] `%3`\n", i++, u, String (u)); 235 print ("\t%1. [%d2] `%3`\n", i++, u, String (u));
239 } 236 }
240 237
241 // ============================================================================= 238 // =============================================================================
242 // 239 //
243 long String::toLong (bool* ok, int base) const 240 long String::toLong (bool* ok, int base) const
244 { 241 {
245 errno = 0; 242 errno = 0;
246 char* endptr; 243 char* endptr;
247 long i = strtol (m_string.c_str(), &endptr, base); 244 long i = strtol (_string.c_str(), &endptr, base);
248 245
249 if (ok) 246 if (ok)
250 *ok = (errno == 0 && *endptr == '\0'); 247 *ok = (errno == 0 && *endptr == '\0');
251 248
252 return i; 249 return i;
256 // 253 //
257 float String::toFloat (bool* ok) const 254 float String::toFloat (bool* ok) const
258 { 255 {
259 errno = 0; 256 errno = 0;
260 char* endptr; 257 char* endptr;
261 float i = strtof (m_string.c_str(), &endptr); 258 float i = strtof (_string.c_str(), &endptr);
262 259
263 if (ok) 260 if (ok)
264 *ok = (errno == 0 && *endptr == '\0'); 261 *ok = (errno == 0 && *endptr == '\0');
265 262
266 return i; 263 return i;
270 // 267 //
271 double String::toDouble (bool* ok) const 268 double String::toDouble (bool* ok) const
272 { 269 {
273 errno = 0; 270 errno = 0;
274 char* endptr; 271 char* endptr;
275 double i = strtod (m_string.c_str(), &endptr); 272 double i = strtod (_string.c_str(), &endptr);
276 273
277 if (ok) 274 if (ok)
278 *ok = (errno == 0 && *endptr == '\0'); 275 *ok = (errno == 0 && *endptr == '\0');
279 276
280 return i; 277 return i;
302 // 299 //
303 bool String::isNumeric() const 300 bool String::isNumeric() const
304 { 301 {
305 bool gotDot = false; 302 bool gotDot = false;
306 303
307 for (const char & c : m_string) 304 for (const char & c : _string)
308 { 305 {
309 // Allow leading hyphen for negatives 306 // Allow leading hyphen for negatives
310 if (&c == &m_string[0] && c == '-') 307 if (&c == &_string[0] && c == '-')
311 continue; 308 continue;
312 309
313 // Check for decimal point 310 // Check for decimal point
314 if (!gotDot && c == '.') 311 if (!gotDot && c == '.')
315 { 312 {
334 { 331 {
335 if (length() < other.length()) 332 if (length() < other.length())
336 return false; 333 return false;
337 334
338 const int ofs = length() - other.length(); 335 const int ofs = length() - other.length();
339 return strncmp (chars() + ofs, other.chars(), other.length()) == 0; 336 return strncmp (c_str() + ofs, other.c_str(), other.length()) == 0;
340 } 337 }
341 338
342 // ============================================================================= 339 // =============================================================================
343 // 340 //
344 bool String::startsWith (const String& other) 341 bool String::startsWith (const String& other)
345 { 342 {
346 if (length() < other.length()) 343 if (length() < other.length())
347 return false; 344 return false;
348 345
349 return strncmp (chars(), other.chars(), other.length()) == 0; 346 return strncmp (c_str(), other.c_str(), other.length()) == 0;
350 } 347 }
351 348
352 // ============================================================================= 349 // =============================================================================
353 // 350 //
354 void String::sprintf (const char* fmtstr, ...) 351 void String::sprintf (const char* fmtstr, ...)
361 do 358 do
362 buf = new char[bufsize]; 359 buf = new char[bufsize];
363 while (vsnprintf (buf, bufsize, fmtstr, va) >= bufsize); 360 while (vsnprintf (buf, bufsize, fmtstr, va) >= bufsize);
364 361
365 va_end (va); 362 va_end (va);
366 m_string = buf; 363 _string = buf;
367 delete[] buf; 364 delete[] buf;
368 } 365 }
369 366
370 // ============================================================================= 367 // =============================================================================
371 // 368 //
389 bool String::maskAgainst (const String& pattern) const 386 bool String::maskAgainst (const String& pattern) const
390 { 387 {
391 // Elevate to uppercase for case-insensitive matching 388 // Elevate to uppercase for case-insensitive matching
392 String pattern_upper = pattern.toUppercase(); 389 String pattern_upper = pattern.toUppercase();
393 String this_upper = toUppercase(); 390 String this_upper = toUppercase();
394 const char* maskstring = pattern_upper.chars(); 391 const char* maskstring = pattern_upper.c_str();
395 const char* mptr = &maskstring[0]; 392 const char* mptr = &maskstring[0];
396 393
397 for (const char* sptr = this_upper.chars(); *sptr != '\0'; sptr++) 394 for (const char* sptr = this_upper.c_str(); *sptr != '\0'; sptr++)
398 { 395 {
399 if (*mptr == '?') 396 if (*mptr == '?')
400 { 397 {
401 if (*(sptr + 1) == '\0') 398 if (*(sptr + 1) == '\0')
402 { 399 {

mercurial