~ubuntu-branches/ubuntu/raring/virtualbox-ose/raring

« back to all changes in this revision

Viewing changes to include/iprt/cpp/ministring.h

  • Committer: Bazaar Package Importer
  • Author(s): Felix Geyer
  • Date: 2011-01-30 23:27:25 UTC
  • mfrom: (0.3.12 upstream)
  • Revision ID: james.westby@ubuntu.com-20110130232725-2ouajjd2ggdet0zd
Tags: 4.0.2-dfsg-1ubuntu1
* Merge from Debian unstable, remaining changes:
  - Add Apport hook.
    - debian/virtualbox-ose.files/source_virtualbox-ose.py
    - debian/virtualbox-ose.install
  - Drop *-source packages.
* Drop ubuntu-01-fix-build-gcc45.patch, fixed upstream.
* Drop ubuntu-02-as-needed.patch, added to the Debian package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 
29
29
#include <iprt/mem.h>
30
30
#include <iprt/string.h>
 
31
#include <iprt/stdarg.h>
31
32
 
32
33
#include <new>
33
34
 
41
42
 * else except IPRT memory management functions.  Semantics are like in
42
43
 * std::string, except it can do a lot less.
43
44
 *
44
 
 *
45
45
 * Note that MiniString does not differentiate between NULL strings and
46
46
 * empty strings. In other words, MiniString("") and MiniString(NULL)
47
47
 * behave the same. In both cases, MiniString allocates no memory, reports
65
65
     */
66
66
    MiniString()
67
67
        : m_psz(NULL),
68
 
          m_cbLength(0),
 
68
          m_cch(0),
69
69
          m_cbAllocated(0)
70
70
    {
71
71
    }
75
75
     *
76
76
     * This allocates s.length() + 1 bytes for the new instance, unless s is empty.
77
77
     *
78
 
     * @param   s               The source string.
 
78
     * @param   a_rSrc          The source string.
79
79
     *
80
80
     * @throws  std::bad_alloc
81
81
     */
82
 
    MiniString(const MiniString &s)
 
82
    MiniString(const MiniString &a_rSrc)
83
83
    {
84
 
        copyFrom(s);
 
84
        copyFromN(a_rSrc.m_psz, a_rSrc.m_cch);
85
85
    }
86
86
 
87
87
    /**
95
95
     */
96
96
    MiniString(const char *pcsz)
97
97
    {
98
 
        copyFrom(pcsz);
 
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);
99
174
    }
100
175
 
101
176
    /**
109
184
    /**
110
185
     * String length in bytes.
111
186
     *
112
 
     * Returns the length of the member string, which is equal to strlen(c_str()).
113
 
     * In other words, this does not count unicode codepoints but returns the number
114
 
     * of bytes.  This is always cached so calling this is cheap and requires no
 
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
115
190
     * strlen() invocation.
116
191
     *
117
192
     * @returns m_cbLength.
118
193
     */
119
194
    size_t length() const
120
195
    {
121
 
        return m_cbLength;
 
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;
122
210
    }
123
211
 
124
212
    /**
149
237
    void reserve(size_t cb)
150
238
    {
151
239
        if (    cb != m_cbAllocated
152
 
             && cb > m_cbLength + 1
 
240
             && cb > m_cch + 1
153
241
           )
154
242
        {
155
 
            char *pszNew = (char*)RTMemRealloc(m_psz, cb);
156
 
            if (RT_LIKELY(pszNew))
157
 
            {
158
 
                if (!m_psz)
159
 
                    *pszNew = '\0';
160
 
                m_psz = pszNew;
 
243
            int vrc = RTStrRealloc(&m_psz, cb);
 
244
            if (RT_SUCCESS(vrc))
161
245
                m_cbAllocated = cb;
162
 
            }
163
246
#ifdef RT_EXCEPTIONS_ENABLED
164
247
            else
165
248
                throw std::bad_alloc();
175
258
        cleanup();
176
259
    }
177
260
 
 
261
    RTMEMEF_NEW_AND_DELETE_OPERATORS();
 
262
 
178
263
    /**
179
264
     * Assigns a copy of pcsz to "this".
180
265
     *
190
275
        if (m_psz != pcsz)
191
276
        {
192
277
            cleanup();
193
 
            copyFrom(pcsz);
 
278
            copyFromN(pcsz, pcsz ? strlen(pcsz) : 0);
194
279
        }
195
280
        return *this;
196
281
    }
210
295
        if (this != &s)
211
296
        {
212
297
            cleanup();
213
 
            copyFrom(s);
 
298
            copyFromN(s.m_psz, s.m_cch);
214
299
        }
215
300
        return *this;
216
301
    }
217
302
 
218
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
    /**
219
332
     * Appends the string "that" to "this".
220
333
     *
221
334
     * @param   that            The string to append.
227
340
    MiniString &append(const MiniString &that);
228
341
 
229
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
    /**
230
354
     * Appends the given character to "this".
231
355
     *
232
 
     * @param   c               The character to append.
233
 
     *
234
 
     * @throws  std::bad_alloc  On allocation error.  The object is left unchanged.
235
 
     *
236
 
     * @returns Reference to the object.
237
 
     */
