VirtualBox

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

Last change on this file since 86716 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 55.1 KB
Line 
1/** @file
2 * IPRT - C++ string class.
3 */
4
5/*
6 * Copyright (C) 2007-2020 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_INCLUDED_cpp_ministring_h
27#define IPRT_INCLUDED_cpp_ministring_h
28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
31
32#include <iprt/mem.h>
33#include <iprt/string.h>
34#include <iprt/stdarg.h>
35#include <iprt/cpp/list.h>
36
37#include <new>
38
39
40/** @defgroup grp_rt_cpp_string C++ String support
41 * @ingroup grp_rt_cpp
42 * @{
43 */
44
45/** @brief C++ string class.
46 *
47 * This is a C++ string class that does not depend on anything else except IPRT
48 * memory management functions. Semantics are like in std::string, except it
49 * can do a lot less.
50 *
51 * Note that RTCString does not differentiate between NULL strings
52 * and empty strings. In other words, RTCString("") and RTCString(NULL)
53 * behave the same. In both cases, RTCString allocates no memory, reports
54 * a zero length and zero allocated bytes for both, and returns an empty
55 * C-style string from c_str().
56 *
57 * @note RTCString ASSUMES that all strings it deals with are valid UTF-8.
58 * The caller is responsible for not breaking this assumption.
59 */
60#ifdef VBOX
61 /** @remarks Much of the code in here used to be in com::Utf8Str so that
62 * com::Utf8Str can now derive from RTCString and only contain code
63 * that is COM-specific, such as com::Bstr conversions. Compared to
64 * the old Utf8Str though, RTCString always knows the length of its
65 * member string and the size of the buffer so it can use memcpy()
66 * instead of strdup().
67 */
68#endif
69class RT_DECL_CLASS RTCString
70{
71public:
72#if defined(RT_NEED_NEW_AND_DELETE) && ( !defined(RTMEM_WRAP_SOME_NEW_AND_DELETE_TO_EF) \
73 || defined(RTMEM_NO_WRAP_SOME_NEW_AND_DELETE_TO_EF))
74 RTMEM_IMPLEMENT_NEW_AND_DELETE();
75#else
76 RTMEMEF_NEW_AND_DELETE_OPERATORS();
77#endif
78
79 /**
80 * Creates an empty string that has no memory allocated.
81 */
82 RTCString()
83 : m_psz(NULL),
84 m_cch(0),
85 m_cbAllocated(0)
86 {
87 }
88
89 /**
90 * Creates a copy of another RTCString.
91 *
92 * This allocates s.length() + 1 bytes for the new instance, unless s is empty.
93 *
94 * @param a_rSrc The source string.
95 *
96 * @throws std::bad_alloc
97 */
98 RTCString(const RTCString &a_rSrc)
99 {
100 copyFromN(a_rSrc.m_psz, a_rSrc.m_cch);
101 }
102
103 /**
104 * Creates a copy of a C-style string.
105 *
106 * This allocates strlen(pcsz) + 1 bytes for the new instance, unless s is empty.
107 *
108 * @param pcsz The source string.
109 *
110 * @throws std::bad_alloc
111 */
112 RTCString(const char *pcsz)
113 {
114 copyFromN(pcsz, pcsz ? strlen(pcsz) : 0);
115 }
116
117 /**
118 * Create a partial copy of another RTCString.
119 *
120 * @param a_rSrc The source string.
121 * @param a_offSrc The byte offset into the source string.
122 * @param a_cchSrc The max number of chars (encoded UTF-8 bytes)
123 * to copy from the source string.
124 */
125 RTCString(const RTCString &a_rSrc, size_t a_offSrc, size_t a_cchSrc = npos)
126 {
127 if (a_offSrc < a_rSrc.m_cch)
128 copyFromN(&a_rSrc.m_psz[a_offSrc], RT_MIN(a_cchSrc, a_rSrc.m_cch - a_offSrc));
129 else
130 {
131 m_psz = NULL;
132 m_cch = 0;
133 m_cbAllocated = 0;
134 }
135 }
136
137 /**
138 * Create a partial copy of a C-style string.
139 *
140 * @param a_pszSrc The source string (UTF-8).
141 * @param a_cchSrc The max number of chars (encoded UTF-8 bytes)
142 * to copy from the source string. This must not
143 * be '0' as the compiler could easily mistake
144 * that for the va_list constructor.
145 */
146 RTCString(const char *a_pszSrc, size_t a_cchSrc)
147 {
148 size_t cchMax = a_pszSrc ? RTStrNLen(a_pszSrc, a_cchSrc) : 0;
149 copyFromN(a_pszSrc, RT_MIN(a_cchSrc, cchMax));
150 }
151
152 /**
153 * Create a string containing @a a_cTimes repetitions of the character @a
154 * a_ch.
155 *
156 * @param a_cTimes The number of times the character is repeated.
157 * @param a_ch The character to fill the string with.
158 */
159 RTCString(size_t a_cTimes, char a_ch)
160 : m_psz(NULL),
161 m_cch(0),
162 m_cbAllocated(0)
163 {
164 Assert((unsigned)a_ch < 0x80);
165 if (a_cTimes)
166 {
167 reserve(a_cTimes + 1);
168 memset(m_psz, a_ch, a_cTimes);
169 m_psz[a_cTimes] = '\0';
170 m_cch = a_cTimes;
171 }
172 }
173
174 /**
175 * Create a new string given the format string and its arguments.
176 *
177 * @param a_pszFormat Pointer to the format string (UTF-8),
178 * @see pg_rt_str_format.
179 * @param a_va Argument vector containing the arguments
180 * specified by the format string.
181 * @sa printfV
182 * @remarks Not part of std::string.
183 */
184 RTCString(const char *a_pszFormat, va_list a_va) RT_IPRT_FORMAT_ATTR(1, 0)
185 : m_psz(NULL),
186 m_cch(0),
187 m_cbAllocated(0)
188 {
189 printfV(a_pszFormat, a_va);
190 }
191
192 /**
193 * Destructor.
194 */
195 virtual ~RTCString()
196 {
197 cleanup();
198 }
199
200 /**
201 * String length in bytes.
202 *
203 * Returns the length of the member string in bytes, which is equal to strlen(c_str()).
204 * In other words, this does not count unicode codepoints; use utf8length() for that.
205 * The byte length is always cached so calling this is cheap and requires no
206 * strlen() invocation.
207 *
208 * @returns m_cbLength.
209 */
210 size_t length() const
211 {
212 return m_cch;
213 }
214
215 /**
216 * String length in unicode codepoints.
217 *
218 * As opposed to length(), which returns the length in bytes, this counts
219 * the number of unicode codepoints. This is *not* cached so calling this
220 * is expensive.
221 *
222 * @returns Number of codepoints in the member string.
223 */
224 size_t uniLength() const
225 {
226 return m_psz ? RTStrUniLen(m_psz) : 0;
227 }
228
229 /**
230 * The allocated buffer size (in bytes).
231 *
232 * Returns the number of bytes allocated in the internal string buffer, which is
233 * at least length() + 1 if length() > 0; for an empty string, this returns 0.
234 *
235 * @returns m_cbAllocated.
236 */
237 size_t capacity() const
238 {
239 return m_cbAllocated;
240 }
241
242 /**
243 * Make sure at that least cb of buffer space is reserved.
244 *
245 * Requests that the contained memory buffer have at least cb bytes allocated.
246 * This may expand or shrink the string's storage, but will never truncate the
247 * contained string. In other words, cb will be ignored if it's smaller than
248 * length() + 1.
249 *
250 * @param cb New minimum size (in bytes) of member memory buffer.
251 *
252 * @throws std::bad_alloc On allocation error. The object is left unchanged.
253 */
254 void reserve(size_t cb)
255 {
256 if ( ( cb != m_cbAllocated
257 && cb > m_cch + 1)
258 || ( m_psz == NULL
259 && cb > 0))
260 {
261 int rc = RTStrRealloc(&m_psz, cb);
262 if (RT_SUCCESS(rc))
263 m_cbAllocated = cb;
264#ifdef RT_EXCEPTIONS_ENABLED
265 else
266 throw std::bad_alloc();
267#endif
268 }
269 }
270
271 /**
272 * A C like version of the reserve method, i.e. return code instead of throw.
273 *
274 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
275 * @param cb New minimum size (in bytes) of member memory buffer.
276 */
277 int reserveNoThrow(size_t cb) RT_NOEXCEPT
278 {
279 if ( ( cb != m_cbAllocated
280 && cb > m_cch + 1)
281 || ( m_psz == NULL
282 && cb > 0))
283 {
284 int rc = RTStrRealloc(&m_psz, cb);
285 if (RT_SUCCESS(rc))
286 m_cbAllocated = cb;
287 else
288 return rc;
289 }
290 return VINF_SUCCESS;
291 }
292
293 /**
294 * Deallocates all memory.
295 */
296 inline void setNull()
297 {
298 cleanup();
299 }
300
301 /**
302 * Assigns a copy of pcsz to @a this.
303 *
304 * @param pcsz The source string.
305 *
306 * @throws std::bad_alloc On allocation failure. The object is left describing
307 * a NULL string.
308 *
309 * @returns Reference to the object.
310 */
311 RTCString &operator=(const char *pcsz)
312 {
313 if (m_psz != pcsz)
314 {
315 cleanup();
316 copyFromN(pcsz, pcsz ? strlen(pcsz) : 0);
317 }
318 return *this;
319 }
320
321 /**
322 * Assigns a copy of s to @a this.
323 *
324 * @param s The source string.
325 *
326 * @throws std::bad_alloc On allocation failure. The object is left describing
327 * a NULL string.
328 *
329 * @returns Reference to the object.
330 */
331 RTCString &operator=(const RTCString &s)
332 {
333 if (this != &s)
334 {
335 cleanup();
336 copyFromN(s.m_psz, s.m_cch);
337 }
338 return *this;
339 }
340
341 /**
342 * Assigns a copy of another RTCString.
343 *
344 * @param a_rSrc Reference to the source string.
345 * @throws std::bad_alloc On allocation error. The object is left unchanged.
346 */
347 RTCString &assign(const RTCString &a_rSrc);
348
349 /**
350 * Assigns a copy of another RTCString.
351 *
352 * @param a_rSrc Reference to the source string.
353 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
354 */
355 int assignNoThrow(const RTCString &a_rSrc) RT_NOEXCEPT;
356
357 /**
358 * Assigns a copy of a C-style string.
359 *
360 * @param a_pszSrc Pointer to the C-style source string.
361 * @throws std::bad_alloc On allocation error. The object is left unchanged.
362 * @remarks ASSUMES valid
363 */
364 RTCString &assign(const char *a_pszSrc);
365
366 /**
367 * Assigns a copy of a C-style string.
368 *
369 * @param a_pszSrc Pointer to the C-style source string.
370 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
371 * @remarks ASSUMES valid
372 */
373 int assignNoThrow(const char *a_pszSrc) RT_NOEXCEPT;
374
375 /**
376 * Assigns a partial copy of another RTCString.
377 *
378 * @param a_rSrc The source string.
379 * @param a_offSrc The byte offset into the source string.
380 * @param a_cchSrc The max number of chars (encoded UTF-8 bytes)
381 * to copy from the source string.
382 * @throws std::bad_alloc On allocation error. The object is left unchanged.
383 */
384 RTCString &assign(const RTCString &a_rSrc, size_t a_offSrc, size_t a_cchSrc = npos);
385
386 /**
387 * Assigns a partial copy of another RTCString.
388 *
389 * @param a_rSrc The source string.
390 * @param a_offSrc The byte offset into the source string.
391 * @param a_cchSrc The max number of chars (encoded UTF-8 bytes)
392 * to copy from the source string.
393 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
394 */
395 int assignNoThrow(const RTCString &a_rSrc, size_t a_offSrc, size_t a_cchSrc = npos) RT_NOEXCEPT;
396
397 /**
398 * Assigns a partial copy of a C-style string.
399 *
400 * @param a_pszSrc The source string (UTF-8).
401 * @param a_cchSrc The max number of chars (encoded UTF-8 bytes)
402 * to copy from the source string.
403 * @throws std::bad_alloc On allocation error. The object is left unchanged.
404 */
405 RTCString &assign(const char *a_pszSrc, size_t a_cchSrc);
406
407 /**
408 * Assigns a partial copy of a C-style string.
409 *
410 * @param a_pszSrc The source string (UTF-8).
411 * @param a_cchSrc The max number of chars (encoded UTF-8 bytes)
412 * to copy from the source string.
413 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
414 */
415 int assignNoThrow(const char *a_pszSrc, size_t a_cchSrc) RT_NOEXCEPT;
416
417 /**
418 * Assigs a string containing @a a_cTimes repetitions of the character @a a_ch.
419 *
420 * @param a_cTimes The number of times the character is repeated.
421 * @param a_ch The character to fill the string with.
422 * @throws std::bad_alloc On allocation error. The object is left unchanged.
423 */
424 RTCString &assign(size_t a_cTimes, char a_ch);
425
426 /**
427 * Assigs a string containing @a a_cTimes repetitions of the character @a a_ch.
428 *
429 * @param a_cTimes The number of times the character is repeated.
430 * @param a_ch The character to fill the string with.
431 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
432 */
433 int assignNoThrow(size_t a_cTimes, char a_ch) RT_NOEXCEPT;
434
435 /**
436 * Assigns the output of the string format operation (RTStrPrintf).
437 *
438 * @param pszFormat Pointer to the format string,
439 * @see pg_rt_str_format.
440 * @param ... Ellipsis containing the arguments specified by
441 * the format string.
442 *
443 * @throws std::bad_alloc On allocation error. Object state is undefined.
444 *
445 * @returns Reference to the object.
446 */
447 RTCString &printf(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
448
449 /**
450 * Assigns the output of the string format operation (RTStrPrintf).
451 *
452 * @param pszFormat Pointer to the format string,
453 * @see pg_rt_str_format.
454 * @param ... Ellipsis containing the arguments specified by
455 * the format string.
456 *
457 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
458 */
459 int printfNoThrow(const char *pszFormat, ...) RT_NOEXCEPT RT_IPRT_FORMAT_ATTR(1, 2);
460
461 /**
462 * Assigns the output of the string format operation (RTStrPrintfV).
463 *
464 * @param pszFormat Pointer to the format string,
465 * @see pg_rt_str_format.
466 * @param va Argument vector containing the arguments
467 * specified by the format string.
468 *
469 * @throws std::bad_alloc On allocation error. Object state is undefined.
470 *
471 * @returns Reference to the object.
472 */
473 RTCString &printfV(const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0);
474
475 /**
476 * Assigns the output of the string format operation (RTStrPrintfV).
477 *
478 * @param pszFormat Pointer to the format string,
479 * @see pg_rt_str_format.
480 * @param va Argument vector containing the arguments
481 * specified by the format string.
482 *
483 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
484 */
485 int printfVNoThrow(const char *pszFormat, va_list va) RT_NOEXCEPT RT_IPRT_FORMAT_ATTR(1, 0);
486
487 /**
488 * Appends the string @a that to @a rThat.
489 *
490 * @param rThat The string to append.
491 * @throws std::bad_alloc On allocation error. The object is left unchanged.
492 * @returns Reference to the object.
493 */
494 RTCString &append(const RTCString &rThat);
495
496 /**
497 * Appends the string @a that to @a rThat.
498 *
499 * @param rThat The string to append.
500 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
501 */
502 int appendNoThrow(const RTCString &rThat) RT_NOEXCEPT;
503
504 /**
505 * Appends the string @a pszSrc to @a this.
506 *
507 * @param pszSrc The C-style string to append.
508 * @throws std::bad_alloc On allocation error. The object is left unchanged.
509 * @returns Reference to the object.
510 */
511 RTCString &append(const char *pszSrc);
512
513 /**
514 * Appends the string @a pszSrc to @a this.
515 *
516 * @param pszSrc The C-style string to append.
517 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
518 */
519 int appendNoThrow(const char *pszSrc) RT_NOEXCEPT;
520
521 /**
522 * Appends the a substring from @a rThat to @a this.
523 *
524 * @param rThat The string to append a substring from.
525 * @param offStart The start of the substring to append (byte offset,
526 * not codepoint).
527 * @param cchMax The maximum number of bytes to append.
528 * @throws std::bad_alloc On allocation error. The object is left unchanged.
529 * @returns Reference to the object.
530 */
531 RTCString &append(const RTCString &rThat, size_t offStart, size_t cchMax = RTSTR_MAX);
532
533 /**
534 * Appends the a substring from @a rThat to @a this.
535 *
536 * @param rThat The string to append a substring from.
537 * @param offStart The start of the substring to append (byte offset,
538 * not codepoint).
539 * @param cchMax The maximum number of bytes to append.
540 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
541 */
542 int appendNoThrow(const RTCString &rThat, size_t offStart, size_t cchMax = RTSTR_MAX) RT_NOEXCEPT;
543
544 /**
545 * Appends the first @a cchMax chars from string @a pszThat to @a this.
546 *
547 * @param pszThat The C-style string to append.
548 * @param cchMax The maximum number of bytes to append.
549 * @throws std::bad_alloc On allocation error. The object is left unchanged.
550 * @returns Reference to the object.
551 */
552 RTCString &append(const char *pszThat, size_t cchMax);
553
554 /**
555 * Appends the first @a cchMax chars from string @a pszThat to @a this.
556 *
557 * @param pszThat The C-style string to append.
558 * @param cchMax The maximum number of bytes to append.
559 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
560 */
561 int appendNoThrow(const char *pszThat, size_t cchMax) RT_NOEXCEPT;
562
563 /**
564 * Appends the given character to @a this.
565 *
566 * @param ch The character to append.
567 * @throws std::bad_alloc On allocation error. The object is left unchanged.
568 * @returns Reference to the object.
569 */
570 RTCString &append(char ch);
571
572 /**
573 * Appends the given character to @a this.
574 *
575 * @param ch The character to append.
576 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
577 */
578 int appendNoThrow(char ch) RT_NOEXCEPT;
579
580 /**
581 * Appends the given unicode code point to @a this.
582 *
583 * @param uc The unicode code point to append.
584 * @throws std::bad_alloc On allocation error. The object is left unchanged.
585 * @returns Reference to the object.
586 */
587 RTCString &appendCodePoint(RTUNICP uc);
588
589 /**
590 * Appends the given unicode code point to @a this.
591 *
592 * @param uc The unicode code point to append.
593 * @returns VINF_SUCCESS, VERR_INVALID_UTF8_ENCODING or VERR_NO_STRING_MEMORY.
594 */
595 int appendCodePointNoThrow(RTUNICP uc) RT_NOEXCEPT;
596
597 /**
598 * Appends the output of the string format operation (RTStrPrintf).
599 *
600 * @param pszFormat Pointer to the format string,
601 * @see pg_rt_str_format.
602 * @param ... Ellipsis containing the arguments specified by
603 * the format string.
604 *
605 * @throws std::bad_alloc On allocation error. Object state is undefined.
606 *
607 * @returns Reference to the object.
608 */
609 RTCString &appendPrintf(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
610
611 /**
612 * Appends the output of the string format operation (RTStrPrintf).
613 *
614 * @param pszFormat Pointer to the format string,
615 * @see pg_rt_str_format.
616 * @param ... Ellipsis containing the arguments specified by
617 * the format string.
618 *
619 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
620 */
621 int appendPrintfNoThrow(const char *pszFormat, ...) RT_NOEXCEPT RT_IPRT_FORMAT_ATTR(1, 2);
622
623 /**
624 * Appends the output of the string format operation (RTStrPrintfV).
625 *
626 * @param pszFormat Pointer to the format string,
627 * @see pg_rt_str_format.
628 * @param va Argument vector containing the arguments
629 * specified by the format string.
630 *
631 * @throws std::bad_alloc On allocation error. Object state is undefined.
632 *
633 * @returns Reference to the object.
634 */
635 RTCString &appendPrintfV(const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0);
636
637 /**
638 * Appends the output of the string format operation (RTStrPrintfV).
639 *
640 * @param pszFormat Pointer to the format string,
641 * @see pg_rt_str_format.
642 * @param va Argument vector containing the arguments
643 * specified by the format string.
644 *
645 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
646 */
647 int appendPrintfVNoThrow(const char *pszFormat, va_list va) RT_NOEXCEPT RT_IPRT_FORMAT_ATTR(1, 0);
648
649 /**
650 * Shortcut to append(), RTCString variant.
651 *
652 * @param rThat The string to append.
653 * @returns Reference to the object.
654 */
655 RTCString &operator+=(const RTCString &rThat)
656 {
657 return append(rThat);
658 }
659
660 /**
661 * Shortcut to append(), const char* variant.
662 *
663 * @param pszThat The C-style string to append.
664 * @returns Reference to the object.
665 */
666 RTCString &operator+=(const char *pszThat)
667 {
668 return append(pszThat);
669 }
670
671 /**
672 * Shortcut to append(), char variant.
673 *
674 * @param ch The character to append.
675 *
676 * @returns Reference to the object.
677 */
678 RTCString &operator+=(char ch)
679 {
680 return append(ch);
681 }
682
683 /**
684 * Converts the member string to upper case.
685 *
686 * @returns Reference to the object.
687 */
688 RTCString &toUpper() RT_NOEXCEPT
689 {
690 if (length())
691 {
692 /* Folding an UTF-8 string may result in a shorter encoding (see
693 testcase), so recalculate the length afterwards. */
694 ::RTStrToUpper(m_psz);
695 size_t cchNew = strlen(m_psz);
696 Assert(cchNew <= m_cch);
697 m_cch = cchNew;
698 }
699 return *this;
700 }
701
702 /**
703 * Converts the member string to lower case.
704 *
705 * @returns Reference to the object.
706 */
707 RTCString &toLower() RT_NOEXCEPT
708 {
709 if (length())
710 {
711 /* Folding an UTF-8 string may result in a shorter encoding (see
712 testcase), so recalculate the length afterwards. */
713 ::RTStrToLower(m_psz);
714 size_t cchNew = strlen(m_psz);
715 Assert(cchNew <= m_cch);
716 m_cch = cchNew;
717 }
718 return *this;
719 }
720
721 /**
722 * Erases a sequence from the string.
723 *
724 * @returns Reference to the object.
725 * @param offStart Where in @a this string to start erasing.
726 * @param cchLength How much following @a offStart to erase.
727 */
728 RTCString &erase(size_t offStart = 0, size_t cchLength = npos) RT_NOEXCEPT;
729
730 /**
731 * Replaces a span of @a this string with a replacement string.
732 *
733 * @returns Reference to the object.
734 * @param offStart Where in @a this string to start replacing.
735 * @param cchLength How much following @a offStart to replace. npos is
736 * accepted.
737 * @param rStrReplacement The replacement string.
738 *
739 * @throws std::bad_alloc On allocation error. The object is left unchanged.
740 *
741 * @note Non-standard behaviour if offStart is beyond the end of the string.
742 * No change will occure and strict builds hits a debug assertion.
743 */
744 RTCString &replace(size_t offStart, size_t cchLength, const RTCString &rStrReplacement);
745
746 /**
747 * Replaces a span of @a this string with a replacement string.
748 *
749 * @returns VINF_SUCCESS, VERR_OUT_OF_RANGE or VERR_NO_STRING_MEMORY.
750 * @param offStart Where in @a this string to start replacing.
751 * @param cchLength How much following @a offStart to replace. npos is
752 * accepted.
753 * @param rStrReplacement The replacement string.
754 */
755 int replaceNoThrow(size_t offStart, size_t cchLength, const RTCString &rStrReplacement) RT_NOEXCEPT;
756
757 /**
758 * Replaces a span of @a this string with a replacement substring.
759 *
760 * @returns Reference to the object.
761 * @param offStart Where in @a this string to start replacing.
762 * @param cchLength How much following @a offStart to replace. npos is
763 * accepted.
764 * @param rStrReplacement The string from which a substring is taken.
765 * @param offReplacement The offset into @a rStrReplacement where the
766 * replacement substring starts.
767 * @param cchReplacement The maximum length of the replacement substring.
768 *
769 * @throws std::bad_alloc On allocation error. The object is left unchanged.
770 *
771 * @note Non-standard behaviour if offStart or offReplacement is beyond the
772 * end of the repective strings. No change is made in the former case,
773 * while we consider it an empty string in the latter. In both
774 * situation a debug assertion is raised in strict builds.
775 */
776 RTCString &replace(size_t offStart, size_t cchLength, const RTCString &rStrReplacement,
777 size_t offReplacement, size_t cchReplacement);
778
779 /**
780 * Replaces a span of @a this string with a replacement substring.
781 *
782 * @returns VINF_SUCCESS, VERR_OUT_OF_RANGE or VERR_NO_STRING_MEMORY.
783 * @param offStart Where in @a this string to start replacing.
784 * @param cchLength How much following @a offStart to replace. npos is
785 * accepted.
786 * @param rStrReplacement The string from which a substring is taken.
787 * @param offReplacement The offset into @a rStrReplacement where the
788 * replacement substring starts.
789 * @param cchReplacement The maximum length of the replacement substring.
790 */
791 int replaceNoThrow(size_t offStart, size_t cchLength, const RTCString &rStrReplacement,
792 size_t offReplacement, size_t cchReplacement) RT_NOEXCEPT;
793
794 /**
795 * Replaces a span of @a this string with the replacement string.
796 *
797 * @returns Reference to the object.
798 * @param offStart Where in @a this string to start replacing.
799 * @param cchLength How much following @a offStart to replace. npos is
800 * accepted.
801 * @param pszReplacement The replacement string.
802 *
803 * @throws std::bad_alloc On allocation error. The object is left unchanged.
804 *
805 * @note Non-standard behaviour if offStart is beyond the end of the string.
806 * No change will occure and strict builds hits a debug assertion.
807 */
808 RTCString &replace(size_t offStart, size_t cchLength, const char *pszReplacement);
809
810 /**
811 * Replaces a span of @a this string with the replacement string.
812 *
813 * @returns VINF_SUCCESS, VERR_OUT_OF_RANGE or VERR_NO_STRING_MEMORY.
814 * @param offStart Where in @a this string to start replacing.
815 * @param cchLength How much following @a offStart to replace. npos is
816 * accepted.
817 * @param pszReplacement The replacement string.
818 */
819 int replaceNoThrow(size_t offStart, size_t cchLength, const char *pszReplacement) RT_NOEXCEPT;
820
821 /**
822 * Replaces a span of @a this string with the replacement string.
823 *
824 * @returns Reference to the object.
825 * @param offStart Where in @a this string to start replacing.
826 * @param cchLength How much following @a offStart to replace. npos is
827 * accepted.
828 * @param pszReplacement The replacement string.
829 * @param cchReplacement How much of @a pszReplacement to use at most. If a
830 * zero terminator is found before reaching this value,
831 * we'll stop there.
832 *
833 * @throws std::bad_alloc On allocation error. The object is left unchanged.
834 *
835 * @note Non-standard behaviour if offStart is beyond the end of the string.
836 * No change will occure and strict builds hits a debug assertion.
837 */
838 RTCString &replace(size_t offStart, size_t cchLength, const char *pszReplacement, size_t cchReplacement);
839
840 /**
841 * Replaces a span of @a this string with the replacement string.
842 *
843 * @returns VINF_SUCCESS, VERR_OUT_OF_RANGE or VERR_NO_STRING_MEMORY.
844 * @param offStart Where in @a this string to start replacing.
845 * @param cchLength How much following @a offStart to replace. npos is
846 * accepted.
847 * @param pszReplacement The replacement string.
848 * @param cchReplacement How much of @a pszReplacement to use at most. If a
849 * zero terminator is found before reaching this value,
850 * we'll stop there.
851 */
852 int replaceNoThrow(size_t offStart, size_t cchLength, const char *pszReplacement, size_t cchReplacement) RT_NOEXCEPT;
853
854 /**
855 * Index operator.
856 *
857 * Returns the byte at the given index, or a null byte if the index is not
858 * smaller than length(). This does _not_ count codepoints but simply points
859 * into the member C-style string.
860 *
861 * @param i The index into the string buffer.
862 * @returns char at the index or null.
863 */
864 inline char operator[](size_t i) const RT_NOEXCEPT
865 {
866 if (i < length())
867 return m_psz[i];
868 return '\0';
869 }
870
871 /**
872 * Returns the contained string as a const C-style string pointer.
873 *
874 * This never returns NULL; if the string is empty, this returns a pointer to
875 * static null byte.
876 *
877 * @returns const pointer to C-style string.
878 */
879 inline const char *c_str() const RT_NOEXCEPT
880 {
881 return (m_psz) ? m_psz : "";
882 }
883
884 /**
885 * Returns a non-const raw pointer that allows to modify the string directly.
886 * As opposed to c_str() and raw(), this DOES return NULL for an empty string
887 * because we cannot return a non-const pointer to a static "" global.
888 *
889 * @warning
890 * -# Be sure not to modify data beyond the allocated memory! Call
891 * capacity() to find out how large that buffer is.
892 * -# After any operation that modifies the length of the string,
893 * you _must_ call RTCString::jolt(), or subsequent copy operations
894 * may go nowhere. Better not use mutableRaw() at all.
895 */
896 char *mutableRaw() RT_NOEXCEPT
897 {
898 return m_psz;
899 }
900
901 /**
902 * Clean up after using mutableRaw.
903 *
904 * Intended to be called after something has messed with the internal string
905 * buffer (e.g. after using mutableRaw() or Utf8Str::asOutParam()). Resets the
906 * internal lengths correctly. Otherwise subsequent copy operations may go
907 * nowhere.
908 */
909 void jolt() RT_NOEXCEPT
910 {
911 if (m_psz)
912 {
913 m_cch = strlen(m_psz);
914 m_cbAllocated = m_cch + 1; /* (Required for the Utf8Str::asOutParam case) */
915 }
916 else
917 {
918 m_cch = 0;
919 m_cbAllocated = 0;
920 }
921 }
922
923 /**
924 * Returns @c true if the member string has no length.
925 *
926 * This is @c true for instances created from both NULL and "" input
927 * strings.
928 *
929 * This states nothing about how much memory might be allocated.
930 *
931 * @returns @c true if empty, @c false if not.
932 */
933 bool isEmpty() const RT_NOEXCEPT
934 {
935 return length() == 0;
936 }
937
938 /**
939 * Returns @c false if the member string has no length.
940 *
941 * This is @c false for instances created from both NULL and "" input
942 * strings.
943 *
944 * This states nothing about how much memory might be allocated.
945 *
946 * @returns @c false if empty, @c true if not.
947 */
948 bool isNotEmpty() const RT_NOEXCEPT
949 {
950 return length() != 0;
951 }
952
953 /** Case sensitivity selector. */
954 enum CaseSensitivity
955 {
956 CaseSensitive,
957 CaseInsensitive
958 };
959
960 /**
961 * Compares the member string to a C-string.
962 *
963 * @param pcszThat The string to compare with.
964 * @param cs Whether comparison should be case-sensitive.
965 * @returns 0 if equal, negative if this is smaller than @a pcsz, positive
966 * if larger.
967 */
968 int compare(const char *pcszThat, CaseSensitivity cs = CaseSensitive) const RT_NOEXCEPT
969 {
970 /* This klugde is for m_cch=0 and m_psz=NULL. pcsz=NULL and psz=""
971 are treated the same way so that str.compare(str2.c_str()) works. */
972 if (length() == 0)
973 return pcszThat == NULL || *pcszThat == '\0' ? 0 : -1;
974
975 if (cs == CaseSensitive)
976 return ::RTStrCmp(m_psz, pcszThat);
977 return ::RTStrICmp(m_psz, pcszThat);
978 }
979
980 /**
981 * Compares the member string to another RTCString.
982 *
983 * @param rThat The string to compare with.
984 * @param cs Whether comparison should be case-sensitive.
985 * @returns 0 if equal, negative if this is smaller than @a pcsz, positive
986 * if larger.
987 */
988 int compare(const RTCString &rThat, CaseSensitivity cs = CaseSensitive) const RT_NOEXCEPT
989 {
990 if (cs == CaseSensitive)
991 return ::RTStrCmp(m_psz, rThat.m_psz);
992 return ::RTStrICmp(m_psz, rThat.m_psz);
993 }
994
995 /**
996 * Compares the two strings.
997 *
998 * @returns true if equal, false if not.
999 * @param rThat The string to compare with.
1000 */
1001 bool equals(const RTCString &rThat) const RT_NOEXCEPT
1002 {
1003 return rThat.length() == length()
1004 && ( length() == 0
1005 || memcmp(rThat.m_psz, m_psz, length()) == 0);
1006 }
1007
1008 /**
1009 * Compares the two strings.
1010 *
1011 * @returns true if equal, false if not.
1012 * @param pszThat The string to compare with.
1013 */
1014 bool equals(const char *pszThat) const RT_NOEXCEPT
1015 {
1016 /* This klugde is for m_cch=0 and m_psz=NULL. pcsz=NULL and psz=""
1017 are treated the same way so that str.equals(str2.c_str()) works. */
1018 if (length() == 0)
1019 return pszThat == NULL || *pszThat == '\0';
1020 return RTStrCmp(pszThat, m_psz) == 0;
1021 }
1022
1023 /**
1024 * Compares the two strings ignoring differences in case.
1025 *
1026 * @returns true if equal, false if not.
1027 * @param that The string to compare with.
1028 */
1029 bool equalsIgnoreCase(const RTCString &that) const RT_NOEXCEPT
1030 {
1031 /* Unfolded upper and lower case characters may require different
1032 amount of encoding space, so the length optimization doesn't work. */
1033 return RTStrICmp(that.m_psz, m_psz) == 0;
1034 }
1035
1036 /**
1037 * Compares the two strings ignoring differences in case.
1038 *
1039 * @returns true if equal, false if not.
1040 * @param pszThat The string to compare with.
1041 */
1042 bool equalsIgnoreCase(const char *pszThat) const RT_NOEXCEPT
1043 {
1044 /* This klugde is for m_cch=0 and m_psz=NULL. pcsz=NULL and psz=""
1045 are treated the same way so that str.equalsIgnoreCase(str2.c_str()) works. */
1046 if (length() == 0)
1047 return pszThat == NULL || *pszThat == '\0';
1048 return RTStrICmp(pszThat, m_psz) == 0;
1049 }
1050
1051 /** @name Comparison operators.
1052 * @{ */
1053 bool operator==(const RTCString &that) const { return equals(that); }
1054 bool operator!=(const RTCString &that) const { return !equals(that); }
1055 bool operator<( const RTCString &that) const { return compare(that) < 0; }
1056 bool operator>( const RTCString &that) const { return compare(that) > 0; }
1057
1058 bool operator==(const char *pszThat) const { return equals(pszThat); }
1059 bool operator!=(const char *pszThat) const { return !equals(pszThat); }
1060 bool operator<( const char *pszThat) const { return compare(pszThat) < 0; }
1061 bool operator>( const char *pszThat) const { return compare(pszThat) > 0; }
1062 /** @} */
1063
1064 /** Max string offset value.
1065 *
1066 * When returned by a method, this indicates failure. When taken as input,
1067 * typically a default, it means all the way to the string terminator.
1068 */
1069 static const size_t npos;
1070
1071 /**
1072 * Find the given substring.
1073 *
1074 * Looks for @a pszNeedle in @a this starting at @a offStart and returns its
1075 * position as a byte (not codepoint) offset, counting from the beginning of
1076 * @a this as 0.
1077 *
1078 * @param pszNeedle The substring to find.
1079 * @param offStart The (byte) offset into the string buffer to start
1080 * searching.
1081 *
1082 * @returns 0 based position of pszNeedle. npos if not found.
1083 */
1084 size_t find(const char *pszNeedle, size_t offStart = 0) const RT_NOEXCEPT;
1085
1086 /**
1087 * Find the given substring.
1088 *
1089 * Looks for @a pStrNeedle in @a this starting at @a offStart and returns its
1090 * position as a byte (not codepoint) offset, counting from the beginning of
1091 * @a this as 0.
1092 *
1093 * @param pStrNeedle The substring to find.
1094 * @param offStart The (byte) offset into the string buffer to start
1095 * searching.
1096 *
1097 * @returns 0 based position of pStrNeedle. npos if not found or pStrNeedle is
1098 * NULL or an empty string.
1099 */
1100 size_t find(const RTCString *pStrNeedle, size_t offStart = 0) const RT_NOEXCEPT;
1101
1102 /**
1103 * Find the given substring.
1104 *
1105 * Looks for @a rStrNeedle in @a this starting at @a offStart and returns its
1106 * position as a byte (not codepoint) offset, counting from the beginning of
1107 * @a this as 0.
1108 *
1109 * @param rStrNeedle The substring to find.
1110 * @param offStart The (byte) offset into the string buffer to start
1111 * searching.
1112 *
1113 * @returns 0 based position of pStrNeedle. npos if not found or pStrNeedle is
1114 * NULL or an empty string.
1115 */
1116 size_t find(const RTCString &rStrNeedle, size_t offStart = 0) const RT_NOEXCEPT;
1117
1118 /**
1119 * Find the given character (byte).
1120 *
1121 * @returns 0 based position of chNeedle. npos if not found or pStrNeedle is
1122 * NULL or an empty string.
1123 * @param chNeedle The character (byte) to find.
1124 * @param offStart The (byte) offset into the string buffer to start
1125 * searching. Default is start of the string.
1126 *
1127 * @note This searches for a C character value, not a codepoint. Use the
1128 * string version to locate codepoints above U+7F.
1129 */
1130 size_t find(char chNeedle, size_t offStart = 0) const RT_NOEXCEPT;
1131
1132 /**
1133 * Replaces all occurences of cFind with cReplace in the member string.
1134 * In order not to produce invalid UTF-8, the characters must be ASCII
1135 * values less than 128; this is not verified.
1136 *
1137 * @param chFind Character to replace. Must be ASCII < 128.
1138 * @param chReplace Character to replace cFind with. Must be ASCII < 128.
1139 */
1140 void findReplace(char chFind, char chReplace) RT_NOEXCEPT;
1141
1142 /**
1143 * Count the occurences of the specified character in the string.
1144 *
1145 * @param ch What to search for. Must be ASCII < 128.
1146 * @remarks QString::count
1147 */
1148 size_t count(char ch) const RT_NOEXCEPT;
1149
1150 /**
1151 * Count the occurences of the specified sub-string in the string.
1152 *
1153 * @param psz What to search for.
1154 * @param cs Case sensitivity selector.
1155 * @remarks QString::count
1156 */
1157 size_t count(const char *psz, CaseSensitivity cs = CaseSensitive) const RT_NOEXCEPT;
1158
1159 /**
1160 * Count the occurences of the specified sub-string in the string.
1161 *
1162 * @param pStr What to search for.
1163 * @param cs Case sensitivity selector.
1164 * @remarks QString::count
1165 */
1166 size_t count(const RTCString *pStr, CaseSensitivity cs = CaseSensitive) const RT_NOEXCEPT;
1167
1168 /**
1169 * Strips leading and trailing spaces.
1170 *
1171 * @returns this
1172 */
1173 RTCString &strip() RT_NOEXCEPT;
1174
1175 /**
1176 * Strips leading spaces.
1177 *
1178 * @returns this
1179 */
1180 RTCString &stripLeft() RT_NOEXCEPT;
1181
1182 /**
1183 * Strips trailing spaces.
1184 *
1185 * @returns this
1186 */
1187 RTCString &stripRight() RT_NOEXCEPT;
1188
1189 /**
1190 * Returns a substring of @a this as a new Utf8Str.
1191 *
1192 * Works exactly like its equivalent in std::string. With the default
1193 * parameters "0" and "npos", this always copies the entire string. The
1194 * "pos" and "n" arguments represent bytes; it is the caller's responsibility
1195 * to ensure that the offsets do not copy invalid UTF-8 sequences. When
1196 * used in conjunction with find() and length(), this will work.
1197 *
1198 * @param pos Index of first byte offset to copy from @a this,
1199 * counting from 0.
1200 * @param n Number of bytes to copy, starting with the one at "pos".
1201 * The copying will stop if the null terminator is encountered before
1202 * n bytes have been copied.
1203 */
1204 RTCString substr(size_t pos = 0, size_t n = npos) const
1205 {
1206 return RTCString(*this, pos, n);
1207 }
1208
1209 /**
1210 * Returns a substring of @a this as a new Utf8Str. As opposed to substr(), this
1211 * variant takes codepoint offsets instead of byte offsets.
1212 *
1213 * @param pos Index of first unicode codepoint to copy from
1214 * @a this, counting from 0.
1215 * @param n Number of unicode codepoints to copy, starting with
1216 * the one at "pos". The copying will stop if the null
1217 * terminator is encountered before n codepoints have
1218 * been copied.
1219 */
1220 RTCString substrCP(size_t pos = 0, size_t n = npos) const;
1221
1222 /**
1223 * Returns true if @a this ends with @a that.
1224 *
1225 * @param that Suffix to test for.
1226 * @param cs Case sensitivity selector.
1227 * @returns true if match, false if mismatch.
1228 */
1229 bool endsWith(const RTCString &that, CaseSensitivity cs = CaseSensitive) const RT_NOEXCEPT;
1230
1231 /**
1232 * Returns true if @a this begins with @a that.
1233 * @param that Prefix to test for.
1234 * @param cs Case sensitivity selector.
1235 * @returns true if match, false if mismatch.
1236 */
1237 bool startsWith(const RTCString &that, CaseSensitivity cs = CaseSensitive) const RT_NOEXCEPT;
1238
1239 /**
1240 * Checks if the string starts with the given word, ignoring leading blanks.
1241 *
1242 * @param pszWord The word to test for.
1243 * @param enmCase Case sensitivity selector.
1244 * @returns true if match, false if mismatch.
1245 */
1246 bool startsWithWord(const char *pszWord, CaseSensitivity enmCase = CaseSensitive) const RT_NOEXCEPT;
1247
1248 /**
1249 * Checks if the string starts with the given word, ignoring leading blanks.
1250 *
1251 * @param rThat Prefix to test for.
1252 * @param enmCase Case sensitivity selector.
1253 * @returns true if match, false if mismatch.
1254 */
1255 bool startsWithWord(const RTCString &rThat, CaseSensitivity enmCase = CaseSensitive) const RT_NOEXCEPT;
1256
1257 /**
1258 * Returns true if @a this contains @a that (strstr).
1259 *
1260 * @param that Substring to look for.
1261 * @param cs Case sensitivity selector.
1262 * @returns true if found, false if not found.
1263 */
1264 bool contains(const RTCString &that, CaseSensitivity cs = CaseSensitive) const RT_NOEXCEPT;
1265
1266 /**
1267 * Returns true if @a this contains @a pszNeedle (strstr).
1268 *
1269 * @param pszNeedle Substring to look for.
1270 * @param cs Case sensitivity selector.
1271 * @returns true if found, false if not found.
1272 */
1273 bool contains(const char *pszNeedle, CaseSensitivity cs = CaseSensitive) const RT_NOEXCEPT;
1274
1275 /**
1276 * Attempts to convert the member string into a 32-bit integer.
1277 *
1278 * @returns 32-bit unsigned number on success.
1279 * @returns 0 on failure.
1280 */
1281 int32_t toInt32() const RT_NOEXCEPT
1282 {
1283 return RTStrToInt32(c_str());
1284 }
1285
1286 /**
1287 * Attempts to convert the member string into an unsigned 32-bit integer.
1288 *
1289 * @returns 32-bit unsigned number on success.
1290 * @returns 0 on failure.
1291 */
1292 uint32_t toUInt32() const RT_NOEXCEPT
1293 {
1294 return RTStrToUInt32(c_str());
1295 }
1296
1297 /**
1298 * Attempts to convert the member string into an 64-bit integer.
1299 *
1300 * @returns 64-bit unsigned number on success.
1301 * @returns 0 on failure.
1302 */
1303 int64_t toInt64() const RT_NOEXCEPT
1304 {
1305 return RTStrToInt64(c_str());
1306 }
1307
1308 /**
1309 * Attempts to convert the member string into an unsigned 64-bit integer.
1310 *
1311 * @returns 64-bit unsigned number on success.
1312 * @returns 0 on failure.
1313 */
1314 uint64_t toUInt64() const RT_NOEXCEPT
1315 {
1316 return RTStrToUInt64(c_str());
1317 }
1318
1319 /**
1320 * Attempts to convert the member string into an unsigned 64-bit integer.
1321 *
1322 * @param i Where to return the value on success.
1323 * @returns IPRT error code, see RTStrToInt64.
1324 */
1325 int toInt(uint64_t &i) const RT_NOEXCEPT;
1326
1327 /**
1328 * Attempts to convert the member string into an unsigned 32-bit integer.
1329 *
1330 * @param i Where to return the value on success.
1331 * @returns IPRT error code, see RTStrToInt32.
1332 */
1333 int toInt(uint32_t &i) const RT_NOEXCEPT;
1334
1335 /** Splitting behavior regarding empty sections in the string. */
1336 enum SplitMode
1337 {
1338 KeepEmptyParts, /**< Empty parts are added as empty strings to the result list. */
1339 RemoveEmptyParts /**< Empty parts are skipped. */
1340 };
1341
1342 /**
1343 * Splits a string separated by strSep into its parts.
1344 *
1345 * @param a_rstrSep The separator to search for.
1346 * @param a_enmMode How should empty parts be handled.
1347 * @returns separated strings as string list.
1348 * @throws std::bad_alloc On allocation error.
1349 */
1350 RTCList<RTCString, RTCString *> split(const RTCString &a_rstrSep,
1351 SplitMode a_enmMode = RemoveEmptyParts) const;
1352
1353 /**
1354 * Joins a list of strings together using the provided separator and
1355 * an optional prefix for each item in the list.
1356 *
1357 * @param a_rList The list to join.
1358 * @param a_rstrPrefix The prefix used for appending to each item.
1359 * @param a_rstrSep The separator used for joining.
1360 * @returns joined string.
1361 * @throws std::bad_alloc On allocation error.
1362 */
1363 static RTCString joinEx(const RTCList<RTCString, RTCString *> &a_rList,
1364 const RTCString &a_rstrPrefix /* = "" */,
1365 const RTCString &a_rstrSep /* = "" */);
1366
1367 /**
1368 * Joins a list of strings together using the provided separator.
1369 *
1370 * @param a_rList The list to join.
1371 * @param a_rstrSep The separator used for joining.
1372 * @returns joined string.
1373 * @throws std::bad_alloc On allocation error.
1374 */
1375 static RTCString join(const RTCList<RTCString, RTCString *> &a_rList,
1376 const RTCString &a_rstrSep = "");
1377
1378 /**
1379 * Swaps two strings in a fast way.
1380 *
1381 * Exception safe.
1382 *
1383 * @param a_rThat The string to swap with.
1384 */
1385 inline void swap(RTCString &a_rThat) RT_NOEXCEPT
1386 {
1387 char *pszTmp = m_psz;
1388 size_t cchTmp = m_cch;
1389 size_t cbAllocatedTmp = m_cbAllocated;
1390
1391 m_psz = a_rThat.m_psz;
1392 m_cch = a_rThat.m_cch;
1393 m_cbAllocated = a_rThat.m_cbAllocated;
1394
1395 a_rThat.m_psz = pszTmp;
1396 a_rThat.m_cch = cchTmp;
1397 a_rThat.m_cbAllocated = cbAllocatedTmp;
1398 }
1399
1400protected:
1401
1402 /**
1403 * Hide operator bool() to force people to use isEmpty() explicitly.
1404 */
1405 operator bool() const;
1406
1407 /**
1408 * Destructor implementation, also used to clean up in operator=() before
1409 * assigning a new string.
1410 */
1411 void cleanup() RT_NOEXCEPT
1412 {
1413 if (m_psz)
1414 {
1415 RTStrFree(m_psz);
1416 m_psz = NULL;
1417 m_cch = 0;
1418 m_cbAllocated = 0;
1419 }
1420 }
1421
1422 /**
1423 * Protected internal helper to copy a string.
1424 *
1425 * This ignores the previous object state, so either call this from a
1426 * constructor or call cleanup() first. copyFromN() unconditionally sets
1427 * the members to a copy of the given other strings and makes no
1428 * assumptions about previous contents. Can therefore be used both in copy
1429 * constructors, when member variables have no defined value, and in
1430 * assignments after having called cleanup().
1431 *
1432 * @param pcszSrc The source string.
1433 * @param cchSrc The number of chars (bytes) to copy from the
1434 * source strings. RTSTR_MAX is NOT accepted.
1435 *
1436 * @throws std::bad_alloc On allocation failure. The object is left
1437 * describing a NULL string.
1438 */
1439 void copyFromN(const char *pcszSrc, size_t cchSrc)
1440 {
1441 if (cchSrc)
1442 {
1443 m_psz = RTStrAlloc(cchSrc + 1);
1444 if (RT_LIKELY(m_psz))
1445 {
1446 m_cch = cchSrc;
1447 m_cbAllocated = cchSrc + 1;
1448 memcpy(m_psz, pcszSrc, cchSrc);
1449 m_psz[cchSrc] = '\0';
1450 }
1451 else
1452 {
1453 m_cch = 0;
1454 m_cbAllocated = 0;
1455#ifdef RT_EXCEPTIONS_ENABLED
1456 throw std::bad_alloc();
1457#endif
1458 }
1459 }
1460 else
1461 {
1462 m_cch = 0;
1463 m_cbAllocated = 0;
1464 m_psz = NULL;
1465 }
1466 }
1467
1468 /**
1469 * Appends exactly @a cchSrc chars from @a pszSrc to @a this.
1470 *
1471 * This is an internal worker for the append() methods.
1472 *
1473 * @returns Reference to the object.
1474 * @param pszSrc The source string.
1475 * @param cchSrc The source string length (exact).
1476 * @throws std::bad_alloc On allocation error. The object is left unchanged.
1477 *
1478 */
1479 RTCString &appendWorker(const char *pszSrc, size_t cchSrc);
1480
1481 /**
1482 * Appends exactly @a cchSrc chars from @a pszSrc to @a this.
1483 *
1484 * This is an internal worker for the appendNoThrow() methods.
1485 *
1486 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
1487 * @param pszSrc The source string.
1488 * @param cchSrc The source string length (exact).
1489 */
1490 int appendWorkerNoThrow(const char *pszSrc, size_t cchSrc) RT_NOEXCEPT;
1491
1492 /**
1493 * Replaces exatly @a cchLength chars at @a offStart with @a cchSrc from @a
1494 * pszSrc.
1495 *
1496 * @returns Reference to the object.
1497 * @param offStart Where in @a this string to start replacing.
1498 * @param cchLength How much following @a offStart to replace. npos is
1499 * accepted.
1500 * @param pszSrc The replacement string.
1501 * @param cchSrc The exactly length of the replacement string.
1502 *
1503 * @throws std::bad_alloc On allocation error. The object is left unchanged.
1504 */
1505 RTCString &replaceWorker(size_t offStart, size_t cchLength, const char *pszSrc, size_t cchSrc);
1506
1507 /**
1508 * Replaces exatly @a cchLength chars at @a offStart with @a cchSrc from @a
1509 * pszSrc.
1510 *
1511 * @returns VINF_SUCCESS, VERR_OUT_OF_RANGE or VERR_NO_STRING_MEMORY.
1512 * @param offStart Where in @a this string to start replacing.
1513 * @param cchLength How much following @a offStart to replace. npos is
1514 * accepted.
1515 * @param pszSrc The replacement string.
1516 * @param cchSrc The exactly length of the replacement string.
1517 */
1518 int replaceWorkerNoThrow(size_t offStart, size_t cchLength, const char *pszSrc, size_t cchSrc) RT_NOEXCEPT;
1519
1520 static DECLCALLBACK(size_t) printfOutputCallback(void *pvArg, const char *pachChars, size_t cbChars);
1521 static DECLCALLBACK(size_t) printfOutputCallbackNoThrow(void *pvArg, const char *pachChars, size_t cbChars) RT_NOEXCEPT;
1522
1523 char *m_psz; /**< The string buffer. */
1524 size_t m_cch; /**< strlen(m_psz) - i.e. no terminator included. */
1525 size_t m_cbAllocated; /**< Size of buffer that m_psz points to; at least m_cbLength + 1. */
1526};
1527
1528/** @} */
1529
1530
1531/** @addtogroup grp_rt_cpp_string
1532 * @{
1533 */
1534
1535/**
1536 * Concatenate two strings.
1537 *
1538 * @param a_rstr1 String one.
1539 * @param a_rstr2 String two.
1540 * @returns the concatenate string.
1541 *
1542 * @relates RTCString
1543 */
1544RTDECL(const RTCString) operator+(const RTCString &a_rstr1, const RTCString &a_rstr2);
1545
1546/**
1547 * Concatenate two strings.
1548 *
1549 * @param a_rstr1 String one.
1550 * @param a_psz2 String two.
1551 * @returns the concatenate string.
1552 *
1553 * @relates RTCString
1554 */
1555RTDECL(const RTCString) operator+(const RTCString &a_rstr1, const char *a_psz2);
1556
1557/**
1558 * Concatenate two strings.
1559 *
1560 * @param a_psz1 String one.
1561 * @param a_rstr2 String two.
1562 * @returns the concatenate string.
1563 *
1564 * @relates RTCString
1565 */
1566RTDECL(const RTCString) operator+(const char *a_psz1, const RTCString &a_rstr2);
1567
1568/**
1569 * Class with RTCString::printf as constructor for your convenience.
1570 *
1571 * Constructing a RTCString string object from a format string and a variable
1572 * number of arguments can easily be confused with the other RTCString
1573 * constructors, thus this child class.
1574 *
1575 * The usage of this class is like the following:
1576 * @code
1577 RTCStringFmt strName("program name = %s", argv[0]);
1578 @endcode
1579 */
1580class RTCStringFmt : public RTCString
1581{
1582public:
1583
1584 /**
1585 * Constructs a new string given the format string and the list of the
1586 * arguments for the format string.
1587 *
1588 * @param a_pszFormat Pointer to the format string (UTF-8),
1589 * @see pg_rt_str_format.
1590 * @param ... Ellipsis containing the arguments specified by
1591 * the format string.
1592 */
1593 explicit RTCStringFmt(const char *a_pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2)
1594 {
1595 va_list va;
1596 va_start(va, a_pszFormat);
1597 printfV(a_pszFormat, va);
1598 va_end(va);
1599 }
1600
1601 RTMEMEF_NEW_AND_DELETE_OPERATORS();
1602
1603protected:
1604 RTCStringFmt() {}
1605};
1606
1607/** @} */
1608
1609#endif /* !IPRT_INCLUDED_cpp_ministring_h */
1610
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