96
96
MiniString(const char *pcsz)
98
copyFromN(pcsz, pcsz ? strlen(pcsz) : 0);
102
* Create a partial copy of another MiniString.
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.
109
MiniString(const MiniString &a_rSrc, size_t a_offSrc, size_t a_cchSrc = npos)
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));
122
* Create a partial copy of a C string.
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.
130
MiniString(const char *a_pszSrc, size_t a_cchSrc)
132
size_t cchMax = a_pszSrc ? RTStrNLen(a_pszSrc, a_cchSrc) : 0;
133
copyFromN(a_pszSrc, RT_MIN(a_cchSrc, cchMax));
137
* Create a string containing @a a_cTimes repetitions of the character @a
140
* @param a_cTimes The number of times the character is repeated.
141
* @param a_ch The character to fill the string with.
143
MiniString(size_t a_cTimes, char a_ch)
148
Assert((unsigned)a_ch < 0x80);
151
reserve(a_cTimes + 1);
152
memset(m_psz, a_ch, a_cTimes);
153
m_psz[a_cTimes] = '\0';
159
* Create a new string given the format string and its arguments.
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.
166
* @remarks Not part of std::string.
168
MiniString(const char *a_pszFormat, va_list a_va)
173
printfV(a_pszFormat, a_va);
110
185
* String length in bytes.
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.
117
192
* @returns m_cbLength.
119
194
size_t length() const
200
* String length in UTF-8 codepoints.
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.
205
* @returns Number of codepoints in the member string.
207
size_t utf8length() const
209
return m_psz ? RTStrUniLen(m_psz) : 0;
298
copyFromN(s.m_psz, s.m_cch);
304
* Assigns the output of the string format operation (RTStrPrintf).
306
* @param pszFormat Pointer to the format string,
307
* @see pg_rt_str_format.
308
* @param ... Ellipsis containing the arguments specified by
311
* @throws std::bad_alloc On allocation error. The object is left unchanged.
313
* @returns Reference to the object.
315
MiniString &printf(const char *pszFormat, ...);
318
* Assigns the output of the string format operation (RTStrPrintfV).
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.
325
* @throws std::bad_alloc On allocation error. The object is left unchanged.
327
* @returns Reference to the object.
329
MiniString &printfV(const char *pszFormat, va_list va);
219
332
* Appends the string "that" to "this".
221
334
* @param that The string to append.
227
340
MiniString &append(const MiniString &that);
343
* Appends the string "that" to "this".
345
* @param pszThat The C string to append.
347
* @throws std::bad_alloc On allocation error. The object is left unchanged.
349
* @returns Reference to the object.
351
MiniString &append(const char *pszThat);
230
354
* Appends the given character to "this".
232
* @param c The character to append.
234
* @throws std::bad_alloc On allocation error. The object is left unchanged.
236
* @returns Reference to the object.
238
MiniString &append(char c);
356
* @param ch The character to append.
358
* @throws std::bad_alloc On allocation error. The object is left unchanged.
360
* @returns Reference to the object.
362
MiniString &append(char ch);
365
* Appends the given unicode code point to "this".
367
* @param uc The unicode code point to append.
369
* @throws std::bad_alloc On allocation error. The object is left unchanged.
371
* @returns Reference to the object.
373
MiniString &appendCodePoint(RTUNICP uc);
376
* Shortcut to append(), MiniString variant.
378
* @param that The string to append.
380
* @returns Reference to the object.
382
MiniString &operator+=(const MiniString &that)
388
* Shortcut to append(), const char* variant.
390
* @param pszThat The C string to append.
392
* @returns Reference to the object.
394
MiniString &operator+=(const char *pszThat)
396
return append(pszThat);
400
* Shortcut to append(), char variant.
402
* @param pszThat The character to append.
404
* @returns Reference to the object.
406
MiniString &operator+=(char c)
412
* Converts the member string to upper case.
414
* @returns Reference to the object.
416
MiniString &toUpper()
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);
431
* Converts the member string to lower case.
433
* @returns Reference to the object.
435
MiniString &toLower()
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);
241
450
* Index operator.
356
* Compares the member string to pcsz.
358
* @param cs Whether comparison should be case-sensitive.
555
* Compares the member string to a C-string.
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
361
int compare(const char *pcsz, CaseSensitivity cs = CaseSensitive) const
562
int compare(const char *pcszThat, CaseSensitivity cs = CaseSensitive) const
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. */
567
return pcszThat == NULL || *pcszThat == '\0' ? 0 : -1;
370
569
if (cs == CaseSensitive)
371
return ::RTStrCmp(m_psz, pcsz);
373
return ::RTStrICmp(m_psz, pcsz);
570
return ::RTStrCmp(m_psz, pcszThat);
571
return ::RTStrICmp(m_psz, pcszThat);
575
* Compares the member string to another MiniString.
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
376
582
int compare(const MiniString &that, CaseSensitivity cs = CaseSensitive) const
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);
590
* Compares the two strings.
592
* @returns true if equal, false if not.
593
* @param that The string to compare with.
595
bool equals(const MiniString &that) const
597
return that.length() == length()
598
&& memcmp(that.m_psz, m_psz, length()) == 0;
602
* Compares the two strings.
604
* @returns true if equal, false if not.
605
* @param pszThat The string to compare with.
607
bool equals(const char *pszThat) const
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. */
612
return pszThat == NULL || *pszThat == '\0';
613
return RTStrCmp(pszThat, m_psz) == 0;
617
* Compares the two strings ignoring differences in case.
619
* @returns true if equal, false if not.
620
* @param that The string to compare with.
622
bool equalsIgnoreCase(const MiniString &that) const
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;
630
* Compares the two strings ignoring differences in case.
632
* @returns true if equal, false if not.
633
* @param pszThat The string to compare with.
635
bool equalsIgnoreCase(const char *pszThat) const
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. */
640
return pszThat == NULL || *pszThat == '\0';
641
return RTStrICmp(pszThat, m_psz) == 0;
381
644
/** @name Comparison operators.
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; }
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; }
394
657
/** Max string offset value.
413
676
size_t find(const char *pcszFind, size_t pos = 0) const;
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.
683
* @param cFind Character to replace. Must be ASCII < 128.
684
* @param cReplace Character to replace cFind with. Must be ASCII < 128.
686
void findReplace(char cFind, char cReplace);
416
689
* Returns a substring of "this" as a new Utf8Str.
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.
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.
702
iprt::MiniString substr(size_t pos = 0, size_t n = npos) const
704
return MiniString(*this, pos, n);
708
* Returns a substring of "this" as a new Utf8Str. As opposed to substr(),
709
* this variant takes codepoint offsets instead of byte offsets.
422
711
* @param pos Index of first unicode codepoint to copy from
423
712
* "this", counting from 0.
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.
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().
528
* This variant copies from another MiniString and is fast since
529
* the length of the source string is known.
531
* @param s The source string.
533
* @throws std::bad_alloc On allocation failure. The object is left describing
536
void copyFrom(const MiniString &s)
538
if ((m_cbLength = s.m_cbLength))
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
548
#ifdef RT_EXCEPTIONS_ENABLED
549
throw std::bad_alloc();
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.
564
* See copyFrom() above.
566
* This variant copies from a C string and needs to call strlen()
567
* on it. It's therefore slower than the one above.
569
* @param pcsz The source string.
571
* @throws std::bad_alloc On allocation failure. The object is left describing
574
void copyFrom(const char *pcsz)
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
587
#ifdef RT_EXCEPTIONS_ENABLED
588
throw std::bad_alloc();
600
char *m_psz; /**< The string buffer. */
601
size_t m_cbLength; /**< strlen(m_psz) - i.e. no terminator included. */
829
* Protected internal helper to copy a string.
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().
838
* @param pcszSrc The source string.
839
* @param cchSrc The number of chars (bytes) to copy from the
842
* @throws std::bad_alloc On allocation failure. The object is left
843
* describing a NULL string.
845
void copyFromN(const char *pcszSrc, size_t cchSrc)
849
m_psz = RTStrAlloc(cchSrc + 1);
850
if (RT_LIKELY(m_psz))
853
m_cbAllocated = cchSrc + 1;
854
memcpy(m_psz, pcszSrc, cchSrc);
855
m_psz[cchSrc] = '\0';
861
#ifdef RT_EXCEPTIONS_ENABLED
862
throw std::bad_alloc();
874
static DECLCALLBACK(size_t) printfOutputCallback(void *pvArg, const char *pachChars, size_t cbChars);
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. */