238
 
    MiniString &append(char c);
 
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
    }
239
448
 
240
449
    /**
241
450
     * Index operator.
267
476
    }
268
477
 
269
478
    /**
270
 
     * Like c_str(), for compatibility with lots of VirtualBox Main code.
271
 
     *
272
 
     * @returns const pointer to C-style string.
273
 
     */
274
 
    inline const char *raw() const
275
 
    {
276
 
        return (m_psz) ? m_psz : "";
277
 
    }
278
 
 
279
 
    /**
280
479
     * Returns a non-const raw pointer that allows to modify the string directly.
281
480
     * As opposed to c_str() and raw(), this DOES return NULL for an empty string
282
481
     * because we cannot return a non-const pointer to a static "" global.
305
504
    {
306
505
        if (m_psz)
307
506
        {
308
 
            m_cbLength = strlen(m_psz);
309
 
            m_cbAllocated = m_cbLength + 1; /* (Required for the Utf8Str::asOutParam case) */
 
507
            m_cch = strlen(m_psz);
 
508
            m_cbAllocated = m_cch + 1; /* (Required for the Utf8Str::asOutParam case) */
310
509
        }
311
510
        else
312
511
        {
313
 
            m_cbLength = 0;
 
512
            m_cch = 0;
314
513
            m_cbAllocated = 0;
315
514
        }
316
515
    }
353
552
    };
354
553
 
355
554
    /**
356
 
     * Compares the member string to pcsz.
357
 
     * @param pcsz
358
 
     * @param cs Whether comparison should be case-sensitive.
359
 
     * @return
 
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.
360
561
     */
361
 
    int compare(const char *pcsz, CaseSensitivity cs = CaseSensitive) const
 
562
    int compare(const char *pcszThat, CaseSensitivity cs = CaseSensitive) const
362
563
    {
363
 
        if (m_psz == pcsz)
364
 
            return 0;
365
 
        if (m_psz == NULL)
366
 
            return -1;
367
 
        if (pcsz == NULL)
368
 
            return 1;
 
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;
369
568
 
370
569
        if (cs == CaseSensitive)
371
 
            return ::RTStrCmp(m_psz, pcsz);
372
 
        else
373
 
            return ::RTStrICmp(m_psz, pcsz);
 
570
            return ::RTStrCmp(m_psz, pcszThat);
 
571
        return ::RTStrICmp(m_psz, pcszThat);
374
572
    }
375
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
     */
376
582
    int compare(const MiniString &that, CaseSensitivity cs = CaseSensitive) const
377
583
    {
378
 
        return compare(that.m_psz, cs);
 
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;
379
642
    }
380
643
 
381
644
    /** @name Comparison operators.
382
645
     * @{  */
383
 
    bool operator==(const MiniString &that) const { return !compare(that); }
384
 
    bool operator!=(const MiniString &that) const { return !!compare(that); }
 
646
    bool operator==(const MiniString &that) const { return equals(that); }
 
647
    bool operator!=(const MiniString &that) const { return !equals(that); }
385
648
    bool operator<( const MiniString &that) const { return compare(that) < 0; }
386
649
    bool operator>( const MiniString &that) const { return compare(that) > 0; }
387
650
 
388
 
    bool operator==(const char *that) const       { return !compare(that); }
389
 
    bool operator!=(const char *that) const       { return !!compare(that); }
390
 
    bool operator<( const char *that) const       { return compare(that) < 0; }
391
 
    bool operator>( const char *that) const       { return compare(that) > 0; }
 
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; }
392
655
    /** @} */
393
656
 
394
657
    /** Max string offset value.
401
664
    /**
402
665
     * Find the given substring.
403
666
     *
404
 
     * Looks for pcszFind in "this" starting at "pos" and returns its position,
405
 
     * counting from the beginning of "this" at 0.
 
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.
406
669
     *
407
670
     * @param   pcszFind        The substring to find.
408
671
     * @param   pos             The (byte) offset into the string buffer to start
413
676
    size_t find(const char *pcszFind, size_t pos = 0) const;
414
677
 
415
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
    /**
416
689
     * Returns a substring of "this" as a new Utf8Str.
417
690
     *
418
 
     * Works exactly like its equivalent in std::string except that this interprets
419
 
     * pos and n as unicode codepoints instead of bytes.  With the default
420
 
     * parameters "0" and "npos", this always copies the entire string.
 
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.
421
710
     *
422
711
     * @param   pos             Index of first unicode codepoint to copy from
423
712
     *                          "this", counting from 0.
425
714
     *                          the one at "pos".  The copying will stop if the null
426
715
     *                          terminator is encountered before n codepoints have
427
716
     *                          been copied.
428
 
     *
429
 
     * @remarks This works on code points, not bytes!
430
717
     */
