VirtualBox

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

Last change on this file since 35567 was 35567, checked in by vboxsync, 14 years ago

IPRT: fix rare crash in MiniString::substr(); rename substr() to substrCP() and add a substr that operates on bytes, not codepoints; more to come

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 26.8 KB
Line 
1/** @file
2 * IPRT - Mini C++ string class.
3 */
4
5/*
6 * Copyright (C) 2007-2009 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 ___VBox_ministring_h
27#define ___VBox_ministring_h
28
29#include <iprt/mem.h>
30#include <iprt/string.h>
31#include <iprt/stdarg.h>
32
33#include <new>
34
35namespace iprt
36{
37
38/**
39 * @brief Mini C++ string class.
40 *
41 * "MiniString" is a small C++ string class that does not depend on anything
42 * else except IPRT memory management functions. Semantics are like in
43 * std::string, except it can do a lot less.
44 *
45 * Note that MiniString does not differentiate between NULL strings and
46 * empty strings. In other words, MiniString("") and MiniString(NULL)
47 * behave the same. In both cases, MiniString allocates no memory, reports
48 * a zero length and zero allocated bytes for both, and returns an empty
49 * C string from c_str().
50 */
51#ifdef VBOX
52 /** @remarks Much of the code in here used to be in com::Utf8Str so that
53 * com::Utf8Str can now derive from MiniString and only contain code
54 * that is COM-specific, such as com::Bstr conversions. Compared to
55 * the old Utf8Str though, MiniString always knows the length of its
56 * member string and the size of the buffer so it can use memcpy()
57 * instead of strdup().
58 */
59#endif
60class RT_DECL_CLASS MiniString
61{
62public:
63 /**
64 * Creates an empty string that has no memory allocated.
65 */
66 MiniString()
67 : m_psz(NULL),
68 m_cch(0),
69 m_cbAllocated(0)
70 {
71 }
72
73 /**
74 * Creates a copy of another MiniString.
75 *
76 * This allocates s.length() + 1 bytes for the new instance, unless s is empty.
77 *
78 * @param a_rSrc The source string.
79 *
80 * @throws std::bad_alloc
81 */
82 MiniString(const MiniString &a_rSrc)
83 {
84 copyFromN(a_rSrc.m_psz, a_rSrc.m_cch);
85 }
86
87 /**
88 * Creates a copy of a C string.
89 *
90 * This allocates strlen(pcsz) + 1 bytes for the new instance, unless s is empty.
91 *
92 * @param pcsz The source string.
93 *
94 * @throws std::bad_alloc
95 */
96 MiniString(const char *pcsz)
97 {
98 copyFromN(pcsz, pcsz ? strlen(pcsz) : 0);
99 }
100
101 /**
102 * Create a partial copy of another MiniString.
103 *
104 * @param a_rSrc The source string.
105 * @param a_offSrc The byte offset into the source string.
106 * @param a_cchSrc The max number of chars (encoded UTF-8 bytes)
107 * to copy from the source string.
108 */
109 MiniString(const MiniString &a_rSrc, size_t a_offSrc, size_t a_cchSrc = npos)
110 {
111 if (a_offSrc < a_rSrc.m_cch)
112 copyFromN(&a_rSrc.m_psz[a_offSrc], RT_MIN(a_cchSrc, a_rSrc.m_cch - a_offSrc));
113 else
114 {
115 m_psz = NULL;
116 m_cch = 0;
117 m_cbAllocated = 0;
118 }
119 }
120
121 /**
122 * Create a partial copy of a C string.
123 *
124 * @param a_pszSrc The source string (UTF-8).
125 * @param a_cchSrc The max number of chars (encoded UTF-8 bytes)
126 * to copy from the source string. This must not
127 * be '0' as the compiler could easily mistake
128 * that for the va_list constructor.
129 */
130 MiniString(const char *a_pszSrc, size_t a_cchSrc)
131 {
132 size_t cchMax = a_pszSrc ? RTStrNLen(a_pszSrc, a_cchSrc) : 0;
133 copyFromN(a_pszSrc, RT_MIN(a_cchSrc, cchMax));
134 }
135
136 /**
137 * Create a string containing @a a_cTimes repetitions of the character @a
138 * a_ch.
139 *
140 * @param a_cTimes The number of times the character is repeated.
141 * @param a_ch The character to fill the string with.
142 */
143 MiniString(size_t a_cTimes, char a_ch)
144 : m_psz(NULL),
145 m_cch(0),
146 m_cbAllocated(0)
147 {
148 Assert((unsigned)a_ch < 0x80);
149 if (a_cTimes)
150 {
151 reserve(a_cTimes + 1);
152 memset(m_psz, a_ch, a_cTimes);
153 m_psz[a_cTimes] = '\0';
154 m_cch = a_cTimes;
155 }
156 }
157
158 /**
159 * Create a new string given the format string and its arguments.
160 *
161 * @param a_pszFormat Pointer to the format string (UTF-8),
162 * @see pg_rt_str_format.
163 * @param a_va Argument vector containing the arguments
164 * specified by the format string.
165 * @sa printfV
166 * @remarks Not part of std::string.
167 */
168 MiniString(const char *a_pszFormat, va_list a_va)
169 : m_psz(NULL),
170 m_cch(0),
171 m_cbAllocated(0)
172 {
173 printfV(a_pszFormat, a_va);
174 }
175
176 /**
177 * Destructor.
178 */
179 virtual ~MiniString()
180 {
181 cleanup();
182 }
183
184 /**
185 * String length in bytes.
186 *
187 * Returns the length of the member string in bytes, which is equal to strlen(c_str()).
188 * In other words, this does not count unicode codepoints; use utf8length() for that.
189 * The byte length is always cached so calling this is cheap and requires no
190 * strlen() invocation.
191 *
192 * @returns m_cbLength.
193 */
194 size_t length() const
195 {
196 return m_cch;
197 }
198
199 /**
200 * String length in UTF-8 codepoints.
201 *
202 * As opposed to length(), which returns the length in bytes, this counts the number
203 * of UTF-8 codepoints. This is *not* cached so calling this is expensive.
204 *
205 * @returns Number of codepoints in the member string.
206 */
207 size_t utf8length() const
208 {
209 return m_psz ? RTStrUniLen(m_psz) : 0;
210 }
211
212 /**
213 * The allocated buffer size (in bytes).
214 *
215 * Returns the number of bytes allocated in the internal string buffer, which is
216 * at least length() + 1 if length() > 0; for an empty string, this returns 0.
217 *
218 * @returns m_cbAllocated.
219 */
220 size_t capacity() const
221 {
222 return m_cbAllocated;
223 }
224
225 /**
226 * Make sure at that least cb of buffer space is reserved.
227 *
228 * Requests that the contained memory buffer have at least cb bytes allocated.
229 * This may expand or shrink the string's storage, but will never truncate the
230 * contained string. In other words, cb will be ignored if it's smaller than
231 * length() + 1.
232 *
233 * @param cb New minimum size (in bytes) of member memory buffer.
234 *
235 * @throws std::bad_alloc On allocation error. The object is left unchanged.
236 */
237 void reserve(size_t cb)
238 {
239 if ( cb != m_cbAllocated
240 && cb > m_cch + 1
241 )
242 {
243 int vrc = RTStrRealloc(&m_psz, cb);
244 if (RT_SUCCESS(vrc))
245 m_cbAllocated = cb;
246#ifdef RT_EXCEPTIONS_ENABLED
247 else
248 throw std::bad_alloc();
249#endif
250 }
251 }
252
253 /**
254 * Deallocates all memory.
255 */
256 inline void setNull()
257 {
258 cleanup();
259 }
260
261 RTMEMEF_NEW_AND_DELETE_OPERATORS();
262
263 /**
264 * Assigns a copy of pcsz to "this".
265 *
266 * @param pcsz The source string.
267 *
268 * @throws std::bad_alloc On allocation failure. The object is left describing
269 * a NULL string.
270 *
271 * @returns Reference to the object.
272 */
273 MiniString &operator=(const char *pcsz)
274 {
275 if (m_psz != pcsz)
276 {
277 cleanup();
278 copyFromN(pcsz, pcsz ? strlen(pcsz) : 0);
279 }
280 return *this;
281 }
282
283 /**
284 * Assigns a copy of s to "this".
285 *
286 * @param s The source string.
287 *
288 * @throws std::bad_alloc On allocation failure. The object is left describing
289 * a NULL string.
290 *
291 * @returns Reference to the object.
292 */
293 MiniString &operator=(const MiniString &s)
294 {
295 if (this != &s)
296 {
297 cleanup();
298 copyFromN(s.m_psz, s.m_cch);
299 }
300 return *this;
301 }
302
303 /**
304 * Assigns the output of the string format operation (RTStrPrintf).
305 *
306 * @param pszFormat Pointer to the format string,
307 * @see pg_rt_str_format.
308 * @param ... Ellipsis containing the arguments specified by
309 * the format string.
310 *
311 * @throws std::bad_alloc On allocation error. The object is left unchanged.
312 *
313 * @returns Reference to the object.
314 */
315 MiniString &printf(const char *pszFormat, ...);
316
317 /**
318 * Assigns the output of the string format operation (RTStrPrintfV).
319 *
320 * @param pszFormat Pointer to the format string,
321 * @see pg_rt_str_format.
322 * @param va Argument vector containing the arguments
323 * specified by the format string.
324 *
325 * @throws std::bad_alloc On allocation error. The object is left unchanged.
326 *
327 * @returns Reference to the object.
328 */
329 MiniString &printfV(const char *pszFormat, va_list va);
330
331 /**
332 * Appends the string "that" to "this".
333 *
334 * @param that The string to append.
335 *
336 * @throws std::bad_alloc On allocation error. The object is left unchanged.
337 *
338 * @returns Reference to the object.
339 */
340 MiniString &append(const MiniString &that);
341
342 /**
343 * Appends the string "that" to "this".
344 *
345 * @param pszThat The C string to append.
346 *
347 * @throws std::bad_alloc On allocation error. The object is left unchanged.
348 *
349 * @returns Reference to the object.
350 */
351 MiniString &append(const char *pszThat);
352
353 /**
354 * Appends the given character to "this".
355 *
356 * @param ch The character to append.
357 *
358 * @throws std::bad_alloc On allocation error. The object is left unchanged.
359 *
360 * @returns Reference to the object.
361 */
362 MiniString &append(char ch);
363
364 /**
365 * Appends the given unicode code point to "this".
366 *
367 * @param uc The unicode code point to append.
368 *
369 * @throws std::bad_alloc On allocation error. The object is left unchanged.
370 *
371 * @returns Reference to the object.
372 */
373 MiniString &appendCodePoint(RTUNICP uc);
374
375 /**
376 * Shortcut to append(), MiniString variant.
377 *
378 * @param that The string to append.
379 *
380 * @returns Reference to the object.
381 */
382 MiniString &operator+=(const MiniString &that)
383 {
384 return append(that);
385 }
386
387 /**
388 * Shortcut to append(), const char* variant.
389 *
390 * @param pszThat The C string to append.
391 *
392 * @returns Reference to the object.
393 */
394 MiniString &operator+=(const char *pszThat)
395 {
396 return append(pszThat);
397 }
398
399 /**
400 * Shortcut to append(), char variant.
401 *
402 * @param pszThat The character to append.
403 *
404 * @returns Reference to the object.
405 */
406 MiniString &operator+=(char c)
407 {
408 return append(c);
409 }
410
411 /**
412 * Converts the member string to upper case.
413 *
414 * @returns Reference to the object.
415 */
416 MiniString &toUpper()
417 {
418 if (length())
419 {
420 /* Folding an UTF-8 string may result in a shorter encoding (see
421 testcase), so recalculate the length afterwars. */
422 ::RTStrToUpper(m_psz);
423 size_t cchNew = strlen(m_psz);
424 Assert(cchNew <= m_cch);
425 m_cch = cchNew;
426 }
427 return *this;
428 }
429
430 /**
431 * Converts the member string to lower case.
432 *
433 * @returns Reference to the object.
434 */
435 MiniString &toLower()
436 {
437 if (length())
438 {
439 /* Folding an UTF-8 string may result in a shorter encoding (see
440 testcase), so recalculate the length afterwars. */
441 ::RTStrToLower(m_psz);
442 size_t cchNew = strlen(m_psz);
443 Assert(cchNew <= m_cch);
444 m_cch = cchNew;
445 }
446 return *this;
447 }
448
449 /**
450 * Index operator.
451 *
452 * Returns the byte at the given index, or a null byte if the index is not
453 * smaller than length(). This does _not_ count codepoints but simply points
454 * into the member C string.
455 *
456 * @param i The index into the string buffer.
457 * @returns char at the index or null.
458 */
459 inline char operator[](size_t i) const
460 {
461 if (i < length())
462 return m_psz[i];
463 return '\0';
464 }
465
466 /**
467 * Returns the contained string as a C-style const char* pointer.
468 * This never returns NULL; if the string is empty, this returns a
469 * pointer to static null byte.
470 *
471 * @returns const pointer to C-style string.
472 */
473 inline const char *c_str() const
474 {
475 return (m_psz) ? m_psz : "";
476 }
477
478 /**
479 * Returns a non-const raw pointer that allows to modify the string directly.
480 * As opposed to c_str() and raw(), this DOES return NULL for an empty string
481 * because we cannot return a non-const pointer to a static "" global.
482 *
483 * @warning
484 * -# Be sure not to modify data beyond the allocated memory! Call
485 * capacity() to find out how large that buffer is.
486 * -# After any operation that modifies the length of the string,
487 * you _must_ call MiniString::jolt(), or subsequent copy operations
488 * may go nowhere. Better not use mutableRaw() at all.
489 */
490 char *mutableRaw()
491 {
492 return m_psz;
493 }
494
495 /**
496 * Clean up after using mutableRaw.
497 *
498 * Intended to be called after something has messed with the internal string
499 * buffer (e.g. after using mutableRaw() or Utf8Str::asOutParam()). Resets the
500 * internal lengths correctly. Otherwise subsequent copy operations may go
501 * nowhere.
502 */
503 void jolt()
504 {
505 if (m_psz)
506 {
507 m_cch = strlen(m_psz);
508 m_cbAllocated = m_cch + 1; /* (Required for the Utf8Str::asOutParam case) */
509 }
510 else
511 {
512 m_cch = 0;
513 m_cbAllocated = 0;
514 }
515 }
516
517 /**
518 * Returns @c true if the member string has no length.
519 *
520 * This is @c true for instances created from both NULL and "" input
521 * strings.
522 *
523 * This states nothing about how much memory might be allocated.
524 *
525 * @returns @c true if empty, @c false if not.
526 */
527 bool isEmpty() const
528 {
529 return length() == 0;
530 }
531
532 /**
533 * Returns @c false if the member string has no length.
534 *
535 * This is @c false for instances created from both NULL and "" input
536 * strings.
537 *
538 * This states nothing about how much memory might be allocated.
539 *
540 * @returns @c false if empty, @c true if not.
541 */
542 bool isNotEmpty() const
543 {
544 return length() != 0;
545 }
546
547 /** Case sensitivity selector. */
548 enum CaseSensitivity
549 {
550 CaseSensitive,
551 CaseInsensitive
552 };
553
554 /**
555 * Compares the member string to a C-string.
556 *
557 * @param pcszThat The string to compare with.
558 * @param cs Whether comparison should be case-sensitive.
559 * @returns 0 if equal, negative if this is smaller than @a pcsz, positive
560 * if larger.
561 */
562 int compare(const char *pcszThat, CaseSensitivity cs = CaseSensitive) const
563 {
564 /* This klugde is for m_cch=0 and m_psz=NULL. pcsz=NULL and psz=""
565 are treated the same way so that str.compare(str2.c_str()) works. */
566 if (length() == 0)
567 return pcszThat == NULL || *pcszThat == '\0' ? 0 : -1;
568
569 if (cs == CaseSensitive)
570 return ::RTStrCmp(m_psz, pcszThat);
571 return ::RTStrICmp(m_psz, pcszThat);
572 }
573
574 /**
575 * Compares the member string to another MiniString.
576 *
577 * @param pcszThat The string to compare with.
578 * @param cs Whether comparison should be case-sensitive.
579 * @returns 0 if equal, negative if this is smaller than @a pcsz, positive
580 * if larger.
581 */
582 int compare(const MiniString &that, CaseSensitivity cs = CaseSensitive) const
583 {
584 if (cs == CaseSensitive)
585 return ::RTStrCmp(m_psz, that.m_psz);
586 return ::RTStrICmp(m_psz, that.m_psz);
587 }
588
589 /**
590 * Compares the two strings.
591 *
592 * @returns true if equal, false if not.
593 * @param that The string to compare with.
594 */
595 bool equals(const MiniString &that) const
596 {
597 return that.length() == length()
598 && memcmp(that.m_psz, m_psz, length()) == 0;
599 }
600
601 /**
602 * Compares the two strings.
603 *
604 * @returns true if equal, false if not.
605 * @param pszThat The string to compare with.
606 */
607 bool equals(const char *pszThat) const
608 {
609 /* This klugde is for m_cch=0 and m_psz=NULL. pcsz=NULL and psz=""
610 are treated the same way so that str.equals(str2.c_str()) works. */
611 if (length() == 0)
612 return pszThat == NULL || *pszThat == '\0';
613 return RTStrCmp(pszThat, m_psz) == 0;
614 }
615
616 /**
617 * Compares the two strings ignoring differences in case.
618 *
619 * @returns true if equal, false if not.
620 * @param that The string to compare with.
621 */
622 bool equalsIgnoreCase(const MiniString &that) const
623 {
624 /* Unfolded upper and lower case characters may require different
625 amount of encoding space, so the length optimization doesn't work. */
626 return RTStrICmp(that.m_psz, m_psz) == 0;
627 }
628
629 /**
630 * Compares the two strings ignoring differences in case.
631 *
632 * @returns true if equal, false if not.
633 * @param pszThat The string to compare with.
634 */
635 bool equalsIgnoreCase(const char *pszThat) const
636 {
637 /* This klugde is for m_cch=0 and m_psz=NULL. pcsz=NULL and psz=""
638 are treated the same way so that str.equalsIgnoreCase(str2.c_str()) works. */
639 if (length() == 0)
640 return pszThat == NULL || *pszThat == '\0';
641 return RTStrICmp(pszThat, m_psz) == 0;
642 }
643
644 /** @name Comparison operators.
645 * @{ */
646 bool operator==(const MiniString &that) const { return equals(that); }
647 bool operator!=(const MiniString &that) const { return !equals(that); }
648 bool operator<( const MiniString &that) const { return compare(that) < 0; }
649 bool operator>( const MiniString &that) const { return compare(that) > 0; }
650
651 bool operator==(const char *pszThat) const { return equals(pszThat); }
652 bool operator!=(const char *pszThat) const { return !equals(pszThat); }
653 bool operator<( const char *pszThat) const { return compare(pszThat) < 0; }
654 bool operator>( const char *pszThat) const { return compare(pszThat) > 0; }
655 /** @} */
656
657 /** Max string offset value.
658 *
659 * When returned by a method, this indicates failure. When taken as input,
660 * typically a default, it means all the way to the string terminator.
661 */
662 static const size_t npos;
663
664 /**
665 * Find the given substring.
666 *
667 * Looks for pcszFind in "this" starting at "pos" and returns its position
668 * as a byte (not codepoint) offset, counting from the beginning of "this" at 0.
669 *
670 * @param pcszFind The substring to find.
671 * @param pos The (byte) offset into the string buffer to start
672 * searching.
673 *
674 * @returns 0 based position of pcszFind. npos if not found.
675 */
676 size_t find(const char *pcszFind, size_t pos = 0) const;
677
678 /**
679 * Replaces all occurences of cFind with cReplace in the member string.
680 * In order not to produce invalid UTF-8, the characters must be ASCII
681 * values less than 128; this is not verified.
682 *
683 * @param cFind Character to replace. Must be ASCII < 128.
684 * @param cReplace Character to replace cFind with. Must be ASCII < 128.
685 */
686 void findReplace(char cFind, char cReplace);
687
688 /**
689 * Returns a substring of "this" as a new Utf8Str.
690 *
691 * Works exactly like its equivalent in std::string. With the default
692 * parameters "0" and "npos", this always copies the entire string. The
693 * "pos" and "n" arguments represent bytes; it is the caller's responsibility
694 * to ensure that the offsets do not copy invalid UTF-8 sequences. When
695 * used in conjunction with find() and length(), this will work.
696 *
697 * @param pos Index of first byte offset to copy from "this", counting from 0.
698 * @param n Number of bytes to copy, starting with the one at "pos".
699 * The copying will stop if the null terminator is encountered before
700 * n bytes have been copied.
701 */
702 iprt::MiniString substr(size_t pos = 0, size_t n = npos) const
703 {
704 return MiniString(*this, pos, n);
705 }
706
707 /**
708 * Returns a substring of "this" as a new Utf8Str. As opposed to substr(),
709 * this variant takes codepoint offsets instead of byte offsets.
710 *
711 * @param pos Index of first unicode codepoint to copy from
712 * "this", counting from 0.
713 * @param n Number of unicode codepoints to copy, starting with
714 * the one at "pos". The copying will stop if the null
715 * terminator is encountered before n codepoints have
716 * been copied.
717 */
718 iprt::MiniString substrCP(size_t pos = 0, size_t n = npos) const;
719
720 /**
721 * Returns true if "this" ends with "that".
722 *
723 * @param that Suffix to test for.
724 * @param cs Case sensitivity selector.
725 * @returns true if match, false if mismatch.
726 */
727 bool endsWith(const iprt::MiniString &that, CaseSensitivity cs = CaseSensitive) const;
728
729 /**
730 * Returns true if "this" begins with "that".
731 * @param that Prefix to test for.
732 * @param cs Case sensitivity selector.
733 * @returns true if match, false if mismatch.
734 */
735 bool startsWith(const iprt::MiniString &that, CaseSensitivity cs = CaseSensitive) const;
736
737 /**
738 * Returns true if "this" contains "that" (strstr).
739 *
740 * @param that Substring to look for.
741 * @param cs Case sensitivity selector.
742 * @returns true if match, false if mismatch.
743 */
744 bool contains(const iprt::MiniString &that, CaseSensitivity cs = CaseSensitive) const;
745
746 /**
747 * Attempts to convert the member string into a 32-bit integer.
748 *
749 * @returns 32-bit unsigned number on success.
750 * @returns 0 on failure.
751 */
752 int32_t toInt32() const
753 {
754 return RTStrToInt32(m_psz);
755 }
756
757 /**
758 * Attempts to convert the member string into an unsigned 32-bit integer.
759 *
760 * @returns 32-bit unsigned number on success.
761 * @returns 0 on failure.
762 */
763 uint32_t toUInt32() const
764 {
765 return RTStrToUInt32(m_psz);
766 }
767
768 /**
769 * Attempts to convert the member string into an 64-bit integer.
770 *
771 * @returns 64-bit unsigned number on success.
772 * @returns 0 on failure.
773 */
774 int64_t toInt64() const
775 {
776 return RTStrToInt64(m_psz);
777 }
778
779 /**
780 * Attempts to convert the member string into an unsigned 64-bit integer.
781 *
782 * @returns 64-bit unsigned number on success.
783 * @returns 0 on failure.
784 */
785 uint64_t toUInt64() const
786 {
787 return RTStrToUInt64(m_psz);
788 }
789
790 /**
791 * Attempts to convert the member string into an unsigned 64-bit integer.
792 *
793 * @param i Where to return the value on success.
794 * @returns IPRT error code, see RTStrToInt64.
795 */
796 int toInt(uint64_t &i) const;
797
798 /**
799 * Attempts to convert the member string into an unsigned 32-bit integer.
800 *
801 * @param i Where to return the value on success.
802 * @returns IPRT error code, see RTStrToInt32.
803 */
804 int toInt(uint32_t &i) const;
805
806protected:
807
808 /**
809 * Hide operator bool() to force people to use isEmpty() explicitly.
810 */
811 operator bool() const;
812
813 /**
814 * Destructor implementation, also used to clean up in operator=() before
815 * assigning a new string.
816 */
817 void cleanup()
818 {
819 if (m_psz)
820 {
821 RTStrFree(m_psz);
822 m_psz = NULL;
823 m_cch = 0;
824 m_cbAllocated = 0;
825 }
826 }
827
828 /**
829 * Protected internal helper to copy a string.
830 *
831 * This ignores the previous object state, so either call this from a
832 * constructor or call cleanup() first. copyFromN() unconditionally sets
833 * the members to a copy of the given other strings and makes no
834 * assumptions about previous contents. Can therefore be used both in copy
835 * constructors, when member variables have no defined value, and in
836 * assignments after having called cleanup().
837 *
838 * @param pcszSrc The source string.
839 * @param cchSrc The number of chars (bytes) to copy from the
840 * source strings.
841 *
842 * @throws std::bad_alloc On allocation failure. The object is left
843 * describing a NULL string.
844 */
845 void copyFromN(const char *pcszSrc, size_t cchSrc)
846 {
847 if (cchSrc)
848 {
849 m_psz = RTStrAlloc(cchSrc + 1);
850 if (RT_LIKELY(m_psz))
851 {
852 m_cch = cchSrc;
853 m_cbAllocated = cchSrc + 1;
854 memcpy(m_psz, pcszSrc, cchSrc);
855 m_psz[cchSrc] = '\0';
856 }
857 else
858 {
859 m_cch = 0;
860 m_cbAllocated = 0;
861#ifdef RT_EXCEPTIONS_ENABLED
862 throw std::bad_alloc();
863#endif
864 }
865 }
866 else
867 {
868 m_cch = 0;
869 m_cbAllocated = 0;
870 m_psz = NULL;
871 }
872 }
873
874 static DECLCALLBACK(size_t) printfOutputCallback(void *pvArg, const char *pachChars, size_t cbChars);
875
876 char *m_psz; /**< The string buffer. */
877 size_t m_cch; /**< strlen(m_psz) - i.e. no terminator included. */
878 size_t m_cbAllocated; /**< Size of buffer that m_psz points to; at least m_cbLength + 1. */
879};
880
881} // namespace iprt
882
883#endif
884
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette