VirtualBox

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

Last change on this file since 67676 was 67676, checked in by vboxsync, 7 years ago

RTCString: Added append methods for appending a substring.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 35.4 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 @a 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 @a 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 @a that to @a 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 @a pszThat to @a 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 a substring from @a rThat to @a this.
383 *
384 * @param rThat The string to append a substring from.
385 * @param offStart The start of the substring to append (byte offset,
386 * not codepoint).
387 * @param cchMax The maximum number of bytes to append.
388 *
389 * @throws std::bad_alloc On allocation error. The object is left unchanged.
390 *
391 * @returns Reference to the object.
392 */
393 RTCString &append(const RTCString &rThat, size_t offStart, size_t cchMax = RTSTR_MAX);
394
395 /**
396 * Appends the first @a cchMax chars from string @a pszThat to @a this.
397 *
398 * @param pszThat The C string to append.
399 * @param cchMax The maximum number of bytes to append.
400 *
401 * @throws std::bad_alloc On allocation error. The object is left unchanged.
402 *
403 * @returns Reference to the object.
404 */
405 RTCString &append(const char *pszThat, size_t cchMax);
406
407 /**
408 * Appends the given character to @a this.
409 *
410 * @param ch The character to append.
411 *
412 * @throws std::bad_alloc On allocation error. The object is left unchanged.
413 *
414 * @returns Reference to the object.
415 */
416 RTCString &append(char ch);
417
418 /**
419 * Appends the given unicode code point to @a this.
420 *
421 * @param uc The unicode code point to append.
422 *
423 * @throws std::bad_alloc On allocation error. The object is left unchanged.
424 *
425 * @returns Reference to the object.
426 */
427 RTCString &appendCodePoint(RTUNICP uc);
428
429 /**
430 * Shortcut to append(), RTCString variant.
431 *
432 * @param that The string to append.
433 *
434 * @returns Reference to the object.
435 */
436 RTCString &operator+=(const RTCString &that)
437 {
438 return append(that);
439 }
440
441 /**
442 * Shortcut to append(), const char* variant.
443 *
444 * @param pszThat The C string to append.
445 *
446 * @returns Reference to the object.
447 */
448 RTCString &operator+=(const char *pszThat)
449 {
450 return append(pszThat);
451 }
452
453 /**
454 * Shortcut to append(), char variant.
455 *
456 * @param ch The character to append.
457 *
458 * @returns Reference to the object.
459 */
460 RTCString &operator+=(char ch)
461 {
462 return append(ch);
463 }
464
465 /**
466 * Converts the member string to upper case.
467 *
468 * @returns Reference to the object.
469 */
470 RTCString &toUpper()
471 {
472 if (length())
473 {
474 /* Folding an UTF-8 string may result in a shorter encoding (see
475 testcase), so recalculate the length afterwards. */
476 ::RTStrToUpper(m_psz);
477 size_t cchNew = strlen(m_psz);
478 Assert(cchNew <= m_cch);
479 m_cch = cchNew;
480 }
481 return *this;
482 }
483
484 /**
485 * Converts the member string to lower case.
486 *
487 * @returns Reference to the object.
488 */
489 RTCString &toLower()
490 {
491 if (length())
492 {
493 /* Folding an UTF-8 string may result in a shorter encoding (see
494 testcase), so recalculate the length afterwards. */
495 ::RTStrToLower(m_psz);
496 size_t cchNew = strlen(m_psz);
497 Assert(cchNew <= m_cch);
498 m_cch = cchNew;
499 }
500 return *this;
501 }
502
503 /**
504 * Index operator.
505 *
506 * Returns the byte at the given index, or a null byte if the index is not
507 * smaller than length(). This does _not_ count codepoints but simply points
508 * into the member C string.
509 *
510 * @param i The index into the string buffer.
511 * @returns char at the index or null.
512 */
513 inline char operator[](size_t i) const
514 {
515 if (i < length())
516 return m_psz[i];
517 return '\0';
518 }
519
520 /**
521 * Returns the contained string as a C-style const char* pointer.
522 * This never returns NULL; if the string is empty, this returns a
523 * pointer to static null byte.
524 *
525 * @returns const pointer to C-style string.
526 */
527 inline const char *c_str() const
528 {
529 return (m_psz) ? m_psz : "";
530 }
531
532 /**
533 * Returns a non-const raw pointer that allows to modify the string directly.
534 * As opposed to c_str() and raw(), this DOES return NULL for an empty string
535 * because we cannot return a non-const pointer to a static "" global.
536 *
537 * @warning
538 * -# Be sure not to modify data beyond the allocated memory! Call
539 * capacity() to find out how large that buffer is.
540 * -# After any operation that modifies the length of the string,
541 * you _must_ call RTCString::jolt(), or subsequent copy operations
542 * may go nowhere. Better not use mutableRaw() at all.
543 */
544 char *mutableRaw()
545 {
546 return m_psz;
547 }
548
549 /**
550 * Clean up after using mutableRaw.
551 *
552 * Intended to be called after something has messed with the internal string
553 * buffer (e.g. after using mutableRaw() or Utf8Str::asOutParam()). Resets the
554 * internal lengths correctly. Otherwise subsequent copy operations may go
555 * nowhere.
556 */
557 void jolt()
558 {
559 if (m_psz)
560 {
561 m_cch = strlen(m_psz);
562 m_cbAllocated = m_cch + 1; /* (Required for the Utf8Str::asOutParam case) */
563 }
564 else
565 {
566 m_cch = 0;
567 m_cbAllocated = 0;
568 }
569 }
570
571 /**
572 * Returns @c true if the member string has no length.
573 *
574 * This is @c true for instances created from both NULL and "" input
575 * strings.
576 *
577 * This states nothing about how much memory might be allocated.
578 *
579 * @returns @c true if empty, @c false if not.
580 */
581 bool isEmpty() const
582 {
583 return length() == 0;
584 }
585
586 /**
587 * Returns @c false if the member string has no length.
588 *
589 * This is @c false for instances created from both NULL and "" input
590 * strings.
591 *
592 * This states nothing about how much memory might be allocated.
593 *
594 * @returns @c false if empty, @c true if not.
595 */
596 bool isNotEmpty() const
597 {
598 return length() != 0;
599 }
600
601 /** Case sensitivity selector. */
602 enum CaseSensitivity
603 {
604 CaseSensitive,
605 CaseInsensitive
606 };
607
608 /**
609 * Compares the member string to a C-string.
610 *
611 * @param pcszThat The string to compare with.
612 * @param cs Whether comparison should be case-sensitive.
613 * @returns 0 if equal, negative if this is smaller than @a pcsz, positive
614 * if larger.
615 */
616 int compare(const char *pcszThat, CaseSensitivity cs = CaseSensitive) const
617 {
618 /* This klugde is for m_cch=0 and m_psz=NULL. pcsz=NULL and psz=""
619 are treated the same way so that str.compare(str2.c_str()) works. */
620 if (length() == 0)
621 return pcszThat == NULL || *pcszThat == '\0' ? 0 : -1;
622
623 if (cs == CaseSensitive)
624 return ::RTStrCmp(m_psz, pcszThat);
625 return ::RTStrICmp(m_psz, pcszThat);
626 }
627
628 /**
629 * Compares the member string to another RTCString.
630 *
631 * @param rThat The string to compare with.
632 * @param cs Whether comparison should be case-sensitive.
633 * @returns 0 if equal, negative if this is smaller than @a pcsz, positive
634 * if larger.
635 */
636 int compare(const RTCString &rThat, CaseSensitivity cs = CaseSensitive) const
637 {
638 if (cs == CaseSensitive)
639 return ::RTStrCmp(m_psz, rThat.m_psz);
640 return ::RTStrICmp(m_psz, rThat.m_psz);
641 }
642
643 /**
644 * Compares the two strings.
645 *
646 * @returns true if equal, false if not.
647 * @param rThat The string to compare with.
648 */
649 bool equals(const RTCString &rThat) const
650 {
651 return rThat.length() == length()
652 && ( length() == 0
653 || memcmp(rThat.m_psz, m_psz, length()) == 0);
654 }
655
656 /**
657 * Compares the two strings.
658 *
659 * @returns true if equal, false if not.
660 * @param pszThat The string to compare with.
661 */
662 bool equals(const char *pszThat) const
663 {
664 /* This klugde is for m_cch=0 and m_psz=NULL. pcsz=NULL and psz=""
665 are treated the same way so that str.equals(str2.c_str()) works. */
666 if (length() == 0)
667 return pszThat == NULL || *pszThat == '\0';
668 return RTStrCmp(pszThat, m_psz) == 0;
669 }
670
671 /**
672 * Compares the two strings ignoring differences in case.
673 *
674 * @returns true if equal, false if not.
675 * @param that The string to compare with.
676 */
677 bool equalsIgnoreCase(const RTCString &that) const
678 {
679 /* Unfolded upper and lower case characters may require different
680 amount of encoding space, so the length optimization doesn't work. */
681 return RTStrICmp(that.m_psz, m_psz) == 0;
682 }
683
684 /**
685 * Compares the two strings ignoring differences in case.
686 *
687 * @returns true if equal, false if not.
688 * @param pszThat The string to compare with.
689 */
690 bool equalsIgnoreCase(const char *pszThat) const
691 {
692 /* This klugde is for m_cch=0 and m_psz=NULL. pcsz=NULL and psz=""
693 are treated the same way so that str.equalsIgnoreCase(str2.c_str()) works. */
694 if (length() == 0)
695 return pszThat == NULL || *pszThat == '\0';
696 return RTStrICmp(pszThat, m_psz) == 0;
697 }
698
699 /** @name Comparison operators.
700 * @{ */
701 bool operator==(const RTCString &that) const { return equals(that); }
702 bool operator!=(const RTCString &that) const { return !equals(that); }
703 bool operator<( const RTCString &that) const { return compare(that) < 0; }
704 bool operator>( const RTCString &that) const { return compare(that) > 0; }
705
706 bool operator==(const char *pszThat) const { return equals(pszThat); }
707 bool operator!=(const char *pszThat) const { return !equals(pszThat); }
708 bool operator<( const char *pszThat) const { return compare(pszThat) < 0; }
709 bool operator>( const char *pszThat) const { return compare(pszThat) > 0; }
710 /** @} */
711
712 /** Max string offset value.
713 *
714 * When returned by a method, this indicates failure. When taken as input,
715 * typically a default, it means all the way to the string terminator.
716 */
717 static const size_t npos;
718
719 /**
720 * Find the given substring.
721 *
722 * Looks for @a pszNeedle in @a this starting at @a offStart and returns its
723 * position as a byte (not codepoint) offset, counting from the beginning of
724 * @a this as 0.
725 *
726 * @param pszNeedle The substring to find.
727 * @param offStart The (byte) offset into the string buffer to start
728 * searching.
729 *
730 * @returns 0 based position of pszNeedle. npos if not found.
731 */
732 size_t find(const char *pszNeedle, size_t offStart = 0) const;
733
734 /**
735 * Find the given substring.
736 *
737 * Looks for @a pStrNeedle in @a this starting at @a offStart and returns its
738 * position as a byte (not codepoint) offset, counting from the beginning of
739 * @a this as 0.
740 *
741 * @param pStrNeedle The substring to find.
742 * @param offStart The (byte) offset into the string buffer to start
743 * searching.
744 *
745 * @returns 0 based position of pStrNeedle. npos if not found or pStrNeedle is
746 * NULL or an empty string.
747 */
748 size_t find(const RTCString *pStrNeedle, size_t offStart = 0) const;
749
750 /**
751 * Replaces all occurences of cFind with cReplace in the member string.
752 * In order not to produce invalid UTF-8, the characters must be ASCII
753 * values less than 128; this is not verified.
754 *
755 * @param chFind Character to replace. Must be ASCII < 128.
756 * @param chReplace Character to replace cFind with. Must be ASCII < 128.
757 */
758 void findReplace(char chFind, char chReplace);
759
760 /**
761 * Count the occurences of the specified character in the string.
762 *
763 * @param ch What to search for. Must be ASCII < 128.
764 * @remarks QString::count
765 */
766 size_t count(char ch) const;
767
768 /**
769 * Count the occurences of the specified sub-string in the string.
770 *
771 * @param psz What to search for.
772 * @param cs Case sensitivity selector.
773 * @remarks QString::count
774 */
775 size_t count(const char *psz, CaseSensitivity cs = CaseSensitive) const;
776
777 /**
778 * Count the occurences of the specified sub-string in the string.
779 *
780 * @param pStr What to search for.
781 * @param cs Case sensitivity selector.
782 * @remarks QString::count
783 */
784 size_t count(const RTCString *pStr, CaseSensitivity cs = CaseSensitive) const;
785
786 /**
787 * Strips leading and trailing spaces.
788 *
789 * @returns this
790 */
791 RTCString &strip();
792
793 /**
794 * Strips leading spaces.
795 *
796 * @returns this
797 */
798 RTCString &stripLeft();
799
800 /**
801 * Strips trailing spaces.
802 *
803 * @returns this
804 */
805 RTCString &stripRight();
806
807 /**
808 * Returns a substring of @a this as a new Utf8Str.
809 *
810 * Works exactly like its equivalent in std::string. With the default
811 * parameters "0" and "npos", this always copies the entire string. The
812 * "pos" and "n" arguments represent bytes; it is the caller's responsibility
813 * to ensure that the offsets do not copy invalid UTF-8 sequences. When
814 * used in conjunction with find() and length(), this will work.
815 *
816 * @param pos Index of first byte offset to copy from @a this,
817 * counting from 0.
818 * @param n Number of bytes to copy, starting with the one at "pos".
819 * The copying will stop if the null terminator is encountered before
820 * n bytes have been copied.
821 */
822 RTCString substr(size_t pos = 0, size_t n = npos) const
823 {
824 return RTCString(*this, pos, n);
825 }
826
827 /**
828 * Returns a substring of @a this as a new Utf8Str. As opposed to substr(), this
829 * variant takes codepoint offsets instead of byte offsets.
830 *
831 * @param pos Index of first unicode codepoint to copy from
832 * @a this, counting from 0.
833 * @param n Number of unicode codepoints to copy, starting with
834 * the one at "pos". The copying will stop if the null
835 * terminator is encountered before n codepoints have
836 * been copied.
837 */
838 RTCString substrCP(size_t pos = 0, size_t n = npos) const;
839
840 /**
841 * Returns true if @a this ends with @a that.
842 *
843 * @param that Suffix to test for.
844 * @param cs Case sensitivity selector.
845 * @returns true if match, false if mismatch.
846 */
847 bool endsWith(const RTCString &that, CaseSensitivity cs = CaseSensitive) const;
848
849 /**
850 * Returns true if @a this begins with @a that.
851 * @param that Prefix to test for.
852 * @param cs Case sensitivity selector.
853 * @returns true if match, false if mismatch.
854 */
855 bool startsWith(const RTCString &that, CaseSensitivity cs = CaseSensitive) const;
856
857 /**
858 * Returns true if @a this contains @a that (strstr).
859 *
860 * @param that Substring to look for.
861 * @param cs Case sensitivity selector.
862 * @returns true if found, false if not found.
863 */
864 bool contains(const RTCString &that, CaseSensitivity cs = CaseSensitive) const;
865
866 /**
867 * Returns true if @a this contains @a pszNeedle (strstr).
868 *
869 * @param pszNeedle Substring to look for.
870 * @param cs Case sensitivity selector.
871 * @returns true if found, false if not found.
872 */
873 bool contains(const char *pszNeedle, CaseSensitivity cs = CaseSensitive) const;
874
875 /**
876 * Attempts to convert the member string into a 32-bit integer.
877 *
878 * @returns 32-bit unsigned number on success.
879 * @returns 0 on failure.
880 */
881 int32_t toInt32() const
882 {
883 return RTStrToInt32(m_psz);
884 }
885
886 /**
887 * Attempts to convert the member string into an unsigned 32-bit integer.
888 *
889 * @returns 32-bit unsigned number on success.
890 * @returns 0 on failure.
891 */
892 uint32_t toUInt32() const
893 {
894 return RTStrToUInt32(m_psz);
895 }
896
897 /**
898 * Attempts to convert the member string into an 64-bit integer.
899 *
900 * @returns 64-bit unsigned number on success.
901 * @returns 0 on failure.
902 */
903 int64_t toInt64() const
904 {
905 return RTStrToInt64(m_psz);
906 }
907
908 /**
909 * Attempts to convert the member string into an unsigned 64-bit integer.
910 *
911 * @returns 64-bit unsigned number on success.
912 * @returns 0 on failure.
913 */
914 uint64_t toUInt64() const
915 {
916 return RTStrToUInt64(m_psz);
917 }
918
919 /**
920 * Attempts to convert the member string into an unsigned 64-bit integer.
921 *
922 * @param i Where to return the value on success.
923 * @returns IPRT error code, see RTStrToInt64.
924 */
925 int toInt(uint64_t &i) const;
926
927 /**
928 * Attempts to convert the member string into an unsigned 32-bit integer.
929 *
930 * @param i Where to return the value on success.
931 * @returns IPRT error code, see RTStrToInt32.
932 */
933 int toInt(uint32_t &i) const;
934
935 /** Splitting behavior regarding empty sections in the string. */
936 enum SplitMode
937 {
938 KeepEmptyParts, /**< Empty parts are added as empty strings to the result list. */
939 RemoveEmptyParts /**< Empty parts are skipped. */
940 };
941
942 /**
943 * Splits a string separated by strSep into its parts.
944 *
945 * @param a_rstrSep The separator to search for.
946 * @param a_enmMode How should empty parts be handled.
947 * @returns separated strings as string list.
948 */
949 RTCList<RTCString, RTCString *> split(const RTCString &a_rstrSep,
950 SplitMode a_enmMode = RemoveEmptyParts) const;
951
952 /**
953 * Joins a list of strings together using the provided separator and
954 * an optional prefix for each item in the list.
955 *
956 * @param a_rList The list to join.
957 * @param a_rstrPrefix The prefix used for appending to each item.
958 * @param a_rstrSep The separator used for joining.
959 * @returns joined string.
960 */
961 static RTCString joinEx(const RTCList<RTCString, RTCString *> &a_rList,
962 const RTCString &a_rstrPrefix /* = "" */,
963 const RTCString &a_rstrSep /* = "" */);
964
965 /**
966 * Joins a list of strings together using the provided separator.
967 *
968 * @param a_rList The list to join.
969 * @param a_rstrSep The separator used for joining.
970 * @returns joined string.
971 */
972 static RTCString join(const RTCList<RTCString, RTCString *> &a_rList,
973 const RTCString &a_rstrSep = "");
974
975 /**
976 * Swaps two strings in a fast way.
977 *
978 * Exception safe.
979 *
980 * @param a_rThat The string to swap with.
981 */
982 inline void swap(RTCString &a_rThat) throw()
983 {
984 char *pszTmp = m_psz;
985 size_t cchTmp = m_cch;
986 size_t cbAllocatedTmp = m_cbAllocated;
987
988 m_psz = a_rThat.m_psz;
989 m_cch = a_rThat.m_cch;
990 m_cbAllocated = a_rThat.m_cbAllocated;
991
992 a_rThat.m_psz = pszTmp;
993 a_rThat.m_cch = cchTmp;
994 a_rThat.m_cbAllocated = cbAllocatedTmp;
995 }
996
997protected:
998
999 /**
1000 * Hide operator bool() to force people to use isEmpty() explicitly.
1001 */
1002 operator bool() const;
1003
1004 /**
1005 * Destructor implementation, also used to clean up in operator=() before
1006 * assigning a new string.
1007 */
1008 void cleanup()
1009 {
1010 if (m_psz)
1011 {
1012 RTStrFree(m_psz);
1013 m_psz = NULL;
1014 m_cch = 0;
1015 m_cbAllocated = 0;
1016 }
1017 }
1018
1019 /**
1020 * Protected internal helper to copy a string.
1021 *
1022 * This ignores the previous object state, so either call this from a
1023 * constructor or call cleanup() first. copyFromN() unconditionally sets
1024 * the members to a copy of the given other strings and makes no
1025 * assumptions about previous contents. Can therefore be used both in copy
1026 * constructors, when member variables have no defined value, and in
1027 * assignments after having called cleanup().
1028 *
1029 * @param pcszSrc The source string.
1030 * @param cchSrc The number of chars (bytes) to copy from the
1031 * source strings. RTSTR_MAX is NOT accepted.
1032 *
1033 * @throws std::bad_alloc On allocation failure. The object is left
1034 * describing a NULL string.
1035 */
1036 void copyFromN(const char *pcszSrc, size_t cchSrc)
1037 {
1038 if (cchSrc)
1039 {
1040 m_psz = RTStrAlloc(cchSrc + 1);
1041 if (RT_LIKELY(m_psz))
1042 {
1043 m_cch = cchSrc;
1044 m_cbAllocated = cchSrc + 1;
1045 memcpy(m_psz, pcszSrc, cchSrc);
1046 m_psz[cchSrc] = '\0';
1047 }
1048 else
1049 {
1050 m_cch = 0;
1051 m_cbAllocated = 0;
1052#ifdef RT_EXCEPTIONS_ENABLED
1053 throw std::bad_alloc();
1054#endif
1055 }
1056 }
1057 else
1058 {
1059 m_cch = 0;
1060 m_cbAllocated = 0;
1061 m_psz = NULL;
1062 }
1063 }
1064
1065 /**
1066 * Appends exactly @a cchSrc chars from @a pszSrc to @a this.
1067 *
1068 * This is an internal worker for the append() methods.
1069 *
1070 * @param pszSrc The source string.
1071 * @param cchSrc The source string length (exact).
1072 * @throws std::bad_alloc On allocation error. The object is left unchanged.
1073 *
1074 * @returns Reference to the object.
1075 */
1076 RTCString &appendWorker(const char *pszSrc, size_t cchSrc);
1077
1078 static DECLCALLBACK(size_t) printfOutputCallback(void *pvArg, const char *pachChars, size_t cbChars);
1079
1080 char *m_psz; /**< The string buffer. */
1081 size_t m_cch; /**< strlen(m_psz) - i.e. no terminator included. */
1082 size_t m_cbAllocated; /**< Size of buffer that m_psz points to; at least m_cbLength + 1. */
1083};
1084
1085/** @} */
1086
1087
1088/** @addtogroup grp_rt_cpp_string
1089 * @{
1090 */
1091
1092/**
1093 * Concatenate two strings.
1094 *
1095 * @param a_rstr1 String one.
1096 * @param a_rstr2 String two.
1097 * @returns the concatenate string.
1098 *
1099 * @relates RTCString
1100 */
1101RTDECL(const RTCString) operator+(const RTCString &a_rstr1, const RTCString &a_rstr2);
1102
1103/**
1104 * Concatenate two strings.
1105 *
1106 * @param a_rstr1 String one.
1107 * @param a_psz2 String two.
1108 * @returns the concatenate string.
1109 *
1110 * @relates RTCString
1111 */
1112RTDECL(const RTCString) operator+(const RTCString &a_rstr1, const char *a_psz2);
1113
1114/**
1115 * Concatenate two strings.
1116 *
1117 * @param a_psz1 String one.
1118 * @param a_rstr2 String two.
1119 * @returns the concatenate string.
1120 *
1121 * @relates RTCString
1122 */
1123RTDECL(const RTCString) operator+(const char *a_psz1, const RTCString &a_rstr2);
1124
1125/**
1126 * Class with RTCString::printf as constructor for your convenience.
1127 *
1128 * Constructing a RTCString string object from a format string and a variable
1129 * number of arguments can easily be confused with the other RTCString
1130 * constructors, thus this child class.
1131 *
1132 * The usage of this class is like the following:
1133 * @code
1134 RTCStringFmt strName("program name = %s", argv[0]);
1135 @endcode
1136 */
1137class RTCStringFmt : public RTCString
1138{
1139public:
1140
1141 /**
1142 * Constructs a new string given the format string and the list of the
1143 * arguments for the format string.
1144 *
1145 * @param a_pszFormat Pointer to the format string (UTF-8),
1146 * @see pg_rt_str_format.
1147 * @param ... Ellipsis containing the arguments specified by
1148 * the format string.
1149 */
1150 explicit RTCStringFmt(const char *a_pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2)
1151 {
1152 va_list va;
1153 va_start(va, a_pszFormat);
1154 printfV(a_pszFormat, va);
1155 va_end(va);
1156 }
1157
1158 RTMEMEF_NEW_AND_DELETE_OPERATORS();
1159
1160protected:
1161 RTCStringFmt() {}
1162};
1163
1164/** @} */
1165
1166#endif
1167
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