1 | /* $Id: ministring.cpp 69111 2017-10-17 14:26:02Z 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-2017 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 | *********************************************************************************************************************************/
|
---|
42 | const 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 |
|
---|
52 | RTCString &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 |
|
---|
68 | RTCString &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 |
|
---|
86 | RTCString &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 |
|
---|
104 | RTCString &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 |
|
---|
119 | RTCString &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 |
|
---|
127 | RTCString &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)
|
---|
146 | RTCString::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 |
|
---|
173 | RTCString &RTCString::printfV(const char *pszFormat, va_list va)
|
---|
174 | {
|
---|
175 | cleanup();
|
---|
176 | RTStrFormatV(printfOutputCallback, this, NULL, NULL, pszFormat, va);
|
---|
177 | return *this;
|
---|
178 | }
|
---|
179 |
|
---|
180 | RTCString &RTCString::append(const RTCString &that)
|
---|
181 | {
|
---|
182 | Assert(&that != this);
|
---|
183 | return appendWorker(that.c_str(), that.length());
|
---|
184 | }
|
---|
185 |
|
---|
186 | RTCString &RTCString::append(const char *pszThat)
|
---|
187 | {
|
---|
188 | return appendWorker(pszThat, strlen(pszThat));
|
---|
189 | }
|
---|
190 |
|
---|
191 | RTCString &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 |
|
---|
201 | RTCString &RTCString::append(const char *pszThat, size_t cchMax)
|
---|
202 | {
|
---|
203 | return appendWorker(pszThat, RTStrNLen(pszThat, cchMax));
|
---|
204 | }
|
---|
205 |
|
---|
206 | RTCString &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 |
|
---|
229 | RTCString &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 |
|
---|
250 | RTCString &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 | RTCString &RTCString::erase(size_t offStart /*= 0*/, size_t cchLength /*= npos*/)
|
---|
281 | {
|
---|
282 | size_t cch = length();
|
---|
283 | if (offStart < cch)
|
---|
284 | {
|
---|
285 | if (cchLength >= cch - offStart)
|
---|
286 | {
|
---|
287 | /* Trail removal, nothing to move. */
|
---|
288 | m_cch = offStart;
|
---|
289 | m_psz[offStart] = '\0';
|
---|
290 | }
|
---|
291 | else if (cchLength > 0)
|
---|
292 | {
|
---|
293 | /* Pull up the tail to offStart. */
|
---|
294 | size_t cchAfter = cch - offStart - cchLength;
|
---|
295 | memmove(&m_psz[offStart], &m_psz[offStart + cchLength], cchAfter);
|
---|
296 | m_cch = cch -= cchLength;
|
---|
297 | m_psz[cch] = '\0';
|
---|
298 | }
|
---|
299 | }
|
---|
300 | return *this;
|
---|
301 | }
|
---|
302 |
|
---|
303 | RTCString &RTCString::replace(size_t offStart, size_t cchLength, const RTCString &rStrReplacement)
|
---|
304 | {
|
---|
305 | return replaceWorker(offStart, cchLength, rStrReplacement.c_str(), rStrReplacement.length());
|
---|
306 | }
|
---|
307 |
|
---|
308 | RTCString &RTCString::replace(size_t offStart, size_t cchLength, const RTCString &rStrReplacement,
|
---|
309 | size_t offReplacement, size_t cchReplacement)
|
---|
310 | {
|
---|
311 | Assert(this != &rStrReplacement);
|
---|
312 | if (cchReplacement > 0)
|
---|
313 | {
|
---|
314 | if (offReplacement < rStrReplacement.length())
|
---|
315 | {
|
---|
316 | size_t cchMaxReplacement = rStrReplacement.length() - offReplacement;
|
---|
317 | return replaceWorker(offStart, cchLength, rStrReplacement.c_str() + offReplacement,
|
---|
318 | RT_MIN(cchReplacement, cchMaxReplacement));
|
---|
319 | }
|
---|
320 | /* Our non-standard handling of out_of_range situations. */
|
---|
321 | AssertMsgFailed(("offReplacement=%zu (cchReplacement=%zu) rStrReplacement.length()=%zu\n",
|
---|
322 | offReplacement, cchReplacement, rStrReplacement.length()));
|
---|
323 | }
|
---|
324 | return replaceWorker(offStart, cchLength, "", 0);
|
---|
325 | }
|
---|
326 |
|
---|
327 | RTCString &RTCString::replace(size_t offStart, size_t cchLength, const char *pszReplacement)
|
---|
328 | {
|
---|
329 | return replaceWorker(offStart, cchLength, pszReplacement, strlen(pszReplacement));
|
---|
330 | }
|
---|
331 |
|
---|
332 | RTCString &RTCString::replace(size_t offStart, size_t cchLength, const char *pszReplacement, size_t cchReplacement)
|
---|
333 | {
|
---|
334 | return replaceWorker(offStart, cchLength, pszReplacement, RTStrNLen(pszReplacement, cchReplacement));
|
---|
335 | }
|
---|
336 |
|
---|
337 | RTCString &RTCString::replaceWorker(size_t offStart, size_t cchLength, const char *pszSrc, size_t cchSrc)
|
---|
338 | {
|
---|
339 | /*
|
---|
340 | * Our non-standard handling of out_of_range situations.
|
---|
341 | */
|
---|
342 | size_t const cchOldLength = length();
|
---|
343 | AssertMsgReturn(offStart < cchOldLength, ("offStart=%zu (cchLength=%zu); length()=%zu\n", offStart, cchLength, cchOldLength),
|
---|
344 | *this);
|
---|
345 |
|
---|
346 | /*
|
---|
347 | * Correct the length parameter.
|
---|
348 | */
|
---|
349 | size_t cchMaxLength = cchOldLength - offStart;
|
---|
350 | if (cchMaxLength < cchLength)
|
---|
351 | cchLength = cchMaxLength;
|
---|
352 |
|
---|
353 | /*
|
---|
354 | * Adjust string allocation if necessary.
|
---|
355 | */
|
---|
356 | size_t cchNew = cchOldLength - cchLength + cchSrc;
|
---|
357 | if (cchNew >= m_cbAllocated)
|
---|
358 | {
|
---|
359 | reserve(RT_ALIGN_Z(cchNew + 1, IPRT_MINISTRING_APPEND_ALIGNMENT));
|
---|
360 | // calls realloc(cchBoth + 1) and sets m_cbAllocated; may throw bad_alloc.
|
---|
361 | #ifndef RT_EXCEPTIONS_ENABLED
|
---|
362 | AssertRelease(capacity() > cchNew);
|
---|
363 | #endif
|
---|
364 | }
|
---|
365 |
|
---|
366 | /*
|
---|
367 | * Make the change.
|
---|
368 | */
|
---|
369 | size_t cchAfter = cchOldLength - offStart - cchLength;
|
---|
370 | if (cchAfter > 0)
|
---|
371 | memmove(&m_psz[offStart + cchSrc], &m_psz[offStart + cchLength], cchAfter);
|
---|
372 | memcpy(&m_psz[offStart], pszSrc, cchSrc);
|
---|
373 | m_psz[cchNew] = '\0';
|
---|
374 | m_cch = cchNew;
|
---|
375 |
|
---|
376 | return *this;
|
---|
377 | }
|
---|
378 |
|
---|
379 |
|
---|
380 | size_t RTCString::find(const char *pszNeedle, size_t offStart /*= 0*/) const
|
---|
381 | {
|
---|
382 | if (offStart < length())
|
---|
383 | {
|
---|
384 | const char *pszThis = c_str();
|
---|
385 | if (pszThis)
|
---|
386 | {
|
---|
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 | return npos;
|
---|
397 | }
|
---|
398 |
|
---|
399 | size_t RTCString::find(const RTCString *pStrNeedle, size_t offStart /*= 0*/) const
|
---|
400 | {
|
---|
401 | if (offStart < length())
|
---|
402 | {
|
---|
403 | const char *pszThis = c_str();
|
---|
404 | if (pszThis)
|
---|
405 | {
|
---|
406 | if (pStrNeedle)
|
---|
407 | {
|
---|
408 | const char *pszNeedle = pStrNeedle->c_str();
|
---|
409 | if (pszNeedle && *pszNeedle != '\0')
|
---|
410 | {
|
---|
411 | const char *pszHit = strstr(pszThis + offStart, pszNeedle);
|
---|
412 | if (pszHit)
|
---|
413 | return pszHit - pszThis;
|
---|
414 | }
|
---|
415 | }
|
---|
416 | }
|
---|
417 | }
|
---|
418 |
|
---|
419 | return npos;
|
---|
420 | }
|
---|
421 |
|
---|
422 | void RTCString::findReplace(char chFind, char chReplace)
|
---|
423 | {
|
---|
424 | Assert((unsigned int)chFind < 128U);
|
---|
425 | Assert((unsigned int)chReplace < 128U);
|
---|
426 |
|
---|
427 | for (size_t i = 0; i < length(); ++i)
|
---|
428 | {
|
---|
429 | char *p = &m_psz[i];
|
---|
430 | if (*p == chFind)
|
---|
431 | *p = chReplace;
|
---|
432 | }
|
---|
433 | }
|
---|
434 |
|
---|
435 | size_t RTCString::count(char ch) const
|
---|
436 | {
|
---|
437 | Assert((unsigned int)ch < 128U);
|
---|
438 |
|
---|
439 | size_t c = 0;
|
---|
440 | const char *psz = m_psz;
|
---|
441 | if (psz)
|
---|
442 | {
|
---|
443 | char chCur;
|
---|
444 | while ((chCur = *psz++) != '\0')
|
---|
445 | if (chCur == ch)
|
---|
446 | c++;
|
---|
447 | }
|
---|
448 | return c;
|
---|
449 | }
|
---|
450 |
|
---|
451 | #if 0 /** @todo implement these when needed. */
|
---|
452 | size_t RTCString::count(const char *psz, CaseSensitivity cs = CaseSensitive) const
|
---|
453 | {
|
---|
454 | }
|
---|
455 |
|
---|
456 | size_t RTCString::count(const RTCString *pStr, CaseSensitivity cs = CaseSensitive) const
|
---|
457 | {
|
---|
458 |
|
---|
459 | }
|
---|
460 | #endif
|
---|
461 |
|
---|
462 |
|
---|
463 | RTCString &RTCString::strip()
|
---|
464 | {
|
---|
465 | stripRight();
|
---|
466 | return stripLeft();
|
---|
467 | }
|
---|
468 |
|
---|
469 |
|
---|
470 | RTCString &RTCString::stripLeft()
|
---|
471 | {
|
---|
472 | char *psz = m_psz;
|
---|
473 | size_t const cch = m_cch;
|
---|
474 | size_t off = 0;
|
---|
475 | while (off < cch && RT_C_IS_SPACE(psz[off]))
|
---|
476 | off++;
|
---|
477 | if (off > 0)
|
---|
478 | {
|
---|
479 | if (off != cch)
|
---|
480 | {
|
---|
481 | memmove(psz, &psz[off], cch - off + 1);
|
---|
482 | m_cch = cch - off;
|
---|
483 | }
|
---|
484 | else
|
---|
485 | setNull();
|
---|
486 | }
|
---|
487 | return *this;
|
---|
488 | }
|
---|
489 |
|
---|
490 |
|
---|
491 | RTCString &RTCString::stripRight()
|
---|
492 | {
|
---|
493 | char *psz = m_psz;
|
---|
494 | size_t cch = m_cch;
|
---|
495 | while (cch > 0 && RT_C_IS_SPACE(psz[cch - 1]))
|
---|
496 | cch--;
|
---|
497 | if (m_cch != cch)
|
---|
498 | {
|
---|
499 | m_cch = cch;
|
---|
500 | psz[cch] = '\0';
|
---|
501 | }
|
---|
502 | return *this;
|
---|
503 | }
|
---|
504 |
|
---|
505 |
|
---|
506 |
|
---|
507 | RTCString RTCString::substrCP(size_t pos /*= 0*/, size_t n /*= npos*/) const
|
---|
508 | {
|
---|
509 | RTCString ret;
|
---|
510 |
|
---|
511 | if (n)
|
---|
512 | {
|
---|
513 | const char *psz;
|
---|
514 |
|
---|
515 | if ((psz = c_str()))
|
---|
516 | {
|
---|
517 | RTUNICP cp;
|
---|
518 |
|
---|
519 | // walk the UTF-8 characters until where the caller wants to start
|
---|
520 | size_t i = pos;
|
---|
521 | while (*psz && i--)
|
---|
522 | if (RT_FAILURE(RTStrGetCpEx(&psz, &cp)))
|
---|
523 | return ret; // return empty string on bad encoding
|
---|
524 |
|
---|
525 | const char *pFirst = psz;
|
---|
526 |
|
---|
527 | if (n == npos)
|
---|
528 | // all the rest:
|
---|
529 | ret = pFirst;
|
---|
530 | else
|
---|
531 | {
|
---|
532 | i = n;
|
---|
533 | while (*psz && i--)
|
---|
534 | if (RT_FAILURE(RTStrGetCpEx(&psz, &cp)))
|
---|
535 | return ret; // return empty string on bad encoding
|
---|
536 |
|
---|
537 | size_t cbCopy = psz - pFirst;
|
---|
538 | if (cbCopy)
|
---|
539 | {
|
---|
540 | ret.reserve(cbCopy + 1); // may throw bad_alloc
|
---|
541 | #ifndef RT_EXCEPTIONS_ENABLED
|
---|
542 | AssertRelease(capacity() >= cbCopy + 1);
|
---|
543 | #endif
|
---|
544 | memcpy(ret.m_psz, pFirst, cbCopy);
|
---|
545 | ret.m_cch = cbCopy;
|
---|
546 | ret.m_psz[cbCopy] = '\0';
|
---|
547 | }
|
---|
548 | }
|
---|
549 | }
|
---|
550 | }
|
---|
551 |
|
---|
552 | return ret;
|
---|
553 | }
|
---|
554 |
|
---|
555 | bool RTCString::endsWith(const RTCString &that, CaseSensitivity cs /*= CaseSensitive*/) const
|
---|
556 | {
|
---|
557 | size_t l1 = length();
|
---|
558 | if (l1 == 0)
|
---|
559 | return false;
|
---|
560 |
|
---|
561 | size_t l2 = that.length();
|
---|
562 | if (l1 < l2)
|
---|
563 | return false;
|
---|
564 | /** @todo r=bird: If l2 is 0, then m_psz can be NULL and we will crash. See
|
---|
565 | * also handling of l2 == in startsWith. */
|
---|
566 |
|
---|
567 | size_t l = l1 - l2;
|
---|
568 | if (cs == CaseSensitive)
|
---|
569 | return ::RTStrCmp(&m_psz[l], that.m_psz) == 0;
|
---|
570 | return ::RTStrICmp(&m_psz[l], that.m_psz) == 0;
|
---|
571 | }
|
---|
572 |
|
---|
573 | bool RTCString::startsWith(const RTCString &that, CaseSensitivity cs /*= CaseSensitive*/) const
|
---|
574 | {
|
---|
575 | size_t l1 = length();
|
---|
576 | size_t l2 = that.length();
|
---|
577 | if (l1 == 0 || l2 == 0) /** @todo r=bird: this differs from endsWith, and I think other IPRT code. If l2 == 0, it matches anything. */
|
---|
578 | return false;
|
---|
579 |
|
---|
580 | if (l1 < l2)
|
---|
581 | return false;
|
---|
582 |
|
---|
583 | if (cs == CaseSensitive)
|
---|
584 | return ::RTStrNCmp(m_psz, that.m_psz, l2) == 0;
|
---|
585 | return ::RTStrNICmp(m_psz, that.m_psz, l2) == 0;
|
---|
586 | }
|
---|
587 |
|
---|
588 | bool RTCString::startsWithWord(const char *pszWord, CaseSensitivity enmCase /*= CaseSensitive*/) const
|
---|
589 | {
|
---|
590 | const char *pszSrc = RTStrStripL(c_str()); /** @todo RTStrStripL doesn't use RTUniCpIsSpace (nbsp) */
|
---|
591 | size_t cchWord = strlen(pszWord);
|
---|
592 | if ( enmCase == CaseSensitive
|
---|
593 | ? RTStrNCmp(pszSrc, pszWord, cchWord) == 0
|
---|
594 | : RTStrNICmp(pszSrc, pszWord, cchWord) == 0)
|
---|
595 | {
|
---|
596 | if ( pszSrc[cchWord] == '\0'
|
---|
597 | || RT_C_IS_SPACE(pszSrc[cchWord])
|
---|
598 | || RT_C_IS_PUNCT(pszSrc[cchWord]) )
|
---|
599 | return true;
|
---|
600 | RTUNICP uc = RTStrGetCp(&pszSrc[cchWord]);
|
---|
601 | if (RTUniCpIsSpace(uc))
|
---|
602 | return true;
|
---|
603 | }
|
---|
604 | return false;
|
---|
605 | }
|
---|
606 |
|
---|
607 | bool RTCString::startsWithWord(const RTCString &rThat, CaseSensitivity enmCase /*= CaseSensitive*/) const
|
---|
608 | {
|
---|
609 | return startsWithWord(rThat.c_str(), enmCase);
|
---|
610 | }
|
---|
611 |
|
---|
612 | bool RTCString::contains(const RTCString &that, CaseSensitivity cs /*= CaseSensitive*/) const
|
---|
613 | {
|
---|
614 | /** @todo r-bird: Not checking for NULL strings like startsWith does (and
|
---|
615 | * endsWith only does half way). */
|
---|
616 | if (cs == CaseSensitive)
|
---|
617 | return ::RTStrStr(m_psz, that.m_psz) != NULL;
|
---|
618 | return ::RTStrIStr(m_psz, that.m_psz) != NULL;
|
---|
619 | }
|
---|
620 |
|
---|
621 | bool RTCString::contains(const char *pszNeedle, CaseSensitivity cs /*= CaseSensitive*/) const
|
---|
622 | {
|
---|
623 | /** @todo r-bird: Not checking for NULL strings like startsWith does (and
|
---|
624 | * endsWith only does half way). */
|
---|
625 | if (cs == CaseSensitive)
|
---|
626 | return ::RTStrStr(m_psz, pszNeedle) != NULL;
|
---|
627 | return ::RTStrIStr(m_psz, pszNeedle) != NULL;
|
---|
628 | }
|
---|
629 |
|
---|
630 | int RTCString::toInt(uint64_t &i) const
|
---|
631 | {
|
---|
632 | if (!m_psz)
|
---|
633 | return VERR_NO_DIGITS;
|
---|
634 | return RTStrToUInt64Ex(m_psz, NULL, 0, &i);
|
---|
635 | }
|
---|
636 |
|
---|
637 | int RTCString::toInt(uint32_t &i) const
|
---|
638 | {
|
---|
639 | if (!m_psz)
|
---|
640 | return VERR_NO_DIGITS;
|
---|
641 | return RTStrToUInt32Ex(m_psz, NULL, 0, &i);
|
---|
642 | }
|
---|
643 |
|
---|
644 | RTCList<RTCString, RTCString *>
|
---|
645 | RTCString::split(const RTCString &a_rstrSep, SplitMode mode /* = RemoveEmptyParts */) const
|
---|
646 | {
|
---|
647 | RTCList<RTCString> strRet;
|
---|
648 | if (!m_psz)
|
---|
649 | return strRet;
|
---|
650 | if (a_rstrSep.isEmpty())
|
---|
651 | {
|
---|
652 | strRet.append(RTCString(m_psz));
|
---|
653 | return strRet;
|
---|
654 | }
|
---|
655 |
|
---|
656 | size_t cch = m_cch;
|
---|
657 | char const *pszTmp = m_psz;
|
---|
658 | while (cch > 0)
|
---|
659 | {
|
---|
660 | char const *pszNext = strstr(pszTmp, a_rstrSep.c_str());
|
---|
661 | if (!pszNext)
|
---|
662 | {
|
---|
663 | strRet.append(RTCString(pszTmp, cch));
|
---|
664 | break;
|
---|
665 | }
|
---|
666 | size_t cchNext = pszNext - pszTmp;
|
---|
667 | if ( cchNext > 0
|
---|
668 | || mode == KeepEmptyParts)
|
---|
669 | strRet.append(RTCString(pszTmp, cchNext));
|
---|
670 | pszTmp += cchNext + a_rstrSep.length();
|
---|
671 | cch -= cchNext + a_rstrSep.length();
|
---|
672 | }
|
---|
673 |
|
---|
674 | return strRet;
|
---|
675 | }
|
---|
676 |
|
---|
677 | /* static */
|
---|
678 | RTCString
|
---|
679 | RTCString::joinEx(const RTCList<RTCString, RTCString *> &a_rList,
|
---|
680 | const RTCString &a_rstrPrefix /* = "" */,
|
---|
681 | const RTCString &a_rstrSep /* = "" */)
|
---|
682 | {
|
---|
683 | RTCString strRet;
|
---|
684 | if (a_rList.size() > 1)
|
---|
685 | {
|
---|
686 | /* calc the required size */
|
---|
687 | size_t cbNeeded = a_rstrSep.length() * (a_rList.size() - 1) + 1;
|
---|
688 | cbNeeded += a_rstrPrefix.length() * (a_rList.size() - 1) + 1;
|
---|
689 | for (size_t i = 0; i < a_rList.size(); ++i)
|
---|
690 | cbNeeded += a_rList.at(i).length();
|
---|
691 | strRet.reserve(cbNeeded);
|
---|
692 |
|
---|
693 | /* do the appending. */
|
---|
694 | for (size_t i = 0; i < a_rList.size() - 1; ++i)
|
---|
695 | {
|
---|
696 | if (a_rstrPrefix.isNotEmpty())
|
---|
697 | strRet.append(a_rstrPrefix);
|
---|
698 | strRet.append(a_rList.at(i));
|
---|
699 | strRet.append(a_rstrSep);
|
---|
700 | }
|
---|
701 | strRet.append(a_rList.last());
|
---|
702 | }
|
---|
703 | /* special case: one list item. */
|
---|
704 | else if (a_rList.size() > 0)
|
---|
705 | {
|
---|
706 | if (a_rstrPrefix.isNotEmpty())
|
---|
707 | strRet.append(a_rstrPrefix);
|
---|
708 | strRet.append(a_rList.last());
|
---|
709 | }
|
---|
710 |
|
---|
711 | return strRet;
|
---|
712 | }
|
---|
713 |
|
---|
714 | /* static */
|
---|
715 | RTCString
|
---|
716 | RTCString::join(const RTCList<RTCString, RTCString *> &a_rList,
|
---|
717 | const RTCString &a_rstrSep /* = "" */)
|
---|
718 | {
|
---|
719 | return RTCString::joinEx(a_rList,
|
---|
720 | "" /* a_rstrPrefix */, a_rstrSep);
|
---|
721 | }
|
---|
722 |
|
---|
723 | const RTCString operator+(const RTCString &a_rStr1, const RTCString &a_rStr2)
|
---|
724 | {
|
---|
725 | RTCString strRet(a_rStr1);
|
---|
726 | strRet += a_rStr2;
|
---|
727 | return strRet;
|
---|
728 | }
|
---|
729 |
|
---|
730 | const RTCString operator+(const RTCString &a_rStr1, const char *a_pszStr2)
|
---|
731 | {
|
---|
732 | RTCString strRet(a_rStr1);
|
---|
733 | strRet += a_pszStr2;
|
---|
734 | return strRet;
|
---|
735 | }
|
---|
736 |
|
---|
737 | const RTCString operator+(const char *a_psz1, const RTCString &a_rStr2)
|
---|
738 | {
|
---|
739 | RTCString strRet(a_psz1);
|
---|
740 | strRet += a_rStr2;
|
---|
741 | return strRet;
|
---|
742 | }
|
---|
743 |
|
---|