sources/mystring.cpp

changeset 145
d0aedc9be448
parent 137
485cb6d6b98c
child 147
12c93c4a137c
equal deleted inserted replaced
144:e8d58327cd7f 145:d0aedc9be448
37 37
38 // ------------------------------------------------------------------------------------------------- 38 // -------------------------------------------------------------------------------------------------
39 // 39 //
40 int String::compare (const String& other) const 40 int String::compare (const String& other) const
41 { 41 {
42 return m_string.compare (other.std_string()); 42 return m_string.compare (other.stdString());
43 } 43 }
44 44
45 // ------------------------------------------------------------------------------------------------- 45 // -------------------------------------------------------------------------------------------------
46 // 46 //
47 void String::trim (int n) 47 void String::trim (int n)
48 { 48 {
49 if (n > 0) 49 if (n > 0)
50 m_string = mid (0, length() - n).std_string(); 50 m_string = mid (0, length() - n).stdString();
51 else 51 else
52 m_string = mid (n, -1).std_string(); 52 m_string = mid (n, -1).stdString();
53 } 53 }
54 54
55 // ------------------------------------------------------------------------------------------------- 55 // -------------------------------------------------------------------------------------------------
56 // 56 //
57 String String::strip (char unwanted) const 57 String String::strip (char unwanted) const
58 { 58 {
59 String result (m_string); 59 String result (m_string);
60 60
61 for (int pos = 0; (pos = result.find (unwanted)) != -1;) 61 for (int pos = 0; (pos = result.find (unwanted)) != -1;)
62 result.remove_at (pos--); 62 result.removeAt (pos--);
63 63
64 return result; 64 return result;
65 } 65 }
66 66
67 // ------------------------------------------------------------------------------------------------- 67 // -------------------------------------------------------------------------------------------------
70 { 70 {
71 String result (m_string); 71 String result (m_string);
72 72
73 for (String c : unwanted) 73 for (String c : unwanted)
74 for (int pos = 0; (pos = result.find (c)) != -1;) 74 for (int pos = 0; (pos = result.find (c)) != -1;)
75 result.remove_at (pos--); 75 result.removeAt (pos--);
76 76
77 return result; 77 return result;
78 } 78 }
79 79
80 // ------------------------------------------------------------------------------------------------- 80 // -------------------------------------------------------------------------------------------------
81 // 81 //
82 String String::to_uppercase() const 82 String String::toUpperCase() const
83 { 83 {
84 String result (m_string); 84 String result (m_string);
85 85
86 for (char &ch : result) 86 for (char &ch : result)
87 { 87 {
92 return result; 92 return result;
93 } 93 }
94 94
95 // ------------------------------------------------------------------------------------------------- 95 // -------------------------------------------------------------------------------------------------
96 // 96 //
97 String String::to_lowercase() const 97 String String::toLowerCase() const
98 { 98 {
99 String result (m_string); 99 String result (m_string);
100 100
101 for (char &ch : result) 101 for (char &ch : result)
102 { 102 {
195 return String(chars() + this->length() - length); 195 return String(chars() + this->length() - length);
196 } 196 }
197 197
198 // ------------------------------------------------------------------------------------------------- 198 // -------------------------------------------------------------------------------------------------
199 // 199 //
200 int String::word_position (int n) const
201 {
202 int count = 0;
203
204 for (char ch : *this)
205 {
206 if (not isspace(ch) or ++count < n)
207 continue;
208 }
209
210 return -1;
211 }
212
213 // -------------------------------------------------------------------------------------------------
214 //
215 int String::find (const char* c, int a) const 200 int String::find (const char* c, int a) const
216 { 201 {
217 int pos = m_string.find (c, a); 202 int pos = m_string.find (c, a);
218 203
219 if (pos == int (std::string::npos)) 204 if (pos == int (std::string::npos))
234 return pos; 219 return pos;
235 } 220 }
236 221
237 // ------------------------------------------------------------------------------------------------- 222 // -------------------------------------------------------------------------------------------------
238 // 223 //
239 int String::find_last (const char* c, int a) const 224 int String::findLast (const char* c, int a) const
240 { 225 {
241 if (a == -1 or a >= length()) 226 if (a == -1 or a >= length())
242 a = length() - 1; 227 a = length() - 1;
243 228
244 for (; a > 0; a--) 229 for (; a > 0; a--)
248 return -1; 233 return -1;
249 } 234 }
250 235
251 // ------------------------------------------------------------------------------------------------- 236 // -------------------------------------------------------------------------------------------------
252 // 237 //
253 long String::to_int (bool* ok, int base) const 238 long String::toInt (bool* ok, int base) const
254 { 239 {
255 errno = 0; 240 errno = 0;
256 char* endptr; 241 char* endptr;
257 long i = strtol (chars(), &endptr, base); 242 long i = strtol (chars(), &endptr, base);
258 243
262 return i; 247 return i;
263 } 248 }
264 249
265 // ------------------------------------------------------------------------------------------------- 250 // -------------------------------------------------------------------------------------------------
266 // 251 //
267 float String::to_float (bool* ok) const 252 float String::toFloat (bool* ok) const
268 { 253 {
269 errno = 0; 254 errno = 0;
270 char* endptr; 255 char* endptr;
271 float i = (float) strtod (chars(), &endptr); 256 float i = (float) strtod (chars(), &endptr);
272 257
276 return i; 261 return i;
277 } 262 }
278 263
279 // ------------------------------------------------------------------------------------------------- 264 // -------------------------------------------------------------------------------------------------
280 // 265 //
281 double String::to_double (bool* ok) const 266 double String::toDouble (bool* ok) const
282 { 267 {
283 errno = 0; 268 errno = 0;
284 char* endptr; 269 char* endptr;
285 double i = strtod (chars(), &endptr); 270 double i = strtod (chars(), &endptr);
286 271
308 return newstr; 293 return newstr;
309 } 294 }
310 295
311 // ------------------------------------------------------------------------------------------------- 296 // -------------------------------------------------------------------------------------------------
312 // 297 //
313 bool String::is_numeric() const 298 bool String::isNumeric() const
314 { 299 {
315 char* endptr; 300 char* endptr;
316 strtol (chars(), &endptr, 10); 301 strtol (chars(), &endptr, 10);
317 return not (endptr && *endptr); 302 return not (endptr && *endptr);
318 } 303 }
319 304
320 // ------------------------------------------------------------------------------------------------- 305 // -------------------------------------------------------------------------------------------------
321 // 306 //
322 bool String::ends_with (const String& other) 307 bool String::endsWith (const String& other) const
323 { 308 {
324 if (length() < other.length()) 309 if (length() < other.length())
325 return false; 310 return false;
326 311
327 const int ofs = length() - other.length(); 312 const int ofs = length() - other.length();
328 return strncmp (chars() + ofs, other.chars(), other.length()) == 0; 313 return strncmp (chars() + ofs, other.chars(), other.length()) == 0;
329 } 314 }
330 315
331 // ------------------------------------------------------------------------------------------------- 316 // -------------------------------------------------------------------------------------------------
332 // 317 //
333 bool String::starts_with (const String& other) const 318 bool String::startsWith (const String& other) const
334 { 319 {
335 if (length() < other.length()) 320 if (length() < other.length())
336 return false; 321 return false;
337 322
338 return strncmp (chars(), other.chars(), other.length()) == 0; 323 return strncmp (chars(), other.chars(), other.length()) == 0;
373 { 358 {
374 String result; 359 String result;
375 360
376 for (const String &item : container()) 361 for (const String &item : container())
377 { 362 {
378 if (result.is_empty() == false) 363 if (result.isEmpty() == false)
379 result += delim; 364 result += delim;
380 365
381 result += item; 366 result += item;
382 } 367 }
383 368
384 return result; 369 return result;
385 } 370 }
386 371
387 // ------------------------------------------------------------------------------------------------- 372 // -------------------------------------------------------------------------------------------------
388 // 373 //
389 bool String::mask_against (const String& pattern) const 374 bool String::maskAgainst (const String& pattern) const
390 { 375 {
391 // Elevate to uppercase for case-insensitive matching 376 // Elevate to uppercase for case-insensitive matching
392 String pattern_upper = pattern.to_uppercase(); 377 String pattern_upper = pattern.toUpperCase();
393 String this_upper = to_uppercase(); 378 String this_upper = toUpperCase();
394 const char* maskstring = pattern_upper.chars(); 379 const char* maskstring = pattern_upper.chars();
395 const char* mptr = &maskstring[0]; 380 const char* mptr = &maskstring[0];
396 381
397 for (const char* sptr = this_upper.chars(); *sptr != '\0'; sptr++) 382 for (const char* sptr = this_upper.chars(); *sptr != '\0'; sptr++)
398 { 383 {
432 return true; 417 return true;
433 } 418 }
434 419
435 // ------------------------------------------------------------------------------------------------- 420 // -------------------------------------------------------------------------------------------------
436 // 421 //
437 String String::from_number (short int a) 422 String String::fromNumber (short int a)
438 { 423 {
439 char buf[32]; 424 char buf[32];
440 ::sprintf (buf, "%d", a); 425 ::sprintf (buf, "%d", a);
441 return String (buf); 426 return String (buf);
442 } 427 }
443 428
444 // ------------------------------------------------------------------------------------------------- 429 // -------------------------------------------------------------------------------------------------
445 // 430 //
446 String String::from_number (int a) 431 String String::fromNumber (int a)
447 { 432 {
448 char buf[32]; 433 char buf[32];
449 ::sprintf (buf, "%d", a); 434 ::sprintf (buf, "%d", a);
450 return String (buf); 435 return String (buf);
451 } 436 }
452 437
453 // ------------------------------------------------------------------------------------------------- 438 // -------------------------------------------------------------------------------------------------
454 // 439 //
455 String String::from_number (long int a) 440 String String::fromNumber (long int a)
456 { 441 {
457 char buf[32]; 442 char buf[32];
458 ::sprintf (buf, "%ld", a); 443 ::sprintf (buf, "%ld", a);
459 return String (buf); 444 return String (buf);
460 } 445 }
461 446
462 // ------------------------------------------------------------------------------------------------- 447 // -------------------------------------------------------------------------------------------------
463 // 448 //
464 String String::from_number (unsigned short int a) 449 String String::fromNumber (unsigned short int a)
465 { 450 {
466 char buf[32]; 451 char buf[32];
467 ::sprintf (buf, "%u", a); 452 ::sprintf (buf, "%u", a);
468 return String (buf); 453 return String (buf);
469 } 454 }
470 455
471 // ------------------------------------------------------------------------------------------------- 456 // -------------------------------------------------------------------------------------------------
472 // 457 //
473 String String::from_number (unsigned int a) 458 String String::fromNumber (unsigned int a)
474 { 459 {
475 char buf[32]; 460 char buf[32];
476 ::sprintf (buf, "%u", a); 461 ::sprintf (buf, "%u", a);
477 return String (buf); 462 return String (buf);
478 } 463 }
479 464
480 // ------------------------------------------------------------------------------------------------- 465 // -------------------------------------------------------------------------------------------------
481 // 466 //
482 String String::from_number (unsigned long int a) 467 String String::fromNumber (unsigned long int a)
483 { 468 {
484 char buf[32]; 469 char buf[32];
485 ::sprintf (buf, "%lu", a); 470 ::sprintf (buf, "%lu", a);
486 return String (buf); 471 return String (buf);
487 } 472 }
488 473
489 // ------------------------------------------------------------------------------------------------- 474 // -------------------------------------------------------------------------------------------------
490 // 475 //
491 String String::from_number (double a) 476 String String::fromNumber (double a)
492 { 477 {
493 char buf[64]; 478 char buf[64];
494 ::sprintf (buf, "%f", a); 479 ::sprintf (buf, "%f", a);
495 return String (buf); 480 return String (buf);
496 } 481 }

mercurial