1 | /* $Id: ministring.cpp 74262 2018-09-14 12:49:31Z 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 | int RTCString::assignNoThrow(const RTCString &a_rSrc) RT_NOEXCEPT
|
---|
69 | {
|
---|
70 | AssertReturn(&a_rSrc != this, VINF_SUCCESS);
|
---|
71 | size_t const cchSrc = a_rSrc.length();
|
---|
72 | if (cchSrc > 0)
|
---|
73 | {
|
---|
74 | int rc = reserveNoThrow(cchSrc + 1);
|
---|
75 | if (RT_SUCCESS(rc))
|
---|
76 | {
|
---|
77 | memcpy(m_psz, a_rSrc.c_str(), cchSrc);
|
---|
78 | m_psz[cchSrc] = '\0';
|
---|
79 | m_cch = cchSrc;
|
---|
80 | return VINF_SUCCESS;
|
---|
81 | }
|
---|
82 | return rc;
|
---|
83 | }
|
---|
84 | setNull();
|
---|
85 | return VINF_SUCCESS;
|
---|
86 |
|
---|
87 | }
|
---|
88 |
|
---|
89 | RTCString &RTCString::assign(const char *a_pszSrc)
|
---|
90 | {
|
---|
91 | if (a_pszSrc)
|
---|
92 | {
|
---|
93 | size_t cchSrc = strlen(a_pszSrc);
|
---|
94 | if (cchSrc)
|
---|
95 | {
|
---|
96 | reserve(cchSrc + 1);
|
---|
97 | memcpy(m_psz, a_pszSrc, cchSrc);
|
---|
98 | m_psz[cchSrc] = '\0';
|
---|
99 | m_cch = cchSrc;
|
---|
100 | return *this;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | setNull();
|
---|
104 | return *this;
|
---|
105 | }
|
---|
106 |
|
---|
107 | int RTCString::assignNoThrow(const char *a_pszSrc) RT_NOEXCEPT
|
---|
108 | {
|
---|
109 | if (a_pszSrc)
|
---|
110 | {
|
---|
111 | size_t cchSrc = strlen(a_pszSrc);
|
---|
112 | if (cchSrc)
|
---|
113 | {
|
---|
114 | int rc = reserveNoThrow(cchSrc + 1);
|
---|
115 | if (RT_SUCCESS(rc))
|
---|
116 | {
|
---|
117 | memcpy(m_psz, a_pszSrc, cchSrc);
|
---|
118 | m_psz[cchSrc] = '\0';
|
---|
119 | m_cch = cchSrc;
|
---|
120 | return VINF_SUCCESS;
|
---|
121 | }
|
---|
122 | return rc;
|
---|
123 | }
|
---|
124 | }
|
---|
125 | setNull();
|
---|
126 | return VINF_SUCCESS;
|
---|
127 | }
|
---|
128 |
|
---|
129 | RTCString &RTCString::assign(const RTCString &a_rSrc, size_t a_offSrc, size_t a_cchSrc /*= npos*/)
|
---|
130 | {
|
---|
131 | AssertReturn(&a_rSrc != this, *this);
|
---|
132 | if (a_offSrc < a_rSrc.length())
|
---|
133 | {
|
---|
134 | size_t cchMax = a_rSrc.length() - a_offSrc;
|
---|
135 | if (a_cchSrc > cchMax)
|
---|
136 | a_cchSrc = cchMax;
|
---|
137 | reserve(a_cchSrc + 1);
|
---|
138 | memcpy(m_psz, a_rSrc.c_str() + a_offSrc, a_cchSrc);
|
---|
139 | m_psz[a_cchSrc] = '\0';
|
---|
140 | m_cch = a_cchSrc;
|
---|
141 | }
|
---|
142 | else
|
---|
143 | setNull();
|
---|
144 | return *this;
|
---|
145 | }
|
---|
146 |
|
---|
147 | int RTCString::assignNoThrow(const RTCString &a_rSrc, size_t a_offSrc, size_t a_cchSrc /*= npos*/) RT_NOEXCEPT
|
---|
148 | {
|
---|
149 | AssertReturn(&a_rSrc != this, VINF_SUCCESS);
|
---|
150 | if (a_offSrc < a_rSrc.length())
|
---|
151 | {
|
---|
152 | size_t cchMax = a_rSrc.length() - a_offSrc;
|
---|
153 | if (a_cchSrc > cchMax)
|
---|
154 | a_cchSrc = cchMax;
|
---|
155 | int rc = reserveNoThrow(a_cchSrc + 1);
|
---|
156 | if (RT_SUCCESS(rc))
|
---|
157 | {
|
---|
158 | memcpy(m_psz, a_rSrc.c_str() + a_offSrc, a_cchSrc);
|
---|
159 | m_psz[a_cchSrc] = '\0';
|
---|
160 | m_cch = a_cchSrc;
|
---|
161 | return VINF_SUCCESS;
|
---|
162 | }
|
---|
163 | return rc;
|
---|
164 | }
|
---|
165 | setNull();
|
---|
166 | return VINF_SUCCESS;
|
---|
167 | }
|
---|
168 |
|
---|
169 | RTCString &RTCString::assign(const char *a_pszSrc, size_t a_cchSrc)
|
---|
170 | {
|
---|
171 | if (a_cchSrc)
|
---|
172 | {
|
---|
173 | a_cchSrc = RTStrNLen(a_pszSrc, a_cchSrc);
|
---|
174 | reserve(a_cchSrc + 1);
|
---|
175 | memcpy(m_psz, a_pszSrc, a_cchSrc);
|
---|
176 | m_psz[a_cchSrc] = '\0';
|
---|
177 | m_cch = a_cchSrc;
|
---|
178 | }
|
---|
179 | else
|
---|
180 | setNull();
|
---|
181 | return *this;
|
---|
182 | }
|
---|
183 |
|
---|
184 | int RTCString::assignNoThrow(const char *a_pszSrc, size_t a_cchSrc) RT_NOEXCEPT
|
---|
185 | {
|
---|
186 | if (a_cchSrc)
|
---|
187 | {
|
---|
188 | a_cchSrc = RTStrNLen(a_pszSrc, a_cchSrc);
|
---|
189 | int rc = reserveNoThrow(a_cchSrc + 1);
|
---|
190 | if (RT_SUCCESS(rc))
|
---|
191 | {
|
---|
192 | memcpy(m_psz, a_pszSrc, a_cchSrc);
|
---|
193 | m_psz[a_cchSrc] = '\0';
|
---|
194 | m_cch = a_cchSrc;
|
---|
195 | return VINF_SUCCESS;
|
---|
196 | }
|
---|
197 | return rc;
|
---|
198 | }
|
---|
199 | setNull();
|
---|
200 | return VINF_SUCCESS;
|
---|
201 | }
|
---|
202 |
|
---|
203 | RTCString &RTCString::assign(size_t a_cTimes, char a_ch)
|
---|
204 | {
|
---|
205 | reserve(a_cTimes + 1);
|
---|
206 | memset(m_psz, a_ch, a_cTimes);
|
---|
207 | m_psz[a_cTimes] = '\0';
|
---|
208 | m_cch = a_cTimes;
|
---|
209 | return *this;
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | int RTCString::assignNoThrow(size_t a_cTimes, char a_ch) RT_NOEXCEPT
|
---|
214 | {
|
---|
215 | int rc = reserveNoThrow(a_cTimes + 1);
|
---|
216 | if (RT_SUCCESS(rc))
|
---|
217 | {
|
---|
218 | memset(m_psz, a_ch, a_cTimes);
|
---|
219 | m_psz[a_cTimes] = '\0';
|
---|
220 | m_cch = a_cTimes;
|
---|
221 | return VINF_SUCCESS;
|
---|
222 | }
|
---|
223 | return rc;
|
---|
224 | }
|
---|
225 |
|
---|
226 |
|
---|
227 | RTCString &RTCString::printf(const char *pszFormat, ...)
|
---|
228 | {
|
---|
229 | va_list va;
|
---|
230 | va_start(va, pszFormat);
|
---|
231 | printfV(pszFormat, va);
|
---|
232 | va_end(va);
|
---|
233 | return *this;
|
---|
234 | }
|
---|
235 |
|
---|
236 | int RTCString::printfNoThrow(const char *pszFormat, ...) RT_NOEXCEPT
|
---|
237 | {
|
---|
238 | va_list va;
|
---|
239 | va_start(va, pszFormat);
|
---|
240 | int rc = printfVNoThrow(pszFormat, va);
|
---|
241 | va_end(va);
|
---|
242 | return rc;
|
---|
243 | }
|
---|
244 |
|
---|
245 | /**
|
---|
246 | * Callback used with RTStrFormatV by RTCString::printfV.
|
---|
247 | *
|
---|
248 | * @returns The number of bytes added (not used).
|
---|
249 | *
|
---|
250 | * @param pvArg The string object.
|
---|
251 | * @param pachChars The characters to append.
|
---|
252 | * @param cbChars The number of characters. 0 on the final callback.
|
---|
253 | */
|
---|
254 | /*static*/ DECLCALLBACK(size_t)
|
---|
255 | RTCString::printfOutputCallback(void *pvArg, const char *pachChars, size_t cbChars)
|
---|
256 | {
|
---|
257 | RTCString *pThis = (RTCString *)pvArg;
|
---|
258 | if (cbChars)
|
---|
259 | {
|
---|
260 | size_t const cchBoth = pThis->m_cch + cbChars;
|
---|
261 | if (cchBoth >= pThis->m_cbAllocated)
|
---|
262 | {
|
---|
263 | /* Double the buffer size, if it's less that _4M. Align sizes like
|
---|
264 | for append. */
|
---|
265 | size_t cbAlloc = RT_ALIGN_Z(pThis->m_cbAllocated, IPRT_MINISTRING_APPEND_ALIGNMENT);
|
---|
266 | cbAlloc += RT_MIN(cbAlloc, _4M);
|
---|
267 | if (cbAlloc <= cchBoth)
|
---|
268 | cbAlloc = RT_ALIGN_Z(cchBoth + 1, IPRT_MINISTRING_APPEND_ALIGNMENT);
|
---|
269 | pThis->reserve(cbAlloc);
|
---|
270 | #ifndef RT_EXCEPTIONS_ENABLED
|
---|
271 | AssertReleaseReturn(pThis->capacity() > cchBoth, 0);
|
---|
272 | #endif
|
---|
273 | }
|
---|
274 |
|
---|
275 | memcpy(&pThis->m_psz[pThis->m_cch], pachChars, cbChars);
|
---|
276 | pThis->m_cch = cchBoth;
|
---|
277 | pThis->m_psz[cchBoth] = '\0';
|
---|
278 | }
|
---|
279 | return cbChars;
|
---|
280 | }
|
---|
281 |
|
---|
282 | RTCString &RTCString::printfV(const char *pszFormat, va_list va)
|
---|
283 | {
|
---|
284 | cleanup();
|
---|
285 | RTStrFormatV(printfOutputCallback, this, NULL, NULL, pszFormat, va);
|
---|
286 | return *this;
|
---|
287 | }
|
---|
288 |
|
---|
289 | RTCString &RTCString::appendPrintfV(const char *pszFormat, va_list va)
|
---|
290 | {
|
---|
291 | RTStrFormatV(printfOutputCallback, this, NULL, NULL, pszFormat, va);
|
---|
292 | return *this;
|
---|
293 | }
|
---|
294 |
|
---|
295 | struct RTCSTRINGOTHROW
|
---|
296 | {
|
---|
297 | RTCString *pThis;
|
---|
298 | int rc;
|
---|
299 | };
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * Callback used with RTStrFormatV by RTCString::printfVNoThrow.
|
---|
303 | *
|
---|
304 | * @returns The number of bytes added (not used).
|
---|
305 | *
|
---|
306 | * @param pvArg Pointer to a RTCSTRINGOTHROW structure.
|
---|
307 | * @param pachChars The characters to append.
|
---|
308 | * @param cbChars The number of characters. 0 on the final callback.
|
---|
309 | */
|
---|
310 | /*static*/ DECLCALLBACK(size_t)
|
---|
311 | RTCString::printfOutputCallbackNoThrow(void *pvArg, const char *pachChars, size_t cbChars) RT_NOEXCEPT
|
---|
312 | {
|
---|
313 | RTCString *pThis = ((RTCSTRINGOTHROW *)pvArg)->pThis;
|
---|
314 | if (cbChars)
|
---|
315 | {
|
---|
316 | size_t const cchBoth = pThis->m_cch + cbChars;
|
---|
317 | if (cchBoth >= pThis->m_cbAllocated)
|
---|
318 | {
|
---|
319 | /* Double the buffer size, if it's less that _4M. Align sizes like
|
---|
320 | for append. */
|
---|
321 | size_t cbAlloc = RT_ALIGN_Z(pThis->m_cbAllocated, IPRT_MINISTRING_APPEND_ALIGNMENT);
|
---|
322 | cbAlloc += RT_MIN(cbAlloc, _4M);
|
---|
323 | if (cbAlloc <= cchBoth)
|
---|
324 | cbAlloc = RT_ALIGN_Z(cchBoth + 1, IPRT_MINISTRING_APPEND_ALIGNMENT);
|
---|
325 | int rc = pThis->reserveNoThrow(cbAlloc);
|
---|
326 | if (RT_SUCCESS(rc))
|
---|
327 | { /* likely */ }
|
---|
328 | else
|
---|
329 | {
|
---|
330 | ((RTCSTRINGOTHROW *)pvArg)->rc = rc;
|
---|
331 | return cbChars;
|
---|
332 | }
|
---|
333 | }
|
---|
334 |
|
---|
335 | memcpy(&pThis->m_psz[pThis->m_cch], pachChars, cbChars);
|
---|
336 | pThis->m_cch = cchBoth;
|
---|
337 | pThis->m_psz[cchBoth] = '\0';
|
---|
338 | }
|
---|
339 | return cbChars;
|
---|
340 | }
|
---|
341 |
|
---|
342 | int RTCString::printfVNoThrow(const char *pszFormat, va_list va) RT_NOEXCEPT
|
---|
343 | {
|
---|
344 | cleanup();
|
---|
345 | RTCSTRINGOTHROW Args = { this, VINF_SUCCESS };
|
---|
346 | RTStrFormatV(printfOutputCallbackNoThrow, &Args, NULL, NULL, pszFormat, va);
|
---|
347 | return Args.rc;
|
---|
348 | }
|
---|
349 |
|
---|
350 | int RTCString::appendPrintfVNoThrow(const char *pszFormat, va_list va) RT_NOEXCEPT
|
---|
351 | {
|
---|
352 | RTCSTRINGOTHROW Args = { this, VINF_SUCCESS };
|
---|
353 | RTStrFormatV(printfOutputCallbackNoThrow, &Args, NULL, NULL, pszFormat, va);
|
---|
354 | return Args.rc;
|
---|
355 | }
|
---|
356 |
|
---|
357 | RTCString &RTCString::appendPrintf(const char *pszFormat, ...)
|
---|
358 | {
|
---|
359 | va_list va;
|
---|
360 | va_start(va, pszFormat);
|
---|
361 | appendPrintfV(pszFormat, va);
|
---|
362 | va_end(va);
|
---|
363 | return *this;
|
---|
364 | }
|
---|
365 |
|
---|
366 | int RTCString::appendPrintfNoThrow(const char *pszFormat, ...) RT_NOEXCEPT
|
---|
367 | {
|
---|
368 | va_list va;
|
---|
369 | va_start(va, pszFormat);
|
---|
370 | int rc = appendPrintfVNoThrow(pszFormat, va);
|
---|
371 | va_end(va);
|
---|
372 | return rc;
|
---|
373 | }
|
---|
374 |
|
---|
375 | RTCString &RTCString::append(const RTCString &that)
|
---|
376 | {
|
---|
377 | Assert(&that != this);
|
---|
378 | return appendWorker(that.c_str(), that.length());
|
---|
379 | }
|
---|
380 |
|
---|
381 | int RTCString::appendNoThrow(const RTCString &that) RT_NOEXCEPT
|
---|
382 | {
|
---|
383 | Assert(&that != this);
|
---|
384 | return appendWorkerNoThrow(that.c_str(), that.length());
|
---|
385 | }
|
---|
386 |
|
---|
387 | RTCString &RTCString::append(const char *pszThat)
|
---|
388 | {
|
---|
389 | return appendWorker(pszThat, strlen(pszThat));
|
---|
390 | }
|
---|
391 |
|
---|
392 | int RTCString::appendNoThrow(const char *pszThat) RT_NOEXCEPT
|
---|
393 | {
|
---|
394 | return appendWorkerNoThrow(pszThat, strlen(pszThat));
|
---|
395 | }
|
---|
396 |
|
---|
397 | RTCString &RTCString::append(const RTCString &rThat, size_t offStart, size_t cchMax /*= RTSTR_MAX*/)
|
---|
398 | {
|
---|
399 | if (offStart < rThat.length())
|
---|
400 | {
|
---|
401 | size_t cchLeft = rThat.length() - offStart;
|
---|
402 | return appendWorker(rThat.c_str() + offStart, RT_MIN(cchLeft, cchMax));
|
---|
403 | }
|
---|
404 | return *this;
|
---|
405 | }
|
---|
406 |
|
---|
407 | int RTCString::appendNoThrow(const RTCString &rThat, size_t offStart, size_t cchMax /*= RTSTR_MAX*/) RT_NOEXCEPT
|
---|
408 | {
|
---|
409 | if (offStart < rThat.length())
|
---|
410 | {
|
---|
411 | size_t cchLeft = rThat.length() - offStart;
|
---|
412 | return appendWorkerNoThrow(rThat.c_str() + offStart, RT_MIN(cchLeft, cchMax));
|
---|
413 | }
|
---|
414 | return VINF_SUCCESS;
|
---|
415 | }
|
---|
416 |
|
---|
417 | RTCString &RTCString::append(const char *pszThat, size_t cchMax)
|
---|
418 | {
|
---|
419 | return appendWorker(pszThat, RTStrNLen(pszThat, cchMax));
|
---|
420 | }
|
---|
421 |
|
---|
422 | int RTCString::appendNoThrow(const char *pszThat, size_t cchMax) RT_NOEXCEPT
|
---|
423 | {
|
---|
424 | return appendWorkerNoThrow(pszThat, RTStrNLen(pszThat, cchMax));
|
---|
425 | }
|
---|
426 |
|
---|
427 | RTCString &RTCString::appendWorker(const char *pszSrc, size_t cchSrc)
|
---|
428 | {
|
---|
429 | if (cchSrc)
|
---|
430 | {
|
---|
431 | size_t cchThis = length();
|
---|
432 | size_t cchBoth = cchThis + cchSrc;
|
---|
433 |
|
---|
434 | if (cchBoth >= m_cbAllocated)
|
---|
435 | {
|
---|
436 | reserve(RT_ALIGN_Z(cchBoth + 1, IPRT_MINISTRING_APPEND_ALIGNMENT));
|
---|
437 | // calls realloc(cchBoth + 1) and sets m_cbAllocated; may throw bad_alloc.
|
---|
438 | #ifndef RT_EXCEPTIONS_ENABLED
|
---|
439 | AssertRelease(capacity() > cchBoth);
|
---|
440 | #endif
|
---|
441 | }
|
---|
442 |
|
---|
443 | memcpy(&m_psz[cchThis], pszSrc, cchSrc);
|
---|
444 | m_psz[cchBoth] = '\0';
|
---|
445 | m_cch = cchBoth;
|
---|
446 | }
|
---|
447 | return *this;
|
---|
448 | }
|
---|
449 |
|
---|
450 | int RTCString::appendWorkerNoThrow(const char *pszSrc, size_t cchSrc) RT_NOEXCEPT
|
---|
451 | {
|
---|
452 | if (cchSrc)
|
---|
453 | {
|
---|
454 | size_t cchThis = length();
|
---|
455 | size_t cchBoth = cchThis + cchSrc;
|
---|
456 |
|
---|
457 | if (cchBoth >= m_cbAllocated)
|
---|
458 | {
|
---|
459 | int rc = reserveNoThrow(RT_ALIGN_Z(cchBoth + 1, IPRT_MINISTRING_APPEND_ALIGNMENT));
|
---|
460 | if (RT_SUCCESS(rc))
|
---|
461 | { /* likely */ }
|
---|
462 | else
|
---|
463 | return rc;
|
---|
464 | }
|
---|
465 |
|
---|
466 | memcpy(&m_psz[cchThis], pszSrc, cchSrc);
|
---|
467 | m_psz[cchBoth] = '\0';
|
---|
468 | m_cch = cchBoth;
|
---|
469 | }
|
---|
470 | return VINF_SUCCESS;
|
---|
471 | }
|
---|
472 |
|
---|
473 | RTCString &RTCString::append(char ch)
|
---|
474 | {
|
---|
475 | Assert((unsigned char)ch < 0x80); /* Don't create invalid UTF-8. */
|
---|
476 | if (ch)
|
---|
477 | {
|
---|
478 | // allocate in chunks of 20 in case this gets called several times
|
---|
479 | if (m_cch + 1 >= m_cbAllocated)
|
---|
480 | {
|
---|
481 | reserve(RT_ALIGN_Z(m_cch + 2, IPRT_MINISTRING_APPEND_ALIGNMENT));
|
---|
482 | // calls realloc(cbBoth) and sets m_cbAllocated; may throw bad_alloc.
|
---|
483 | #ifndef RT_EXCEPTIONS_ENABLED
|
---|
484 | AssertRelease(capacity() > m_cch + 1);
|
---|
485 | #endif
|
---|
486 | }
|
---|
487 |
|
---|
488 | m_psz[m_cch] = ch;
|
---|
489 | m_psz[++m_cch] = '\0';
|
---|
490 | }
|
---|
491 | return *this;
|
---|
492 | }
|
---|
493 |
|
---|
494 | int RTCString::appendNoThrow(char ch) RT_NOEXCEPT
|
---|
495 | {
|
---|
496 | Assert((unsigned char)ch < 0x80); /* Don't create invalid UTF-8. */
|
---|
497 | if (ch)
|
---|
498 | {
|
---|
499 | // allocate in chunks of 20 in case this gets called several times
|
---|
500 | if (m_cch + 1 >= m_cbAllocated)
|
---|
501 | {
|
---|
502 | int rc = reserveNoThrow(RT_ALIGN_Z(m_cch + 2, IPRT_MINISTRING_APPEND_ALIGNMENT));
|
---|
503 | if (RT_SUCCESS(rc))
|
---|
504 | { /* likely */ }
|
---|
505 | else
|
---|
506 | return rc;
|
---|
507 | }
|
---|
508 |
|
---|
509 | m_psz[m_cch] = ch;
|
---|
510 | m_psz[++m_cch] = '\0';
|
---|
511 | }
|
---|
512 | return VINF_SUCCESS;
|
---|
513 | }
|
---|
514 |
|
---|
515 | RTCString &RTCString::appendCodePoint(RTUNICP uc)
|
---|
516 | {
|
---|
517 | /*
|
---|
518 | * Single byte encoding.
|
---|
519 | */
|
---|
520 | if (uc < 0x80)
|
---|
521 | return RTCString::append((char)uc);
|
---|
522 |
|
---|
523 | /*
|
---|
524 | * Multibyte encoding.
|
---|
525 | * Assume max encoding length when resizing the string, that's simpler.
|
---|
526 | */
|
---|
527 | AssertReturn(uc <= UINT32_C(0x7fffffff), *this);
|
---|
528 |
|
---|
529 | if (m_cch + 6 >= m_cbAllocated)
|
---|
530 | {
|
---|
531 | reserve(RT_ALIGN_Z(m_cch + 6 + 1, IPRT_MINISTRING_APPEND_ALIGNMENT));
|
---|
532 | // calls realloc(cbBoth) and sets m_cbAllocated; may throw bad_alloc.
|
---|
533 | #ifndef RT_EXCEPTIONS_ENABLED
|
---|
534 | AssertRelease(capacity() > m_cch + 6);
|
---|
535 | #endif
|
---|
536 | }
|
---|
537 |
|
---|
538 | char *pszNext = RTStrPutCp(&m_psz[m_cch], uc);
|
---|
539 | m_cch = pszNext - m_psz;
|
---|
540 | *pszNext = '\0';
|
---|
541 |
|
---|
542 | return *this;
|
---|
543 | }
|
---|
544 |
|
---|
545 | int RTCString::appendCodePointNoThrow(RTUNICP uc) RT_NOEXCEPT
|
---|
546 | {
|
---|
547 | /*
|
---|
548 | * Single byte encoding.
|
---|
549 | */
|
---|
550 | if (uc < 0x80)
|
---|
551 | return RTCString::appendNoThrow((char)uc);
|
---|
552 |
|
---|
553 | /*
|
---|
554 | * Multibyte encoding.
|
---|
555 | * Assume max encoding length when resizing the string, that's simpler.
|
---|
556 | */
|
---|
557 | AssertReturn(uc <= UINT32_C(0x7fffffff), VERR_INVALID_UTF8_ENCODING);
|
---|
558 |
|
---|
559 | if (m_cch + 6 >= m_cbAllocated)
|
---|
560 | {
|
---|
561 | int rc = reserveNoThrow(RT_ALIGN_Z(m_cch + 6 + 1, IPRT_MINISTRING_APPEND_ALIGNMENT));
|
---|
562 | if (RT_SUCCESS(rc))
|
---|
563 | { /* likely */ }
|
---|
564 | else
|
---|
565 | return rc;
|
---|
566 | }
|
---|
567 |
|
---|
568 | char *pszNext = RTStrPutCp(&m_psz[m_cch], uc);
|
---|
569 | m_cch = pszNext - m_psz;
|
---|
570 | *pszNext = '\0';
|
---|
571 |
|
---|
572 | return VINF_SUCCESS;
|
---|
573 | }
|
---|
574 |
|
---|
575 | RTCString &RTCString::erase(size_t offStart /*= 0*/, size_t cchLength /*= npos*/) RT_NOEXCEPT
|
---|
576 | {
|
---|
577 | size_t cch = length();
|
---|
578 | if (offStart < cch)
|
---|
579 | {
|
---|
580 | if (cchLength >= cch - offStart)
|
---|
581 | {
|
---|
582 | /* Trail removal, nothing to move. */
|
---|
583 | m_cch = offStart;
|
---|
584 | m_psz[offStart] = '\0';
|
---|
585 | }
|
---|
586 | else if (cchLength > 0)
|
---|
587 | {
|
---|
588 | /* Pull up the tail to offStart. */
|
---|
589 | size_t cchAfter = cch - offStart - cchLength;
|
---|
590 | memmove(&m_psz[offStart], &m_psz[offStart + cchLength], cchAfter);
|
---|
591 | m_cch = cch -= cchLength;
|
---|
592 | m_psz[cch] = '\0';
|
---|
593 | }
|
---|
594 | }
|
---|
595 | return *this;
|
---|
596 | }
|
---|
597 |
|
---|
598 | RTCString &RTCString::replace(size_t offStart, size_t cchLength, const RTCString &rStrReplacement)
|
---|
599 | {
|
---|
600 | return replaceWorker(offStart, cchLength, rStrReplacement.c_str(), rStrReplacement.length());
|
---|
601 | }
|
---|
602 |
|
---|
603 | int RTCString::replaceNoThrow(size_t offStart, size_t cchLength, const RTCString &rStrReplacement) RT_NOEXCEPT
|
---|
604 | {
|
---|
605 | return replaceWorkerNoThrow(offStart, cchLength, rStrReplacement.c_str(), rStrReplacement.length());
|
---|
606 | }
|
---|
607 |
|
---|
608 | RTCString &RTCString::replace(size_t offStart, size_t cchLength, const RTCString &rStrReplacement,
|
---|
609 | size_t offReplacement, size_t cchReplacement)
|
---|
610 | {
|
---|
611 | Assert(this != &rStrReplacement);
|
---|
612 | if (cchReplacement > 0)
|
---|
613 | {
|
---|
614 | if (offReplacement < rStrReplacement.length())
|
---|
615 | {
|
---|
616 | size_t cchMaxReplacement = rStrReplacement.length() - offReplacement;
|
---|
617 | return replaceWorker(offStart, cchLength, rStrReplacement.c_str() + offReplacement,
|
---|
618 | RT_MIN(cchReplacement, cchMaxReplacement));
|
---|
619 | }
|
---|
620 | /* Our non-standard handling of out_of_range situations. */
|
---|
621 | AssertMsgFailed(("offReplacement=%zu (cchReplacement=%zu) rStrReplacement.length()=%zu\n",
|
---|
622 | offReplacement, cchReplacement, rStrReplacement.length()));
|
---|
623 | }
|
---|
624 | return replaceWorker(offStart, cchLength, "", 0);
|
---|
625 | }
|
---|
626 |
|
---|
627 | int RTCString::replaceNoThrow(size_t offStart, size_t cchLength, const RTCString &rStrReplacement,
|
---|
628 | size_t offReplacement, size_t cchReplacement) RT_NOEXCEPT
|
---|
629 | {
|
---|
630 | Assert(this != &rStrReplacement);
|
---|
631 | if (cchReplacement > 0)
|
---|
632 | {
|
---|
633 | if (offReplacement < rStrReplacement.length())
|
---|
634 | {
|
---|
635 | size_t cchMaxReplacement = rStrReplacement.length() - offReplacement;
|
---|
636 | return replaceWorkerNoThrow(offStart, cchLength, rStrReplacement.c_str() + offReplacement,
|
---|
637 | RT_MIN(cchReplacement, cchMaxReplacement));
|
---|
638 | }
|
---|
639 | return VERR_OUT_OF_RANGE;
|
---|
640 | }
|
---|
641 | return replaceWorkerNoThrow(offStart, cchLength, "", 0);
|
---|
642 | }
|
---|
643 |
|
---|
644 | RTCString &RTCString::replace(size_t offStart, size_t cchLength, const char *pszReplacement)
|
---|
645 | {
|
---|
646 | return replaceWorker(offStart, cchLength, pszReplacement, strlen(pszReplacement));
|
---|
647 | }
|
---|
648 |
|
---|
649 | int RTCString::replaceNoThrow(size_t offStart, size_t cchLength, const char *pszReplacement) RT_NOEXCEPT
|
---|
650 | {
|
---|
651 | return replaceWorkerNoThrow(offStart, cchLength, pszReplacement, strlen(pszReplacement));
|
---|
652 | }
|
---|
653 |
|
---|
654 | RTCString &RTCString::replace(size_t offStart, size_t cchLength, const char *pszReplacement, size_t cchReplacement)
|
---|
655 | {
|
---|
656 | return replaceWorker(offStart, cchLength, pszReplacement, RTStrNLen(pszReplacement, cchReplacement));
|
---|
657 | }
|
---|
658 |
|
---|
659 | int RTCString::replaceNoThrow(size_t offStart, size_t cchLength, const char *pszReplacement, size_t cchReplacement) RT_NOEXCEPT
|
---|
660 | {
|
---|
661 | return replaceWorkerNoThrow(offStart, cchLength, pszReplacement, RTStrNLen(pszReplacement, cchReplacement));
|
---|
662 | }
|
---|
663 |
|
---|
664 | RTCString &RTCString::replaceWorker(size_t offStart, size_t cchLength, const char *pszSrc, size_t cchSrc)
|
---|
665 | {
|
---|
666 | /*
|
---|
667 | * Our non-standard handling of out_of_range situations.
|
---|
668 | */
|
---|
669 | size_t const cchOldLength = length();
|
---|
670 | AssertMsgReturn(offStart < cchOldLength, ("offStart=%zu (cchLength=%zu); length()=%zu\n", offStart, cchLength, cchOldLength),
|
---|
671 | *this);
|
---|
672 |
|
---|
673 | /*
|
---|
674 | * Correct the length parameter.
|
---|
675 | */
|
---|
676 | size_t cchMaxLength = cchOldLength - offStart;
|
---|
677 | if (cchMaxLength < cchLength)
|
---|
678 | cchLength = cchMaxLength;
|
---|
679 |
|
---|
680 | /*
|
---|
681 | * Adjust string allocation if necessary.
|
---|
682 | */
|
---|
683 | size_t cchNew = cchOldLength - cchLength + cchSrc;
|
---|
684 | if (cchNew >= m_cbAllocated)
|
---|
685 | {
|
---|
686 | reserve(RT_ALIGN_Z(cchNew + 1, IPRT_MINISTRING_APPEND_ALIGNMENT));
|
---|
687 | // calls realloc(cchBoth + 1) and sets m_cbAllocated; may throw bad_alloc.
|
---|
688 | #ifndef RT_EXCEPTIONS_ENABLED
|
---|
689 | AssertRelease(capacity() > cchNew);
|
---|
690 | #endif
|
---|
691 | }
|
---|
692 |
|
---|
693 | /*
|
---|
694 | * Make the change.
|
---|
695 | */
|
---|
696 | size_t cchAfter = cchOldLength - offStart - cchLength;
|
---|
697 | if (cchAfter > 0)
|
---|
698 | memmove(&m_psz[offStart + cchSrc], &m_psz[offStart + cchLength], cchAfter);
|
---|
699 | memcpy(&m_psz[offStart], pszSrc, cchSrc);
|
---|
700 | m_psz[cchNew] = '\0';
|
---|
701 | m_cch = cchNew;
|
---|
702 |
|
---|
703 | return *this;
|
---|
704 | }
|
---|
705 |
|
---|
706 | int RTCString::replaceWorkerNoThrow(size_t offStart, size_t cchLength, const char *pszSrc, size_t cchSrc) RT_NOEXCEPT
|
---|
707 | {
|
---|
708 | /*
|
---|
709 | * Our non-standard handling of out_of_range situations.
|
---|
710 | */
|
---|
711 | size_t const cchOldLength = length();
|
---|
712 | AssertMsgReturn(offStart < cchOldLength, ("offStart=%zu (cchLength=%zu); length()=%zu\n", offStart, cchLength, cchOldLength),
|
---|
713 | VERR_OUT_OF_RANGE);
|
---|
714 |
|
---|
715 | /*
|
---|
716 | * Correct the length parameter.
|
---|
717 | */
|
---|
718 | size_t cchMaxLength = cchOldLength - offStart;
|
---|
719 | if (cchMaxLength < cchLength)
|
---|
720 | cchLength = cchMaxLength;
|
---|
721 |
|
---|
722 | /*
|
---|
723 | * Adjust string allocation if necessary.
|
---|
724 | */
|
---|
725 | size_t cchNew = cchOldLength - cchLength + cchSrc;
|
---|
726 | if (cchNew >= m_cbAllocated)
|
---|
727 | {
|
---|
728 | int rc = reserveNoThrow(RT_ALIGN_Z(cchNew + 1, IPRT_MINISTRING_APPEND_ALIGNMENT));
|
---|
729 | if (RT_SUCCESS(rc))
|
---|
730 | { /* likely */ }
|
---|
731 | else
|
---|
732 | return rc;
|
---|
733 | }
|
---|
734 |
|
---|
735 | /*
|
---|
736 | * Make the change.
|
---|
737 | */
|
---|
738 | size_t cchAfter = cchOldLength - offStart - cchLength;
|
---|
739 | if (cchAfter > 0)
|
---|
740 | memmove(&m_psz[offStart + cchSrc], &m_psz[offStart + cchLength], cchAfter);
|
---|
741 | memcpy(&m_psz[offStart], pszSrc, cchSrc);
|
---|
742 | m_psz[cchNew] = '\0';
|
---|
743 | m_cch = cchNew;
|
---|
744 |
|
---|
745 | return VINF_SUCCESS;
|
---|
746 | }
|
---|
747 |
|
---|
748 |
|
---|
749 | size_t RTCString::find(const char *pszNeedle, size_t offStart /*= 0*/) const RT_NOEXCEPT
|
---|
750 | {
|
---|
751 | if (offStart < length())
|
---|
752 | {
|
---|
753 | const char *pszThis = c_str();
|
---|
754 | if (pszThis)
|
---|
755 | {
|
---|
756 | if (pszNeedle && *pszNeedle != '\0')
|
---|
757 | {
|
---|
758 | const char *pszHit = strstr(pszThis + offStart, pszNeedle);
|
---|
759 | if (pszHit)
|
---|
760 | return pszHit - pszThis;
|
---|
761 | }
|
---|
762 | }
|
---|
763 | }
|
---|
764 |
|
---|
765 | return npos;
|
---|
766 | }
|
---|
767 |
|
---|
768 | size_t RTCString::find(const RTCString *pStrNeedle, size_t offStart /*= 0*/) const RT_NOEXCEPT
|
---|
769 | {
|
---|
770 | if (offStart < length())
|
---|
771 | {
|
---|
772 | const char *pszThis = c_str();
|
---|
773 | if (pszThis)
|
---|
774 | {
|
---|
775 | if (pStrNeedle)
|
---|
776 | {
|
---|
777 | const char *pszNeedle = pStrNeedle->c_str();
|
---|
778 | if (pszNeedle && *pszNeedle != '\0')
|
---|
779 | {
|
---|
780 | const char *pszHit = strstr(pszThis + offStart, pszNeedle);
|
---|
781 | if (pszHit)
|
---|
782 | return pszHit - pszThis;
|
---|
783 | }
|
---|
784 | }
|
---|
785 | }
|
---|
786 | }
|
---|
787 |
|
---|
788 | return npos;
|
---|
789 | }
|
---|
790 |
|
---|
791 |
|
---|
792 | size_t RTCString::find(const RTCString &rStrNeedle, size_t offStart /*= 0*/) const RT_NOEXCEPT
|
---|
793 | {
|
---|
794 | return find(&rStrNeedle, offStart);
|
---|
795 | }
|
---|
796 |
|
---|
797 |
|
---|
798 | size_t RTCString::find(const char chNeedle, size_t offStart /*= 0*/) const RT_NOEXCEPT
|
---|
799 | {
|
---|
800 | Assert((unsigned int)chNeedle < 128U);
|
---|
801 | if (offStart < length())
|
---|
802 | {
|
---|
803 | const char *pszThis = c_str();
|
---|
804 | if (pszThis)
|
---|
805 | {
|
---|
806 | const char *pszHit = (const char *)memchr(&pszThis[offStart], chNeedle, length() - offStart);
|
---|
807 | if (pszHit)
|
---|
808 | return pszHit - pszThis;
|
---|
809 | }
|
---|
810 | }
|
---|
811 | return npos;
|
---|
812 | }
|
---|
813 |
|
---|
814 |
|
---|
815 | void RTCString::findReplace(char chFind, char chReplace) RT_NOEXCEPT
|
---|
816 | {
|
---|
817 | Assert((unsigned int)chFind < 128U);
|
---|
818 | Assert((unsigned int)chReplace < 128U);
|
---|
819 |
|
---|
820 | for (size_t i = 0; i < length(); ++i)
|
---|
821 | {
|
---|
822 | char *p = &m_psz[i];
|
---|
823 | if (*p == chFind)
|
---|
824 | *p = chReplace;
|
---|
825 | }
|
---|
826 | }
|
---|
827 |
|
---|
828 | size_t RTCString::count(char ch) const RT_NOEXCEPT
|
---|
829 | {
|
---|
830 | Assert((unsigned int)ch < 128U);
|
---|
831 |
|
---|
832 | size_t c = 0;
|
---|
833 | const char *psz = m_psz;
|
---|
834 | if (psz)
|
---|
835 | {
|
---|
836 | char chCur;
|
---|
837 | while ((chCur = *psz++) != '\0')
|
---|
838 | if (chCur == ch)
|
---|
839 | c++;
|
---|
840 | }
|
---|
841 | return c;
|
---|
842 | }
|
---|
843 |
|
---|
844 | #if 0 /** @todo implement these when needed. */
|
---|
845 | size_t RTCString::count(const char *psz, CaseSensitivity cs = CaseSensitive) const RT_NOEXCEPT
|
---|
846 | {
|
---|
847 | }
|
---|
848 |
|
---|
849 | size_t RTCString::count(const RTCString *pStr, CaseSensitivity cs = CaseSensitive) const RT_NOEXCEPT
|
---|
850 | {
|
---|
851 |
|
---|
852 | }
|
---|
853 | #endif
|
---|
854 |
|
---|
855 |
|
---|
856 | RTCString &RTCString::strip() RT_NOEXCEPT
|
---|
857 | {
|
---|
858 | stripRight();
|
---|
859 | return stripLeft();
|
---|
860 | }
|
---|
861 |
|
---|
862 |
|
---|
863 | RTCString &RTCString::stripLeft() RT_NOEXCEPT
|
---|
864 | {
|
---|
865 | char *psz = m_psz;
|
---|
866 | size_t const cch = m_cch;
|
---|
867 | size_t off = 0;
|
---|
868 | while (off < cch && RT_C_IS_SPACE(psz[off]))
|
---|
869 | off++;
|
---|
870 | if (off > 0)
|
---|
871 | {
|
---|
872 | if (off != cch)
|
---|
873 | {
|
---|
874 | memmove(psz, &psz[off], cch - off + 1);
|
---|
875 | m_cch = cch - off;
|
---|
876 | }
|
---|
877 | else
|
---|
878 | setNull();
|
---|
879 | }
|
---|
880 | return *this;
|
---|
881 | }
|
---|
882 |
|
---|
883 |
|
---|
884 | RTCString &RTCString::stripRight() RT_NOEXCEPT
|
---|
885 | {
|
---|
886 | char *psz = m_psz;
|
---|
887 | size_t cch = m_cch;
|
---|
888 | while (cch > 0 && RT_C_IS_SPACE(psz[cch - 1]))
|
---|
889 | cch--;
|
---|
890 | if (m_cch != cch)
|
---|
891 | {
|
---|
892 | m_cch = cch;
|
---|
893 | psz[cch] = '\0';
|
---|
894 | }
|
---|
895 | return *this;
|
---|
896 | }
|
---|
897 |
|
---|
898 |
|
---|
899 |
|
---|
900 | RTCString RTCString::substrCP(size_t pos /*= 0*/, size_t n /*= npos*/) const
|
---|
901 | {
|
---|
902 | RTCString ret;
|
---|
903 |
|
---|
904 | if (n)
|
---|
905 | {
|
---|
906 | const char *psz;
|
---|
907 |
|
---|
908 | if ((psz = c_str()))
|
---|
909 | {
|
---|
910 | RTUNICP cp;
|
---|
911 |
|
---|
912 | // walk the UTF-8 characters until where the caller wants to start
|
---|
913 | size_t i = pos;
|
---|
914 | while (*psz && i--)
|
---|
915 | if (RT_FAILURE(RTStrGetCpEx(&psz, &cp)))
|
---|
916 | return ret; // return empty string on bad encoding
|
---|
917 |
|
---|
918 | const char *pFirst = psz;
|
---|
919 |
|
---|
920 | if (n == npos)
|
---|
921 | // all the rest:
|
---|
922 | ret = pFirst;
|
---|
923 | else
|
---|
924 | {
|
---|
925 | i = n;
|
---|
926 | while (*psz && i--)
|
---|
927 | if (RT_FAILURE(RTStrGetCpEx(&psz, &cp)))
|
---|
928 | return ret; // return empty string on bad encoding
|
---|
929 |
|
---|
930 | size_t cbCopy = psz - pFirst;
|
---|
931 | if (cbCopy)
|
---|
932 | {
|
---|
933 | ret.reserve(cbCopy + 1); // may throw bad_alloc
|
---|
934 | #ifndef RT_EXCEPTIONS_ENABLED
|
---|
935 | AssertRelease(capacity() >= cbCopy + 1);
|
---|
936 | #endif
|
---|
937 | memcpy(ret.m_psz, pFirst, cbCopy);
|
---|
938 | ret.m_cch = cbCopy;
|
---|
939 | ret.m_psz[cbCopy] = '\0';
|
---|
940 | }
|
---|
941 | }
|
---|
942 | }
|
---|
943 | }
|
---|
944 |
|
---|
945 | return ret;
|
---|
946 | }
|
---|
947 |
|
---|
948 | bool RTCString::endsWith(const RTCString &that, CaseSensitivity cs /*= CaseSensitive*/) const RT_NOEXCEPT
|
---|
949 | {
|
---|
950 | size_t l1 = length();
|
---|
951 | if (l1 == 0)
|
---|
952 | return false;
|
---|
953 |
|
---|
954 | size_t l2 = that.length();
|
---|
955 | if (l1 < l2)
|
---|
956 | return false;
|
---|
957 | /** @todo r=bird: If l2 is 0, then m_psz can be NULL and we will crash. See
|
---|
958 | * also handling of l2 == in startsWith. */
|
---|
959 |
|
---|
960 | size_t l = l1 - l2;
|
---|
961 | if (cs == CaseSensitive)
|
---|
962 | return ::RTStrCmp(&m_psz[l], that.m_psz) == 0;
|
---|
963 | return ::RTStrICmp(&m_psz[l], that.m_psz) == 0;
|
---|
964 | }
|
---|
965 |
|
---|
966 | bool RTCString::startsWith(const RTCString &that, CaseSensitivity cs /*= CaseSensitive*/) const RT_NOEXCEPT
|
---|
967 | {
|
---|
968 | size_t l1 = length();
|
---|
969 | size_t l2 = that.length();
|
---|
970 | if (l1 == 0 || l2 == 0) /** @todo r=bird: this differs from endsWith, and I think other IPRT code. If l2 == 0, it matches anything. */
|
---|
971 | return false;
|
---|
972 |
|
---|
973 | if (l1 < l2)
|
---|
974 | return false;
|
---|
975 |
|
---|
976 | if (cs == CaseSensitive)
|
---|
977 | return ::RTStrNCmp(m_psz, that.m_psz, l2) == 0;
|
---|
978 | return ::RTStrNICmp(m_psz, that.m_psz, l2) == 0;
|
---|
979 | }
|
---|
980 |
|
---|
981 | bool RTCString::startsWithWord(const char *pszWord, CaseSensitivity enmCase /*= CaseSensitive*/) const RT_NOEXCEPT
|
---|
982 | {
|
---|
983 | const char *pszSrc = RTStrStripL(c_str()); /** @todo RTStrStripL doesn't use RTUniCpIsSpace (nbsp) */
|
---|
984 | size_t cchWord = strlen(pszWord);
|
---|
985 | if ( enmCase == CaseSensitive
|
---|
986 | ? RTStrNCmp(pszSrc, pszWord, cchWord) == 0
|
---|
987 | : RTStrNICmp(pszSrc, pszWord, cchWord) == 0)
|
---|
988 | {
|
---|
989 | if ( pszSrc[cchWord] == '\0'
|
---|
990 | || RT_C_IS_SPACE(pszSrc[cchWord])
|
---|
991 | || RT_C_IS_PUNCT(pszSrc[cchWord]) )
|
---|
992 | return true;
|
---|
993 | RTUNICP uc = RTStrGetCp(&pszSrc[cchWord]);
|
---|
994 | if (RTUniCpIsSpace(uc))
|
---|
995 | return true;
|
---|
996 | }
|
---|
997 | return false;
|
---|
998 | }
|
---|
999 |
|
---|
1000 | bool RTCString::startsWithWord(const RTCString &rThat, CaseSensitivity enmCase /*= CaseSensitive*/) const RT_NOEXCEPT
|
---|
1001 | {
|
---|
1002 | return startsWithWord(rThat.c_str(), enmCase);
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 | bool RTCString::contains(const RTCString &that, CaseSensitivity cs /*= CaseSensitive*/) const RT_NOEXCEPT
|
---|
1006 | {
|
---|
1007 | /** @todo r-bird: Not checking for NULL strings like startsWith does (and
|
---|
1008 | * endsWith only does half way). */
|
---|
1009 | if (cs == CaseSensitive)
|
---|
1010 | return ::RTStrStr(m_psz, that.m_psz) != NULL;
|
---|
1011 | return ::RTStrIStr(m_psz, that.m_psz) != NULL;
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | bool RTCString::contains(const char *pszNeedle, CaseSensitivity cs /*= CaseSensitive*/) const RT_NOEXCEPT
|
---|
1015 | {
|
---|
1016 | /** @todo r-bird: Not checking for NULL strings like startsWith does (and
|
---|
1017 | * endsWith only does half way). */
|
---|
1018 | if (cs == CaseSensitive)
|
---|
1019 | return ::RTStrStr(m_psz, pszNeedle) != NULL;
|
---|
1020 | return ::RTStrIStr(m_psz, pszNeedle) != NULL;
|
---|
1021 | }
|
---|
1022 |
|
---|
1023 | int RTCString::toInt(uint64_t &i) const RT_NOEXCEPT
|
---|
1024 | {
|
---|
1025 | if (!m_psz)
|
---|
1026 | return VERR_NO_DIGITS;
|
---|
1027 | return RTStrToUInt64Ex(m_psz, NULL, 0, &i);
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 | int RTCString::toInt(uint32_t &i) const RT_NOEXCEPT
|
---|
1031 | {
|
---|
1032 | if (!m_psz)
|
---|
1033 | return VERR_NO_DIGITS;
|
---|
1034 | return RTStrToUInt32Ex(m_psz, NULL, 0, &i);
|
---|
1035 | }
|
---|
1036 |
|
---|
1037 | RTCList<RTCString, RTCString *>
|
---|
1038 | RTCString::split(const RTCString &a_rstrSep, SplitMode mode /* = RemoveEmptyParts */) const
|
---|
1039 | {
|
---|
1040 | RTCList<RTCString> strRet;
|
---|
1041 | if (!m_psz)
|
---|
1042 | return strRet;
|
---|
1043 | if (a_rstrSep.isEmpty())
|
---|
1044 | {
|
---|
1045 | strRet.append(RTCString(m_psz));
|
---|
1046 | return strRet;
|
---|
1047 | }
|
---|
1048 |
|
---|
1049 | size_t cch = m_cch;
|
---|
1050 | char const *pszTmp = m_psz;
|
---|
1051 | while (cch > 0)
|
---|
1052 | {
|
---|
1053 | char const *pszNext = strstr(pszTmp, a_rstrSep.c_str());
|
---|
1054 | if (!pszNext)
|
---|
1055 | {
|
---|
1056 | strRet.append(RTCString(pszTmp, cch));
|
---|
1057 | break;
|
---|
1058 | }
|
---|
1059 | size_t cchNext = pszNext - pszTmp;
|
---|
1060 | if ( cchNext > 0
|
---|
1061 | || mode == KeepEmptyParts)
|
---|
1062 | strRet.append(RTCString(pszTmp, cchNext));
|
---|
1063 | pszTmp += cchNext + a_rstrSep.length();
|
---|
1064 | cch -= cchNext + a_rstrSep.length();
|
---|
1065 | }
|
---|
1066 |
|
---|
1067 | return strRet;
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | /* static */
|
---|
1071 | RTCString
|
---|
1072 | RTCString::joinEx(const RTCList<RTCString, RTCString *> &a_rList,
|
---|
1073 | const RTCString &a_rstrPrefix /* = "" */,
|
---|
1074 | const RTCString &a_rstrSep /* = "" */)
|
---|
1075 | {
|
---|
1076 | RTCString strRet;
|
---|
1077 | if (a_rList.size() > 1)
|
---|
1078 | {
|
---|
1079 | /* calc the required size */
|
---|
1080 | size_t cbNeeded = a_rstrSep.length() * (a_rList.size() - 1) + 1;
|
---|
1081 | cbNeeded += a_rstrPrefix.length() * (a_rList.size() - 1) + 1;
|
---|
1082 | for (size_t i = 0; i < a_rList.size(); ++i)
|
---|
1083 | cbNeeded += a_rList.at(i).length();
|
---|
1084 | strRet.reserve(cbNeeded);
|
---|
1085 |
|
---|
1086 | /* do the appending. */
|
---|
1087 | for (size_t i = 0; i < a_rList.size() - 1; ++i)
|
---|
1088 | {
|
---|
1089 | if (a_rstrPrefix.isNotEmpty())
|
---|
1090 | strRet.append(a_rstrPrefix);
|
---|
1091 | strRet.append(a_rList.at(i));
|
---|
1092 | strRet.append(a_rstrSep);
|
---|
1093 | }
|
---|
1094 | strRet.append(a_rList.last());
|
---|
1095 | }
|
---|
1096 | /* special case: one list item. */
|
---|
1097 | else if (a_rList.size() > 0)
|
---|
1098 | {
|
---|
1099 | if (a_rstrPrefix.isNotEmpty())
|
---|
1100 | strRet.append(a_rstrPrefix);
|
---|
1101 | strRet.append(a_rList.last());
|
---|
1102 | }
|
---|
1103 |
|
---|
1104 | return strRet;
|
---|
1105 | }
|
---|
1106 |
|
---|
1107 | /* static */
|
---|
1108 | RTCString
|
---|
1109 | RTCString::join(const RTCList<RTCString, RTCString *> &a_rList,
|
---|
1110 | const RTCString &a_rstrSep /* = "" */)
|
---|
1111 | {
|
---|
1112 | return RTCString::joinEx(a_rList,
|
---|
1113 | "" /* a_rstrPrefix */, a_rstrSep);
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | const RTCString operator+(const RTCString &a_rStr1, const RTCString &a_rStr2)
|
---|
1117 | {
|
---|
1118 | RTCString strRet(a_rStr1);
|
---|
1119 | strRet += a_rStr2;
|
---|
1120 | return strRet;
|
---|
1121 | }
|
---|
1122 |
|
---|
1123 | const RTCString operator+(const RTCString &a_rStr1, const char *a_pszStr2)
|
---|
1124 | {
|
---|
1125 | RTCString strRet(a_rStr1);
|
---|
1126 | strRet += a_pszStr2;
|
---|
1127 | return strRet;
|
---|
1128 | }
|
---|
1129 |
|
---|
1130 | const RTCString operator+(const char *a_psz1, const RTCString &a_rStr2)
|
---|
1131 | {
|
---|
1132 | RTCString strRet(a_psz1);
|
---|
1133 | strRet += a_rStr2;
|
---|
1134 | return strRet;
|
---|
1135 | }
|
---|
1136 |
|
---|