VirtualBox

source: vbox/trunk/include/iprt/cpp/ministring.h@ 62474

Last change on this file since 62474 was 62474, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 32.5 KB
Line 
1/** @file
2 * IPRT - C++ string class.
3 */
4
5/*
6 * Copyright (C) 2007-2016 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___iprt_cpp_ministring_h
27#define ___iprt_cpp_ministring_h
28
29#include <iprt/mem.h>
30#include <iprt/string.h>
31#include <iprt/stdarg.h>
32#include <iprt/cpp/list.h>
33
34#include <new>
35
36
37/** @defgroup grp_rt_cpp_string C++ String support
38 * @ingroup grp_rt_cpp
39 * @{
40 */
41
42/** @brief C++ string class.
43 *
44 * This is a C++ string class that does not depend on anything else except IPRT
45 * memory management functions. Semantics are like in std::string, except it
46 * can do a lot less.
47 *
48 * Note that RTCString does not differentiate between NULL strings
49 * and empty strings. In other words, RTCString("") and RTCString(NULL)
50 * behave the same. In both cases, RTCString allocates no memory, reports
51 * a zero length and zero allocated bytes for both, and returns an empty
52 * C string from c_str().
53 *
54 * @note RTCString ASSUMES that all strings it deals with are valid UTF-8.
55 * The caller is responsible for not breaking this assumption.
56 */
57#ifdef VBOX
58 /** @remarks Much of the code in here used to be in com::Utf8Str so that
59 * com::Utf8Str can now derive from RTCString and only contain code
60 * that is COM-specific, such as com::Bstr conversions. Compared to
61 * the old Utf8Str though, RTCString always knows the length of its
62 * member string and the size of the buffer so it can use memcpy()
63 * instead of strdup().
64 */
65#endif
66class RT_DECL_CLASS RTCString
67{
68public:
69 /**
70 * Creates an empty string that has no memory allocated.
71 */
72 RTCString()
73 : m_psz(NULL),
74 m_cch(0),
75 m_cbAllocated(0)
76 {
77 }
78
79 /**
80 * Creates a copy of another RTCString.
81 *
82 * This allocates s.length() + 1 bytes for the new instance, unless s is empty.
83 *
84 * @param a_rSrc The source string.
85 *
86 * @throws std::bad_alloc
87 */
88 RTCString(const RTCString &a_rSrc)
89 {
90 copyFromN(a_rSrc.m_psz, a_rSrc.m_cch);
91 }
92
93 /**
94 * Creates a copy of a C string.
95 *
96 * This allocates strlen(pcsz) + 1 bytes for the new instance, unless s is empty.
97 *
98 * @param pcsz The source string.
99 *
100 * @throws std::bad_alloc
101 */
102 RTCString(const char *pcsz)
103 {
104 copyFromN(pcsz, pcsz ? strlen(pcsz) : 0);
105 }
106
107 /**
108 * Create a partial copy of another RTCString.
109 *
110 * @param a_rSrc The source string.
111 * @param a_offSrc The byte offset into the source string.
112 * @param a_cchSrc The max number of chars (encoded UTF-8 bytes)
113 * to copy from the source string.
114 */
115 RTCString(const RTCString &a_rSrc, size_t a_offSrc, size_t a_cchSrc = npos)
116 {
117 if (a_offSrc < a_rSrc.m_cch)
118 copyFromN(&a_rSrc.m_psz[a_offSrc], RT_MIN(a_cchSrc, a_rSrc.m_cch - a_offSrc));
119 else
120 {
121 m_psz = NULL;
122 m_cch = 0;
123 m_cbAllocated = 0;
124 }
125 }
126
127 /**
128 * Create a partial copy of a C string.
129 *
130 * @param a_pszSrc The source string (UTF-8).
131 * @param a_cchSrc The max number of chars (encoded UTF-8 bytes)
132 * to copy from the source string. This must not
133 * be '0' as the compiler could easily mistake
134 * that for the va_list constructor.
135 */
136 RTCString(const char *a_pszSrc, size_t a_cchSrc)
137 {
138 size_t cchMax = a_pszSrc ? RTStrNLen(a_pszSrc, a_cchSrc) : 0;
139 copyFromN(a_pszSrc, RT_MIN(a_cchSrc, cchMax));
140 }
141
142 /**
143 * Create a string containing @a a_cTimes repetitions of the character @a
144 * a_ch.
145 *
146 * @param a_cTimes The number of times the character is repeated.
147 * @param a_ch The character to fill the string with.
148 */
149 RTCString(size_t a_cTimes, char a_ch)
150 : m_psz(NULL),
151 m_cch(0),
152 m_cbAllocated(0)
153 {
154 Assert((unsigned)a_ch < 0x80);
155 if (a_cTimes)
156 {
157 reserve(a_cTimes + 1);
158 memset(m_psz, a_ch, a_cTimes);
159 m_psz[a_cTimes] = '\0';
160 m_cch = a_cTimes;
161 }
162 }
163
164 /**
165 * Create a new string given the format string and its arguments.
166 *
167 * @param a_pszFormat Pointer to the format string (UTF-8),
168 * @see pg_rt_str_format.
169 * @param a_va Argument vector containing the arguments
170 * specified by the format string.
171 * @sa printfV
172 * @remarks Not part of std::string.
173 */
174 RTCString(const char *a_pszFormat, va_list a_va) RT_IPRT_FORMAT_ATTR(1, 0)
175 : m_psz(NULL),
176 m_cch(0),
177 m_cbAllocated(0)
178 {
179 printfV(a_pszFormat, a_va);
180 }
181
182 /**
183 * Destructor.
184 */
185 virtual ~RTCString()
186 {
187 cleanup();
188 }
189
190 /**
191 * String length in bytes.
192 *
193 * Returns the length of the member string in bytes, which is equal to strlen(c_str()).
194 * In other words, this does not count unicode codepoints; use utf8length() for that.
195 * The byte length is always cached so calling this is cheap and requires no
196 * strlen() invocation.
197 *
198 * @returns m_cbLength.
199 */
200 size_t length() const
201 {
202 return m_cch;
203 }
204
205 /**
206 * String length in unicode codepoints.
207 *
208 * As opposed to length(), which returns the length in bytes, this counts
209 * the number of unicode codepoints. This is *not* cached so calling this
210 * is expensive.
211 *
212 * @returns Number of codepoints in the member string.
213 */
214 size_t uniLength() const
215 {
216 return m_psz ? RTStrUniLen(m_psz) : 0;
217 }
218
219 /**
220 * The allocated buffer size (in bytes).
221 *
222 * Returns the number of bytes allocated in the internal string buffer, which is
223 * at least length() + 1 if length() > 0; for an empty string, this returns 0.
224 *
225 * @returns m_cbAllocated.
226 */
227 size_t capacity() const
228 {
229 return m_cbAllocated;
230 }
231
232 /**
233 * Make sure at that least cb of buffer space is reserved.
234 *
235 * Requests that the contained memory buffer have at least cb bytes allocated.
236 * This may expand or shrink the string's storage, but will never truncate the
237 * contained string. In other words, cb will be ignored if it's smaller than
238 * length() + 1.
239 *
240 * @param cb New minimum size (in bytes) of member memory buffer.
241 *
242 * @throws std::bad_alloc On allocation error. The object is left unchanged.
243 */
244 void reserve(size_t cb)
245 {
246 if ( cb != m_cbAllocated
247 && cb > m_cch + 1
248 )
249 {
250 int rc = RTStrRealloc(&m_psz, cb);
251 if (RT_SUCCESS(rc))
252 m_cbAllocated = cb;
253#ifdef RT_EXCEPTIONS_ENABLED
254 else
255 throw std::bad_alloc();
256#endif
257 }
258 }
259
260 /**
261 * A C like version of the reserve method, i.e. return code instead of throw.
262 *
263 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
264 * @param cb New minimum size (in bytes) of member memory buffer.
265 */
266 int reserveNoThrow(size_t cb)
267 {
268 if ( cb != m_cbAllocated
269 && cb > m_cch + 1
270 )
271 {
272 int rc = RTStrRealloc(&m_psz, cb);
273 if (RT_SUCCESS(rc))
274 m_cbAllocated = cb;
275 else
276 return rc;
277 }
278 return VINF_SUCCESS;
279 }
280
281 /**
282 * Deallocates all memory.
283 */
284 inline void setNull()
285 {
286 cleanup();
287 }
288
289 RTMEMEF_NEW_AND_DELETE_OPERATORS();
290
291 /**
292 * Assigns a copy of pcsz to "this".
293 *
294 * @param pcsz The source string.
295 *
296 * @throws std::bad_alloc On allocation failure. The object is left describing
297 * a NULL string.
298 *
299 * @returns Reference to the object.
300 */
301 RTCString &operator=(const char *pcsz)
302 {
303 if (m_psz != pcsz)
304 {
305 cleanup();
306 copyFromN(pcsz, pcsz ? strlen(pcsz) : 0);
307 }
308 return *this;
309 }
310
311 /**
312 * Assigns a copy of s to "this".
313 *
314 * @param s The source string.
315 *
316 * @throws std::bad_alloc On allocation failure. The object is left describing
317 * a NULL string.
318 *
319 * @returns Reference to the object.
320 */
321 RTCString &operator=(const RTCString &s)
322 {
323 if (this != &s)
324 {
325 cleanup();
326 copyFromN(s.m_psz, s.m_cch);
327 }
328 return *this;
329 }
330
331 /**
332 * Assigns the output of the string format operation (RTStrPrintf).
333 *
334 * @param pszFormat Pointer to the format string,
335 * @see pg_rt_str_format.
336 * @param ... Ellipsis containing the arguments specified by
337 * the format string.
338 *
339 * @throws std::bad_alloc On allocation error. The object is left unchanged.
340 *
341 * @returns Reference to the object.
342 */
343 RTCString &printf(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
344
345 /**
346 * Assigns the output of the string format operation (RTStrPrintfV).
347 *
348 * @param pszFormat Pointer to the format string,
349 * @see pg_rt_str_format.
350 * @param va Argument vector containing the arguments
351 * specified by the format string.
352 *
353 * @throws std::bad_alloc On allocation error. The object is left unchanged.
354 *
355 * @returns Reference to the object.
356 */
357 RTCString &printfV(const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0);
358
359 /**
360 * Appends the string "that" to "this".
361 *
362 * @param that The string to append.
363 *
364 * @throws std::bad_alloc On allocation error. The object is left unchanged.
365 *
366 * @returns Reference to the object.
367 */
368 RTCString &append(const RTCString &that);
369
370 /**
371 * Appends the string "that" to "this".
372 *
373 * @param pszThat The C string to append.
374 *
375 * @throws std::bad_alloc On allocation error. The object is left unchanged.
376 *
377 * @returns Reference to the object.
378 */
379 RTCString &append(const char *pszThat);
380
381 /**
382 * Appends the given character to "this".
383 *
384 * @param ch The character to append.
385 *
386 * @throws std::bad_alloc On allocation error. The object is left unchanged.
387 *
388 * @returns Reference to the object.
389 */
390 RTCString &append(char ch);
391
392 /**
393 * Appends the given unicode code point to "this".
394 *
395 * @param uc The unicode code point to append.
396 *
397 * @throws std::bad_alloc On allocation error. The object is left unchanged.
398 *
399 * @returns Reference to the object.
400 */
401 RTCString &appendCodePoint(RTUNICP uc);
402
403 /**
404 * Shortcut to append(), RTCString variant.
405 *
406 * @param that The string to append.
407 *
408 * @returns Reference to the object.
409 */
410 RTCString &operator+=(const RTCString &that)
411 {
412 return append(that);
413 }
414
415 /**
416 * Shortcut to append(), const char* variant.
417 *
418 * @param pszThat The C string to append.
419 *
420 * @returns Reference to the object.
421 */
422 RTCString &operator+=(const char *pszThat)
423 {
424 return append(pszThat);
425 }
426
427 /**
428 * Shortcut to append(), char variant.
429 *
430 * @param ch The character to append.
431 *
432 * @returns Reference to the object.
433 */
434 RTCString &operator+=(char ch)
435 {
436 return append(ch);
437 }
438
439 /**
440 * Converts the member string to upper case.
441 *
442 * @returns Reference to the object.
443 */
444 RTCString &toUpper()
445 {
446 if (length())
447 {
448 /* Folding an UTF-8 string may result in a shorter encoding (see
449 testcase), so recalculate the length afterwards. */
450 ::RTStrToUpper(m_psz);
451 size_t cchNew = strlen(m_psz);
452 Assert(cchNew <= m_cch);
453 m_cch = cchNew;
454 }
455 return *this;
456 }
457
458 /**
459 * Converts the member string to lower case.
460 *
461 * @returns Reference to the object.
462 */
463 RTCString &toLower()
464 {
465 if (length())
466 {
467 /* Folding an UTF-8 string may result in a shorter encoding (see
468 testcase), so recalculate the length afterwards. */
469 ::RTStrToLower(m_psz);
470 size_t cchNew = strlen(m_psz);
471 Assert(cchNew <= m_cch);
472 m_cch = cchNew;
473 }
474 return *this;
475 }
476
477 /**
478 * Index operator.
479 *
480 * Returns the byte at the given index, or a null byte if the index is not
481 * smaller than length(). This does _not_ count codepoints but simply points
482 * into the member C string.
483 *
484 * @param i The index into the string buffer.
485 * @returns char at the index or null.
486 */
487 inline char operator[](size_t i) const
488 {
489 if (i < length())
490 return m_psz[i];
491 return '\0';
492 }
493
494 /**
495 * Returns the contained string as a C-style const char* pointer.
496 * This never returns NULL; if the string is empty, this returns a
497 * pointer to static null byte.
498 *
499 * @returns const pointer to C-style string.
500 */
501 inline const char *c_str() const
502 {
503 return (m_psz) ? m_psz : "";
504 }
505
506 /**
507 * Returns a non-const raw pointer that allows to modify the string directly.
508 * As opposed to c_str() and raw(), this DOES return NULL for an empty string
509 * because we cannot return a non-const pointer to a static "" global.
510 *
511 * @warning
512 * -# Be sure not to modify data beyond the allocated memory! Call
513 * capacity() to find out how large that buffer is.
514 * -# After any operation that modifies the length of the string,
515 * you _must_ call RTCString::jolt(), or subsequent copy operations
516 * may go nowhere. Better not use mutableRaw() at all.
517 */
518 char *mutableRaw()
519 {
520 return m_psz;
521 }
522
523 /**
524 * Clean up after using mutableRaw.
525 *
526 * Intended to be called after something has messed with the internal string
527 * buffer (e.g. after using mutableRaw() or Utf8Str::asOutParam()). Resets the
528 * internal lengths correctly. Otherwise subsequent copy operations may go
529 * nowhere.
530 */
531 void jolt()
532 {
533 if (m_psz)
534 {
535 m_cch = strlen(m_psz);
536 m_cbAllocated = m_cch + 1; /* (Required for the Utf8Str::asOutParam case) */
537 }
538 else
539 {
540 m_cch = 0;
541 m_cbAllocated = 0;
542 }
543 }
544
545 /**
546 * Returns @c true if the member string has no length.
547 *
548 * This is @c true for instances created from both NULL and "" input
549 * strings.
550 *
551 * This states nothing about how much memory might be allocated.
552 *
553 * @returns @c true if empty, @c false if not.
554 */
555 bool isEmpty() const
556 {
557 return length() == 0;
558 }
559
560 /**
561 * Returns @c false if the member string has no length.
562 *
563 * This is @c false for instances created from both NULL and "" input
564 * strings.
565 *
566 * This states nothing about how much memory might be allocated.
567 *
568 * @returns @c false if empty, @c true if not.
569 */
570 bool isNotEmpty() const
571 {
572 return length() != 0;
573 }
574
575 /** Case sensitivity selector. */
576 enum CaseSensitivity
577 {
578 CaseSensitive,
579 CaseInsensitive
580 };
581
582 /**
583 * Compares the member string to a C-string.
584 *
585 * @param pcszThat The string to compare with.
586 * @param cs Whether comparison should be case-sensitive.
587 * @returns 0 if equal, negative if this is smaller than @a pcsz, positive
588 * if larger.
589 */
590 int compare(const char *pcszThat, CaseSensitivity cs = CaseSensitive) const
591 {
592 /* This klugde is for m_cch=0 and m_psz=NULL. pcsz=NULL and psz=""
593 are treated the same way so that str.compare(str2.c_str()) works. */
594 if (length() == 0)
595 return pcszThat == NULL || *pcszThat == '\0' ? 0 : -1;
596
597 if (cs == CaseSensitive)
598 return ::RTStrCmp(m_psz, pcszThat);
599 return ::RTStrICmp(m_psz, pcszThat);
600 }
601
602 /**
603 * Compares the member string to another RTCString.
604 *
605 * @param rThat The string to compare with.
606 * @param cs Whether comparison should be case-sensitive.
607 * @returns 0 if equal, negative if this is smaller than @a pcsz, positive
608 * if larger.
609 */
610 int compare(const RTCString &rThat, CaseSensitivity cs = CaseSensitive) const
611 {
612 if (cs == CaseSensitive)
613 return ::RTStrCmp(m_psz, rThat.m_psz);
614 return ::RTStrICmp(m_psz, rThat.m_psz);
615 }
616
617 /**
618 * Compares the two strings.
619 *
620 * @returns true if equal, false if not.
621 * @param rThat The string to compare with.
622 */
623 bool equals(const RTCString &rThat) const
624 {
625 return rThat.length() == length()
626 && memcmp(rThat.m_psz, m_psz, length()) == 0;
627 }
628
629 /**
630 * Compares the two strings.
631 *
632 * @returns true if equal, false if not.
633 * @param pszThat The string to compare with.
634 */
635 bool equals(const char *pszThat) const
636 {
637 /* This klugde is for m_cch=0 and m_psz=NULL. pcsz=NULL and psz=""
638 are treated the same way so that str.equals(str2.c_str()) works. */
639 if (length() == 0)
640 return pszThat == NULL || *pszThat == '\0';
641 return RTStrCmp(pszThat, m_psz) == 0;
642 }
643
644 /**
645 * Compares the two strings ignoring differences in case.
646 *
647 * @returns true if equal, false if not.
648 * @param that The string to compare with.
649 */
650 bool equalsIgnoreCase(const RTCString &that) const
651 {
652 /* Unfolded upper and lower case characters may require different
653 amount of encoding space, so the length optimization doesn't work. */
654 return RTStrICmp(that.m_psz, m_psz) == 0;
655 }
656
657 /**
658 * Compares the two strings ignoring differences in case.
659 *
660 * @returns true if equal, false if not.
661 * @param pszThat The string to compare with.
662 */
663 bool equalsIgnoreCase(const char *pszThat) const
664 {
665 /* This klugde is for m_cch=0 and m_psz=NULL. pcsz=NULL and psz=""
666 are treated the same way so that str.equalsIgnoreCase(str2.c_str()) works. */
667 if (length() == 0)
668 return pszThat == NULL || *pszThat == '\0';
669 return RTStrICmp(pszThat, m_psz) == 0;
670 }
671
672 /** @name Comparison operators.
673 * @{ */
674 bool operator==(const RTCString &that) const { return equals(that); }
675 bool operator!=(const RTCString &that) const { return !equals(that); }
676 bool operator<( const RTCString &that) const { return compare(that) < 0; }
677 bool operator>( const RTCString &that) const { return compare(that) > 0; }
678
679 bool operator==(const char *pszThat) const { return equals(pszThat); }
680 bool operator!=(const char *pszThat) const { return !equals(pszThat); }
681 bool operator<( const char *pszThat) const { return compare(pszThat) < 0; }
682 bool operator>( const char *pszThat) const { return compare(pszThat) > 0; }
683 /** @} */
684
685 /** Max string offset value.
686 *
687 * When returned by a method, this indicates failure. When taken as input,
688 * typically a default, it means all the way to the string terminator.
689 */
690 static const size_t npos;
691
692 /**
693 * Find the given substring.
694 *
695 * Looks for pcszFind in "this" starting at "pos" and returns its position
696 * as a byte (not codepoint) offset, counting from the beginning of "this" at 0.
697 *
698 * @param pcszFind The substring to find.
699 * @param pos The (byte) offset into the string buffer to start
700 * searching.
701 *
702 * @returns 0 based position of pcszFind. npos if not found.
703 */
704 size_t find(const char *pcszFind, size_t pos = 0) const;
705
706 /**
707 * Replaces all occurences of cFind with cReplace in the member string.
708 * In order not to produce invalid UTF-8, the characters must be ASCII
709 * values less than 128; this is not verified.
710 *
711 * @param chFind Character to replace. Must be ASCII < 128.
712 * @param chReplace Character to replace cFind with. Must be ASCII < 128.
713 */
714 void findReplace(char chFind, char chReplace);
715
716 /**
717 * Count the occurences of the specified character in the string.
718 *
719 * @param ch What to search for. Must be ASCII < 128.
720 * @remarks QString::count
721 */
722 size_t count(char ch) const;
723
724 /**
725 * Count the occurences of the specified sub-string in the string.
726 *
727 * @param psz What to search for.
728 * @param cs Case sensitivity selector.
729 * @remarks QString::count
730 */
731 size_t count(const char *psz, CaseSensitivity cs = CaseSensitive) const;
732
733 /**
734 * Count the occurences of the specified sub-string in the string.
735 *
736 * @param pStr What to search for.
737 * @param cs Case sensitivity selector.
738 * @remarks QString::count
739 */
740 size_t count(const RTCString *pStr, CaseSensitivity cs = CaseSensitive) const;
741
742 /**
743 * Returns a substring of "this" as a new Utf8Str.
744 *
745 * Works exactly like its equivalent in std::string. With the default
746 * parameters "0" and "npos", this always copies the entire string. The
747 * "pos" and "n" arguments represent bytes; it is the caller's responsibility
748 * to ensure that the offsets do not copy invalid UTF-8 sequences. When
749 * used in conjunction with find() and length(), this will work.
750 *
751 * @param pos Index of first byte offset to copy from "this", counting from 0.
752 * @param n Number of bytes to copy, starting with the one at "pos".
753 * The copying will stop if the null terminator is encountered before
754 * n bytes have been copied.
755 */
756 RTCString substr(size_t pos = 0, size_t n = npos) const
757 {
758 return RTCString(*this, pos, n);
759 }
760
761 /**
762 * Returns a substring of "this" as a new Utf8Str. As opposed to substr(),
763 * this variant takes codepoint offsets instead of byte offsets.
764 *
765 * @param pos Index of first unicode codepoint to copy from
766 * "this", counting from 0.
767 * @param n Number of unicode codepoints to copy, starting with
768 * the one at "pos". The copying will stop if the null
769 * terminator is encountered before n codepoints have
770 * been copied.
771 */
772 RTCString substrCP(size_t pos = 0, size_t n = npos) const;
773
774 /**
775 * Returns true if "this" ends with "that".
776 *
777 * @param that Suffix to test for.
778 * @param cs Case sensitivity selector.
779 * @returns true if match, false if mismatch.
780 */
781 bool endsWith(const RTCString &that, CaseSensitivity cs = CaseSensitive) const;
782
783 /**
784 * Returns true if "this" begins with "that".
785 * @param that Prefix to test for.
786 * @param cs Case sensitivity selector.
787 * @returns true if match, false if mismatch.
788 */
789 bool startsWith(const RTCString &that, CaseSensitivity cs = CaseSensitive) const;
790
791 /**
792 * Returns true if "this" contains "that" (strstr).
793 *
794 * @param that Substring to look for.
795 * @param cs Case sensitivity selector.
796 * @returns true if match, false if mismatch.
797 */
798 bool contains(const RTCString &that, CaseSensitivity cs = CaseSensitive) const;
799
800 /**
801 * Attempts to convert the member string into a 32-bit integer.
802 *
803 * @returns 32-bit unsigned number on success.
804 * @returns 0 on failure.
805 */
806 int32_t toInt32() const
807 {
808 return RTStrToInt32(m_psz);
809 }
810
811 /**
812 * Attempts to convert the member string into an unsigned 32-bit integer.
813 *
814 * @returns 32-bit unsigned number on success.
815 * @returns 0 on failure.
816 */
817 uint32_t toUInt32() const
818 {
819 return RTStrToUInt32(m_psz);
820 }
821
822 /**
823 * Attempts to convert the member string into an 64-bit integer.
824 *
825 * @returns 64-bit unsigned number on success.
826 * @returns 0 on failure.
827 */
828 int64_t toInt64() const
829 {
830 return RTStrToInt64(m_psz);
831 }
832
833 /**
834 * Attempts to convert the member string into an unsigned 64-bit integer.
835 *
836 * @returns 64-bit unsigned number on success.
837 * @returns 0 on failure.
838 */
839 uint64_t toUInt64() const
840 {
841 return RTStrToUInt64(m_psz);
842 }
843
844 /**
845 * Attempts to convert the member string into an unsigned 64-bit integer.
846 *
847 * @param i Where to return the value on success.
848 * @returns IPRT error code, see RTStrToInt64.
849 */
850 int toInt(uint64_t &i) const;
851
852 /**
853 * Attempts to convert the member string into an unsigned 32-bit integer.
854 *
855 * @param i Where to return the value on success.
856 * @returns IPRT error code, see RTStrToInt32.
857 */
858 int toInt(uint32_t &i) const;
859
860 /** Splitting behavior regarding empty sections in the string. */
861 enum SplitMode
862 {
863 KeepEmptyParts, /**< Empty parts are added as empty strings to the result list. */
864 RemoveEmptyParts /**< Empty parts are skipped. */
865 };
866
867 /**
868 * Splits a string separated by strSep into its parts.
869 *
870 * @param a_rstrSep The separator to search for.
871 * @param a_enmMode How should empty parts be handled.
872 * @returns separated strings as string list.
873 */
874 RTCList<RTCString, RTCString *> split(const RTCString &a_rstrSep,
875 SplitMode a_enmMode = RemoveEmptyParts) const;
876
877 /**
878 * Joins a list of strings together using the provided separator and
879 * an optional prefix for each item in the list.
880 *
881 * @param a_rList The list to join.
882 * @param a_rstrPrefix The prefix used for appending to each item.
883 * @param a_rstrSep The separator used for joining.
884 * @returns joined string.
885 */
886 static RTCString joinEx(const RTCList<RTCString, RTCString *> &a_rList,
887 const RTCString &a_rstrPrefix /* = "" */,
888 const RTCString &a_rstrSep /* = "" */);
889
890 /**
891 * Joins a list of strings together using the provided separator.
892 *
893 * @param a_rList The list to join.
894 * @param a_rstrSep The separator used for joining.
895 * @returns joined string.
896 */
897 static RTCString join(const RTCList<RTCString, RTCString *> &a_rList,
898 const RTCString &a_rstrSep = "");
899
900 /**
901 * Swaps two strings in a fast way.
902 *
903 * Exception safe.
904 *
905 * @param a_rThat The string to swap with.
906 */
907 inline void swap(RTCString &a_rThat) throw()
908 {
909 char *pszTmp = m_psz;
910 size_t cchTmp = m_cch;
911 size_t cbAllocatedTmp = m_cbAllocated;
912
913 m_psz = a_rThat.m_psz;
914 m_cch = a_rThat.m_cch;
915 m_cbAllocated = a_rThat.m_cbAllocated;
916
917 a_rThat.m_psz = pszTmp;
918 a_rThat.m_cch = cchTmp;
919 a_rThat.m_cbAllocated = cbAllocatedTmp;
920 }
921
922protected:
923
924 /**
925 * Hide operator bool() to force people to use isEmpty() explicitly.
926 */
927 operator bool() const;
928
929 /**
930 * Destructor implementation, also used to clean up in operator=() before
931 * assigning a new string.
932 */
933 void cleanup()
934 {
935 if (m_psz)
936 {
937 RTStrFree(m_psz);
938 m_psz = NULL;
939 m_cch = 0;
940 m_cbAllocated = 0;
941 }
942 }
943
944 /**
945 * Protected internal helper to copy a string.
946 *
947 * This ignores the previous object state, so either call this from a
948 * constructor or call cleanup() first. copyFromN() unconditionally sets
949 * the members to a copy of the given other strings and makes no
950 * assumptions about previous contents. Can therefore be used both in copy
951 * constructors, when member variables have no defined value, and in
952 * assignments after having called cleanup().
953 *
954 * @param pcszSrc The source string.
955 * @param cchSrc The number of chars (bytes) to copy from the
956 * source strings. RTSTR_MAX is NOT accepted.
957 *
958 * @throws std::bad_alloc On allocation failure. The object is left
959 * describing a NULL string.
960 */
961 void copyFromN(const char *pcszSrc, size_t cchSrc)
962 {
963 if (cchSrc)
964 {
965 m_psz = RTStrAlloc(cchSrc + 1);
966 if (RT_LIKELY(m_psz))
967 {
968 m_cch = cchSrc;
969 m_cbAllocated = cchSrc + 1;
970 memcpy(m_psz, pcszSrc, cchSrc);
971 m_psz[cchSrc] = '\0';
972 }
973 else
974 {
975 m_cch = 0;
976 m_cbAllocated = 0;
977#ifdef RT_EXCEPTIONS_ENABLED
978 throw std::bad_alloc();
979#endif
980 }
981 }
982 else
983 {
984 m_cch = 0;
985 m_cbAllocated = 0;
986 m_psz = NULL;
987 }
988 }
989
990 static DECLCALLBACK(size_t) printfOutputCallback(void *pvArg, const char *pachChars, size_t cbChars);
991
992 char *m_psz; /**< The string buffer. */
993 size_t m_cch; /**< strlen(m_psz) - i.e. no terminator included. */
994 size_t m_cbAllocated; /**< Size of buffer that m_psz points to; at least m_cbLength + 1. */
995};
996
997/** @} */
998
999
1000/** @addtogroup grp_rt_cpp_string
1001 * @{
1002 */
1003
1004/**
1005 * Concatenate two strings.
1006 *
1007 * @param a_rstr1 String one.
1008 * @param a_rstr2 String two.
1009 * @returns the concatenate string.
1010 *
1011 * @relates RTCString
1012 */
1013RTDECL(const RTCString) operator+(const RTCString &a_rstr1, const RTCString &a_rstr2);
1014
1015/**
1016 * Concatenate two strings.
1017 *
1018 * @param a_rstr1 String one.
1019 * @param a_psz2 String two.
1020 * @returns the concatenate string.
1021 *
1022 * @relates RTCString
1023 */
1024RTDECL(const RTCString) operator+(const RTCString &a_rstr1, const char *a_psz2);
1025
1026/**
1027 * Concatenate two strings.
1028 *
1029 * @param a_psz1 String one.
1030 * @param a_rstr2 String two.
1031 * @returns the concatenate string.
1032 *
1033 * @relates RTCString
1034 */
1035RTDECL(const RTCString) operator+(const char *a_psz1, const RTCString &a_rstr2);
1036
1037/**
1038 * Class with RTCString::printf as constructor for your convenience.
1039 *
1040 * Constructing a RTCString string object from a format string and a variable
1041 * number of arguments can easily be confused with the other RTCString
1042 * constructors, thus this child class.
1043 *
1044 * The usage of this class is like the following:
1045 * @code
1046 RTCStringFmt strName("program name = %s", argv[0]);
1047 @endcode
1048 */
1049class RTCStringFmt : public RTCString
1050{
1051public:
1052
1053 /**
1054 * Constructs a new string given the format string and the list of the
1055 * arguments for the format string.
1056 *
1057 * @param a_pszFormat Pointer to the format string (UTF-8),
1058 * @see pg_rt_str_format.
1059 * @param ... Ellipsis containing the arguments specified by
1060 * the format string.
1061 */
1062 explicit RTCStringFmt(const char *a_pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2)
1063 {
1064 va_list va;
1065 va_start(va, a_pszFormat);
1066 printfV(a_pszFormat, va);
1067 va_end(va);
1068 }
1069
1070 RTMEMEF_NEW_AND_DELETE_OPERATORS();
1071
1072protected:
1073 RTCStringFmt() {}
1074};
1075
1076/** @} */
1077
1078#endif
1079
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette