VirtualBox

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

Last change on this file since 74653 was 74262, checked in by vboxsync, 6 years ago

IPRT/RTCString: Added find(char,size_t) and find(RTCString const &,size_t). bugref:9167

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