VirtualBox

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

Last change on this file since 69844 was 69105, checked in by vboxsync, 7 years ago

include/iprt/: (C) year

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