VirtualBox

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

Last change on this file since 36532 was 36532, checked in by vboxsync, 14 years ago

IPRT-C++: iprt::list -> RTCList; iprt::mtlist -> RTCMTList

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 28.8 KB
Line 
1/** @file
2 * IPRT - C++ string class.
3 */
4
5/*
6 * Copyright (C) 2007-2011 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)
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 vrc = RTStrRealloc(&m_psz, cb);
251 if (RT_SUCCESS(vrc))
252 m_cbAllocated = cb;
253#ifdef RT_EXCEPTIONS_ENABLED
254 else
255 throw std::bad_alloc();
256#endif
257 }
258 }
259
260 /**
261 * Deallocates all memory.
262 */
263 inline void setNull()
264 {
265 cleanup();
266 }
267
268 RTMEMEF_NEW_AND_DELETE_OPERATORS();
269
270 /**
271 * Assigns a copy of pcsz to "this".
272 *
273 * @param pcsz The source string.
274 *
275 * @throws std::bad_alloc On allocation failure. The object is left describing
276 * a NULL string.
277 *
278 * @returns Reference to the object.
279 */
280 RTCString &operator=(const char *pcsz)
281 {
282 if (m_psz != pcsz)
283 {
284 cleanup();
285 copyFromN(pcsz, pcsz ? strlen(pcsz) : 0);
286 }
287 return *this;
288 }
289
290 /**
291 * Assigns a copy of s to "this".
292 *
293 * @param s The source string.
294 *
295 * @throws std::bad_alloc On allocation failure. The object is left describing
296 * a NULL string.
297 *
298 * @returns Reference to the object.
299 */
300 RTCString &operator=(const RTCString &s)
301 {
302 if (this != &s)
303 {
304 cleanup();
305 copyFromN(s.m_psz, s.m_cch);
306 }
307 return *this;
308 }
309
310 /**
311 * Assigns the output of the string format operation (RTStrPrintf).
312 *
313 * @param pszFormat Pointer to the format string,
314 * @see pg_rt_str_format.
315 * @param ... Ellipsis containing the arguments specified by
316 * the format string.
317 *
318 * @throws std::bad_alloc On allocation error. The object is left unchanged.
319 *
320 * @returns Reference to the object.
321 */
322 RTCString &printf(const char *pszFormat, ...);
323
324 /**
325 * Assigns the output of the string format operation (RTStrPrintfV).
326 *
327 * @param pszFormat Pointer to the format string,
328 * @see pg_rt_str_format.
329 * @param va Argument vector containing the arguments
330 * specified by the format string.
331 *
332 * @throws std::bad_alloc On allocation error. The object is left unchanged.
333 *
334 * @returns Reference to the object.
335 */
336 RTCString &printfV(const char *pszFormat, va_list va);
337
338 /**
339 * Appends the string "that" to "this".
340 *
341 * @param that The string to append.
342 *
343 * @throws std::bad_alloc On allocation error. The object is left unchanged.
344 *
345 * @returns Reference to the object.
346 */
347 RTCString &append(const RTCString &that);
348
349 /**
350 * Appends the string "that" to "this".
351 *
352 * @param pszThat The C string to append.
353 *
354 * @throws std::bad_alloc On allocation error. The object is left unchanged.
355 *
356 * @returns Reference to the object.
357 */
358 RTCString &append(const char *pszThat);
359
360 /**
361 * Appends the given character to "this".
362 *
363 * @param ch The character to append.
364 *
365 * @throws std::bad_alloc On allocation error. The object is left unchanged.
366 *
367 * @returns Reference to the object.
368 */
369 RTCString &append(char ch);
370
371 /**
372 * Appends the given unicode code point to "this".
373 *
374 * @param uc The unicode code point to append.
375 *
376 * @throws std::bad_alloc On allocation error. The object is left unchanged.
377 *
378 * @returns Reference to the object.
379 */
380 RTCString &appendCodePoint(RTUNICP uc);
381
382 /**
383 * Shortcut to append(), RTCString variant.
384 *
385 * @param that The string to append.
386 *
387 * @returns Reference to the object.
388 */
389 RTCString &operator+=(const RTCString &that)
390 {
391 return append(that);
392 }
393
394 /**
395 * Shortcut to append(), const char* variant.
396 *
397 * @param pszThat The C string to append.
398 *
399 * @returns Reference to the object.
400 */
401 RTCString &operator+=(const char *pszThat)
402 {
403 return append(pszThat);
404 }
405
406 /**
407 * Shortcut to append(), char variant.
408 *
409 * @param pszThat The character to append.
410 *
411 * @returns Reference to the object.
412 */
413 RTCString &operator+=(char c)
414 {
415 return append(c);
416 }
417
418 /**
419 * Converts the member string to upper case.
420 *
421 * @returns Reference to the object.
422 */
423 RTCString &toUpper()
424 {
425 if (length())
426 {
427 /* Folding an UTF-8 string may result in a shorter encoding (see
428 testcase), so recalculate the length afterwars. */
429 ::RTStrToUpper(m_psz);
430 size_t cchNew = strlen(m_psz);
431 Assert(cchNew <= m_cch);
432 m_cch = cchNew;
433 }
434 return *this;
435 }
436
437 /**
438 * Converts the member string to lower case.
439 *
440 * @returns Reference to the object.
441 */
442 RTCString &toLower()
443 {
444 if (length())
445 {
446 /* Folding an UTF-8 string may result in a shorter encoding (see
447 testcase), so recalculate the length afterwars. */
448 ::RTStrToLower(m_psz);
449 size_t cchNew = strlen(m_psz);
450 Assert(cchNew <= m_cch);
451 m_cch = cchNew;
452 }
453 return *this;
454 }
455
456 /**
457 * Index operator.
458 *
459 * Returns the byte at the given index, or a null byte if the index is not
460 * smaller than length(). This does _not_ count codepoints but simply points
461 * into the member C string.
462 *
463 * @param i The index into the string buffer.
464 * @returns char at the index or null.
465 */
466 inline char operator[](size_t i) const
467 {
468 if (i < length())
469 return m_psz[i];
470 return '\0';
471 }
472
473 /**
474 * Returns the contained string as a C-style const char* pointer.
475 * This never returns NULL; if the string is empty, this returns a
476 * pointer to static null byte.
477 *
478 * @returns const pointer to C-style string.
479 */
480 inline const char *c_str() const
481 {
482 return (m_psz) ? m_psz : "";
483 }
484
485 /**
486 * Returns a non-const raw pointer that allows to modify the string directly.
487 * As opposed to c_str() and raw(), this DOES return NULL for an empty string
488 * because we cannot return a non-const pointer to a static "" global.
489 *
490 * @warning
491 * -# Be sure not to modify data beyond the allocated memory! Call
492 * capacity() to find out how large that buffer is.
493 * -# After any operation that modifies the length of the string,
494 * you _must_ call RTCString::jolt(), or subsequent copy operations
495 * may go nowhere. Better not use mutableRaw() at all.
496 */
497 char *mutableRaw()
498 {
499 return m_psz;
500 }
501
502 /**
503 * Clean up after using mutableRaw.
504 *
505 * Intended to be called after something has messed with the internal string
506 * buffer (e.g. after using mutableRaw() or Utf8Str::asOutParam()). Resets the
507 * internal lengths correctly. Otherwise subsequent copy operations may go
508 * nowhere.
509 */
510 void jolt()
511 {
512 if (m_psz)
513 {
514 m_cch = strlen(m_psz);
515 m_cbAllocated = m_cch + 1; /* (Required for the Utf8Str::asOutParam case) */
516 }
517 else
518 {
519 m_cch = 0;
520 m_cbAllocated = 0;
521 }
522 }
523
524 /**
525 * Returns @c true if the member string has no length.
526 *
527 * This is @c true for instances created from both NULL and "" input
528 * strings.
529 *
530 * This states nothing about how much memory might be allocated.
531 *
532 * @returns @c true if empty, @c false if not.
533 */
534 bool isEmpty() const
535 {
536 return length() == 0;
537 }
538
539 /**
540 * Returns @c false if the member string has no length.
541 *
542 * This is @c false for instances created from both NULL and "" input
543 * strings.
544 *
545 * This states nothing about how much memory might be allocated.
546 *
547 * @returns @c false if empty, @c true if not.
548 */
549 bool isNotEmpty() const
550 {
551 return length() != 0;
552 }
553
554 /** Case sensitivity selector. */
555 enum CaseSensitivity
556 {
557 CaseSensitive,
558 CaseInsensitive
559 };
560
561 /**
562 * Compares the member string to a C-string.
563 *
564 * @param pcszThat The string to compare with.
565 * @param cs Whether comparison should be case-sensitive.
566 * @returns 0 if equal, negative if this is smaller than @a pcsz, positive
567 * if larger.
568 */
569 int compare(const char *pcszThat, CaseSensitivity cs = CaseSensitive) const
570 {
571 /* This klugde is for m_cch=0 and m_psz=NULL. pcsz=NULL and psz=""
572 are treated the same way so that str.compare(str2.c_str()) works. */
573 if (length() == 0)
574 return pcszThat == NULL || *pcszThat == '\0' ? 0 : -1;
575
576 if (cs == CaseSensitive)
577 return ::RTStrCmp(m_psz, pcszThat);
578 return ::RTStrICmp(m_psz, pcszThat);
579 }
580
581 /**
582 * Compares the member string to another RTCString.
583 *
584 * @param pcszThat The string to compare with.
585 * @param cs Whether comparison should be case-sensitive.
586 * @returns 0 if equal, negative if this is smaller than @a pcsz, positive
587 * if larger.
588 */
589 int compare(const RTCString &that, CaseSensitivity cs = CaseSensitive) const
590 {
591 if (cs == CaseSensitive)
592 return ::RTStrCmp(m_psz, that.m_psz);
593 return ::RTStrICmp(m_psz, that.m_psz);
594 }
595
596 /**
597 * Compares the two strings.
598 *
599 * @returns true if equal, false if not.
600 * @param that The string to compare with.
601 */
602 bool equals(const RTCString &that) const
603 {
604 return that.length() == length()
605 && memcmp(that.m_psz, m_psz, length()) == 0;
606 }
607
608 /**
609 * Compares the two strings.
610 *
611 * @returns true if equal, false if not.
612 * @param pszThat The string to compare with.
613 */
614 bool equals(const char *pszThat) const
615 {
616 /* This klugde is for m_cch=0 and m_psz=NULL. pcsz=NULL and psz=""
617 are treated the same way so that str.equals(str2.c_str()) works. */
618 if (length() == 0)
619 return pszThat == NULL || *pszThat == '\0';
620 return RTStrCmp(pszThat, m_psz) == 0;
621 }
622
623 /**
624 * Compares the two strings ignoring differences in case.
625 *
626 * @returns true if equal, false if not.
627 * @param that The string to compare with.
628 */
629 bool equalsIgnoreCase(const RTCString &that) const
630 {
631 /* Unfolded upper and lower case characters may require different
632 amount of encoding space, so the length optimization doesn't work. */
633 return RTStrICmp(that.m_psz, m_psz) == 0;
634 }
635
636 /**
637 * Compares the two strings ignoring differences in case.
638 *
639 * @returns true if equal, false if not.
640 * @param pszThat The string to compare with.
641 */
642 bool equalsIgnoreCase(const char *pszThat) const
643 {
644 /* This klugde is for m_cch=0 and m_psz=NULL. pcsz=NULL and psz=""
645 are treated the same way so that str.equalsIgnoreCase(str2.c_str()) works. */
646 if (length() == 0)
647 return pszThat == NULL || *pszThat == '\0';
648 return RTStrICmp(pszThat, m_psz) == 0;
649 }
650
651 /** @name Comparison operators.
652 * @{ */
653 bool operator==(const RTCString &that) const { return equals(that); }
654 bool operator!=(const RTCString &that) const { return !equals(that); }
655 bool operator<( const RTCString &that) const { return compare(that) < 0; }
656 bool operator>( const RTCString &that) const { return compare(that) > 0; }
657
658 bool operator==(const char *pszThat) const { return equals(pszThat); }
659 bool operator!=(const char *pszThat) const { return !equals(pszThat); }
660 bool operator<( const char *pszThat) const { return compare(pszThat) < 0; }
661 bool operator>( const char *pszThat) const { return compare(pszThat) > 0; }
662 /** @} */
663
664 /** Max string offset value.
665 *
666 * When returned by a method, this indicates failure. When taken as input,
667 * typically a default, it means all the way to the string terminator.
668 */
669 static const size_t npos;
670
671 /**
672 * Find the given substring.
673 *
674 * Looks for pcszFind in "this" starting at "pos" and returns its position
675 * as a byte (not codepoint) offset, counting from the beginning of "this" at 0.
676 *
677 * @param pcszFind The substring to find.
678 * @param pos The (byte) offset into the string buffer to start
679 * searching.
680 *
681 * @returns 0 based position of pcszFind. npos if not found.
682 */
683 size_t find(const char *pcszFind, size_t pos = 0) const;
684
685 /**
686 * Replaces all occurences of cFind with cReplace in the member string.
687 * In order not to produce invalid UTF-8, the characters must be ASCII
688 * values less than 128; this is not verified.
689 *
690 * @param cFind Character to replace. Must be ASCII < 128.
691 * @param cReplace Character to replace cFind with. Must be ASCII < 128.
692 */
693 void findReplace(char cFind, char cReplace);
694
695 /**
696 * Returns a substring of "this" as a new Utf8Str.
697 *
698 * Works exactly like its equivalent in std::string. With the default
699 * parameters "0" and "npos", this always copies the entire string. The
700 * "pos" and "n" arguments represent bytes; it is the caller's responsibility
701 * to ensure that the offsets do not copy invalid UTF-8 sequences. When
702 * used in conjunction with find() and length(), this will work.
703 *
704 * @param pos Index of first byte offset to copy from "this", counting from 0.
705 * @param n Number of bytes to copy, starting with the one at "pos".
706 * The copying will stop if the null terminator is encountered before
707 * n bytes have been copied.
708 */
709 RTCString substr(size_t pos = 0, size_t n = npos) const
710 {
711 return RTCString(*this, pos, n);
712 }
713
714 /**
715 * Returns a substring of "this" as a new Utf8Str. As opposed to substr(),
716 * this variant takes codepoint offsets instead of byte offsets.
717 *
718 * @param pos Index of first unicode codepoint to copy from
719 * "this", counting from 0.
720 * @param n Number of unicode codepoints to copy, starting with
721 * the one at "pos". The copying will stop if the null
722 * terminator is encountered before n codepoints have
723 * been copied.
724 */
725 RTCString substrCP(size_t pos = 0, size_t n = npos) const;
726
727 /**
728 * Returns true if "this" ends with "that".
729 *
730 * @param that Suffix to test for.
731 * @param cs Case sensitivity selector.
732 * @returns true if match, false if mismatch.
733 */
734 bool endsWith(const RTCString &that, CaseSensitivity cs = CaseSensitive) const;
735
736 /**
737 * Returns true if "this" begins with "that".
738 * @param that Prefix to test for.
739 * @param cs Case sensitivity selector.
740 * @returns true if match, false if mismatch.
741 */
742 bool startsWith(const RTCString &that, CaseSensitivity cs = CaseSensitive) const;
743
744 /**
745 * Returns true if "this" contains "that" (strstr).
746 *
747 * @param that Substring to look for.
748 * @param cs Case sensitivity selector.
749 * @returns true if match, false if mismatch.
750 */
751 bool contains(const RTCString &that, CaseSensitivity cs = CaseSensitive) const;
752
753 /**
754 * Attempts to convert the member string into a 32-bit integer.
755 *
756 * @returns 32-bit unsigned number on success.
757 * @returns 0 on failure.
758 */
759 int32_t toInt32() const
760 {
761 return RTStrToInt32(m_psz);
762 }
763
764 /**
765 * Attempts to convert the member string into an unsigned 32-bit integer.
766 *
767 * @returns 32-bit unsigned number on success.
768 * @returns 0 on failure.
769 */
770 uint32_t toUInt32() const
771 {
772 return RTStrToUInt32(m_psz);
773 }
774
775 /**
776 * Attempts to convert the member string into an 64-bit integer.
777 *
778 * @returns 64-bit unsigned number on success.
779 * @returns 0 on failure.
780 */
781 int64_t toInt64() const
782 {
783 return RTStrToInt64(m_psz);
784 }
785
786 /**
787 * Attempts to convert the member string into an unsigned 64-bit integer.
788 *
789 * @returns 64-bit unsigned number on success.
790 * @returns 0 on failure.
791 */
792 uint64_t toUInt64() const
793 {
794 return RTStrToUInt64(m_psz);
795 }
796
797 /**
798 * Attempts to convert the member string into an unsigned 64-bit integer.
799 *
800 * @param i Where to return the value on success.
801 * @returns IPRT error code, see RTStrToInt64.
802 */
803 int toInt(uint64_t &i) const;
804
805 /**
806 * Attempts to convert the member string into an unsigned 32-bit integer.
807 *
808 * @param i Where to return the value on success.
809 * @returns IPRT error code, see RTStrToInt32.
810 */
811 int toInt(uint32_t &i) const;
812
813 /** Splitting behavior regarding empty sections in the string. */
814 enum SplitMode
815 {
816 KeepEmptyParts, /**< Empty parts are added as empty strings to the result list. */
817 RemoveEmptyParts /**< Empty parts are skipped. */
818 };
819
820 /**
821 * Splits a string separated by strSep into its parts.
822 *
823 * @param a_rstrSep The separator to search for.
824 * @param a_enmMode How should empty parts be handled.
825 * @returns separated strings as string list.
826 */
827 RTCList<RTCString, RTCString *> split(const RTCString &a_rstrSep,
828 SplitMode a_enmMode = RemoveEmptyParts);
829
830 /**
831 * Joins a list of strings together using the provided separator.
832 *
833 * @param a_rList The list to join.
834 * @param a_rstrSep The separator used for joining.
835 * @returns joined string.
836 */
837 static RTCString join(const RTCList<RTCString, RTCString *> &a_rList,
838 const RTCString &a_rstrSep = "");
839
840protected:
841
842 /**
843 * Hide operator bool() to force people to use isEmpty() explicitly.
844 */
845 operator bool() const;
846
847 /**
848 * Destructor implementation, also used to clean up in operator=() before
849 * assigning a new string.
850 */
851 void cleanup()
852 {
853 if (m_psz)
854 {
855 RTStrFree(m_psz);
856 m_psz = NULL;
857 m_cch = 0;
858 m_cbAllocated = 0;
859 }
860 }
861
862 /**
863 * Protected internal helper to copy a string.
864 *
865 * This ignores the previous object state, so either call this from a
866 * constructor or call cleanup() first. copyFromN() unconditionally sets
867 * the members to a copy of the given other strings and makes no
868 * assumptions about previous contents. Can therefore be used both in copy
869 * constructors, when member variables have no defined value, and in
870 * assignments after having called cleanup().
871 *
872 * @param pcszSrc The source string.
873 * @param cchSrc The number of chars (bytes) to copy from the
874 * source strings. RTSTR_MAX is NOT accepted.
875 *
876 * @throws std::bad_alloc On allocation failure. The object is left
877 * describing a NULL string.
878 */
879 void copyFromN(const char *pcszSrc, size_t cchSrc)
880 {
881 if (cchSrc)
882 {
883 m_psz = RTStrAlloc(cchSrc + 1);
884 if (RT_LIKELY(m_psz))
885 {
886 m_cch = cchSrc;
887 m_cbAllocated = cchSrc + 1;
888 memcpy(m_psz, pcszSrc, cchSrc);
889 m_psz[cchSrc] = '\0';
890 }
891 else
892 {
893 m_cch = 0;
894 m_cbAllocated = 0;
895#ifdef RT_EXCEPTIONS_ENABLED
896 throw std::bad_alloc();
897#endif
898 }
899 }
900 else
901 {
902 m_cch = 0;
903 m_cbAllocated = 0;
904 m_psz = NULL;
905 }
906 }
907
908 static DECLCALLBACK(size_t) printfOutputCallback(void *pvArg, const char *pachChars, size_t cbChars);
909
910 char *m_psz; /**< The string buffer. */
911 size_t m_cch; /**< strlen(m_psz) - i.e. no terminator included. */
912 size_t m_cbAllocated; /**< Size of buffer that m_psz points to; at least m_cbLength + 1. */
913};
914
915/** @} */
916
917
918/** @addtogroup grp_rt_cpp_string
919 * @{
920 */
921
922/**
923 * Concatenate two strings.
924 *
925 * @param a_rstr1 String one.
926 * @param a_rstr2 String two.
927 * @returns the concatenate string.
928 *
929 * @relates RTCString
930 */
931RTDECL(const RTCString) operator+(const RTCString &a_rstr1, const RTCString &a_rstr2);
932
933/**
934 * Concatenate two strings.
935 *
936 * @param a_rstr1 String one.
937 * @param a_psz2 String two.
938 * @returns the concatenate string.
939 *
940 * @relates RTCString
941 */
942RTDECL(const RTCString) operator+(const RTCString &a_rstr1, const char *a_psz2);
943
944/**
945 * Concatenate two strings.
946 *
947 * @param a_psz1 String one.
948 * @param a_rstr2 String two.
949 * @returns the concatenate string.
950 *
951 * @relates RTCString
952 */
953RTDECL(const RTCString) operator+(const char *a_psz1, const RTCString &a_rstr2);
954
955/** @} */
956
957#endif
958
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