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 |
|
---|
35 | namespace 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
|
---|
60 | class RT_DECL_CLASS MiniString
|
---|
61 | {
|
---|
62 | public:
|
---|
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, which is equal to strlen(c_str()).
|
---|
188 | * In other words, this does not count unicode codepoints but returns the number
|
---|
189 | * of bytes. This 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 | * The allocated buffer size (in bytes).
|
---|
201 | *
|
---|
202 | * Returns the number of bytes allocated in the internal string buffer, which is
|
---|
203 | * at least length() + 1 if length() > 0; for an empty string, this returns 0.
|
---|
204 | *
|
---|
205 | * @returns m_cbAllocated.
|
---|
206 | */
|
---|
207 | size_t capacity() const
|
---|
208 | {
|
---|
209 | return m_cbAllocated;
|
---|
210 | }
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Make sure at that least cb of buffer space is reserved.
|
---|
214 | *
|
---|
215 | * Requests that the contained memory buffer have at least cb bytes allocated.
|
---|
216 | * This may expand or shrink the string's storage, but will never truncate the
|
---|
217 | * contained string. In other words, cb will be ignored if it's smaller than
|
---|
218 | * length() + 1.
|
---|
219 | *
|
---|
220 | * @param cb New minimum size (in bytes) of member memory buffer.
|
---|
221 | *
|
---|
222 | * @throws std::bad_alloc On allocation error. The object is left unchanged.
|
---|
223 | */
|
---|
224 | void reserve(size_t cb)
|
---|
225 | {
|
---|
226 | if ( cb != m_cbAllocated
|
---|
227 | && cb > m_cch + 1
|
---|
228 | )
|
---|
229 | {
|
---|
230 | int vrc = RTStrRealloc(&m_psz, cb);
|
---|
231 | if (RT_SUCCESS(vrc))
|
---|
232 | m_cbAllocated = cb;
|
---|
233 | #ifdef RT_EXCEPTIONS_ENABLED
|
---|
234 | else
|
---|
235 | throw std::bad_alloc();
|
---|
236 | #endif
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Deallocates all memory.
|
---|
242 | */
|
---|
243 | inline void setNull()
|
---|
244 | {
|
---|
245 | cleanup();
|
---|
246 | }
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * Assigns a copy of pcsz to "this".
|
---|
250 | *
|
---|
251 | * @param pcsz The source string.
|
---|
252 | *
|
---|
253 | * @throws std::bad_alloc On allocation failure. The object is left describing
|
---|
254 | * a NULL string.
|
---|
255 | *
|
---|
256 | * @returns Reference to the object.
|
---|
257 | */
|
---|
258 | MiniString &operator=(const char *pcsz)
|
---|
259 | {
|
---|
260 | if (m_psz != pcsz)
|
---|
261 | {
|
---|
262 | cleanup();
|
---|
263 | copyFromN(pcsz, pcsz ? strlen(pcsz) : 0);
|
---|
264 | }
|
---|
265 | return *this;
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * Assigns a copy of s to "this".
|
---|
270 | *
|
---|
271 | * @param s The source string.
|
---|
272 | *
|
---|
273 | * @throws std::bad_alloc On allocation failure. The object is left describing
|
---|
274 | * a NULL string.
|
---|
275 | *
|
---|
276 | * @returns Reference to the object.
|
---|
277 | */
|
---|
278 | MiniString &operator=(const MiniString &s)
|
---|
279 | {
|
---|
280 | if (this != &s)
|
---|
281 | {
|
---|
282 | cleanup();
|
---|
283 | copyFromN(s.m_psz, s.m_cch);
|
---|
284 | }
|
---|
285 | return *this;
|
---|
286 | }
|
---|
287 |
|
---|
288 | /**
|
---|
289 | * Assigns the output of the string format operation (RTStrPrintf).
|
---|
290 | *
|
---|
291 | * @param pszFormat Pointer to the format string,
|
---|
292 | * @see pg_rt_str_format.
|
---|
293 | * @param ... Ellipsis containing the arguments specified by
|
---|
294 | * the format string.
|
---|
295 | *
|
---|
296 | * @throws std::bad_alloc On allocation error. The object is left unchanged.
|
---|
297 | *
|
---|
298 | * @returns Reference to the object.
|
---|
299 | */
|
---|
300 | MiniString &printf(const char *pszFormat, ...);
|
---|
301 |
|
---|
302 | /**
|
---|
303 | * Assigns the output of the string format operation (RTStrPrintfV).
|
---|
304 | *
|
---|
305 | * @param pszFormat Pointer to the format string,
|
---|
306 | * @see pg_rt_str_format.
|
---|
307 | * @param va Argument vector containing the arguments
|
---|
308 | * specified by the format string.
|
---|
309 | *
|
---|
310 | * @throws std::bad_alloc On allocation error. The object is left unchanged.
|
---|
311 | *
|
---|
312 | * @returns Reference to the object.
|
---|
313 | */
|
---|
314 | MiniString &printfV(const char *pszFormat, va_list va);
|
---|
315 |
|
---|
316 | /**
|
---|
317 | * Appends the string "that" to "this".
|
---|
318 | *
|
---|
319 | * @param that The string to append.
|
---|
320 | *
|
---|
321 | * @throws std::bad_alloc On allocation error. The object is left unchanged.
|
---|
322 | *
|
---|
323 | * @returns Reference to the object.
|
---|
324 | */
|
---|
325 | MiniString &append(const MiniString &that);
|
---|
326 |
|
---|
327 | /**
|
---|
328 | * Appends the string "that" to "this".
|
---|
329 | *
|
---|
330 | * @param pszThat The C string to append.
|
---|
331 | *
|
---|
332 | * @throws std::bad_alloc On allocation error. The object is left unchanged.
|
---|
333 | *
|
---|
334 | * @returns Reference to the object.
|
---|
335 | */
|
---|
336 | MiniString &append(const char *pszThat);
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * Appends the given character to "this".
|
---|
340 | *
|
---|
341 | * @param ch The character to append.
|
---|
342 | *
|
---|
343 | * @throws std::bad_alloc On allocation error. The object is left unchanged.
|
---|
344 | *
|
---|
345 | * @returns Reference to the object.
|
---|
346 | */
|
---|
347 | MiniString &append(char ch);
|
---|
348 |
|
---|
349 | /**
|
---|
350 | * Appends the given unicode code point to "this".
|
---|
351 | *
|
---|
352 | * @param uc The unicode code point to append.
|
---|
353 | *
|
---|
354 | * @throws std::bad_alloc On allocation error. The object is left unchanged.
|
---|
355 | *
|
---|
356 | * @returns Reference to the object.
|
---|
357 | */
|
---|
358 | MiniString &appendCodePoint(RTUNICP uc);
|
---|
359 |
|
---|
360 | /**
|
---|
361 | * Shortcut to append(), MiniString variant.
|
---|
362 | *
|
---|
363 | * @param that The string to append.
|
---|
364 | *
|
---|
365 | * @returns Reference to the object.
|
---|
366 | */
|
---|
367 | MiniString &operator+=(const MiniString &that)
|
---|
368 | {
|
---|
369 | return append(that);
|
---|
370 | }
|
---|
371 |
|
---|
372 | /**
|
---|
373 | * Shortcut to append(), const char* variant.
|
---|
374 | *
|
---|
375 | * @param pszThat The C string to append.
|
---|
376 | *
|
---|
377 | * @returns Reference to the object.
|
---|
378 | */
|
---|
379 | MiniString &operator+=(const char *pszThat)
|
---|
380 | {
|
---|
381 | return append(pszThat);
|
---|
382 | }
|
---|
383 |
|
---|
384 | /**
|
---|
385 | * Shortcut to append(), char variant.
|
---|
386 | *
|
---|
387 | * @param pszThat The character to append.
|
---|
388 | *
|
---|
389 | * @returns Reference to the object.
|
---|
390 | */
|
---|
391 | MiniString &operator+=(char c)
|
---|
392 | {
|
---|
393 | return append(c);
|
---|
394 | }
|
---|
395 |
|
---|
396 | /**
|
---|
397 | * Converts the member string to upper case.
|
---|
398 | *
|
---|
399 | * @returns Reference to the object.
|
---|
400 | */
|
---|
401 | MiniString &toUpper()
|
---|
402 | {
|
---|
403 | if (length())
|
---|
404 | {
|
---|
405 | /* Folding an UTF-8 string may result in a shorter encoding (see
|
---|
406 | testcase), so recalculate the length afterwars. */
|
---|
407 | ::RTStrToUpper(m_psz);
|
---|
408 | size_t cchNew = strlen(m_psz);
|
---|
409 | Assert(cchNew <= m_cch);
|
---|
410 | m_cch = cchNew;
|
---|
411 | }
|
---|
412 | return *this;
|
---|
413 | }
|
---|
414 |
|
---|
415 | /**
|
---|
416 | * Converts the member string to lower case.
|
---|
417 | *
|
---|
418 | * @returns Reference to the object.
|
---|
419 | */
|
---|
420 | MiniString &toLower()
|
---|
421 | {
|
---|
422 | if (length())
|
---|
423 | {
|
---|
424 | /* Folding an UTF-8 string may result in a shorter encoding (see
|
---|
425 | testcase), so recalculate the length afterwars. */
|
---|
426 | ::RTStrToLower(m_psz);
|
---|
427 | size_t cchNew = strlen(m_psz);
|
---|
428 | Assert(cchNew <= m_cch);
|
---|
429 | m_cch = cchNew;
|
---|
430 | }
|
---|
431 | return *this;
|
---|
432 | }
|
---|
433 |
|
---|
434 | /**
|
---|
435 | * Index operator.
|
---|
436 | *
|
---|
437 | * Returns the byte at the given index, or a null byte if the index is not
|
---|
438 | * smaller than length(). This does _not_ count codepoints but simply points
|
---|
439 | * into the member C string.
|
---|
440 | *
|
---|
441 | * @param i The index into the string buffer.
|
---|
442 | * @returns char at the index or null.
|
---|
443 | */
|
---|
444 | inline char operator[](size_t i) const
|
---|
445 | {
|
---|
446 | if (i < length())
|
---|
447 | return m_psz[i];
|
---|
448 | return '\0';
|
---|
449 | }
|
---|
450 |
|
---|
451 | /**
|
---|
452 | * Returns the contained string as a C-style const char* pointer.
|
---|
453 | * This never returns NULL; if the string is empty, this returns a
|
---|
454 | * pointer to static null byte.
|
---|
455 | *
|
---|
456 | * @returns const pointer to C-style string.
|
---|
457 | */
|
---|
458 | inline const char *c_str() const
|
---|
459 | {
|
---|
460 | return (m_psz) ? m_psz : "";
|
---|
461 | }
|
---|
462 |
|
---|
463 | /**
|
---|
464 | * Returns a non-const raw pointer that allows to modify the string directly.
|
---|
465 | * As opposed to c_str() and raw(), this DOES return NULL for an empty string
|
---|
466 | * because we cannot return a non-const pointer to a static "" global.
|
---|
467 | *
|
---|
468 | * @warning
|
---|
469 | * -# Be sure not to modify data beyond the allocated memory! Call
|
---|
470 | * capacity() to find out how large that buffer is.
|
---|
471 | * -# After any operation that modifies the length of the string,
|
---|
472 | * you _must_ call MiniString::jolt(), or subsequent copy operations
|
---|
473 | * may go nowhere. Better not use mutableRaw() at all.
|
---|
474 | */
|
---|
475 | char *mutableRaw()
|
---|
476 | {
|
---|
477 | return m_psz;
|
---|
478 | }
|
---|
479 |
|
---|
480 | /**
|
---|
481 | * Clean up after using mutableRaw.
|
---|
482 | *
|
---|
483 | * Intended to be called after something has messed with the internal string
|
---|
484 | * buffer (e.g. after using mutableRaw() or Utf8Str::asOutParam()). Resets the
|
---|
485 | * internal lengths correctly. Otherwise subsequent copy operations may go
|
---|
486 | * nowhere.
|
---|
487 | */
|
---|
488 | void jolt()
|
---|
489 | {
|
---|
490 | if (m_psz)
|
---|
491 | {
|
---|
492 | m_cch = strlen(m_psz);
|
---|
493 | m_cbAllocated = m_cch + 1; /* (Required for the Utf8Str::asOutParam case) */
|
---|
494 | }
|
---|
495 | else
|
---|
496 | {
|
---|
497 | m_cch = 0;
|
---|
498 | m_cbAllocated = 0;
|
---|
499 | }
|
---|
500 | }
|
---|
501 |
|
---|
502 | /**
|
---|
503 | * Returns @c true if the member string has no length.
|
---|
504 | *
|
---|
505 | * This is @c true for instances created from both NULL and "" input
|
---|
506 | * strings.
|
---|
507 | *
|
---|
508 | * This states nothing about how much memory might be allocated.
|
---|
509 | *
|
---|
510 | * @returns @c true if empty, @c false if not.
|
---|
511 | */
|
---|
512 | bool isEmpty() const
|
---|
513 | {
|
---|
514 | return length() == 0;
|
---|
515 | }
|
---|
516 |
|
---|
517 | /**
|
---|
518 | * Returns @c false if the member string has no length.
|
---|
519 | *
|
---|
520 | * This is @c false 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 false if empty, @c true if not.
|
---|
526 | */
|
---|
527 | bool isNotEmpty() const
|
---|
528 | {
|
---|
529 | return length() != 0;
|
---|
530 | }
|
---|
531 |
|
---|
532 | /** Case sensitivity selector. */
|
---|
533 | enum CaseSensitivity
|
---|
534 | {
|
---|
535 | CaseSensitive,
|
---|
536 | CaseInsensitive
|
---|
537 | };
|
---|
538 |
|
---|
539 | /**
|
---|
540 | * Compares the member string to a C-string.
|
---|
541 | *
|
---|
542 | * @param pcszThat The string to compare with.
|
---|
543 | * @param cs Whether comparison should be case-sensitive.
|
---|
544 | * @returns 0 if equal, negative if this is smaller than @a pcsz, positive
|
---|
545 | * if larger.
|
---|
546 | */
|
---|
547 | int compare(const char *pcszThat, CaseSensitivity cs = CaseSensitive) const
|
---|
548 | {
|
---|
549 | /* This klugde is for m_cch=0 and m_psz=NULL. pcsz=NULL and psz=""
|
---|
550 | are treated the same way so that str.compare(str2.c_str()) works. */
|
---|
551 | if (length() == 0)
|
---|
552 | return pcszThat == NULL || *pcszThat == '\0' ? 0 : -1;
|
---|
553 |
|
---|
554 | if (cs == CaseSensitive)
|
---|
555 | return ::RTStrCmp(m_psz, pcszThat);
|
---|
556 | return ::RTStrICmp(m_psz, pcszThat);
|
---|
557 | }
|
---|
558 |
|
---|
559 | /**
|
---|
560 | * Compares the member string to another MiniString.
|
---|
561 | *
|
---|
562 | * @param pcszThat The string to compare with.
|
---|
563 | * @param cs Whether comparison should be case-sensitive.
|
---|
564 | * @returns 0 if equal, negative if this is smaller than @a pcsz, positive
|
---|
565 | * if larger.
|
---|
566 | */
|
---|
567 | int compare(const MiniString &that, CaseSensitivity cs = CaseSensitive) const
|
---|
568 | {
|
---|
569 | if (cs == CaseSensitive)
|
---|
570 | return ::RTStrCmp(m_psz, that.m_psz);
|
---|
571 | return ::RTStrICmp(m_psz, that.m_psz);
|
---|
572 | }
|
---|
573 |
|
---|
574 | /**
|
---|
575 | * Compares the two strings.
|
---|
576 | *
|
---|
577 | * @returns true if equal, false if not.
|
---|
578 | * @param that The string to compare with.
|
---|
579 | */
|
---|
580 | bool equals(const MiniString &that) const
|
---|
581 | {
|
---|
582 | return that.length() == length()
|
---|
583 | && memcmp(that.m_psz, m_psz, length()) == 0;
|
---|
584 | }
|
---|
585 |
|
---|
586 | /**
|
---|
587 | * Compares the two strings.
|
---|
588 | *
|
---|
589 | * @returns true if equal, false if not.
|
---|
590 | * @param pszThat The string to compare with.
|
---|
591 | */
|
---|
592 | bool equals(const char *pszThat) const
|
---|
593 | {
|
---|
594 | /* This klugde is for m_cch=0 and m_psz=NULL. pcsz=NULL and psz=""
|
---|
595 | are treated the same way so that str.equals(str2.c_str()) works. */
|
---|
596 | if (length() == 0)
|
---|
597 | return pszThat == NULL || *pszThat == '\0';
|
---|
598 | return RTStrCmp(pszThat, m_psz) == 0;
|
---|
599 | }
|
---|
600 |
|
---|
601 | /**
|
---|
602 | * Compares the two strings ignoring differences in case.
|
---|
603 | *
|
---|
604 | * @returns true if equal, false if not.
|
---|
605 | * @param that The string to compare with.
|
---|
606 | */
|
---|
607 | bool equalsIgnoreCase(const MiniString &that) const
|
---|
608 | {
|
---|
609 | /* Unfolded upper and lower case characters may require different
|
---|
610 | amount of encoding space, so the length optimization doesn't work. */
|
---|
611 | return RTStrICmp(that.m_psz, m_psz) == 0;
|
---|
612 | }
|
---|
613 |
|
---|
614 | /**
|
---|
615 | * Compares the two strings ignoring differences in case.
|
---|
616 | *
|
---|
617 | * @returns true if equal, false if not.
|
---|
618 | * @param pszThat The string to compare with.
|
---|
619 | */
|
---|
620 | bool equalsIgnoreCase(const char *pszThat) const
|
---|
621 | {
|
---|
622 | /* This klugde is for m_cch=0 and m_psz=NULL. pcsz=NULL and psz=""
|
---|
623 | are treated the same way so that str.equalsIgnoreCase(str2.c_str()) works. */
|
---|
624 | if (length() == 0)
|
---|
625 | return pszThat == NULL || *pszThat == '\0';
|
---|
626 | return RTStrICmp(pszThat, m_psz) == 0;
|
---|
627 | }
|
---|
628 |
|
---|
629 | /** @name Comparison operators.
|
---|
630 | * @{ */
|
---|
631 | bool operator==(const MiniString &that) const { return equals(that); }
|
---|
632 | bool operator!=(const MiniString &that) const { return !equals(that); }
|
---|
633 | bool operator<( const MiniString &that) const { return compare(that) < 0; }
|
---|
634 | bool operator>( const MiniString &that) const { return compare(that) > 0; }
|
---|
635 |
|
---|
636 | bool operator==(const char *pszThat) const { return equals(pszThat); }
|
---|
637 | bool operator!=(const char *pszThat) const { return !equals(pszThat); }
|
---|
638 | bool operator<( const char *pszThat) const { return compare(pszThat) < 0; }
|
---|
639 | bool operator>( const char *pszThat) const { return compare(pszThat) > 0; }
|
---|
640 | /** @} */
|
---|
641 |
|
---|
642 | /** Max string offset value.
|
---|
643 | *
|
---|
644 | * When returned by a method, this indicates failure. When taken as input,
|
---|
645 | * typically a default, it means all the way to the string terminator.
|
---|
646 | */
|
---|
647 | static const size_t npos;
|
---|
648 |
|
---|
649 | /**
|
---|
650 | * Find the given substring.
|
---|
651 | *
|
---|
652 | * Looks for pcszFind in "this" starting at "pos" and returns its position,
|
---|
653 | * counting from the beginning of "this" at 0.
|
---|
654 | *
|
---|
655 | * @param pcszFind The substring to find.
|
---|
656 | * @param pos The (byte) offset into the string buffer to start
|
---|
657 | * searching.
|
---|
658 | *
|
---|
659 | * @returns 0 based position of pcszFind. npos if not found.
|
---|
660 | */
|
---|
661 | size_t find(const char *pcszFind, size_t pos = 0) const;
|
---|
662 |
|
---|
663 | /**
|
---|
664 | * Returns a substring of "this" as a new Utf8Str.
|
---|
665 | *
|
---|
666 | * Works exactly like its equivalent in std::string except that this interprets
|
---|
667 | * pos and n as unicode codepoints instead of bytes. With the default
|
---|
668 | * parameters "0" and "npos", this always copies the entire string.
|
---|
669 | *
|
---|
670 | * @param pos Index of first unicode codepoint to copy from
|
---|
671 | * "this", counting from 0.
|
---|
672 | * @param n Number of unicode codepoints to copy, starting with
|
---|
673 | * the one at "pos". The copying will stop if the null
|
---|
674 | * terminator is encountered before n codepoints have
|
---|
675 | * been copied.
|
---|
676 | *
|
---|
677 | * @remarks This works on code points, not bytes!
|
---|
678 | */
|
---|
679 | iprt::MiniString substr(size_t pos = 0, size_t n = npos) const;
|
---|
680 |
|
---|
681 | /**
|
---|
682 | * Returns true if "this" ends with "that".
|
---|
683 | *
|
---|
684 | * @param that Suffix to test for.
|
---|
685 | * @param cs Case sensitivity selector.
|
---|
686 | * @returns true if match, false if mismatch.
|
---|
687 | */
|
---|
688 | bool endsWith(const iprt::MiniString &that, CaseSensitivity cs = CaseSensitive) const;
|
---|
689 |
|
---|
690 | /**
|
---|
691 | * Returns true if "this" begins with "that".
|
---|
692 | * @param that Prefix to test for.
|
---|
693 | * @param cs Case sensitivity selector.
|
---|
694 | * @returns true if match, false if mismatch.
|
---|
695 | */
|
---|
696 | bool startsWith(const iprt::MiniString &that, CaseSensitivity cs = CaseSensitive) const;
|
---|
697 |
|
---|
698 | /**
|
---|
699 | * Returns true if "this" contains "that" (strstr).
|
---|
700 | *
|
---|
701 | * @param that Substring to look for.
|
---|
702 | * @param cs Case sensitivity selector.
|
---|
703 | * @returns true if match, false if mismatch.
|
---|
704 | */
|
---|
705 | bool contains(const iprt::MiniString &that, CaseSensitivity cs = CaseSensitive) const;
|
---|
706 |
|
---|
707 | /**
|
---|
708 | * Attempts to convert the member string into a 32-bit integer.
|
---|
709 | *
|
---|
710 | * @returns 32-bit unsigned number on success.
|
---|
711 | * @returns 0 on failure.
|
---|
712 | */
|
---|
713 | int32_t toInt32() const
|
---|
714 | {
|
---|
715 | return RTStrToInt32(m_psz);
|
---|
716 | }
|
---|
717 |
|
---|
718 | /**
|
---|
719 | * Attempts to convert the member string into an unsigned 32-bit integer.
|
---|
720 | *
|
---|
721 | * @returns 32-bit unsigned number on success.
|
---|
722 | * @returns 0 on failure.
|
---|
723 | */
|
---|
724 | uint32_t toUInt32() const
|
---|
725 | {
|
---|
726 | return RTStrToUInt32(m_psz);
|
---|
727 | }
|
---|
728 |
|
---|
729 | /**
|
---|
730 | * Attempts to convert the member string into an 64-bit integer.
|
---|
731 | *
|
---|
732 | * @returns 64-bit unsigned number on success.
|
---|
733 | * @returns 0 on failure.
|
---|
734 | */
|
---|
735 | int64_t toInt64() const
|
---|
736 | {
|
---|
737 | return RTStrToInt64(m_psz);
|
---|
738 | }
|
---|
739 |
|
---|
740 | /**
|
---|
741 | * Attempts to convert the member string into an unsigned 64-bit integer.
|
---|
742 | *
|
---|
743 | * @returns 64-bit unsigned number on success.
|
---|
744 | * @returns 0 on failure.
|
---|
745 | */
|
---|
746 | uint64_t toUInt64() const
|
---|
747 | {
|
---|
748 | return RTStrToUInt64(m_psz);
|
---|
749 | }
|
---|
750 |
|
---|
751 | /**
|
---|
752 | * Attempts to convert the member string into an unsigned 64-bit integer.
|
---|
753 | *
|
---|
754 | * @param i Where to return the value on success.
|
---|
755 | * @returns IPRT error code, see RTStrToInt64.
|
---|
756 | */
|
---|
757 | int toInt(uint64_t &i) const;
|
---|
758 |
|
---|
759 | /**
|
---|
760 | * Attempts to convert the member string into an unsigned 32-bit integer.
|
---|
761 | *
|
---|
762 | * @param i Where to return the value on success.
|
---|
763 | * @returns IPRT error code, see RTStrToInt32.
|
---|
764 | */
|
---|
765 | int toInt(uint32_t &i) const;
|
---|
766 |
|
---|
767 | protected:
|
---|
768 |
|
---|
769 | /**
|
---|
770 | * Hide operator bool() to force people to use isEmpty() explicitly.
|
---|
771 | */
|
---|
772 | operator bool() const;
|
---|
773 |
|
---|
774 | /**
|
---|
775 | * Destructor implementation, also used to clean up in operator=() before
|
---|
776 | * assigning a new string.
|
---|
777 | */
|
---|
778 | void cleanup()
|
---|
779 | {
|
---|
780 | if (m_psz)
|
---|
781 | {
|
---|
782 | RTStrFree(m_psz);
|
---|
783 | m_psz = NULL;
|
---|
784 | m_cch = 0;
|
---|
785 | m_cbAllocated = 0;
|
---|
786 | }
|
---|
787 | }
|
---|
788 |
|
---|
789 | /**
|
---|
790 | * Protected internal helper to copy a string.
|
---|
791 | *
|
---|
792 | * This ignores the previous object state, so either call this from a
|
---|
793 | * constructor or call cleanup() first. copyFromN() unconditionally sets
|
---|
794 | * the members to a copy of the given other strings and makes no
|
---|
795 | * assumptions about previous contents. Can therefore be used both in copy
|
---|
796 | * constructors, when member variables have no defined value, and in
|
---|
797 | * assignments after having called cleanup().
|
---|
798 | *
|
---|
799 | * @param pcszSrc The source string.
|
---|
800 | * @param cchSrc The number of chars (bytes) to copy from the
|
---|
801 | * source strings.
|
---|
802 | *
|
---|
803 | * @throws std::bad_alloc On allocation failure. The object is left
|
---|
804 | * describing a NULL string.
|
---|
805 | */
|
---|
806 | void copyFromN(const char *pcszSrc, size_t cchSrc)
|
---|
807 | {
|
---|
808 | if (cchSrc)
|
---|
809 | {
|
---|
810 | m_psz = RTStrAlloc(cchSrc + 1);
|
---|
811 | if (RT_LIKELY(m_psz))
|
---|
812 | {
|
---|
813 | m_cch = cchSrc;
|
---|
814 | m_cbAllocated = cchSrc + 1;
|
---|
815 | memcpy(m_psz, pcszSrc, cchSrc);
|
---|
816 | m_psz[cchSrc] = '\0';
|
---|
817 | }
|
---|
818 | else
|
---|
819 | {
|
---|
820 | m_cch = 0;
|
---|
821 | m_cbAllocated = 0;
|
---|
822 | #ifdef RT_EXCEPTIONS_ENABLED
|
---|
823 | throw std::bad_alloc();
|
---|
824 | #endif
|
---|
825 | }
|
---|
826 | }
|
---|
827 | else
|
---|
828 | {
|
---|
829 | m_cch = 0;
|
---|
830 | m_cbAllocated = 0;
|
---|
831 | m_psz = NULL;
|
---|
832 | }
|
---|
833 | }
|
---|
834 |
|
---|
835 | static DECLCALLBACK(size_t) printfOutputCallback(void *pvArg, const char *pachChars, size_t cbChars);
|
---|
836 |
|
---|
837 | char *m_psz; /**< The string buffer. */
|
---|
838 | size_t m_cch; /**< strlen(m_psz) - i.e. no terminator included. */
|
---|
839 | size_t m_cbAllocated; /**< Size of buffer that m_psz points to; at least m_cbLength + 1. */
|
---|
840 | };
|
---|
841 |
|
---|
842 | } // namespace iprt
|
---|
843 |
|
---|
844 | #endif
|
---|
845 |
|
---|