431
 
    iprt::MiniString substr(size_t pos = 0, size_t n = npos) const;
 
718
    iprt::MiniString substrCP(size_t pos = 0, size_t n = npos) const;
432
719
 
433
720
    /**
434
721
     * Returns true if "this" ends with "that".
457
744
    bool contains(const iprt::MiniString &that, CaseSensitivity cs = CaseSensitive) const;
458
745
 
459
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
    /**
460
769
     * Attempts to convert the member string into an 64-bit integer.
461
770
     *
462
771
     * @returns 64-bit unsigned number on success.
509
818
    {
510
819
        if (m_psz)
511
820
        {
512
 
            RTMemFree(m_psz);
513
 
            m_psz = NULL;
514
 
            m_cbLength = 0;
515
 
            m_cbAllocated = 0;
516
 
        }
517
 
    }
518
 
 
519
 
    /**
520
 
     * Protected internal helper to copy a string. This ignores the previous object
521
 
     * state, so either call this from a constructor or call cleanup() first.
522
 
     *
523
 
     * copyFrom() unconditionally sets the members to a copy of the given other
524
 
     * strings and makes no assumptions about previous contents. Can therefore be
525
 
     * used both in copy constructors, when member variables have no defined value,
526
 
     * and in assignments after having called cleanup().
527
 
     *
528
 
     * This variant copies from another MiniString and is fast since
529
 
     * the length of the source string is known.
530
 
     *
531
 
     * @param   s               The source string.
532
 
     *
533
 
     * @throws  std::bad_alloc  On allocation failure. The object is left describing
534
 
     *             a NULL string.
535
 
     */
536
 
    void copyFrom(const MiniString &s)
537
 
    {
538
 
        if ((m_cbLength = s.m_cbLength))
539
 
        {
540
 
            m_cbAllocated = m_cbLength + 1;
541
 
            m_psz = (char *)RTMemAlloc(m_cbAllocated);
542
 
            if (RT_LIKELY(m_psz))
543
 
                memcpy(m_psz, s.m_psz, m_cbAllocated);      // include 0 terminator
544
 
            else
545
 
            {
546
 
                m_cbLength = 0;
547
 
                m_cbAllocated = 0;
548
 
#ifdef RT_EXCEPTIONS_ENABLED
549
 
                throw std::bad_alloc();
550
 
#endif
551
 
            }
552
 
        }
553
 
        else
554
 
        {
555
 
            m_cbAllocated = 0;
556
 
            m_psz = NULL;
557
 
        }
558
 
    }
559
 
 
560
 
    /**
561
 
     * Protected internal helper to copy a string. This ignores the previous object
562
 
     * state, so either call this from a constructor or call cleanup() first.
563
 
     *
564
 
     * See copyFrom() above.
565
 
     *
566
 
     * This variant copies from a C string and needs to call strlen()
567
 
     * on it. It's therefore slower than the one above.
568
 
     *
569
 
     * @param   pcsz            The source string.
570
 
     *
571
 
     * @throws  std::bad_alloc  On allocation failure. The object is left describing
572
 
     *             a NULL string.
573
 
     */
574
 
    void copyFrom(const char *pcsz)
575
 
    {
576
 
        if (pcsz && *pcsz)
577
 
        {
578
 
            m_cbLength = strlen(pcsz);
579
 
            m_cbAllocated = m_cbLength + 1;
580
 
            m_psz = (char *)RTMemAlloc(m_cbAllocated);
581
 
            if (RT_LIKELY(m_psz))
582
 
                memcpy(m_psz, pcsz, m_cbAllocated);     // include 0 terminator
583
 
            else
584
 
            {
585
 
                m_cbLength = 0;
586
 
                m_cbAllocated = 0;
587
 
#ifdef RT_EXCEPTIONS_ENABLED
588
 
                throw std::bad_alloc();
589
 
#endif
590
 
            }
591
 
        }
592
 
        else
593
 
        {
594
 
            m_cbLength = 0;
595
 
            m_cbAllocated = 0;
596
 
            m_psz = NULL;
597
 
        }
598
 
    }
599
 
 
600
 
    char    *m_psz;                     /**< The string buffer. */
601
 
    size_t  m_cbLength;                 /**< strlen(m_psz) - i.e. no terminator included. */
 
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. */
602
878
    size_t  m_cbAllocated;              /**< Size of buffer that m_psz points to; at least m_cbLength + 1. */
603
879
};
604
880