VirtualBox

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

Last change on this file since 79839 was 79481, checked in by vboxsync, 6 years ago

iprt/cpp/list.h,ministring.h: Heed RT_NEED_NEW_AND_DELETE.

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