sources/mystring.cpp

changeset 69
eb4c25284a19
parent 66
bd28a5730fd0
child 73
07dda51a7a8e
equal deleted inserted replaced
68:202e74157de5 69:eb4c25284a19
34 #include "format.h" 34 #include "format.h"
35 #include "md5.h" 35 #include "md5.h"
36 36
37 // ------------------------------------------------------------------------------------------------- 37 // -------------------------------------------------------------------------------------------------
38 // 38 //
39 METHOD 39 int String::compare (const String& other) const
40 String::compare (const String& other) const -> int
41 { 40 {
42 return m_string.compare (other.std_string()); 41 return m_string.compare (other.std_string());
43 } 42 }
44 43
45 // ------------------------------------------------------------------------------------------------- 44 // -------------------------------------------------------------------------------------------------
46 // 45 //
47 METHOD 46 void String::trim (int n)
48 String::trim (int n) -> void
49 { 47 {
50 if (n > 0) 48 if (n > 0)
51 m_string = mid (0, length() - n).std_string(); 49 m_string = mid (0, length() - n).std_string();
52 else 50 else
53 m_string = mid (n, -1).std_string(); 51 m_string = mid (n, -1).std_string();
54 } 52 }
55 53
56 // ------------------------------------------------------------------------------------------------- 54 // -------------------------------------------------------------------------------------------------
57 // 55 //
58 METHOD 56 String String::strip (const List<char>& unwanted)
59 String::strip (const List<char>& unwanted) -> String
60 { 57 {
61 String copy (m_string); 58 String copy (m_string);
62 59
63 for (char c : unwanted) 60 for (char c : unwanted)
64 { 61 {
69 return copy; 66 return copy;
70 } 67 }
71 68
72 // ------------------------------------------------------------------------------------------------- 69 // -------------------------------------------------------------------------------------------------
73 // 70 //
74 METHOD 71 String String::to_uppercase() const
75 String::to_uppercase() const -> String
76 { 72 {
77 String newstr (m_string); 73 String newstr (m_string);
78 74
79 for (char& c : newstr) 75 for (char& c : newstr)
80 { 76 {
85 return newstr; 81 return newstr;
86 } 82 }
87 83
88 // ------------------------------------------------------------------------------------------------- 84 // -------------------------------------------------------------------------------------------------
89 // 85 //
90 METHOD 86 String String::to_lowercase() const
91 String::to_lowercase() const -> String
92 { 87 {
93 String newstr (m_string); 88 String newstr (m_string);
94 89
95 for (char& c : newstr) 90 for (char& c : newstr)
96 { 91 {
101 return newstr; 96 return newstr;
102 } 97 }
103 98
104 // ------------------------------------------------------------------------------------------------- 99 // -------------------------------------------------------------------------------------------------
105 // 100 //
106 METHOD 101 StringList String::split (char del) const
107 String::split (char del) const -> StringList
108 { 102 {
109 String delimstr; 103 String delimstr;
110 delimstr += del; 104 delimstr += del;
111 return split (delimstr); 105 return split (delimstr);
112 } 106 }
113 107
114 // ------------------------------------------------------------------------------------------------- 108 // -------------------------------------------------------------------------------------------------
115 // 109 //
116 METHOD 110 StringList String::split (const String& del) const
117 String::split (const String& del) const -> StringList
118 { 111 {
119 StringList res; 112 StringList res;
120 int a = 0; 113 int a = 0;
121 int b; 114 int b;
122 115
138 return res; 131 return res;
139 } 132 }
140 133
141 // ------------------------------------------------------------------------------------------------- 134 // -------------------------------------------------------------------------------------------------
142 // 135 //
143 METHOD 136 void String::replace (const char* a, const char* b)
144 String::replace (const char* a, const char* b) -> void
145 { 137 {
146 long pos; 138 long pos;
147 139
148 while ((pos = find (a)) != -1) 140 while ((pos = find (a)) != -1)
149 m_string = m_string.replace (pos, strlen (a), b); 141 m_string = m_string.replace (pos, strlen (a), b);
150 } 142 }
151 143
152 // ------------------------------------------------------------------------------------------------- 144 // -------------------------------------------------------------------------------------------------
153 // 145 //
154 METHOD 146 int String::count (char needle) const
155 String::count (char needle) const -> int
156 { 147 {
157 int needles = 0; 148 int needles = 0;
158 149
159 for (const char & c : m_string) 150 for (const char & c : m_string)
160 if (c == needle) 151 if (c == needle)
163 return needles; 154 return needles;
164 } 155 }
165 156
166 // ------------------------------------------------------------------------------------------------- 157 // -------------------------------------------------------------------------------------------------
167 // 158 //
168 METHOD 159 String String::mid (long a, long b) const
169 String::mid (long a, long b) const -> String
170 { 160 {
171 if (b == -1 or b > length()) 161 if (b == -1 or b > length())
172 b = length(); 162 b = length();
173 163
174 if (b == a) 164 if (b == a)
188 return other; 178 return other;
189 } 179 }
190 180
191 // ------------------------------------------------------------------------------------------------- 181 // -------------------------------------------------------------------------------------------------
192 // 182 //
193 METHOD 183 int String::word_position (int n) const
194 String::word_position (int n) const -> int
195 { 184 {
196 int count = 0; 185 int count = 0;
197 186
198 for (int i = 0; i < length(); ++i) 187 for (int i = 0; i < length(); ++i)
199 { 188 {
206 return -1; 195 return -1;
207 } 196 }
208 197
209 // ------------------------------------------------------------------------------------------------- 198 // -------------------------------------------------------------------------------------------------
210 // 199 //
211 METHOD 200 int String::find (const char* c, int a) const
212 String::find (const char* c, int a) const -> int
213 { 201 {
214 int pos = m_string.find (c, a); 202 int pos = m_string.find (c, a);
215 203
216 if (pos == int (std::string::npos)) 204 if (pos == int (std::string::npos))
217 return -1; 205 return -1;
219 return pos; 207 return pos;
220 } 208 }
221 209
222 // ------------------------------------------------------------------------------------------------- 210 // -------------------------------------------------------------------------------------------------
223 // 211 //
224 METHOD 212 int String::find_last (const char* c, int a) const
225 String::find_last (const char* c, int a) const -> int
226 { 213 {
227 if (a == -1 or a >= length()) 214 if (a == -1 or a >= length())
228 a = length() - 1; 215 a = length() - 1;
229 216
230 for (; a > 0; a--) 217 for (; a > 0; a--)
234 return -1; 221 return -1;
235 } 222 }
236 223
237 // ------------------------------------------------------------------------------------------------- 224 // -------------------------------------------------------------------------------------------------
238 // 225 //
239 METHOD 226 long String::to_int (bool* ok, int base) const
240 String::to_int (bool* ok, int base) const -> long
241 { 227 {
242 errno = 0; 228 errno = 0;
243 char* endptr; 229 char* endptr;
244 long i = strtol (chars(), &endptr, base); 230 long i = strtol (chars(), &endptr, base);
245 231
249 return i; 235 return i;
250 } 236 }
251 237
252 // ------------------------------------------------------------------------------------------------- 238 // -------------------------------------------------------------------------------------------------
253 // 239 //
254 METHOD 240 float String::to_float (bool* ok) const
255 String::to_float (bool* ok) const -> float
256 { 241 {
257 errno = 0; 242 errno = 0;
258 char* endptr; 243 char* endptr;
259 float i = strtof (chars(), &endptr); 244 float i = strtof (chars(), &endptr);
260 245
264 return i; 249 return i;
265 } 250 }
266 251
267 // ------------------------------------------------------------------------------------------------- 252 // -------------------------------------------------------------------------------------------------
268 // 253 //
269 METHOD 254 double String::to_double (bool* ok) const
270 String::to_double (bool* ok) const -> double
271 { 255 {
272 errno = 0; 256 errno = 0;
273 char* endptr; 257 char* endptr;
274 double i = strtod (chars(), &endptr); 258 double i = strtod (chars(), &endptr);
275 259
279 return i; 263 return i;
280 } 264 }
281 265
282 // ------------------------------------------------------------------------------------------------- 266 // -------------------------------------------------------------------------------------------------
283 // 267 //
284 METHOD 268 String String::operator+ (const String& data) const
285 String::operator+ (const String& data) const -> String
286 { 269 {
287 String newString = *this; 270 String newString = *this;
288 newString.append (data); 271 newString.append (data);
289 return newString; 272 return newString;
290 } 273 }
291 274
292 // ------------------------------------------------------------------------------------------------- 275 // -------------------------------------------------------------------------------------------------
293 // 276 //
294 METHOD 277 String String::operator+ (const char* data) const
295 String::operator+ (const char* data) const -> String
296 { 278 {
297 String newstr = *this; 279 String newstr = *this;
298 newstr.append (data); 280 newstr.append (data);
299 return newstr; 281 return newstr;
300 } 282 }
301 283
302 // ------------------------------------------------------------------------------------------------- 284 // -------------------------------------------------------------------------------------------------
303 // 285 //
304 METHOD 286 bool String::is_numeric() const
305 String::is_numeric() const -> bool 287 {
306 { 288 char* endptr;
307 bool gotDot = false; 289 strtol (chars(), &endptr, 10);
308 290 return not (endptr && *endptr);
309 for (const char & c : m_string) 291 }
310 { 292
311 // Allow leading hyphen for negatives 293 // -------------------------------------------------------------------------------------------------
312 if (&c == &m_string[0] and c == '-') 294 //
313 continue; 295 bool String::ends_with (const String& other)
314
315 // Check for decimal point
316 if (!gotDot and c == '.')
317 {
318 gotDot = true;
319 continue;
320 }
321
322 if (c >= '0' and c <= '9')
323 continue; // Digit
324
325 // If the above cases didn't catch this character, it was
326 // illegal and this is therefore not a number.
327 return false;
328 }
329
330 return true;
331 }
332
333 // -------------------------------------------------------------------------------------------------
334 //
335 METHOD
336 String::ends_with (const String& other) -> bool
337 { 296 {
338 if (length() < other.length()) 297 if (length() < other.length())
339 return false; 298 return false;
340 299
341 const int ofs = length() - other.length(); 300 const int ofs = length() - other.length();
342 return strncmp (chars() + ofs, other.chars(), other.length()) == 0; 301 return strncmp (chars() + ofs, other.chars(), other.length()) == 0;
343 } 302 }
344 303
345 // ------------------------------------------------------------------------------------------------- 304 // -------------------------------------------------------------------------------------------------
346 // 305 //
347 METHOD 306 bool String::starts_with (const String& other)
348 String::starts_with (const String& other) -> bool
349 { 307 {
350 if (length() < other.length()) 308 if (length() < other.length())
351 return false; 309 return false;
352 310
353 return strncmp (chars(), other.chars(), other.length()) == 0; 311 return strncmp (chars(), other.chars(), other.length()) == 0;
354 } 312 }
355 313
356 // ------------------------------------------------------------------------------------------------- 314 // -------------------------------------------------------------------------------------------------
357 // 315 //
358 METHOD 316 void String::sprintf (const char* fmtstr, ...)
359 String::sprintf (const char* fmtstr, ...) -> void 317 {
360 { 318 char* buf = nullptr;
361 char* buf;
362 int bufsize = 256; 319 int bufsize = 256;
363 va_list va; 320 va_list va;
364 va_start (va, fmtstr); 321 va_start (va, fmtstr);
365 322
366 do 323 do
324 {
325 delete[] buf;
367 buf = new char[bufsize]; 326 buf = new char[bufsize];
327 }
368 while (vsnprintf (buf, bufsize, fmtstr, va) >= bufsize); 328 while (vsnprintf (buf, bufsize, fmtstr, va) >= bufsize);
369 329
370 va_end (va); 330 va_end (va);
371 m_string = buf; 331 m_string = buf;
372 delete[] buf; 332 delete[] buf;
373 } 333 }
374 334
375 // ------------------------------------------------------------------------------------------------- 335 // -------------------------------------------------------------------------------------------------
376 // 336 //
377 METHOD 337 String StringList::join (const String& delim)
378 StringList::join (const String& delim) -> String
379 { 338 {
380 String result; 339 String result;
381 340
382 for (const String& it : container()) 341 for (const String& it : container())
383 { 342 {
390 return result; 349 return result;
391 } 350 }
392 351
393 // ------------------------------------------------------------------------------------------------- 352 // -------------------------------------------------------------------------------------------------
394 // 353 //
395 METHOD 354 bool String::mask_against (const String& pattern) const
396 String::mask_against (const String& pattern) const -> bool
397 { 355 {
398 // Elevate to uppercase for case-insensitive matching 356 // Elevate to uppercase for case-insensitive matching
399 String pattern_upper = pattern.to_uppercase(); 357 String pattern_upper = pattern.to_uppercase();
400 String this_upper = to_uppercase(); 358 String this_upper = to_uppercase();
401 const char* maskstring = pattern_upper.chars(); 359 const char* maskstring = pattern_upper.chars();
439 return true; 397 return true;
440 } 398 }
441 399
442 // ------------------------------------------------------------------------------------------------- 400 // -------------------------------------------------------------------------------------------------
443 // 401 //
444 METHOD 402 String String::from_number (short int a)
445 String::from_number (short int a) -> String
446 { 403 {
447 char buf[32]; 404 char buf[32];
448 ::sprintf (buf, "%d", a); 405 ::sprintf (buf, "%d", a);
449 return String (buf); 406 return String (buf);
450 } 407 }
451 408
452 // ------------------------------------------------------------------------------------------------- 409 // -------------------------------------------------------------------------------------------------
453 // 410 //
454 METHOD 411 String String::from_number (int a)
455 String::from_number (int a) -> String
456 { 412 {
457 char buf[32]; 413 char buf[32];
458 ::sprintf (buf, "%d", a); 414 ::sprintf (buf, "%d", a);
459 return String (buf); 415 return String (buf);
460 } 416 }
461 417
462 // ------------------------------------------------------------------------------------------------- 418 // -------------------------------------------------------------------------------------------------
463 // 419 //
464 METHOD 420 String String::from_number (long int a)
465 String::from_number (long int a) -> String
466 { 421 {
467 char buf[32]; 422 char buf[32];
468 ::sprintf (buf, "%ld", a); 423 ::sprintf (buf, "%ld", a);
469 return String (buf); 424 return String (buf);
470 } 425 }
471 426
472 // ------------------------------------------------------------------------------------------------- 427 // -------------------------------------------------------------------------------------------------
473 // 428 //
474 METHOD 429 String String::from_number (unsigned short int a)
475 String::from_number (unsigned short int a) -> String
476 { 430 {
477 char buf[32]; 431 char buf[32];
478 ::sprintf (buf, "%u", a); 432 ::sprintf (buf, "%u", a);
479 return String (buf); 433 return String (buf);
480 } 434 }
481 435
482 // ------------------------------------------------------------------------------------------------- 436 // -------------------------------------------------------------------------------------------------
483 // 437 //
484 METHOD 438 String String::from_number (unsigned int a)
485 String::from_number (unsigned int a) -> String
486 { 439 {
487 char buf[32]; 440 char buf[32];
488 ::sprintf (buf, "%u", a); 441 ::sprintf (buf, "%u", a);
489 return String (buf); 442 return String (buf);
490 } 443 }
491 444
492 // ------------------------------------------------------------------------------------------------- 445 // -------------------------------------------------------------------------------------------------
493 // 446 //
494 METHOD 447 String String::from_number (unsigned long int a)
495 String::from_number (unsigned long int a) -> String
496 { 448 {
497 char buf[32]; 449 char buf[32];
498 ::sprintf (buf, "%lu", a); 450 ::sprintf (buf, "%lu", a);
499 return String (buf); 451 return String (buf);
500 } 452 }
501 453
502 // ------------------------------------------------------------------------------------------------- 454 // -------------------------------------------------------------------------------------------------
503 // 455 //
504 METHOD 456 String String::from_number (double a)
505 String::from_number (double a) -> String
506 { 457 {
507 char buf[64]; 458 char buf[64];
508 ::sprintf (buf, "%f", a); 459 ::sprintf (buf, "%f", a);
509 return String (buf); 460 return String (buf);
510 } 461 }
511 462
512 // ------------------------------------------------------------------------------------------------- 463 // -------------------------------------------------------------------------------------------------
513 // 464 //
514 METHOD 465 String String::md5() const
515 String::md5() const -> String
516 { 466 {
517 char checksum[33]; 467 char checksum[33];
518 CalculateMD5 (reinterpret_cast<const unsigned char*> (chars()), length(), checksum); 468 CalculateMD5 (reinterpret_cast<const unsigned char*> (chars()), length(), checksum);
519 checksum[sizeof checksum - 1] = '\0'; 469 checksum[sizeof checksum - 1] = '\0';
520 return String (checksum); 470 return String (checksum);
521 } 471 }
522 472
523 // ------------------------------------------------------------------------------------------------- 473 // -------------------------------------------------------------------------------------------------
524 // 474 //
525 METHOD 475 void String::normalize (int (*filter)(int))
526 String::normalize (int (*filter)(int)) -> void
527 { 476 {
528 int a = 0; 477 int a = 0;
529 int b = length() - 1; 478 int b = length() - 1;
530 479
531 while ((*filter) (m_string[a]) and a != b) 480 while ((*filter) (m_string[a]) and a != b)

mercurial