VirtualBox

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

Last change on this file since 73977 was 73908, checked in by vboxsync, 6 years ago

iprt/cpp/ministring.h: Added appendPrintf with all variants. Marked a lot of methods with noexcept.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 53.5 KB
Line 
1/** @file
2 * IPRT - C++ string class.
3 */
4
5/*
6 * Copyright (C) 2007-2017 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-style 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-style 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-style 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) RT_NOEXCEPT
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 a copy of another RTCString.
333 *
334 * @param a_rSrc Reference to the source string.
335 * @throws std::bad_alloc On allocation error. The object is left unchanged.
336 */
337 RTCString &assign(const RTCString &a_rSrc);
338
339 /**
340 * Assigns a copy of another RTCString.
341 *
342 * @param a_rSrc Reference to the source string.
343 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
344 */
345 int assignNoThrow(const RTCString &a_rSrc) RT_NOEXCEPT;
346
347 /**
348 * Assigns a copy of a C-style string.
349 *
350 * @param a_pszSrc Pointer to the C-style source string.
351 * @throws std::bad_alloc On allocation error. The object is left unchanged.
352 * @remarks ASSUMES valid
353 */
354 RTCString &assign(const char *a_pszSrc);
355
356 /**
357 * Assigns a copy of a C-style string.
358 *
359 * @param a_pszSrc Pointer to the C-style source string.
360 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
361 * @remarks ASSUMES valid
362 */
363 int assignNoThrow(const char *a_pszSrc) RT_NOEXCEPT;
364
365 /**
366 * Assigns a partial copy of another RTCString.
367 *
368 * @param a_rSrc The source string.
369 * @param a_offSrc The byte offset into the source string.
370 * @param a_cchSrc The max number of chars (encoded UTF-8 bytes)
371 * to copy from the source string.
372 * @throws std::bad_alloc On allocation error. The object is left unchanged.
373 */
374 RTCString &assign(const RTCString &a_rSrc, size_t a_offSrc, size_t a_cchSrc = npos);
375
376 /**
377 * Assigns a partial copy of another RTCString.
378 *
379 * @param a_rSrc The source string.
380 * @param a_offSrc The byte offset into the source string.
381 * @param a_cchSrc The max number of chars (encoded UTF-8 bytes)
382 * to copy from the source string.
383 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
384 */
385 int assignNoThrow(const RTCString &a_rSrc, size_t a_offSrc, size_t a_cchSrc = npos) RT_NOEXCEPT;
386
387 /**
388 * Assigns a partial copy of a C-style string.
389 *
390 * @param a_pszSrc The source string (UTF-8).
391 * @param a_cchSrc The max number of chars (encoded UTF-8 bytes)
392 * to copy from the source string.
393 * @throws std::bad_alloc On allocation error. The object is left unchanged.
394 */
395 RTCString &assign(const char *a_pszSrc, size_t a_cchSrc);
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 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
404 */
405 int assignNoThrow(const char *a_pszSrc, size_t a_cchSrc) RT_NOEXCEPT;
406
407 /**
408 * Assigs a string containing @a a_cTimes repetitions of the character @a a_ch.
409 *
410 * @param a_cTimes The number of times the character is repeated.
411 * @param a_ch The character to fill the string with.
412 * @throws std::bad_alloc On allocation error. The object is left unchanged.
413 */
414 RTCString &assign(size_t a_cTimes, char a_ch);
415
416 /**
417 * Assigs a string containing @a a_cTimes repetitions of the character @a a_ch.
418 *
419 * @param a_cTimes The number of times the character is repeated.
420 * @param a_ch The character to fill the string with.
421 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
422 */
423 int assignNoThrow(size_t a_cTimes, char a_ch) RT_NOEXCEPT;
424
425 /**
426 * Assigns the output of the string format operation (RTStrPrintf).
427 *
428 * @param pszFormat Pointer to the format string,
429 * @see pg_rt_str_format.
430 * @param ... Ellipsis containing the arguments specified by
431 * the format string.
432 *
433 * @throws std::bad_alloc On allocation error. The object is left unchanged.
434 *
435 * @returns Reference to the object.
436 */
437 RTCString &printf(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
438
439 /**
440 * Assigns the output of the string format operation (RTStrPrintf).
441 *
442 * @param pszFormat Pointer to the format string,
443 * @see pg_rt_str_format.
444 * @param ... Ellipsis containing the arguments specified by
445 * the format string.
446 *
447 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
448 */
449 int printfNoThrow(const char *pszFormat, ...) RT_NOEXCEPT RT_IPRT_FORMAT_ATTR(1, 2);
450
451 /**
452 * Assigns the output of the string format operation (RTStrPrintfV).
453 *
454 * @param pszFormat Pointer to the format string,
455 * @see pg_rt_str_format.
456 * @param va Argument vector containing the arguments
457 * specified by the format string.
458 *
459 * @throws std::bad_alloc On allocation error. The object is left unchanged.
460 *
461 * @returns Reference to the object.
462 */
463 RTCString &printfV(const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0);
464
465 /**
466 * Assigns the output of the string format operation (RTStrPrintfV).
467 *
468 * @param pszFormat Pointer to the format string,
469 * @see pg_rt_str_format.
470 * @param va Argument vector containing the arguments
471 * specified by the format string.
472 *
473 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
474 */
475 int printfVNoThrow(const char *pszFormat, va_list va) RT_NOEXCEPT RT_IPRT_FORMAT_ATTR(1, 0);
476
477 /**
478 * Appends the string @a that to @a rThat.
479 *
480 * @param rThat The string to append.
481 * @throws std::bad_alloc On allocation error. The object is left unchanged.
482 * @returns Reference to the object.
483 */
484 RTCString &append(const RTCString &rThat);
485
486 /**
487 * Appends the string @a that to @a rThat.
488 *
489 * @param rThat The string to append.
490 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
491 */
492 int appendNoThrow(const RTCString &rThat) RT_NOEXCEPT;
493
494 /**
495 * Appends the string @a pszSrc to @a this.
496 *
497 * @param pszSrc The C-style string to append.
498 * @throws std::bad_alloc On allocation error. The object is left unchanged.
499 * @returns Reference to the object.
500 */
501 RTCString &append(const char *pszSrc);
502
503 /**
504 * Appends the string @a pszSrc to @a this.
505 *
506 * @param pszSrc The C-style string to append.
507 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
508 */
509 int appendNoThrow(const char *pszSrc) RT_NOEXCEPT;
510
511 /**
512 * Appends the a substring from @a rThat to @a this.
513 *
514 * @param rThat The string to append a substring from.
515 * @param offStart The start of the substring to append (byte offset,
516 * not codepoint).
517 * @param cchMax The maximum number of bytes to append.
518 * @throws std::bad_alloc On allocation error. The object is left unchanged.
519 * @returns Reference to the object.
520 */
521 RTCString &append(const RTCString &rThat, size_t offStart, size_t cchMax = RTSTR_MAX);
522
523 /**
524 * Appends the a substring from @a rThat to @a this.
525 *
526 * @param rThat The string to append a substring from.
527 * @param offStart The start of the substring to append (byte offset,
528 * not codepoint).
529 * @param cchMax The maximum number of bytes to append.
530 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
531 */
532 int appendNoThrow(const RTCString &rThat, size_t offStart, size_t cchMax = RTSTR_MAX) RT_NOEXCEPT;
533
534 /**
535 * Appends the first @a cchMax chars from string @a pszThat to @a this.
536 *
537 * @param pszThat The C-style string to append.
538 * @param cchMax The maximum number of bytes to append.
539 * @throws std::bad_alloc On allocation error. The object is left unchanged.
540 * @returns Reference to the object.
541 */
542 RTCString &append(const char *pszThat, size_t cchMax);
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 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
550 */
551 int appendNoThrow(const char *pszThat, size_t cchMax) RT_NOEXCEPT;
552
553 /**
554 * Appends the given character to @a this.
555 *
556 * @param ch The character to append.
557 * @throws std::bad_alloc On allocation error. The object is left unchanged.
558 * @returns Reference to the object.
559 */
560 RTCString &append(char ch);
561
562 /**
563 * Appends the given character to @a this.
564 *
565 * @param ch The character to append.
566 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
567 */
568 int appendNoThrow(char ch) RT_NOEXCEPT;
569
570 /**
571 * Appends the given unicode code point to @a this.
572 *
573 * @param uc The unicode code point to append.
574 * @throws std::bad_alloc On allocation error. The object is left unchanged.
575 * @returns Reference to the object.
576 */
577 RTCString &appendCodePoint(RTUNICP uc);
578
579 /**
580 * Appends the given unicode code point to @a this.
581 *
582 * @param uc The unicode code point to append.
583 * @returns VINF_SUCCESS, VERR_INVALID_UTF8_ENCODING or VERR_NO_STRING_MEMORY.
584 */
585 int appendCodePointNoThrow(RTUNICP uc) RT_NOEXCEPT;
586
587 /**
588 * Appends the output of the string format operation (RTStrPrintf).
589 *
590 * @param pszFormat Pointer to the format string,
591 * @see pg_rt_str_format.
592 * @param ... Ellipsis containing the arguments specified by
593 * the format string.
594 *
595 * @throws std::bad_alloc On allocation error. The object is left unchanged.
596 *
597 * @returns Reference to the object.
598 */
599 RTCString &appendPrintf(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
600
601 /**
602 * Appends the output of the string format operation (RTStrPrintf).
603 *
604 * @param pszFormat Pointer to the format string,
605 * @see pg_rt_str_format.
606 * @param ... Ellipsis containing the arguments specified by
607 * the format string.
608 *
609 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
610 */
611 int appendPrintfNoThrow(const char *pszFormat, ...) RT_NOEXCEPT RT_IPRT_FORMAT_ATTR(1, 2);
612
613 /**
614 * Appends the output of the string format operation (RTStrPrintfV).
615 *
616 * @param pszFormat Pointer to the format string,
617 * @see pg_rt_str_format.
618 * @param va Argument vector containing the arguments
619 * specified by the format string.
620 *
621 * @throws std::bad_alloc On allocation error. The object is left unchanged.
622 *
623 * @returns Reference to the object.
624 */
625 RTCString &appendPrintfV(const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0);
626
627 /**
628 * Assigns the output of the string format operation (RTStrPrintfV).
629 *
630 * @param pszFormat Pointer to the format string,
631 * @see pg_rt_str_format.
632 * @param va Argument vector containing the arguments
633 * specified by the format string.
634 *
635 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
636 */
637 int appendPrintfVNoThrow(const char *pszFormat, va_list va) RT_NOEXCEPT RT_IPRT_FORMAT_ATTR(1, 0);
638
639 /**
640 * Shortcut to append(), RTCString variant.
641 *
642 * @param rThat The string to append.
643 * @returns Reference to the object.
644 */
645 RTCString &operator+=(const RTCString &rThat)
646 {
647 return append(rThat);
648 }
649
650 /**
651 * Shortcut to append(), const char* variant.
652 *
653 * @param pszThat The C-style string to append.
654 * @returns Reference to the object.
655 */
656 RTCString &operator+=(const char *pszThat)
657 {
658 return append(pszThat);
659 }
660
661 /**
662 * Shortcut to append(), char variant.
663 *
664 * @param ch The character to append.
665 *
666 * @returns Reference to the object.
667 */
668 RTCString &operator+=(char ch)
669 {
670 return append(ch);
671 }
672
673 /**
674 * Converts the member string to upper case.
675 *
676 * @returns Reference to the object.
677 */
678 RTCString &toUpper() RT_NOEXCEPT
679 {
680 if (length())
681 {
682 /* Folding an UTF-8 string may result in a shorter encoding (see
683 testcase), so recalculate the length afterwards. */
684 ::RTStrToUpper(m_psz);
685 size_t cchNew = strlen(m_psz);
686 Assert(cchNew <= m_cch);
687 m_cch = cchNew;
688 }
689 return *this;
690 }
691
692 /**
693 * Converts the member string to lower case.
694 *
695 * @returns Reference to the object.
696 */
697 RTCString &toLower() RT_NOEXCEPT
698 {
699 if (length())
700 {
701 /* Folding an UTF-8 string may result in a shorter encoding (see
702 testcase), so recalculate the length afterwards. */
703 ::RTStrToLower(m_psz);
704 size_t cchNew = strlen(m_psz);
705 Assert(cchNew <= m_cch);
706 m_cch = cchNew;
707 }
708 return *this;
709 }
710
711 /**
712 * Erases a sequence from the string.
713 *
714 * @returns Reference to the object.
715 * @param offStart Where in @a this string to start erasing.
716 * @param cchLength How much following @a offStart to erase.
717 */
718 RTCString &erase(size_t offStart = 0, size_t cchLength = npos) RT_NOEXCEPT;
719
720 /**
721 * Replaces a span of @a this string with a replacement string.
722 *
723 * @returns Reference to the object.
724 * @param offStart Where in @a this string to start replacing.
725 * @param cchLength How much following @a offStart to replace. npos is
726 * accepted.
727 * @param rStrReplacement The replacement string.
728 *
729 * @throws std::bad_alloc On allocation error. The object is left unchanged.
730 *
731 * @note Non-standard behaviour if offStart is beyond the end of the string.
732 * No change will occure and strict builds hits a debug assertion.
733 */
734 RTCString &replace(size_t offStart, size_t cchLength, const RTCString &rStrReplacement);
735
736 /**
737 * Replaces a span of @a this string with a replacement string.
738 *
739 * @returns VINF_SUCCESS, VERR_OUT_OF_RANGE or VERR_NO_STRING_MEMORY.
740 * @param offStart Where in @a this string to start replacing.
741 * @param cchLength How much following @a offStart to replace. npos is
742 * accepted.
743 * @param rStrReplacement The replacement string.
744 */
745 int replaceNoThrow(size_t offStart, size_t cchLength, const RTCString &rStrReplacement) RT_NOEXCEPT;
746
747 /**
748 * Replaces a span of @a this string with a replacement substring.
749 *
750 * @returns Reference to the object.
751 * @param offStart Where in @a this string to start replacing.
752 * @param cchLength How much following @a offStart to replace. npos is
753 * accepted.
754 * @param rStrReplacement The string from which a substring is taken.
755 * @param offReplacement The offset into @a rStrReplacement where the
756 * replacement substring starts.
757 * @param cchReplacement The maximum length of the replacement substring.
758 *
759 * @throws std::bad_alloc On allocation error. The object is left unchanged.
760 *
761 * @note Non-standard behaviour if offStart or offReplacement is beyond the
762 * end of the repective strings. No change is made in the former case,
763 * while we consider it an empty string in the latter. In both
764 * situation a debug assertion is raised in strict builds.
765 */
766 RTCString &replace(size_t offStart, size_t cchLength, const RTCString &rStrReplacement,
767 size_t offReplacement, size_t cchReplacement);
768
769 /**
770 * Replaces a span of @a this string with a replacement substring.
771 *
772 * @returns VINF_SUCCESS, VERR_OUT_OF_RANGE or VERR_NO_STRING_MEMORY.
773 * @param offStart Where in @a this string to start replacing.
774 * @param cchLength How much following @a offStart to replace. npos is
775 * accepted.
776 * @param rStrReplacement The string from which a substring is taken.
777 * @param offReplacement The offset into @a rStrReplacement where the
778 * replacement substring starts.
779 * @param cchReplacement The maximum length of the replacement substring.
780 */
781 int replaceNoThrow(size_t offStart, size_t cchLength, const RTCString &rStrReplacement,
782 size_t offReplacement, size_t cchReplacement) RT_NOEXCEPT;
783
784 /**
785 * Replaces a span of @a this string with the replacement string.
786 *
787 * @returns Reference to the object.
788 * @param offStart Where in @a this string to start replacing.
789 * @param cchLength How much following @a offStart to replace. npos is
790 * accepted.
791 * @param pszReplacement The replacement string.
792 *
793 * @throws std::bad_alloc On allocation error. The object is left unchanged.
794 *
795 * @note Non-standard behaviour if offStart is beyond the end of the string.
796 * No change will occure and strict builds hits a debug assertion.
797 */
798 RTCString &replace(size_t offStart, size_t cchLength, const char *pszReplacement);
799
800 /**
801 * Replaces a span of @a this string with the replacement string.
802 *
803 * @returns VINF_SUCCESS, VERR_OUT_OF_RANGE or VERR_NO_STRING_MEMORY.
804 * @param offStart Where in @a this string to start replacing.
805 * @param cchLength How much following @a offStart to replace. npos is
806 * accepted.
807 * @param pszReplacement The replacement string.
808 */
809 int replaceNoThrow(size_t offStart, size_t cchLength, const char *pszReplacement) RT_NOEXCEPT;
810
811 /**
812 * Replaces a span of @a this string with the replacement string.
813 *
814 * @returns Reference to the object.
815 * @param offStart Where in @a this string to start replacing.
816 * @param cchLength How much following @a offStart to replace. npos is
817 * accepted.
818 * @param pszReplacement The replacement string.
819 * @param cchReplacement How much of @a pszReplacement to use at most. If a
820 * zero terminator is found before reaching this value,
821 * we'll stop there.
822 *
823 * @throws std::bad_alloc On allocation error. The object is left unchanged.
824 *
825 * @note Non-standard behaviour if offStart is beyond the end of the string.
826 * No change will occure and strict builds hits a debug assertion.
827 */
828 RTCString &replace(size_t offStart, size_t cchLength, const char *pszReplacement, size_t cchReplacement);
829
830 /**
831 * Replaces a span of @a this string with the replacement string.
832 *
833 * @returns VINF_SUCCESS, VERR_OUT_OF_RANGE or VERR_NO_STRING_MEMORY.
834 * @param offStart Where in @a this string to start replacing.
835 * @param cchLength How much following @a offStart to replace. npos is
836 * accepted.
837 * @param pszReplacement The replacement string.
838 * @param cchReplacement How much of @a pszReplacement to use at most. If a
839 * zero terminator is found before reaching this value,
840 * we'll stop there.
841 */
842 int replaceNoThrow(size_t offStart, size_t cchLength, const char *pszReplacement, size_t cchReplacement) RT_NOEXCEPT;
843
844 /**
845 * Index operator.
846 *
847 * Returns the byte at the given index, or a null byte if the index is not
848 * smaller than length(). This does _not_ count codepoints but simply points
849 * into the member C-style string.
850 *
851 * @param i The index into the string buffer.
852 * @returns char at the index or null.
853 */
854 inline char operator[](size_t i) const RT_NOEXCEPT
855 {
856 if (i < length())
857 return m_psz[i];
858 return '\0';
859 }
860
861 /**
862 * Returns the contained string as a const C-style string pointer.
863 *
864 * This never returns NULL; if the string is empty, this returns a pointer to
865 * static null byte.
866 *
867 * @returns const pointer to C-style string.
868 */
869 inline const char *c_str() const RT_NOEXCEPT
870 {
871 return (m_psz) ? m_psz : "";
872 }
873
874 /**
875 * Returns a non-const raw pointer that allows to modify the string directly.
876 * As opposed to c_str() and raw(), this DOES return NULL for an empty string
877 * because we cannot return a non-const pointer to a static "" global.
878 *
879 * @warning
880 * -# Be sure not to modify data beyond the allocated memory! Call
881 * capacity() to find out how large that buffer is.
882 * -# After any operation that modifies the length of the string,
883 * you _must_ call RTCString::jolt(), or subsequent copy operations
884 * may go nowhere. Better not use mutableRaw() at all.
885 */
886 char *mutableRaw() RT_NOEXCEPT
887 {
888 return m_psz;
889 }
890
891 /**
892 * Clean up after using mutableRaw.
893 *
894 * Intended to be called after something has messed with the internal string
895 * buffer (e.g. after using mutableRaw() or Utf8Str::asOutParam()). Resets the
896 * internal lengths correctly. Otherwise subsequent copy operations may go
897 * nowhere.
898 */
899 void jolt() RT_NOEXCEPT
900 {
901 if (m_psz)
902 {
903 m_cch = strlen(m_psz);
904 m_cbAllocated = m_cch + 1; /* (Required for the Utf8Str::asOutParam case) */
905 }
906 else
907 {
908 m_cch = 0;
909 m_cbAllocated = 0;
910 }
911 }
912
913 /**
914 * Returns @c true if the member string has no length.
915 *
916 * This is @c true for instances created from both NULL and "" input
917 * strings.
918 *
919 * This states nothing about how much memory might be allocated.
920 *
921 * @returns @c true if empty, @c false if not.
922 */
923 bool isEmpty() const RT_NOEXCEPT
924 {
925 return length() == 0;
926 }
927
928 /**
929 * Returns @c false if the member string has no length.
930 *
931 * This is @c false for instances created from both NULL and "" input
932 * strings.
933 *
934 * This states nothing about how much memory might be allocated.
935 *
936 * @returns @c false if empty, @c true if not.
937 */
938 bool isNotEmpty() const RT_NOEXCEPT
939 {
940 return length() != 0;
941 }
942
943 /** Case sensitivity selector. */
944 enum CaseSensitivity
945 {
946 CaseSensitive,
947 CaseInsensitive
948 };
949
950 /**
951 * Compares the member string to a C-string.
952 *
953 * @param pcszThat The string to compare with.
954 * @param cs Whether comparison should be case-sensitive.
955 * @returns 0 if equal, negative if this is smaller than @a pcsz, positive
956 * if larger.
957 */
958 int compare(const char *pcszThat, CaseSensitivity cs = CaseSensitive) const RT_NOEXCEPT
959 {
960 /* This klugde is for m_cch=0 and m_psz=NULL. pcsz=NULL and psz=""
961 are treated the same way so that str.compare(str2.c_str()) works. */
962 if (length() == 0)
963 return pcszThat == NULL || *pcszThat == '\0' ? 0 : -1;
964
965 if (cs == CaseSensitive)
966 return ::RTStrCmp(m_psz, pcszThat);
967 return ::RTStrICmp(m_psz, pcszThat);
968 }
969
970 /**
971 * Compares the member string to another RTCString.
972 *
973 * @param rThat The string to compare with.
974 * @param cs Whether comparison should be case-sensitive.
975 * @returns 0 if equal, negative if this is smaller than @a pcsz, positive
976 * if larger.
977 */
978 int compare(const RTCString &rThat, CaseSensitivity cs = CaseSensitive) const RT_NOEXCEPT
979 {
980 if (cs == CaseSensitive)
981 return ::RTStrCmp(m_psz, rThat.m_psz);
982 return ::RTStrICmp(m_psz, rThat.m_psz);
983 }
984
985 /**
986 * Compares the two strings.
987 *
988 * @returns true if equal, false if not.
989 * @param rThat The string to compare with.
990 */
991 bool equals(const RTCString &rThat) const RT_NOEXCEPT
992 {
993 return rThat.length() == length()
994 && ( length() == 0
995 || memcmp(rThat.m_psz, m_psz, length()) == 0);
996 }
997
998 /**
999 * Compares the two strings.
1000 *
1001 * @returns true if equal, false if not.
1002 * @param pszThat The string to compare with.
1003 */
1004 bool equals(const char *pszThat) const RT_NOEXCEPT
1005 {
1006 /* This klugde is for m_cch=0 and m_psz=NULL. pcsz=NULL and psz=""
1007 are treated the same way so that str.equals(str2.c_str()) works. */
1008 if (length() == 0)
1009 return pszThat == NULL || *pszThat == '\0';
1010 return RTStrCmp(pszThat, m_psz) == 0;
1011 }
1012
1013 /**
1014 * Compares the two strings ignoring differences in case.
1015 *
1016 * @returns true if equal, false if not.
1017 * @param that The string to compare with.
1018 */
1019 bool equalsIgnoreCase(const RTCString &that) const RT_NOEXCEPT
1020 {
1021 /* Unfolded upper and lower case characters may require different
1022 amount of encoding space, so the length optimization doesn't work. */
1023 return RTStrICmp(that.m_psz, m_psz) == 0;
1024 }
1025
1026 /**
1027 * Compares the two strings ignoring differences in case.
1028 *
1029 * @returns true if equal, false if not.
1030 * @param pszThat The string to compare with.
1031 */
1032 bool equalsIgnoreCase(const char *pszThat) const RT_NOEXCEPT
1033 {
1034 /* This klugde is for m_cch=0 and m_psz=NULL. pcsz=NULL and psz=""
1035 are treated the same way so that str.equalsIgnoreCase(str2.c_str()) works. */
1036 if (length() == 0)
1037 return pszThat == NULL || *pszThat == '\0';
1038 return RTStrICmp(pszThat, m_psz) == 0;
1039 }
1040
1041 /** @name Comparison operators.
1042 * @{ */
1043 bool operator==(const RTCString &that) const { return equals(that); }
1044 bool operator!=(const RTCString &that) const { return !equals(that); }
1045 bool operator<( const RTCString &that) const { return compare(that) < 0; }
1046 bool operator>( const RTCString &that) const { return compare(that) > 0; }
1047
1048 bool operator==(const char *pszThat) const { return equals(pszThat); }
1049 bool operator!=(const char *pszThat) const { return !equals(pszThat); }
1050 bool operator<( const char *pszThat) const { return compare(pszThat) < 0; }
1051 bool operator>( const char *pszThat) const { return compare(pszThat) > 0; }
1052 /** @} */
1053
1054 /** Max string offset value.
1055 *
1056 * When returned by a method, this indicates failure. When taken as input,
1057 * typically a default, it means all the way to the string terminator.
1058 */
1059 static const size_t npos;
1060
1061 /**
1062 * Find the given substring.
1063 *
1064 * Looks for @a pszNeedle in @a this starting at @a offStart and returns its
1065 * position as a byte (not codepoint) offset, counting from the beginning of
1066 * @a this as 0.
1067 *
1068 * @param pszNeedle The substring to find.
1069 * @param offStart The (byte) offset into the string buffer to start
1070 * searching.
1071 *
1072 * @returns 0 based position of pszNeedle. npos if not found.
1073 */
1074 size_t find(const char *pszNeedle, size_t offStart = 0) const RT_NOEXCEPT;
1075
1076 /**
1077 * Find the given substring.
1078 *
1079 * Looks for @a pStrNeedle in @a this starting at @a offStart and returns its
1080 * position as a byte (not codepoint) offset, counting from the beginning of
1081 * @a this as 0.
1082 *
1083 * @param pStrNeedle The substring to find.
1084 * @param offStart The (byte) offset into the string buffer to start
1085 * searching.
1086 *
1087 * @returns 0 based position of pStrNeedle. npos if not found or pStrNeedle is
1088 * NULL or an empty string.
1089 */
1090 size_t find(const RTCString *pStrNeedle, size_t offStart = 0) const RT_NOEXCEPT;
1091
1092 /**
1093 * Replaces all occurences of cFind with cReplace in the member string.
1094 * In order not to produce invalid UTF-8, the characters must be ASCII
1095 * values less than 128; this is not verified.
1096 *
1097 * @param chFind Character to replace. Must be ASCII < 128.
1098 * @param chReplace Character to replace cFind with. Must be ASCII < 128.
1099 */
1100 void findReplace(char chFind, char chReplace) RT_NOEXCEPT;
1101
1102 /**
1103 * Count the occurences of the specified character in the string.
1104 *
1105 * @param ch What to search for. Must be ASCII < 128.
1106 * @remarks QString::count
1107 */
1108 size_t count(char ch) const RT_NOEXCEPT;
1109
1110 /**
1111 * Count the occurences of the specified sub-string in the string.
1112 *
1113 * @param psz What to search for.
1114 * @param cs Case sensitivity selector.
1115 * @remarks QString::count
1116 */
1117 size_t count(const char *psz, CaseSensitivity cs = CaseSensitive) const RT_NOEXCEPT;
1118
1119 /**
1120 * Count the occurences of the specified sub-string in the string.
1121 *
1122 * @param pStr What to search for.
1123 * @param cs Case sensitivity selector.
1124 * @remarks QString::count
1125 */
1126 size_t count(const RTCString *pStr, CaseSensitivity cs = CaseSensitive) const RT_NOEXCEPT;
1127
1128 /**
1129 * Strips leading and trailing spaces.
1130 *
1131 * @returns this
1132 */
1133 RTCString &strip() RT_NOEXCEPT;
1134
1135 /**
1136 * Strips leading spaces.
1137 *
1138 * @returns this
1139 */
1140 RTCString &stripLeft() RT_NOEXCEPT;
1141
1142 /**
1143 * Strips trailing spaces.
1144 *
1145 * @returns this
1146 */
1147 RTCString &stripRight() RT_NOEXCEPT;
1148
1149 /**
1150 * Returns a substring of @a this as a new Utf8Str.
1151 *
1152 * Works exactly like its equivalent in std::string. With the default
1153 * parameters "0" and "npos", this always copies the entire string. The
1154 * "pos" and "n" arguments represent bytes; it is the caller's responsibility
1155 * to ensure that the offsets do not copy invalid UTF-8 sequences. When
1156 * used in conjunction with find() and length(), this will work.
1157 *
1158 * @param pos Index of first byte offset to copy from @a this,
1159 * counting from 0.
1160 * @param n Number of bytes to copy, starting with the one at "pos".
1161 * The copying will stop if the null terminator is encountered before
1162 * n bytes have been copied.
1163 */
1164 RTCString substr(size_t pos = 0, size_t n = npos) const
1165 {
1166 return RTCString(*this, pos, n);
1167 }
1168
1169 /**
1170 * Returns a substring of @a this as a new Utf8Str. As opposed to substr(), this
1171 * variant takes codepoint offsets instead of byte offsets.
1172 *
1173 * @param pos Index of first unicode codepoint to copy from
1174 * @a this, counting from 0.
1175 * @param n Number of unicode codepoints to copy, starting with
1176 * the one at "pos". The copying will stop if the null
1177 * terminator is encountered before n codepoints have
1178 * been copied.
1179 */
1180 RTCString substrCP(size_t pos = 0, size_t n = npos) const;
1181
1182 /**
1183 * Returns true if @a this ends with @a that.
1184 *
1185 * @param that Suffix to test for.
1186 * @param cs Case sensitivity selector.
1187 * @returns true if match, false if mismatch.
1188 */
1189 bool endsWith(const RTCString &that, CaseSensitivity cs = CaseSensitive) const RT_NOEXCEPT;
1190
1191 /**
1192 * Returns true if @a this begins with @a that.
1193 * @param that Prefix to test for.
1194 * @param cs Case sensitivity selector.
1195 * @returns true if match, false if mismatch.
1196 */
1197 bool startsWith(const RTCString &that, CaseSensitivity cs = CaseSensitive) const RT_NOEXCEPT;
1198
1199 /**
1200 * Checks if the string starts with the given word, ignoring leading blanks.
1201 *
1202 * @param pszWord The word to test for.
1203 * @param enmCase Case sensitivity selector.
1204 * @returns true if match, false if mismatch.
1205 */
1206 bool startsWithWord(const char *pszWord, CaseSensitivity enmCase = CaseSensitive) const RT_NOEXCEPT;
1207
1208 /**
1209 * Checks if the string starts with the given word, ignoring leading blanks.
1210 *
1211 * @param rThat Prefix to test for.
1212 * @param enmCase Case sensitivity selector.
1213 * @returns true if match, false if mismatch.
1214 */
1215 bool startsWithWord(const RTCString &rThat, CaseSensitivity enmCase = CaseSensitive) const RT_NOEXCEPT;
1216
1217 /**
1218 * Returns true if @a this contains @a that (strstr).
1219 *
1220 * @param that Substring to look for.
1221 * @param cs Case sensitivity selector.
1222 * @returns true if found, false if not found.
1223 */
1224 bool contains(const RTCString &that, CaseSensitivity cs = CaseSensitive) const RT_NOEXCEPT;
1225
1226 /**
1227 * Returns true if @a this contains @a pszNeedle (strstr).
1228 *
1229 * @param pszNeedle Substring to look for.
1230 * @param cs Case sensitivity selector.
1231 * @returns true if found, false if not found.
1232 */
1233 bool contains(const char *pszNeedle, CaseSensitivity cs = CaseSensitive) const RT_NOEXCEPT;
1234
1235 /**
1236 * Attempts to convert the member string into a 32-bit integer.
1237 *
1238 * @returns 32-bit unsigned number on success.
1239 * @returns 0 on failure.
1240 */
1241 int32_t toInt32() const RT_NOEXCEPT
1242 {
1243 return RTStrToInt32(c_str());
1244 }
1245
1246 /**
1247 * Attempts to convert the member string into an unsigned 32-bit integer.
1248 *
1249 * @returns 32-bit unsigned number on success.
1250 * @returns 0 on failure.
1251 */
1252 uint32_t toUInt32() const RT_NOEXCEPT
1253 {
1254 return RTStrToUInt32(c_str());
1255 }
1256
1257 /**
1258 * Attempts to convert the member string into an 64-bit integer.
1259 *
1260 * @returns 64-bit unsigned number on success.
1261 * @returns 0 on failure.
1262 */
1263 int64_t toInt64() const RT_NOEXCEPT
1264 {
1265 return RTStrToInt64(c_str());
1266 }
1267
1268 /**
1269 * Attempts to convert the member string into an unsigned 64-bit integer.
1270 *
1271 * @returns 64-bit unsigned number on success.
1272 * @returns 0 on failure.
1273 */
1274 uint64_t toUInt64() const RT_NOEXCEPT
1275 {
1276 return RTStrToUInt64(c_str());
1277 }
1278
1279 /**
1280 * Attempts to convert the member string into an unsigned 64-bit integer.
1281 *
1282 * @param i Where to return the value on success.
1283 * @returns IPRT error code, see RTStrToInt64.
1284 */
1285 int toInt(uint64_t &i) const RT_NOEXCEPT;
1286
1287 /**
1288 * Attempts to convert the member string into an unsigned 32-bit integer.
1289 *
1290 * @param i Where to return the value on success.
1291 * @returns IPRT error code, see RTStrToInt32.
1292 */
1293 int toInt(uint32_t &i) const RT_NOEXCEPT;
1294
1295 /** Splitting behavior regarding empty sections in the string. */
1296 enum SplitMode
1297 {
1298 KeepEmptyParts, /**< Empty parts are added as empty strings to the result list. */
1299 RemoveEmptyParts /**< Empty parts are skipped. */
1300 };
1301
1302 /**
1303 * Splits a string separated by strSep into its parts.
1304 *
1305 * @param a_rstrSep The separator to search for.
1306 * @param a_enmMode How should empty parts be handled.
1307 * @returns separated strings as string list.
1308 * @throws std::bad_alloc On allocation error.
1309 */
1310 RTCList<RTCString, RTCString *> split(const RTCString &a_rstrSep,
1311 SplitMode a_enmMode = RemoveEmptyParts) const;
1312
1313 /**
1314 * Joins a list of strings together using the provided separator and
1315 * an optional prefix for each item in the list.
1316 *
1317 * @param a_rList The list to join.
1318 * @param a_rstrPrefix The prefix used for appending to each item.
1319 * @param a_rstrSep The separator used for joining.
1320 * @returns joined string.
1321 * @throws std::bad_alloc On allocation error.
1322 */
1323 static RTCString joinEx(const RTCList<RTCString, RTCString *> &a_rList,
1324 const RTCString &a_rstrPrefix /* = "" */,
1325 const RTCString &a_rstrSep /* = "" */);
1326
1327 /**
1328 * Joins a list of strings together using the provided separator.
1329 *
1330 * @param a_rList The list to join.
1331 * @param a_rstrSep The separator used for joining.
1332 * @returns joined string.
1333 * @throws std::bad_alloc On allocation error.
1334 */
1335 static RTCString join(const RTCList<RTCString, RTCString *> &a_rList,
1336 const RTCString &a_rstrSep = "");
1337
1338 /**
1339 * Swaps two strings in a fast way.
1340 *
1341 * Exception safe.
1342 *
1343 * @param a_rThat The string to swap with.
1344 */
1345 inline void swap(RTCString &a_rThat) RT_NOEXCEPT
1346 {
1347 char *pszTmp = m_psz;
1348 size_t cchTmp = m_cch;
1349 size_t cbAllocatedTmp = m_cbAllocated;
1350
1351 m_psz = a_rThat.m_psz;
1352 m_cch = a_rThat.m_cch;
1353 m_cbAllocated = a_rThat.m_cbAllocated;
1354
1355 a_rThat.m_psz = pszTmp;
1356 a_rThat.m_cch = cchTmp;
1357 a_rThat.m_cbAllocated = cbAllocatedTmp;
1358 }
1359
1360protected:
1361
1362 /**
1363 * Hide operator bool() to force people to use isEmpty() explicitly.
1364 */
1365 operator bool() const;
1366
1367 /**
1368 * Destructor implementation, also used to clean up in operator=() before
1369 * assigning a new string.
1370 */
1371 void cleanup() RT_NOEXCEPT
1372 {
1373 if (m_psz)
1374 {
1375 RTStrFree(m_psz);
1376 m_psz = NULL;
1377 m_cch = 0;
1378 m_cbAllocated = 0;
1379 }
1380 }
1381
1382 /**
1383 * Protected internal helper to copy a string.
1384 *
1385 * This ignores the previous object state, so either call this from a
1386 * constructor or call cleanup() first. copyFromN() unconditionally sets
1387 * the members to a copy of the given other strings and makes no
1388 * assumptions about previous contents. Can therefore be used both in copy
1389 * constructors, when member variables have no defined value, and in
1390 * assignments after having called cleanup().
1391 *
1392 * @param pcszSrc The source string.
1393 * @param cchSrc The number of chars (bytes) to copy from the
1394 * source strings. RTSTR_MAX is NOT accepted.
1395 *
1396 * @throws std::bad_alloc On allocation failure. The object is left
1397 * describing a NULL string.
1398 */
1399 void copyFromN(const char *pcszSrc, size_t cchSrc)
1400 {
1401 if (cchSrc)
1402 {
1403 m_psz = RTStrAlloc(cchSrc + 1);
1404 if (RT_LIKELY(m_psz))
1405 {
1406 m_cch = cchSrc;
1407 m_cbAllocated = cchSrc + 1;
1408 memcpy(m_psz, pcszSrc, cchSrc);
1409 m_psz[cchSrc] = '\0';
1410 }
1411 else
1412 {
1413 m_cch = 0;
1414 m_cbAllocated = 0;
1415#ifdef RT_EXCEPTIONS_ENABLED
1416 throw std::bad_alloc();
1417#endif
1418 }
1419 }
1420 else
1421 {
1422 m_cch = 0;
1423 m_cbAllocated = 0;
1424 m_psz = NULL;
1425 }
1426 }
1427
1428 /**
1429 * Appends exactly @a cchSrc chars from @a pszSrc to @a this.
1430 *
1431 * This is an internal worker for the append() methods.
1432 *
1433 * @returns Reference to the object.
1434 * @param pszSrc The source string.
1435 * @param cchSrc The source string length (exact).
1436 * @throws std::bad_alloc On allocation error. The object is left unchanged.
1437 *
1438 */
1439 RTCString &appendWorker(const char *pszSrc, size_t cchSrc);
1440
1441 /**
1442 * Appends exactly @a cchSrc chars from @a pszSrc to @a this.
1443 *
1444 * This is an internal worker for the appendNoThrow() methods.
1445 *
1446 * @returns VINF_SUCCESS or VERR_NO_STRING_MEMORY.
1447 * @param pszSrc The source string.
1448 * @param cchSrc The source string length (exact).
1449 */
1450 int appendWorkerNoThrow(const char *pszSrc, size_t cchSrc) RT_NOEXCEPT;
1451
1452 /**
1453 * Replaces exatly @a cchLength chars at @a offStart with @a cchSrc from @a
1454 * pszSrc.
1455 *
1456 * @returns Reference to the object.
1457 * @param offStart Where in @a this string to start replacing.
1458 * @param cchLength How much following @a offStart to replace. npos is
1459 * accepted.
1460 * @param pszSrc The replacement string.
1461 * @param cchSrc The exactly length of the replacement string.
1462 *
1463 * @throws std::bad_alloc On allocation error. The object is left unchanged.
1464 */
1465 RTCString &replaceWorker(size_t offStart, size_t cchLength, const char *pszSrc, size_t cchSrc);
1466
1467 /**
1468 * Replaces exatly @a cchLength chars at @a offStart with @a cchSrc from @a
1469 * pszSrc.
1470 *
1471 * @returns VINF_SUCCESS, VERR_OUT_OF_RANGE or VERR_NO_STRING_MEMORY.
1472 * @param offStart Where in @a this string to start replacing.
1473 * @param cchLength How much following @a offStart to replace. npos is
1474 * accepted.
1475 * @param pszSrc The replacement string.
1476 * @param cchSrc The exactly length of the replacement string.
1477 */
1478 int replaceWorkerNoThrow(size_t offStart, size_t cchLength, const char *pszSrc, size_t cchSrc) RT_NOEXCEPT;
1479
1480 static DECLCALLBACK(size_t) printfOutputCallback(void *pvArg, const char *pachChars, size_t cbChars);
1481 static DECLCALLBACK(size_t) printfOutputCallbackNoThrow(void *pvArg, const char *pachChars, size_t cbChars) RT_NOEXCEPT;
1482
1483 char *m_psz; /**< The string buffer. */
1484 size_t m_cch; /**< strlen(m_psz) - i.e. no terminator included. */
1485 size_t m_cbAllocated; /**< Size of buffer that m_psz points to; at least m_cbLength + 1. */
1486};
1487
1488/** @} */
1489
1490
1491/** @addtogroup grp_rt_cpp_string
1492 * @{
1493 */
1494
1495/**
1496 * Concatenate two strings.
1497 *
1498 * @param a_rstr1 String one.
1499 * @param a_rstr2 String two.
1500 * @returns the concatenate string.
1501 *
1502 * @relates RTCString
1503 */
1504RTDECL(const RTCString) operator+(const RTCString &a_rstr1, const RTCString &a_rstr2);
1505
1506/**
1507 * Concatenate two strings.
1508 *
1509 * @param a_rstr1 String one.
1510 * @param a_psz2 String two.
1511 * @returns the concatenate string.
1512 *
1513 * @relates RTCString
1514 */
1515RTDECL(const RTCString) operator+(const RTCString &a_rstr1, const char *a_psz2);
1516
1517/**
1518 * Concatenate two strings.
1519 *
1520 * @param a_psz1 String one.
1521 * @param a_rstr2 String two.
1522 * @returns the concatenate string.
1523 *
1524 * @relates RTCString
1525 */
1526RTDECL(const RTCString) operator+(const char *a_psz1, const RTCString &a_rstr2);
1527
1528/**
1529 * Class with RTCString::printf as constructor for your convenience.
1530 *
1531 * Constructing a RTCString string object from a format string and a variable
1532 * number of arguments can easily be confused with the other RTCString
1533 * constructors, thus this child class.
1534 *
1535 * The usage of this class is like the following:
1536 * @code
1537 RTCStringFmt strName("program name = %s", argv[0]);
1538 @endcode
1539 */
1540class RTCStringFmt : public RTCString
1541{
1542public:
1543
1544 /**
1545 * Constructs a new string given the format string and the list of the
1546 * arguments for the format string.
1547 *
1548 * @param a_pszFormat Pointer to the format string (UTF-8),
1549 * @see pg_rt_str_format.
1550 * @param ... Ellipsis containing the arguments specified by
1551 * the format string.
1552 */
1553 explicit RTCStringFmt(const char *a_pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2)
1554 {
1555 va_list va;
1556 va_start(va, a_pszFormat);
1557 printfV(a_pszFormat, va);
1558 va_end(va);
1559 }
1560
1561 RTMEMEF_NEW_AND_DELETE_OPERATORS();
1562
1563protected:
1564 RTCStringFmt() {}
1565};
1566
1567/** @} */
1568
1569#endif
1570
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