VirtualBox

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

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

RTCString::equals(): don't call memcmp() with a zero length

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 32.6 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 && ( length() == 0
627 || memcmp(rThat.m_psz, m_psz, length()) == 0);
628 }
629
630 /**
631 * Compares the two strings.
632 *
633 * @returns true if equal, false if not.
634 * @param pszThat The string to compare with.
635 */
636 bool equals(const char *pszThat) const
637 {
638 /* This klugde is for m_cch=0 and m_psz=NULL. pcsz=NULL and psz=""
639 are treated the same way so that str.equals(str2.c_str()) works. */
640 if (length() == 0)
641 return pszThat == NULL || *pszThat == '\0';
642 return RTStrCmp(pszThat, m_psz) == 0;
643 }
644
645 /**
646 * Compares the two strings ignoring differences in case.
647 *
648 * @returns true if equal, false if not.
649 * @param that The string to compare with.
650 */
651 bool equalsIgnoreCase(const RTCString &that) const
652 {
653 /* Unfolded upper and lower case characters may require different
654 amount of encoding space, so the length optimization doesn't work. */
655 return RTStrICmp(that.m_psz, m_psz) == 0;
656 }
657
658 /**
659 * Compares the two strings ignoring differences in case.
660 *
661 * @returns true if equal, false if not.
662 * @param pszThat The string to compare with.
663 */
664 bool equalsIgnoreCase(const char *pszThat) const
665 {
666 /* This klugde is for m_cch=0 and m_psz=NULL. pcsz=NULL and psz=""
667 are treated the same way so that str.equalsIgnoreCase(str2.c_str()) works. */
668 if (length() == 0)
669 return pszThat == NULL || *pszThat == '\0';
670 return RTStrICmp(pszThat, m_psz) == 0;
671 }
672
673 /** @name Comparison operators.
674 * @{ */
675 bool operator==(const RTCString &that) const { return equals(that); }
676 bool operator!=(const RTCString &that) const { return !equals(that); }
677 bool operator<( const RTCString &that) const { return compare(that) < 0; }
678 bool operator>( const RTCString &that) const { return compare(that) > 0; }
679
680 bool operator==(const char *pszThat) const { return equals(pszThat); }
681 bool operator!=(const char *pszThat) const { return !equals(pszThat); }
682 bool operator<( const char *pszThat) const { return compare(pszThat) < 0; }
683 bool operator>( const char *pszThat) const { return compare(pszThat) > 0; }
684 /** @} */
685
686 /** Max string offset value.
687 *
688 * When returned by a method, this indicates failure. When taken as input,
689 * typically a default, it means all the way to the string terminator.
690 */
691 static const size_t npos;
692
693 /**
694 * Find the given substring.
695 *
696 * Looks for pcszFind in "this" starting at "pos" and returns its position
697 * as a byte (not codepoint) offset, counting from the beginning of "this" at 0.
698 *
699 * @param pcszFind The substring to find.
700 * @param pos The (byte) offset into the string buffer to start
701 * searching.
702 *
703 * @returns 0 based position of pcszFind. npos if not found.
704 */
705 size_t find(const char *pcszFind, size_t pos = 0) const;
706
707 /**
708 * Replaces all occurences of cFind with cReplace in the member string.
709 * In order not to produce invalid UTF-8, the characters must be ASCII
710 * values less than 128; this is not verified.
711 *
712 * @param chFind Character to replace. Must be ASCII < 128.
713 * @param chReplace Character to replace cFind with. Must be ASCII < 128.
714 */
715 void findReplace(char chFind, char chReplace);
716
717 /**
718 * Count the occurences of the specified character in the string.
719 *
720 * @param ch What to search for. Must be ASCII < 128.
721 * @remarks QString::count
722 */
723 size_t count(char ch) const;
724
725 /**
726 * Count the occurences of the specified sub-string in the string.
727 *
728 * @param psz What to search for.
729 * @param cs Case sensitivity selector.
730 * @remarks QString::count
731 */
732 size_t count(const char *psz, CaseSensitivity cs = CaseSensitive) const;
733
734 /**
735 * Count the occurences of the specified sub-string in the string.
736 *
737 * @param pStr What to search for.
738 * @param cs Case sensitivity selector.
739 * @remarks QString::count
740 */
741 size_t count(const RTCString *pStr, CaseSensitivity cs = CaseSensitive) const;
742
743 /**
744 * Returns a substring of "this" as a new Utf8Str.
745 *
746 * Works exactly like its equivalent in std::string. With the default
747 * parameters "0" and "npos", this always copies the entire string. The
748 * "pos" and "n" arguments represent bytes; it is the caller's responsibility
749 * to ensure that the offsets do not copy invalid UTF-8 sequences. When
750 * used in conjunction with find() and length(), this will work.
751 *
752 * @param pos Index of first byte offset to copy from "this", counting from 0.
753 * @param n Number of bytes to copy, starting with the one at "pos".
754 * The copying will stop if the null terminator is encountered before
755 * n bytes have been copied.
756 */
757 RTCString substr(size_t pos = 0, size_t n = npos) const
758 {
759 return RTCString(*this, pos, n);
760 }
761
762 /**
763 * Returns a substring of "this" as a new Utf8Str. As opposed to substr(),
764 * this variant takes codepoint offsets instead of byte offsets.
765 *
766 * @param pos Index of first unicode codepoint to copy from
767 * "this", counting from 0.
768 * @param n Number of unicode codepoints to copy, starting with
769 * the one at "pos". The copying will stop if the null
770 * terminator is encountered before n codepoints have
771 * been copied.
772 */
773 RTCString substrCP(size_t pos = 0, size_t n = npos) const;
774
775 /**
776 * Returns true if "this" ends with "that".
777 *
778 * @param that Suffix to test for.
779 * @param cs Case sensitivity selector.
780 * @returns true if match, false if mismatch.
781 */
782 bool endsWith(const RTCString &that, CaseSensitivity cs = CaseSensitive) const;
783
784 /**
785 * Returns true if "this" begins with "that".
786 * @param that Prefix to test for.
787 * @param cs Case sensitivity selector.
788 * @returns true if match, false if mismatch.
789 */
790 bool startsWith(const RTCString &that, CaseSensitivity cs = CaseSensitive) const;
791
792 /**
793 * Returns true if "this" contains "that" (strstr).
794 *
795 * @param that Substring to look for.
796 * @param cs Case sensitivity selector.
797 * @returns true if match, false if mismatch.
798 */
799 bool contains(const RTCString &that, CaseSensitivity cs = CaseSensitive) const;
800
801 /**
802 * Attempts to convert the member string into a 32-bit integer.
803 *
804 * @returns 32-bit unsigned number on success.
805 * @returns 0 on failure.
806 */
807 int32_t toInt32() const
808 {
809 return RTStrToInt32(m_psz);
810 }
811
812 /**
813 * Attempts to convert the member string into an unsigned 32-bit integer.
814 *
815 * @returns 32-bit unsigned number on success.
816 * @returns 0 on failure.
817 */
818 uint32_t toUInt32() const
819 {
820 return RTStrToUInt32(m_psz);
821 }
822
823 /**
824 * Attempts to convert the member string into an 64-bit integer.
825 *
826 * @returns 64-bit unsigned number on success.
827 * @returns 0 on failure.
828 */
829 int64_t toInt64() const
830 {
831 return RTStrToInt64(m_psz);
832 }
833
834 /**
835 * Attempts to convert the member string into an unsigned 64-bit integer.
836 *
837 * @returns 64-bit unsigned number on success.
838 * @returns 0 on failure.
839 */
840 uint64_t toUInt64() const
841 {
842 return RTStrToUInt64(m_psz);
843 }
844
845 /**
846 * Attempts to convert the member string into an unsigned 64-bit integer.
847 *
848 * @param i Where to return the value on success.
849 * @returns IPRT error code, see RTStrToInt64.
850 */
851 int toInt(uint64_t &i) const;
852
853 /**
854 * Attempts to convert the member string into an unsigned 32-bit integer.
855 *
856 * @param i Where to return the value on success.
857 * @returns IPRT error code, see RTStrToInt32.
858 */
859 int toInt(uint32_t &i) const;
860
861 /** Splitting behavior regarding empty sections in the string. */
862 enum SplitMode
863 {
864 KeepEmptyParts, /**< Empty parts are added as empty strings to the result list. */
865 RemoveEmptyParts /**< Empty parts are skipped. */
866 };
867
868 /**
869 * Splits a string separated by strSep into its parts.
870 *
871 * @param a_rstrSep The separator to search for.
872 * @param a_enmMode How should empty parts be handled.
873 * @returns separated strings as string list.
874 */
875 RTCList<RTCString, RTCString *> split(const RTCString &a_rstrSep,
876 SplitMode a_enmMode = RemoveEmptyParts) const;
877
878 /**
879 * Joins a list of strings together using the provided separator and
880 * an optional prefix for each item in the list.
881 *
882 * @param a_rList The list to join.
883 * @param a_rstrPrefix The prefix used for appending to each item.
884 * @param a_rstrSep The separator used for joining.
885 * @returns joined string.
886 */
887 static RTCString joinEx(const RTCList<RTCString, RTCString *> &a_rList,
888 const RTCString &a_rstrPrefix /* = "" */,
889 const RTCString &a_rstrSep /* = "" */);
890
891 /**
892 * Joins a list of strings together using the provided separator.
893 *
894 * @param a_rList The list to join.
895 * @param a_rstrSep The separator used for joining.
896 * @returns joined string.
897 */
898 static RTCString join(const RTCList<RTCString, RTCString *> &a_rList,
899 const RTCString &a_rstrSep = "");
900
901 /**
902 * Swaps two strings in a fast way.
903 *
904 * Exception safe.
905 *
906 * @param a_rThat The string to swap with.
907 */
908 inline void swap(RTCString &a_rThat) throw()
909 {
910 char *pszTmp = m_psz;
911 size_t cchTmp = m_cch;
912 size_t cbAllocatedTmp = m_cbAllocated;
913
914 m_psz = a_rThat.m_psz;
915 m_cch = a_rThat.m_cch;
916 m_cbAllocated = a_rThat.m_cbAllocated;
917
918 a_rThat.m_psz = pszTmp;
919 a_rThat.m_cch = cchTmp;
920 a_rThat.m_cbAllocated = cbAllocatedTmp;
921 }
922
923protected:
924
925 /**
926 * Hide operator bool() to force people to use isEmpty() explicitly.
927 */
928 operator bool() const;
929
930 /**
931 * Destructor implementation, also used to clean up in operator=() before
932 * assigning a new string.
933 */
934 void cleanup()
935 {
936 if (m_psz)
937 {
938 RTStrFree(m_psz);
939 m_psz = NULL;
940 m_cch = 0;
941 m_cbAllocated = 0;
942 }
943 }
944
945 /**
946 * Protected internal helper to copy a string.
947 *
948 * This ignores the previous object state, so either call this from a
949 * constructor or call cleanup() first. copyFromN() unconditionally sets
950 * the members to a copy of the given other strings and makes no
951 * assumptions about previous contents. Can therefore be used both in copy
952 * constructors, when member variables have no defined value, and in
953 * assignments after having called cleanup().
954 *
955 * @param pcszSrc The source string.
956 * @param cchSrc The number of chars (bytes) to copy from the
957 * source strings. RTSTR_MAX is NOT accepted.
958 *
959 * @throws std::bad_alloc On allocation failure. The object is left
960 * describing a NULL string.
961 */
962 void copyFromN(const char *pcszSrc, size_t cchSrc)
963 {
964 if (cchSrc)
965 {
966 m_psz = RTStrAlloc(cchSrc + 1);
967 if (RT_LIKELY(m_psz))
968 {
969 m_cch = cchSrc;
970 m_cbAllocated = cchSrc + 1;
971 memcpy(m_psz, pcszSrc, cchSrc);
972 m_psz[cchSrc] = '\0';
973 }
974 else
975 {
976 m_cch = 0;
977 m_cbAllocated = 0;
978#ifdef RT_EXCEPTIONS_ENABLED
979 throw std::bad_alloc();
980#endif
981 }
982 }
983 else
984 {
985 m_cch = 0;
986 m_cbAllocated = 0;
987 m_psz = NULL;
988 }
989 }
990
991 static DECLCALLBACK(size_t) printfOutputCallback(void *pvArg, const char *pachChars, size_t cbChars);
992
993 char *m_psz; /**< The string buffer. */
994 size_t m_cch; /**< strlen(m_psz) - i.e. no terminator included. */
995 size_t m_cbAllocated; /**< Size of buffer that m_psz points to; at least m_cbLength + 1. */
996};
997
998/** @} */
999
1000
1001/** @addtogroup grp_rt_cpp_string
1002 * @{
1003 */
1004
1005/**
1006 * Concatenate two strings.
1007 *
1008 * @param a_rstr1 String one.
1009 * @param a_rstr2 String two.
1010 * @returns the concatenate string.
1011 *
1012 * @relates RTCString
1013 */
1014RTDECL(const RTCString) operator+(const RTCString &a_rstr1, const RTCString &a_rstr2);
1015
1016/**
1017 * Concatenate two strings.
1018 *
1019 * @param a_rstr1 String one.
1020 * @param a_psz2 String two.
1021 * @returns the concatenate string.
1022 *
1023 * @relates RTCString
1024 */
1025RTDECL(const RTCString) operator+(const RTCString &a_rstr1, const char *a_psz2);
1026
1027/**
1028 * Concatenate two strings.
1029 *
1030 * @param a_psz1 String one.
1031 * @param a_rstr2 String two.
1032 * @returns the concatenate string.
1033 *
1034 * @relates RTCString
1035 */
1036RTDECL(const RTCString) operator+(const char *a_psz1, const RTCString &a_rstr2);
1037
1038/**
1039 * Class with RTCString::printf as constructor for your convenience.
1040 *
1041 * Constructing a RTCString string object from a format string and a variable
1042 * number of arguments can easily be confused with the other RTCString
1043 * constructors, thus this child class.
1044 *
1045 * The usage of this class is like the following:
1046 * @code
1047 RTCStringFmt strName("program name = %s", argv[0]);
1048 @endcode
1049 */
1050class RTCStringFmt : public RTCString
1051{
1052public:
1053
1054 /**
1055 * Constructs a new string given the format string and the list of the
1056 * arguments for the format string.
1057 *
1058 * @param a_pszFormat Pointer to the format string (UTF-8),
1059 * @see pg_rt_str_format.
1060 * @param ... Ellipsis containing the arguments specified by
1061 * the format string.
1062 */
1063 explicit RTCStringFmt(const char *a_pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2)
1064 {
1065 va_list va;
1066 va_start(va, a_pszFormat);
1067 printfV(a_pszFormat, va);
1068 va_end(va);
1069 }
1070
1071 RTMEMEF_NEW_AND_DELETE_OPERATORS();
1072
1073protected:
1074 RTCStringFmt() {}
1075};
1076
1077/** @} */
1078
1079#endif
1080
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