VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/string/ministring.cpp@ 68125

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

RTCString: Added assign() methods. [bugfix]

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 20.3 KB
Line 
1/* $Id: ministring.cpp 68125 2017-07-26 15:43:54Z vboxsync $ */
2/** @file
3 * IPRT - Mini C++ string class.
4 *
5 * This is a base for both Utf8Str and other places where IPRT may want to use
6 * a lean C++ string class.
7 */
8
9/*
10 * Copyright (C) 2007-2016 Oracle Corporation
11 *
12 * This file is part of VirtualBox Open Source Edition (OSE), as
13 * available from http://www.virtualbox.org. This file is free software;
14 * you can redistribute it and/or modify it under the terms of the GNU
15 * General Public License (GPL) as published by the Free Software
16 * Foundation, in version 2 as it comes in the "COPYING" file of the
17 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
18 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
19 *
20 * The contents of this file may alternatively be used under the terms
21 * of the Common Development and Distribution License Version 1.0
22 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
23 * VirtualBox OSE distribution, in which case the provisions of the
24 * CDDL are applicable instead of those of the GPL.
25 *
26 * You may elect to license modified versions of this file under the
27 * terms and conditions of either the GPL or the CDDL or both.
28 */
29
30
31/*********************************************************************************************************************************
32* Header Files *
33*********************************************************************************************************************************/
34#include <iprt/cpp/ministring.h>
35#include <iprt/ctype.h>
36#include <iprt/uni.h>
37
38
39/*********************************************************************************************************************************
40* Global Variables *
41*********************************************************************************************************************************/
42const size_t RTCString::npos = ~(size_t)0;
43
44
45/*********************************************************************************************************************************
46* Defined Constants And Macros *
47*********************************************************************************************************************************/
48/** Allocation block alignment used when appending bytes to a string. */
49#define IPRT_MINISTRING_APPEND_ALIGNMENT 64
50
51
52RTCString &RTCString::assign(const RTCString &a_rSrc)
53{
54 size_t const cchSrc = a_rSrc.length();
55 if (cchSrc > 0)
56 {
57 reserve(cchSrc + 1);
58 memcpy(m_psz, a_rSrc.c_str(), cchSrc);
59 m_psz[cchSrc] = '\0';
60 m_cch = cchSrc;
61 return *this;
62 }
63 setNull();
64 return *this;
65
66}
67
68RTCString &RTCString::assign(const char *a_pszSrc)
69{
70 if (a_pszSrc)
71 {
72 size_t cchSrc = strlen(a_pszSrc);
73 if (cchSrc)
74 {
75 reserve(cchSrc + 1);
76 memcpy(m_psz, a_pszSrc, cchSrc);
77 m_psz[cchSrc] = '\0';
78 m_cch = cchSrc;
79 return *this;
80 }
81 }
82 setNull();
83 return *this;
84}
85
86RTCString &RTCString::assign(const RTCString &a_rSrc, size_t a_offSrc, size_t a_cchSrc /*= npos*/)
87{
88 AssertReturn(&a_rSrc != this, *this);
89 if (a_offSrc < a_rSrc.length())
90 {
91 size_t cchMax = a_rSrc.length() - a_offSrc;
92 if (a_cchSrc > cchMax)
93 a_cchSrc = cchMax;
94 reserve(a_cchSrc + 1);
95 memcpy(m_psz, a_rSrc.c_str() + a_offSrc, a_cchSrc);
96 m_psz[a_cchSrc] = '\0';
97 m_cch = a_cchSrc;
98 }
99 else
100 setNull();
101 return *this;
102}
103
104RTCString &RTCString::assign(const char *a_pszSrc, size_t a_cchSrc)
105{
106 if (a_cchSrc)
107 {
108 a_cchSrc = RTStrNLen(a_pszSrc, a_cchSrc);
109 reserve(a_cchSrc + 1);
110 memcpy(m_psz, a_pszSrc, a_cchSrc);
111 m_psz[a_cchSrc] = '\0';
112 m_cch = a_cchSrc;
113 }
114 else
115 setNull();
116 return *this;
117}
118
119RTCString &RTCString::assign(size_t a_cTimes, char a_ch)
120{
121 reserve(a_cTimes + 1);
122 memset(m_psz, a_ch, a_cTimes);
123 return *this;
124}
125
126
127RTCString &RTCString::printf(const char *pszFormat, ...)
128{
129 va_list va;
130 va_start(va, pszFormat);
131 printfV(pszFormat, va);
132 va_end(va);
133 return *this;
134}
135
136/**
137 * Callback used with RTStrFormatV by RTCString::printfV.
138 *
139 * @returns The number of bytes added (not used).
140 *
141 * @param pvArg The string object.
142 * @param pachChars The characters to append.
143 * @param cbChars The number of characters. 0 on the final callback.
144 */
145/*static*/ DECLCALLBACK(size_t)
146RTCString::printfOutputCallback(void *pvArg, const char *pachChars, size_t cbChars)
147{
148 RTCString *pThis = (RTCString *)pvArg;
149 if (cbChars)
150 {
151 size_t cchBoth = pThis->m_cch + cbChars;
152 if (cchBoth >= pThis->m_cbAllocated)
153 {
154 /* Double the buffer size, if it's less that _4M. Align sizes like
155 for append. */
156 size_t cbAlloc = RT_ALIGN_Z(pThis->m_cbAllocated, IPRT_MINISTRING_APPEND_ALIGNMENT);
157 cbAlloc += RT_MIN(cbAlloc, _4M);
158 if (cbAlloc <= cchBoth)
159 cbAlloc = RT_ALIGN_Z(cchBoth + 1, IPRT_MINISTRING_APPEND_ALIGNMENT);
160 pThis->reserve(cbAlloc);
161#ifndef RT_EXCEPTIONS_ENABLED
162 AssertReleaseReturn(pThis->capacity() > cchBoth, 0);
163#endif
164 }
165
166 memcpy(&pThis->m_psz[pThis->m_cch], pachChars, cbChars);
167 pThis->m_cch = cchBoth;
168 pThis->m_psz[cchBoth] = '\0';
169 }
170 return cbChars;
171}
172
173RTCString &RTCString::printfV(const char *pszFormat, va_list va)
174{
175 cleanup();
176 RTStrFormatV(printfOutputCallback, this, NULL, NULL, pszFormat, va);
177 return *this;
178}
179
180RTCString &RTCString::append(const RTCString &that)
181{
182 Assert(&that != this);
183 return appendWorker(that.c_str(), that.length());
184}
185
186RTCString &RTCString::append(const char *pszThat)
187{
188 return appendWorker(pszThat, strlen(pszThat));
189}
190
191RTCString &RTCString::append(const RTCString &rThat, size_t offStart, size_t cchMax /*= RTSTR_MAX*/)
192{
193 if (offStart < rThat.length())
194 {
195 size_t cchLeft = rThat.length() - offStart;
196 return appendWorker(rThat.c_str() + offStart, RT_MIN(cchLeft, cchMax));
197 }
198 return *this;
199}
200
201RTCString &RTCString::append(const char *pszThat, size_t cchMax)
202{
203 return appendWorker(pszThat, RTStrNLen(pszThat, cchMax));
204}
205
206RTCString &RTCString::appendWorker(const char *pszSrc, size_t cchSrc)
207{
208 if (cchSrc)
209 {
210 size_t cchThis = length();
211 size_t cchBoth = cchThis + cchSrc;
212
213 if (cchBoth >= m_cbAllocated)
214 {
215 reserve(RT_ALIGN_Z(cchBoth + 1, IPRT_MINISTRING_APPEND_ALIGNMENT));
216 // calls realloc(cchBoth + 1) and sets m_cbAllocated; may throw bad_alloc.
217#ifndef RT_EXCEPTIONS_ENABLED
218 AssertRelease(capacity() > cchBoth);
219#endif
220 }
221
222 memcpy(&m_psz[cchThis], pszSrc, cchSrc);
223 m_psz[cchBoth] = '\0';
224 m_cch = cchBoth;
225 }
226 return *this;
227}
228
229RTCString &RTCString::append(char ch)
230{
231 Assert((unsigned char)ch < 0x80); /* Don't create invalid UTF-8. */
232 if (ch)
233 {
234 // allocate in chunks of 20 in case this gets called several times
235 if (m_cch + 1 >= m_cbAllocated)
236 {
237 reserve(RT_ALIGN_Z(m_cch + 2, IPRT_MINISTRING_APPEND_ALIGNMENT));
238 // calls realloc(cbBoth) and sets m_cbAllocated; may throw bad_alloc.
239#ifndef RT_EXCEPTIONS_ENABLED
240 AssertRelease(capacity() > m_cch + 1);
241#endif
242 }
243
244 m_psz[m_cch] = ch;
245 m_psz[++m_cch] = '\0';
246 }
247 return *this;
248}
249
250RTCString &RTCString::appendCodePoint(RTUNICP uc)
251{
252 /*
253 * Single byte encoding.
254 */
255 if (uc < 0x80)
256 return RTCString::append((char)uc);
257
258 /*
259 * Multibyte encoding.
260 * Assume max encoding length when resizing the string, that's simpler.
261 */
262 AssertReturn(uc <= UINT32_C(0x7fffffff), *this);
263
264 if (m_cch + 6 >= m_cbAllocated)
265 {
266 reserve(RT_ALIGN_Z(m_cch + 6 + 1, IPRT_MINISTRING_APPEND_ALIGNMENT));
267 // calls realloc(cbBoth) and sets m_cbAllocated; may throw bad_alloc.
268#ifndef RT_EXCEPTIONS_ENABLED
269 AssertRelease(capacity() > m_cch + 6);
270#endif
271 }
272
273 char *pszNext = RTStrPutCp(&m_psz[m_cch], uc);
274 m_cch = pszNext - m_psz;
275 *pszNext = '\0';
276
277 return *this;
278}
279
280
281RTCString &RTCString::replace(size_t offStart, size_t cchLength, const RTCString &rStrReplacement)
282{
283 return replaceWorker(offStart, cchLength, rStrReplacement.c_str(), rStrReplacement.length());
284}
285
286RTCString &RTCString::replace(size_t offStart, size_t cchLength, const RTCString &rStrReplacement,
287 size_t offReplacement, size_t cchReplacement)
288{
289 Assert(this != &rStrReplacement);
290 if (cchReplacement > 0)
291 {
292 if (offReplacement < rStrReplacement.length())
293 {
294 size_t cchMaxReplacement = rStrReplacement.length() - offReplacement;
295 return replaceWorker(offStart, cchLength, rStrReplacement.c_str() + offReplacement,
296 RT_MIN(cchReplacement, cchMaxReplacement));
297 }
298 /* Our non-standard handling of out_of_range situations. */
299 AssertMsgFailed(("offReplacement=%zu (cchReplacement=%zu) rStrReplacement.length()=%zu\n",
300 offReplacement, cchReplacement, rStrReplacement.length()));
301 }
302 return replaceWorker(offStart, cchLength, "", 0);
303}
304
305RTCString &RTCString::replace(size_t offStart, size_t cchLength, const char *pszReplacement)
306{
307 return replaceWorker(offStart, cchLength, pszReplacement, strlen(pszReplacement));
308}
309
310RTCString &RTCString::replace(size_t offStart, size_t cchLength, const char *pszReplacement, size_t cchReplacement)
311{
312 return replaceWorker(offStart, cchLength, pszReplacement, RTStrNLen(pszReplacement, cchReplacement));
313}
314
315RTCString &RTCString::replaceWorker(size_t offStart, size_t cchLength, const char *pszSrc, size_t cchSrc)
316{
317 /*
318 * Our non-standard handling of out_of_range situations.
319 */
320 size_t const cchOldLength = length();
321 AssertMsgReturn(offStart < cchOldLength, ("offStart=%zu (cchLength=%zu); length()=%zu\n", offStart, cchLength, cchOldLength),
322 *this);
323
324 /*
325 * Correct the length parameter.
326 */
327 size_t cchMaxLength = cchOldLength - offStart;
328 if (cchMaxLength < cchLength)
329 cchLength = cchMaxLength;
330
331 /*
332 * Adjust string allocation if necessary.
333 */
334 size_t cchNew = cchOldLength - cchLength + cchSrc;
335 if (cchNew >= m_cbAllocated)
336 {
337 reserve(RT_ALIGN_Z(cchNew + 1, IPRT_MINISTRING_APPEND_ALIGNMENT));
338 // calls realloc(cchBoth + 1) and sets m_cbAllocated; may throw bad_alloc.
339#ifndef RT_EXCEPTIONS_ENABLED
340 AssertRelease(capacity() > cchNew);
341#endif
342 }
343
344 /*
345 * Make the change.
346 */
347 size_t cchAfter = cchOldLength - offStart - cchLength;
348 if (cchAfter > 0)
349 memmove(&m_psz[offStart + cchSrc], &m_psz[offStart + cchLength], cchAfter);
350 memcpy(&m_psz[offStart], pszSrc, cchSrc);
351 m_psz[cchNew] = '\0';
352 m_cch = cchNew;
353
354 return *this;
355}
356
357
358size_t RTCString::find(const char *pszNeedle, size_t offStart /*= 0*/) const
359{
360 if (offStart < length())
361 {
362 const char *pszThis = c_str();
363 if (pszThis)
364 {
365 if (pszNeedle && *pszNeedle != '\0')
366 {
367 const char *pszHit = strstr(pszThis + offStart, pszNeedle);
368 if (pszHit)
369 return pszHit - pszThis;
370 }
371 }
372 }
373
374 return npos;
375}
376
377size_t RTCString::find(const RTCString *pStrNeedle, size_t offStart /*= 0*/) const
378{
379 if (offStart < length())
380 {
381 const char *pszThis = c_str();
382 if (pszThis)
383 {
384 if (pStrNeedle)
385 {
386 const char *pszNeedle = pStrNeedle->c_str();
387 if (pszNeedle && *pszNeedle != '\0')
388 {
389 const char *pszHit = strstr(pszThis + offStart, pszNeedle);
390 if (pszHit)
391 return pszHit - pszThis;
392 }
393 }
394 }
395 }
396
397 return npos;
398}
399
400void RTCString::findReplace(char chFind, char chReplace)
401{
402 Assert((unsigned int)chFind < 128U);
403 Assert((unsigned int)chReplace < 128U);
404
405 for (size_t i = 0; i < length(); ++i)
406 {
407 char *p = &m_psz[i];
408 if (*p == chFind)
409 *p = chReplace;
410 }
411}
412
413size_t RTCString::count(char ch) const
414{
415 Assert((unsigned int)ch < 128U);
416
417 size_t c = 0;
418 const char *psz = m_psz;
419 if (psz)
420 {
421 char chCur;
422 while ((chCur = *psz++) != '\0')
423 if (chCur == ch)
424 c++;
425 }
426 return c;
427}
428
429#if 0 /** @todo implement these when needed. */
430size_t RTCString::count(const char *psz, CaseSensitivity cs = CaseSensitive) const
431{
432}
433
434size_t RTCString::count(const RTCString *pStr, CaseSensitivity cs = CaseSensitive) const
435{
436
437}
438#endif
439
440
441RTCString &RTCString::strip()
442{
443 stripRight();
444 return stripLeft();
445}
446
447
448RTCString &RTCString::stripLeft()
449{
450 char *psz = m_psz;
451 size_t const cch = m_cch;
452 size_t off = 0;
453 while (off < cch && RT_C_IS_SPACE(psz[off]))
454 off++;
455 if (off > 0)
456 {
457 if (off != cch)
458 {
459 memmove(psz, &psz[off], cch - off + 1);
460 m_cch = cch - off;
461 }
462 else
463 setNull();
464 }
465 return *this;
466}
467
468
469RTCString &RTCString::stripRight()
470{
471 char *psz = m_psz;
472 size_t cch = m_cch;
473 while (cch > 0 && RT_C_IS_SPACE(psz[cch - 1]))
474 cch--;
475 if (m_cch != cch)
476 {
477 m_cch = cch;
478 psz[cch] = '\0';
479 }
480 return *this;
481}
482
483
484
485RTCString RTCString::substrCP(size_t pos /*= 0*/, size_t n /*= npos*/) const
486{
487 RTCString ret;
488
489 if (n)
490 {
491 const char *psz;
492
493 if ((psz = c_str()))
494 {
495 RTUNICP cp;
496
497 // walk the UTF-8 characters until where the caller wants to start
498 size_t i = pos;
499 while (*psz && i--)
500 if (RT_FAILURE(RTStrGetCpEx(&psz, &cp)))
501 return ret; // return empty string on bad encoding
502
503 const char *pFirst = psz;
504
505 if (n == npos)
506 // all the rest:
507 ret = pFirst;
508 else
509 {
510 i = n;
511 while (*psz && i--)
512 if (RT_FAILURE(RTStrGetCpEx(&psz, &cp)))
513 return ret; // return empty string on bad encoding
514
515 size_t cbCopy = psz - pFirst;
516 if (cbCopy)
517 {
518 ret.reserve(cbCopy + 1); // may throw bad_alloc
519#ifndef RT_EXCEPTIONS_ENABLED
520 AssertRelease(capacity() >= cbCopy + 1);
521#endif
522 memcpy(ret.m_psz, pFirst, cbCopy);
523 ret.m_cch = cbCopy;
524 ret.m_psz[cbCopy] = '\0';
525 }
526 }
527 }
528 }
529
530 return ret;
531}
532
533bool RTCString::endsWith(const RTCString &that, CaseSensitivity cs /*= CaseSensitive*/) const
534{
535 size_t l1 = length();
536 if (l1 == 0)
537 return false;
538
539 size_t l2 = that.length();
540 if (l1 < l2)
541 return false;
542 /** @todo r=bird: If l2 is 0, then m_psz can be NULL and we will crash. See
543 * also handling of l2 == in startsWith. */
544
545 size_t l = l1 - l2;
546 if (cs == CaseSensitive)
547 return ::RTStrCmp(&m_psz[l], that.m_psz) == 0;
548 return ::RTStrICmp(&m_psz[l], that.m_psz) == 0;
549}
550
551bool RTCString::startsWith(const RTCString &that, CaseSensitivity cs /*= CaseSensitive*/) const
552{
553 size_t l1 = length();
554 size_t l2 = that.length();
555 if (l1 == 0 || l2 == 0) /** @todo r=bird: this differs from endsWith, and I think other IPRT code. If l2 == 0, it matches anything. */
556 return false;
557
558 if (l1 < l2)
559 return false;
560
561 if (cs == CaseSensitive)
562 return ::RTStrNCmp(m_psz, that.m_psz, l2) == 0;
563 return ::RTStrNICmp(m_psz, that.m_psz, l2) == 0;
564}
565
566bool RTCString::startsWithWord(const char *pszWord, CaseSensitivity enmCase /*= CaseSensitive*/) const
567{
568 const char *pszSrc = RTStrStripL(c_str()); /** @todo RTStrStripL doesn't use RTUniCpIsSpace (nbsp) */
569 size_t cchWord = strlen(pszWord);
570 if ( enmCase == CaseSensitive
571 ? RTStrNCmp(pszSrc, pszWord, cchWord) == 0
572 : RTStrNICmp(pszSrc, pszWord, cchWord) == 0)
573 {
574 if ( pszSrc[cchWord] == '\0'
575 || RT_C_IS_SPACE(pszSrc[cchWord])
576 || RT_C_IS_PUNCT(pszSrc[cchWord]) )
577 return true;
578 RTUNICP uc = RTStrGetCp(&pszSrc[cchWord]);
579 if (RTUniCpIsSpace(uc))
580 return true;
581 }
582 return false;
583}
584
585bool RTCString::startsWithWord(const RTCString &rThat, CaseSensitivity enmCase /*= CaseSensitive*/) const
586{
587 return startsWithWord(rThat.c_str(), enmCase);
588}
589
590bool RTCString::contains(const RTCString &that, CaseSensitivity cs /*= CaseSensitive*/) const
591{
592 /** @todo r-bird: Not checking for NULL strings like startsWith does (and
593 * endsWith only does half way). */
594 if (cs == CaseSensitive)
595 return ::RTStrStr(m_psz, that.m_psz) != NULL;
596 return ::RTStrIStr(m_psz, that.m_psz) != NULL;
597}
598
599bool RTCString::contains(const char *pszNeedle, CaseSensitivity cs /*= CaseSensitive*/) const
600{
601 /** @todo r-bird: Not checking for NULL strings like startsWith does (and
602 * endsWith only does half way). */
603 if (cs == CaseSensitive)
604 return ::RTStrStr(m_psz, pszNeedle) != NULL;
605 return ::RTStrIStr(m_psz, pszNeedle) != NULL;
606}
607
608int RTCString::toInt(uint64_t &i) const
609{
610 if (!m_psz)
611 return VERR_NO_DIGITS;
612 return RTStrToUInt64Ex(m_psz, NULL, 0, &i);
613}
614
615int RTCString::toInt(uint32_t &i) const
616{
617 if (!m_psz)
618 return VERR_NO_DIGITS;
619 return RTStrToUInt32Ex(m_psz, NULL, 0, &i);
620}
621
622RTCList<RTCString, RTCString *>
623RTCString::split(const RTCString &a_rstrSep, SplitMode mode /* = RemoveEmptyParts */) const
624{
625 RTCList<RTCString> strRet;
626 if (!m_psz)
627 return strRet;
628 if (a_rstrSep.isEmpty())
629 {
630 strRet.append(RTCString(m_psz));
631 return strRet;
632 }
633
634 size_t cch = m_cch;
635 char const *pszTmp = m_psz;
636 while (cch > 0)
637 {
638 char const *pszNext = strstr(pszTmp, a_rstrSep.c_str());
639 if (!pszNext)
640 {
641 strRet.append(RTCString(pszTmp, cch));
642 break;
643 }
644 size_t cchNext = pszNext - pszTmp;
645 if ( cchNext > 0
646 || mode == KeepEmptyParts)
647 strRet.append(RTCString(pszTmp, cchNext));
648 pszTmp += cchNext + a_rstrSep.length();
649 cch -= cchNext + a_rstrSep.length();
650 }
651
652 return strRet;
653}
654
655/* static */
656RTCString
657RTCString::joinEx(const RTCList<RTCString, RTCString *> &a_rList,
658 const RTCString &a_rstrPrefix /* = "" */,
659 const RTCString &a_rstrSep /* = "" */)
660{
661 RTCString strRet;
662 if (a_rList.size() > 1)
663 {
664 /* calc the required size */
665 size_t cbNeeded = a_rstrSep.length() * (a_rList.size() - 1) + 1;
666 cbNeeded += a_rstrPrefix.length() * (a_rList.size() - 1) + 1;
667 for (size_t i = 0; i < a_rList.size(); ++i)
668 cbNeeded += a_rList.at(i).length();
669 strRet.reserve(cbNeeded);
670
671 /* do the appending. */
672 for (size_t i = 0; i < a_rList.size() - 1; ++i)
673 {
674 if (a_rstrPrefix.isNotEmpty())
675 strRet.append(a_rstrPrefix);
676 strRet.append(a_rList.at(i));
677 strRet.append(a_rstrSep);
678 }
679 strRet.append(a_rList.last());
680 }
681 /* special case: one list item. */
682 else if (a_rList.size() > 0)
683 {
684 if (a_rstrPrefix.isNotEmpty())
685 strRet.append(a_rstrPrefix);
686 strRet.append(a_rList.last());
687 }
688
689 return strRet;
690}
691
692/* static */
693RTCString
694RTCString::join(const RTCList<RTCString, RTCString *> &a_rList,
695 const RTCString &a_rstrSep /* = "" */)
696{
697 return RTCString::joinEx(a_rList,
698 "" /* a_rstrPrefix */, a_rstrSep);
699}
700
701const RTCString operator+(const RTCString &a_rStr1, const RTCString &a_rStr2)
702{
703 RTCString strRet(a_rStr1);
704 strRet += a_rStr2;
705 return strRet;
706}
707
708const RTCString operator+(const RTCString &a_rStr1, const char *a_pszStr2)
709{
710 RTCString strRet(a_rStr1);
711 strRet += a_pszStr2;
712 return strRet;
713}
714
715const RTCString operator+(const char *a_psz1, const RTCString &a_rStr2)
716{
717 RTCString strRet(a_psz1);
718 strRet += a_rStr2;
719 return strRet;
720}
721
Note: See TracBrowser for help on using the repository browser.

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