VirtualBox

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

Last change on this file since 76327 was 75008, checked in by vboxsync, 6 years ago

IPRT/RTCString: reserve(1) called on an empty string must allocate memory.

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