1 | /* $Id: string.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * MS COM / XPCOM Abstraction Layer - UTF-8 and UTF-16 string classes.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #include "VBox/com/string.h"
|
---|
29 |
|
---|
30 | #include <iprt/err.h>
|
---|
31 | #include <iprt/log.h>
|
---|
32 | #include <iprt/path.h>
|
---|
33 | #include <iprt/string.h>
|
---|
34 | #include <iprt/uni.h>
|
---|
35 |
|
---|
36 | namespace com
|
---|
37 | {
|
---|
38 |
|
---|
39 | // BSTR representing a null wide char with 32 bits of length prefix (0);
|
---|
40 | // this will work on Windows as well as other platforms where BSTR does
|
---|
41 | // not use length prefixes
|
---|
42 | const OLECHAR g_achEmptyBstr[3] = { 0, 0, 0 };
|
---|
43 | const BSTR g_bstrEmpty = (BSTR)&g_achEmptyBstr[2];
|
---|
44 |
|
---|
45 | /* static */
|
---|
46 | const Bstr Bstr::Empty; /* default ctor is OK */
|
---|
47 |
|
---|
48 |
|
---|
49 | Bstr &Bstr::printf(const char *pszFormat, ...)
|
---|
50 | {
|
---|
51 | va_list va;
|
---|
52 | va_start(va, pszFormat);
|
---|
53 | HRESULT hrc = printfVNoThrow(pszFormat, va);
|
---|
54 | va_end(va);
|
---|
55 | #ifdef RT_EXCEPTIONS_ENABLED
|
---|
56 | if (hrc == S_OK)
|
---|
57 | { /* likely */ }
|
---|
58 | else
|
---|
59 | throw std::bad_alloc();
|
---|
60 | #else
|
---|
61 | Assert(hrc == S_OK); RT_NOREF(hrc);
|
---|
62 | #endif
|
---|
63 | return *this;
|
---|
64 | }
|
---|
65 |
|
---|
66 | HRESULT Bstr::printfNoThrow(const char *pszFormat, ...) RT_NOEXCEPT
|
---|
67 | {
|
---|
68 | va_list va;
|
---|
69 | va_start(va, pszFormat);
|
---|
70 | HRESULT hrc = printfVNoThrow(pszFormat, va);
|
---|
71 | va_end(va);
|
---|
72 | return hrc;
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | Bstr &Bstr::printfV(const char *pszFormat, va_list va)
|
---|
77 | {
|
---|
78 | HRESULT hrc = printfVNoThrow(pszFormat, va);
|
---|
79 | #ifdef RT_EXCEPTIONS_ENABLED
|
---|
80 | if (hrc == S_OK)
|
---|
81 | { /* likely */ }
|
---|
82 | else
|
---|
83 | throw std::bad_alloc();
|
---|
84 | #else
|
---|
85 | Assert(hrc == S_OK); RT_NOREF(hrc);
|
---|
86 | #endif
|
---|
87 | return *this;
|
---|
88 | }
|
---|
89 |
|
---|
90 | struct BSTRNOTHROW
|
---|
91 | {
|
---|
92 | Bstr *pThis;
|
---|
93 | size_t cwcAlloc;
|
---|
94 | size_t offDst;
|
---|
95 | HRESULT hrc;
|
---|
96 | };
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Callback used with RTStrFormatV by Bstr::printfVNoThrow.
|
---|
100 | *
|
---|
101 | * @returns The number of bytes added (not used).
|
---|
102 | *
|
---|
103 | * @param pvArg Pointer to a BSTRNOTHROW structure.
|
---|
104 | * @param pachChars The characters to append.
|
---|
105 | * @param cbChars The number of characters. 0 on the final callback.
|
---|
106 | */
|
---|
107 | /*static*/ DECLCALLBACK(size_t)
|
---|
108 | Bstr::printfOutputCallbackNoThrow(void *pvArg, const char *pachChars, size_t cbChars) RT_NOEXCEPT
|
---|
109 | {
|
---|
110 | BSTRNOTHROW *pArgs = (BSTRNOTHROW *)pvArg;
|
---|
111 | if (cbChars)
|
---|
112 | {
|
---|
113 | size_t cwcAppend;
|
---|
114 | int rc = ::RTStrCalcUtf16LenEx(pachChars, cbChars, &cwcAppend);
|
---|
115 | AssertRCReturnStmt(rc, pArgs->hrc = E_UNEXPECTED, 0);
|
---|
116 |
|
---|
117 | /*
|
---|
118 | * Ensure we've got sufficient memory.
|
---|
119 | */
|
---|
120 | Bstr *pThis = pArgs->pThis;
|
---|
121 | size_t const cwcBoth = pArgs->offDst + cwcAppend;
|
---|
122 | if (cwcBoth >= pArgs->cwcAlloc)
|
---|
123 | {
|
---|
124 | if (pArgs->hrc == S_OK)
|
---|
125 | {
|
---|
126 | /* Double the buffer size, if it's less that _1M. Align sizes like
|
---|
127 | for append. */
|
---|
128 | size_t cwcAlloc = RT_ALIGN_Z(pArgs->cwcAlloc, 128);
|
---|
129 | cwcAlloc += RT_MIN(cwcAlloc, _1M);
|
---|
130 | if (cwcAlloc <= cwcBoth)
|
---|
131 | cwcAlloc = RT_ALIGN_Z(cwcBoth + 1, 512);
|
---|
132 | pArgs->hrc = pThis->reserveNoThrow(cwcAlloc, true /*fForce*/);
|
---|
133 | AssertMsgReturn(pArgs->hrc == S_OK, ("cwcAlloc=%#zx\n", cwcAlloc), 0);
|
---|
134 | pArgs->cwcAlloc = cwcAlloc;
|
---|
135 | }
|
---|
136 | else
|
---|
137 | return 0;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /*
|
---|
141 | * Do the conversion.
|
---|
142 | */
|
---|
143 | PRTUTF16 pwszDst = pThis->m_bstr + pArgs->offDst;
|
---|
144 | Assert(pArgs->cwcAlloc > pArgs->offDst);
|
---|
145 | rc = ::RTStrToUtf16Ex(pachChars, cbChars, &pwszDst, pArgs->cwcAlloc - pArgs->offDst, &cwcAppend);
|
---|
146 | AssertRCReturnStmt(rc, pArgs->hrc = E_UNEXPECTED, 0);
|
---|
147 | pArgs->offDst += cwcAppend;
|
---|
148 | }
|
---|
149 | return cbChars;
|
---|
150 | }
|
---|
151 |
|
---|
152 | HRESULT Bstr::printfVNoThrow(const char *pszFormat, va_list va) RT_NOEXCEPT
|
---|
153 | {
|
---|
154 | cleanup();
|
---|
155 |
|
---|
156 | BSTRNOTHROW Args = { this, 0, 0, S_OK };
|
---|
157 | RTStrFormatV(printfOutputCallbackNoThrow, &Args, NULL, NULL, pszFormat, va);
|
---|
158 | if (Args.hrc == S_OK)
|
---|
159 | {
|
---|
160 | Args.hrc = joltNoThrow(Args.offDst);
|
---|
161 | if (Args.hrc == S_OK)
|
---|
162 | return S_OK;
|
---|
163 | }
|
---|
164 |
|
---|
165 | cleanup();
|
---|
166 | return Args.hrc;
|
---|
167 | }
|
---|
168 |
|
---|
169 | void Bstr::copyFromN(const char *a_pszSrc, size_t a_cchMax)
|
---|
170 | {
|
---|
171 | /*
|
---|
172 | * Initialize m_bstr first in case of throws further down in the code, then
|
---|
173 | * check for empty input (m_bstr == NULL means empty, there are no NULL
|
---|
174 | * strings).
|
---|
175 | */
|
---|
176 | m_bstr = NULL;
|
---|
177 | if (!a_cchMax || !a_pszSrc || !*a_pszSrc)
|
---|
178 | return;
|
---|
179 |
|
---|
180 | /*
|
---|
181 | * Calculate the length and allocate a BSTR string buffer of the right
|
---|
182 | * size, i.e. optimize heap usage.
|
---|
183 | */
|
---|
184 | size_t cwc;
|
---|
185 | int vrc = ::RTStrCalcUtf16LenEx(a_pszSrc, a_cchMax, &cwc);
|
---|
186 | if (RT_SUCCESS(vrc))
|
---|
187 | {
|
---|
188 | m_bstr = ::SysAllocStringByteLen(NULL, (unsigned)(cwc * sizeof(OLECHAR)));
|
---|
189 | if (RT_LIKELY(m_bstr))
|
---|
190 | {
|
---|
191 | PRTUTF16 pwsz = (PRTUTF16)m_bstr;
|
---|
192 | vrc = ::RTStrToUtf16Ex(a_pszSrc, a_cchMax, &pwsz, cwc + 1, NULL);
|
---|
193 | if (RT_SUCCESS(vrc))
|
---|
194 | return;
|
---|
195 |
|
---|
196 | /* This should not happen! */
|
---|
197 | AssertRC(vrc);
|
---|
198 | cleanup();
|
---|
199 | }
|
---|
200 | }
|
---|
201 | else /* ASSUME: input is valid Utf-8. Fake out of memory error. */
|
---|
202 | AssertLogRelMsgFailed(("%Rrc %.*Rhxs\n", vrc, RTStrNLen(a_pszSrc, a_cchMax), a_pszSrc));
|
---|
203 | #ifdef RT_EXCEPTIONS_ENABLED
|
---|
204 | throw std::bad_alloc();
|
---|
205 | #endif
|
---|
206 | }
|
---|
207 |
|
---|
208 | HRESULT Bstr::cleanupAndCopyFromNoThrow(const char *a_pszSrc, size_t a_cchMax) RT_NOEXCEPT
|
---|
209 | {
|
---|
210 | /*
|
---|
211 | * Check for empty input (m_bstr == NULL means empty, there are no NULL strings).
|
---|
212 | */
|
---|
213 | cleanup();
|
---|
214 | if (!a_cchMax || !a_pszSrc || !*a_pszSrc)
|
---|
215 | return S_OK;
|
---|
216 |
|
---|
217 | /*
|
---|
218 | * Calculate the length and allocate a BSTR string buffer of the right
|
---|
219 | * size, i.e. optimize heap usage.
|
---|
220 | */
|
---|
221 | HRESULT hrc;
|
---|
222 | size_t cwc;
|
---|
223 | int vrc = ::RTStrCalcUtf16LenEx(a_pszSrc, a_cchMax, &cwc);
|
---|
224 | if (RT_SUCCESS(vrc))
|
---|
225 | {
|
---|
226 | m_bstr = ::SysAllocStringByteLen(NULL, (unsigned)(cwc * sizeof(OLECHAR)));
|
---|
227 | if (RT_LIKELY(m_bstr))
|
---|
228 | {
|
---|
229 | PRTUTF16 pwsz = (PRTUTF16)m_bstr;
|
---|
230 | vrc = ::RTStrToUtf16Ex(a_pszSrc, a_cchMax, &pwsz, cwc + 1, NULL);
|
---|
231 | if (RT_SUCCESS(vrc))
|
---|
232 | return S_OK;
|
---|
233 |
|
---|
234 | /* This should not happen! */
|
---|
235 | AssertRC(vrc);
|
---|
236 | cleanup();
|
---|
237 | hrc = E_UNEXPECTED;
|
---|
238 | }
|
---|
239 | else
|
---|
240 | hrc = E_OUTOFMEMORY;
|
---|
241 | }
|
---|
242 | else
|
---|
243 | {
|
---|
244 | /* Unexpected: Invalid UTF-8 input. */
|
---|
245 | AssertLogRelMsgFailed(("%Rrc %.*Rhxs\n", vrc, RTStrNLen(a_pszSrc, a_cchMax), a_pszSrc));
|
---|
246 | hrc = E_UNEXPECTED;
|
---|
247 | }
|
---|
248 | return hrc;
|
---|
249 | }
|
---|
250 |
|
---|
251 |
|
---|
252 | int Bstr::compareUtf8(const char *a_pszRight, CaseSensitivity a_enmCase /*= CaseSensitive*/) const
|
---|
253 | {
|
---|
254 | PCRTUTF16 pwszLeft = m_bstr;
|
---|
255 |
|
---|
256 | /*
|
---|
257 | * Special case for null/empty strings. Unlike RTUtf16Cmp we
|
---|
258 | * treat null and empty equally.
|
---|
259 | */
|
---|
260 | if (!pwszLeft)
|
---|
261 | return !a_pszRight || *a_pszRight == '\0' ? 0 : -1;
|
---|
262 | if (!a_pszRight)
|
---|
263 | return *pwszLeft == '\0' ? 0 : 1;
|
---|
264 |
|
---|
265 | /*
|
---|
266 | * Compare with a UTF-8 string by enumerating them char by char.
|
---|
267 | */
|
---|
268 | for (;;)
|
---|
269 | {
|
---|
270 | RTUNICP ucLeft;
|
---|
271 | int rc = RTUtf16GetCpEx(&pwszLeft, &ucLeft);
|
---|
272 | AssertRCReturn(rc, 1);
|
---|
273 |
|
---|
274 | RTUNICP ucRight;
|
---|
275 | rc = RTStrGetCpEx(&a_pszRight, &ucRight);
|
---|
276 | AssertRCReturn(rc, -1);
|
---|
277 | if (ucLeft == ucRight)
|
---|
278 | {
|
---|
279 | if (ucLeft)
|
---|
280 | continue;
|
---|
281 | return 0;
|
---|
282 | }
|
---|
283 |
|
---|
284 | if (a_enmCase == CaseInsensitive)
|
---|
285 | {
|
---|
286 | if (RTUniCpToUpper(ucLeft) == RTUniCpToUpper(ucRight))
|
---|
287 | continue;
|
---|
288 | if (RTUniCpToLower(ucLeft) == RTUniCpToLower(ucRight))
|
---|
289 | continue;
|
---|
290 | }
|
---|
291 |
|
---|
292 | return ucLeft < ucRight ? -1 : 1;
|
---|
293 | }
|
---|
294 | }
|
---|
295 |
|
---|
296 |
|
---|
297 | bool Bstr::startsWith(Bstr const &a_rStart) const
|
---|
298 | {
|
---|
299 | return RTUtf16NCmp(m_bstr, a_rStart.m_bstr, a_rStart.length()) == 0;
|
---|
300 | }
|
---|
301 |
|
---|
302 |
|
---|
303 | bool Bstr::startsWith(RTCString const &a_rStart) const
|
---|
304 | {
|
---|
305 | return RTUtf16NCmpUtf8(m_bstr, a_rStart.c_str(), RTSTR_MAX, a_rStart.length()) == 0;
|
---|
306 | }
|
---|
307 |
|
---|
308 |
|
---|
309 | bool Bstr::startsWith(const char *a_pszStart) const
|
---|
310 | {
|
---|
311 | return RTUtf16NCmpUtf8(m_bstr, a_pszStart, RTSTR_MAX, strlen(a_pszStart)) == 0;
|
---|
312 | }
|
---|
313 |
|
---|
314 |
|
---|
315 | #ifndef VBOX_WITH_XPCOM
|
---|
316 |
|
---|
317 | HRESULT Bstr::joltNoThrow(ssize_t cwcNew /* = -1*/) RT_NOEXCEPT
|
---|
318 | {
|
---|
319 | if (m_bstr)
|
---|
320 | {
|
---|
321 | size_t const cwcAlloc = ::SysStringLen(m_bstr);
|
---|
322 | size_t const cwcActual = cwcNew < 0 ? ::RTUtf16Len(m_bstr) : (size_t)cwcNew;
|
---|
323 | Assert(cwcNew < 0 || cwcActual == ::RTUtf16Len(m_bstr));
|
---|
324 | if (cwcActual != cwcAlloc)
|
---|
325 | {
|
---|
326 | Assert(cwcActual <= cwcAlloc);
|
---|
327 | Assert((unsigned int)cwcActual == cwcActual);
|
---|
328 |
|
---|
329 | /* Official way: Reallocate the string. We could of course just update the size-prefix if we dared... */
|
---|
330 | if (!::SysReAllocStringLen(&m_bstr, NULL, (unsigned int)cwcActual))
|
---|
331 | {
|
---|
332 | AssertFailed();
|
---|
333 | return E_OUTOFMEMORY;
|
---|
334 | }
|
---|
335 | }
|
---|
336 | }
|
---|
337 | else
|
---|
338 | Assert(cwcNew <= 0);
|
---|
339 | return S_OK;
|
---|
340 | }
|
---|
341 |
|
---|
342 |
|
---|
343 | void Bstr::jolt(ssize_t cwcNew /* = -1*/)
|
---|
344 | {
|
---|
345 | HRESULT hrc = joltNoThrow(cwcNew);
|
---|
346 | # ifdef RT_EXCEPTIONS_ENABLED
|
---|
347 | if (hrc != S_OK)
|
---|
348 | throw std::bad_alloc();
|
---|
349 | # else
|
---|
350 | Assert(hrc == S_OK); RT_NOREF(hrc);
|
---|
351 | # endif
|
---|
352 | }
|
---|
353 |
|
---|
354 | #endif /* !VBOX_WITH_XPCOM */
|
---|
355 |
|
---|
356 |
|
---|
357 | HRESULT Bstr::reserveNoThrow(size_t cwcMin, bool fForce /*= false*/) RT_NOEXCEPT
|
---|
358 | {
|
---|
359 | /* If not forcing the string to the cwcMin length, check cwcMin against the
|
---|
360 | current string length: */
|
---|
361 | if (!fForce)
|
---|
362 | {
|
---|
363 | size_t cwcCur = m_bstr ? ::SysStringLen(m_bstr) : 0;
|
---|
364 | if (cwcCur >= cwcMin)
|
---|
365 | return S_OK;
|
---|
366 | }
|
---|
367 |
|
---|
368 | /* The documentation for SysReAllocStringLen hints about it being allergic
|
---|
369 | to NULL in some way or another, so we call SysAllocStringLen directly
|
---|
370 | when appropriate: */
|
---|
371 | if (m_bstr)
|
---|
372 | AssertReturn(::SysReAllocStringLen(&m_bstr, NULL, (unsigned int)cwcMin) != FALSE, E_OUTOFMEMORY);
|
---|
373 | else if (cwcMin > 0)
|
---|
374 | {
|
---|
375 | m_bstr = ::SysAllocStringLen(NULL, (unsigned int)cwcMin);
|
---|
376 | AssertReturn(m_bstr, E_OUTOFMEMORY);
|
---|
377 | }
|
---|
378 |
|
---|
379 | return S_OK;
|
---|
380 | }
|
---|
381 |
|
---|
382 |
|
---|
383 | void Bstr::reserve(size_t cwcMin, bool fForce /*= false*/)
|
---|
384 | {
|
---|
385 | HRESULT hrc = reserveNoThrow(cwcMin, fForce);
|
---|
386 | #ifdef RT_EXCEPTIONS_ENABLED
|
---|
387 | if (hrc != S_OK)
|
---|
388 | throw std::bad_alloc();
|
---|
389 | #else
|
---|
390 | Assert(hrc == S_OK); RT_NOREF(hrc);
|
---|
391 | #endif
|
---|
392 | }
|
---|
393 |
|
---|
394 |
|
---|
395 | Bstr &Bstr::append(const Bstr &rThat)
|
---|
396 | {
|
---|
397 | if (rThat.isNotEmpty())
|
---|
398 | return appendWorkerUtf16(rThat.m_bstr, rThat.length());
|
---|
399 | return *this;
|
---|
400 | }
|
---|
401 |
|
---|
402 |
|
---|
403 | HRESULT Bstr::appendNoThrow(const Bstr &rThat) RT_NOEXCEPT
|
---|
404 | {
|
---|
405 | if (rThat.isNotEmpty())
|
---|
406 | return appendWorkerUtf16NoThrow(rThat.m_bstr, rThat.length());
|
---|
407 | return S_OK;
|
---|
408 | }
|
---|
409 |
|
---|
410 |
|
---|
411 | Bstr &Bstr::append(const RTCString &rThat)
|
---|
412 | {
|
---|
413 | if (rThat.isNotEmpty())
|
---|
414 | return appendWorkerUtf8(rThat.c_str(), rThat.length());
|
---|
415 | return *this;
|
---|
416 | }
|
---|
417 |
|
---|
418 |
|
---|
419 | HRESULT Bstr::appendNoThrow(const RTCString &rThat) RT_NOEXCEPT
|
---|
420 | {
|
---|
421 | if (rThat.isNotEmpty())
|
---|
422 | return appendWorkerUtf8NoThrow(rThat.c_str(), rThat.length());
|
---|
423 | return S_OK;
|
---|
424 | }
|
---|
425 |
|
---|
426 |
|
---|
427 | Bstr &Bstr::append(CBSTR pwszSrc)
|
---|
428 | {
|
---|
429 | if (pwszSrc && *pwszSrc)
|
---|
430 | return appendWorkerUtf16(pwszSrc, RTUtf16Len(pwszSrc));
|
---|
431 | return *this;
|
---|
432 | }
|
---|
433 |
|
---|
434 |
|
---|
435 | HRESULT Bstr::appendNoThrow(CBSTR pwszSrc) RT_NOEXCEPT
|
---|
436 | {
|
---|
437 | if (pwszSrc && *pwszSrc)
|
---|
438 | return appendWorkerUtf16NoThrow(pwszSrc, RTUtf16Len(pwszSrc));
|
---|
439 | return S_OK;
|
---|
440 | }
|
---|
441 |
|
---|
442 |
|
---|
443 | Bstr &Bstr::append(const char *pszSrc)
|
---|
444 | {
|
---|
445 | if (pszSrc && *pszSrc)
|
---|
446 | return appendWorkerUtf8(pszSrc, strlen(pszSrc));
|
---|
447 | return *this;
|
---|
448 | }
|
---|
449 |
|
---|
450 |
|
---|
451 | HRESULT Bstr::appendNoThrow(const char *pszSrc) RT_NOEXCEPT
|
---|
452 | {
|
---|
453 | if (pszSrc && *pszSrc)
|
---|
454 | return appendWorkerUtf8NoThrow(pszSrc, strlen(pszSrc));
|
---|
455 | return S_OK;
|
---|
456 | }
|
---|
457 |
|
---|
458 |
|
---|
459 | Bstr &Bstr::append(const Bstr &rThat, size_t offStart, size_t cwcMax /*= RTSTR_MAX*/)
|
---|
460 | {
|
---|
461 | size_t cwcSrc = rThat.length();
|
---|
462 | if (offStart < cwcSrc)
|
---|
463 | return appendWorkerUtf16(rThat.raw() + offStart, RT_MIN(cwcSrc - offStart, cwcMax));
|
---|
464 | return *this;
|
---|
465 | }
|
---|
466 |
|
---|
467 |
|
---|
468 | HRESULT Bstr::appendNoThrow(const Bstr &rThat, size_t offStart, size_t cwcMax /*= RTSTR_MAX*/) RT_NOEXCEPT
|
---|
469 | {
|
---|
470 | size_t cwcSrc = rThat.length();
|
---|
471 | if (offStart < cwcSrc)
|
---|
472 | return appendWorkerUtf16NoThrow(rThat.raw() + offStart, RT_MIN(cwcSrc - offStart, cwcMax));
|
---|
473 | return S_OK;
|
---|
474 | }
|
---|
475 |
|
---|
476 |
|
---|
477 | Bstr &Bstr::append(const RTCString &rThat, size_t offStart, size_t cchMax /*= RTSTR_MAX*/)
|
---|
478 | {
|
---|
479 | if (offStart < rThat.length())
|
---|
480 | return appendWorkerUtf8(rThat.c_str() + offStart, RT_MIN(rThat.length() - offStart, cchMax));
|
---|
481 | return *this;
|
---|
482 | }
|
---|
483 |
|
---|
484 |
|
---|
485 | HRESULT Bstr::appendNoThrow(const RTCString &rThat, size_t offStart, size_t cchMax /*= RTSTR_MAX*/) RT_NOEXCEPT
|
---|
486 | {
|
---|
487 | if (offStart < rThat.length())
|
---|
488 | return appendWorkerUtf8NoThrow(rThat.c_str() + offStart, RT_MIN(rThat.length() - offStart, cchMax));
|
---|
489 | return S_OK;
|
---|
490 | }
|
---|
491 |
|
---|
492 |
|
---|
493 | Bstr &Bstr::append(CBSTR pwszThat, size_t cchMax)
|
---|
494 | {
|
---|
495 | return appendWorkerUtf16(pwszThat, RTUtf16NLen(pwszThat, cchMax));
|
---|
496 | }
|
---|
497 |
|
---|
498 |
|
---|
499 | HRESULT Bstr::appendNoThrow(CBSTR pwszThat, size_t cchMax) RT_NOEXCEPT
|
---|
500 | {
|
---|
501 | return appendWorkerUtf16NoThrow(pwszThat, RTUtf16NLen(pwszThat, cchMax));
|
---|
502 | }
|
---|
503 |
|
---|
504 |
|
---|
505 | Bstr &Bstr::append(const char *pszThat, size_t cchMax)
|
---|
506 | {
|
---|
507 | return appendWorkerUtf8(pszThat, RTStrNLen(pszThat, cchMax));
|
---|
508 | }
|
---|
509 |
|
---|
510 |
|
---|
511 | HRESULT Bstr::appendNoThrow(const char *pszThat, size_t cchMax) RT_NOEXCEPT
|
---|
512 | {
|
---|
513 | return appendWorkerUtf8NoThrow(pszThat, RTStrNLen(pszThat, cchMax));
|
---|
514 | }
|
---|
515 |
|
---|
516 |
|
---|
517 | Bstr &Bstr::append(char ch)
|
---|
518 | {
|
---|
519 | AssertMsg(ch > 0 && ch < 127, ("%#x\n", ch));
|
---|
520 | return appendWorkerUtf8(&ch, 1);
|
---|
521 | }
|
---|
522 |
|
---|
523 |
|
---|
524 | HRESULT Bstr::appendNoThrow(char ch) RT_NOEXCEPT
|
---|
525 | {
|
---|
526 | AssertMsg(ch > 0 && ch < 127, ("%#x\n", ch));
|
---|
527 | return appendWorkerUtf8NoThrow(&ch, 1);
|
---|
528 | }
|
---|
529 |
|
---|
530 |
|
---|
531 | Bstr &Bstr::appendCodePoint(RTUNICP uc)
|
---|
532 | {
|
---|
533 | RTUTF16 wszTmp[3];
|
---|
534 | PRTUTF16 pwszEnd = RTUtf16PutCp(wszTmp, uc);
|
---|
535 | *pwszEnd = '\0';
|
---|
536 | return appendWorkerUtf16(&wszTmp[0], pwszEnd - &wszTmp[0]);
|
---|
537 | }
|
---|
538 |
|
---|
539 |
|
---|
540 | HRESULT Bstr::appendCodePointNoThrow(RTUNICP uc) RT_NOEXCEPT
|
---|
541 | {
|
---|
542 | RTUTF16 wszTmp[3];
|
---|
543 | PRTUTF16 pwszEnd = RTUtf16PutCp(wszTmp, uc);
|
---|
544 | *pwszEnd = '\0';
|
---|
545 | return appendWorkerUtf16NoThrow(&wszTmp[0], pwszEnd - &wszTmp[0]);
|
---|
546 | }
|
---|
547 |
|
---|
548 |
|
---|
549 | Bstr &Bstr::appendWorkerUtf16(PCRTUTF16 pwszSrc, size_t cwcSrc)
|
---|
550 | {
|
---|
551 | size_t cwcOld = length();
|
---|
552 | size_t cwcTotal = cwcOld + cwcSrc;
|
---|
553 | reserve(cwcTotal, true /*fForce*/);
|
---|
554 | if (cwcSrc)
|
---|
555 | memcpy(&m_bstr[cwcOld], pwszSrc, cwcSrc * sizeof(RTUTF16));
|
---|
556 | m_bstr[cwcTotal] = '\0';
|
---|
557 | return *this;
|
---|
558 | }
|
---|
559 |
|
---|
560 |
|
---|
561 | HRESULT Bstr::appendWorkerUtf16NoThrow(PCRTUTF16 pwszSrc, size_t cwcSrc) RT_NOEXCEPT
|
---|
562 | {
|
---|
563 | size_t cwcOld = length();
|
---|
564 | size_t cwcTotal = cwcOld + cwcSrc;
|
---|
565 | HRESULT hrc = reserveNoThrow(cwcTotal, true /*fForce*/);
|
---|
566 | if (hrc == S_OK)
|
---|
567 | {
|
---|
568 | if (cwcSrc)
|
---|
569 | memcpy(&m_bstr[cwcOld], pwszSrc, cwcSrc * sizeof(RTUTF16));
|
---|
570 | m_bstr[cwcTotal] = '\0';
|
---|
571 | }
|
---|
572 | return hrc;
|
---|
573 | }
|
---|
574 |
|
---|
575 |
|
---|
576 | Bstr &Bstr::appendWorkerUtf8(const char *pszSrc, size_t cchSrc)
|
---|
577 | {
|
---|
578 | size_t cwcSrc;
|
---|
579 | int rc = RTStrCalcUtf16LenEx(pszSrc, cchSrc, &cwcSrc);
|
---|
580 | #ifdef RT_EXCEPTIONS_ENABLED
|
---|
581 | AssertRCStmt(rc, throw std::bad_alloc());
|
---|
582 | #else
|
---|
583 | AssertRCReturn(rc, *this);
|
---|
584 | #endif
|
---|
585 |
|
---|
586 | size_t cwcOld = length();
|
---|
587 | size_t cwcTotal = cwcOld + cwcSrc;
|
---|
588 | reserve(cwcTotal, true /*fForce*/);
|
---|
589 | if (cwcSrc)
|
---|
590 | {
|
---|
591 | PRTUTF16 pwszDst = &m_bstr[cwcOld];
|
---|
592 | rc = RTStrToUtf16Ex(pszSrc, cchSrc, &pwszDst, cwcSrc + 1, NULL);
|
---|
593 | #ifdef RT_EXCEPTIONS_ENABLED
|
---|
594 | AssertRCStmt(rc, throw std::bad_alloc());
|
---|
595 | #else
|
---|
596 | AssertRC(rc);
|
---|
597 | #endif
|
---|
598 | }
|
---|
599 | m_bstr[cwcTotal] = '\0';
|
---|
600 | return *this;
|
---|
601 | }
|
---|
602 |
|
---|
603 |
|
---|
604 | HRESULT Bstr::appendWorkerUtf8NoThrow(const char *pszSrc, size_t cchSrc) RT_NOEXCEPT
|
---|
605 | {
|
---|
606 | size_t cwcSrc;
|
---|
607 | int rc = RTStrCalcUtf16LenEx(pszSrc, cchSrc, &cwcSrc);
|
---|
608 | AssertRCStmt(rc, E_INVALIDARG);
|
---|
609 |
|
---|
610 | size_t cwcOld = length();
|
---|
611 | size_t cwcTotal = cwcOld + cwcSrc;
|
---|
612 | HRESULT hrc = reserveNoThrow(cwcTotal, true /*fForce*/);
|
---|
613 | AssertReturn(hrc == S_OK, hrc);
|
---|
614 | if (cwcSrc)
|
---|
615 | {
|
---|
616 | PRTUTF16 pwszDst = &m_bstr[cwcOld];
|
---|
617 | rc = RTStrToUtf16Ex(pszSrc, cchSrc, &pwszDst, cwcSrc + 1, NULL);
|
---|
618 | AssertRCStmt(rc, E_INVALIDARG);
|
---|
619 | }
|
---|
620 | m_bstr[cwcTotal] = '\0';
|
---|
621 | return S_OK;
|
---|
622 | }
|
---|
623 |
|
---|
624 |
|
---|
625 | Bstr &Bstr::appendPrintf(const char *pszFormat, ...)
|
---|
626 | {
|
---|
627 | va_list va;
|
---|
628 | va_start(va, pszFormat);
|
---|
629 | HRESULT hrc = appendPrintfVNoThrow(pszFormat, va);
|
---|
630 | va_end(va);
|
---|
631 | #ifdef RT_EXCEPTIONS_ENABLED
|
---|
632 | if (hrc != S_OK)
|
---|
633 | throw std::bad_alloc();
|
---|
634 | #else
|
---|
635 | Assert(hrc == S_OK); RT_NOREF(hrc);
|
---|
636 | #endif
|
---|
637 | return *this;
|
---|
638 | }
|
---|
639 |
|
---|
640 |
|
---|
641 | HRESULT Bstr::appendPrintfNoThrow(const char *pszFormat, ...) RT_NOEXCEPT
|
---|
642 | {
|
---|
643 | va_list va;
|
---|
644 | va_start(va, pszFormat);
|
---|
645 | HRESULT hrc = appendPrintfVNoThrow(pszFormat, va);
|
---|
646 | va_end(va);
|
---|
647 | return hrc;
|
---|
648 | }
|
---|
649 |
|
---|
650 |
|
---|
651 | Bstr &Bstr::appendPrintfV(const char *pszFormat, va_list va)
|
---|
652 | {
|
---|
653 | HRESULT hrc = appendPrintfVNoThrow(pszFormat, va);
|
---|
654 | #ifdef RT_EXCEPTIONS_ENABLED
|
---|
655 | if (hrc != S_OK)
|
---|
656 | throw std::bad_alloc();
|
---|
657 | #else
|
---|
658 | Assert(hrc == S_OK); RT_NOREF(hrc);
|
---|
659 | #endif
|
---|
660 | return *this;
|
---|
661 | }
|
---|
662 |
|
---|
663 |
|
---|
664 | HRESULT Bstr::appendPrintfVNoThrow(const char *pszFormat, va_list va) RT_NOEXCEPT
|
---|
665 | {
|
---|
666 | size_t const cwcOld = length();
|
---|
667 | BSTRNOTHROW Args = { this, cwcOld, cwcOld, S_OK };
|
---|
668 |
|
---|
669 | RTStrFormatV(printfOutputCallbackNoThrow, &Args, NULL, NULL, pszFormat, va);
|
---|
670 | if (Args.hrc == S_OK)
|
---|
671 | {
|
---|
672 | Args.hrc = joltNoThrow(Args.offDst);
|
---|
673 | if (Args.hrc == S_OK)
|
---|
674 | return S_OK;
|
---|
675 | }
|
---|
676 |
|
---|
677 | if (m_bstr)
|
---|
678 | m_bstr[cwcOld] = '\0';
|
---|
679 | return Args.hrc;
|
---|
680 | }
|
---|
681 |
|
---|
682 |
|
---|
683 | Bstr &Bstr::erase(size_t offStart /*= 0*/, size_t cwcLength /*= RTSTR_MAX*/) RT_NOEXCEPT
|
---|
684 | {
|
---|
685 | size_t cwc = length();
|
---|
686 | if (offStart < cwc)
|
---|
687 | {
|
---|
688 | if (cwcLength >= cwc - offStart)
|
---|
689 | {
|
---|
690 | if (!offStart)
|
---|
691 | cleanup();
|
---|
692 | else
|
---|
693 | {
|
---|
694 | /* Trail removal, nothing to move. */
|
---|
695 | m_bstr[offStart] = '\0';
|
---|
696 | joltNoThrow(offStart); /* not entirely optimal... */
|
---|
697 | }
|
---|
698 | }
|
---|
699 | else if (cwcLength > 0)
|
---|
700 | {
|
---|
701 | /* Pull up the tail to offStart. */
|
---|
702 | size_t cwcAfter = cwc - offStart - cwcLength;
|
---|
703 | memmove(&m_bstr[offStart], &m_bstr[offStart + cwcLength], cwcAfter * sizeof(*m_bstr));
|
---|
704 | cwc -= cwcLength;
|
---|
705 | m_bstr[cwc] = '\0';
|
---|
706 | joltNoThrow(cwc); /* not entirely optimal... */
|
---|
707 | }
|
---|
708 | }
|
---|
709 | return *this;
|
---|
710 | }
|
---|
711 |
|
---|
712 |
|
---|
713 | void Bstr::cleanup()
|
---|
714 | {
|
---|
715 | if (m_bstr)
|
---|
716 | {
|
---|
717 | ::SysFreeString(m_bstr);
|
---|
718 | m_bstr = NULL;
|
---|
719 | }
|
---|
720 | }
|
---|
721 |
|
---|
722 |
|
---|
723 | void Bstr::copyFrom(const OLECHAR *a_bstrSrc)
|
---|
724 | {
|
---|
725 | if (a_bstrSrc && *a_bstrSrc)
|
---|
726 | {
|
---|
727 | m_bstr = ::SysAllocString(a_bstrSrc);
|
---|
728 | #ifdef RT_EXCEPTIONS_ENABLED
|
---|
729 | if (RT_LIKELY(m_bstr))
|
---|
730 | { /* likely */ }
|
---|
731 | else
|
---|
732 | throw std::bad_alloc();
|
---|
733 | #else
|
---|
734 | Assert(m_bstr);
|
---|
735 | #endif
|
---|
736 | }
|
---|
737 | else
|
---|
738 | m_bstr = NULL;
|
---|
739 | }
|
---|
740 |
|
---|
741 |
|
---|
742 | void Bstr::cleanupAndCopyFrom(const OLECHAR *a_bstrSrc)
|
---|
743 | {
|
---|
744 | cleanup();
|
---|
745 | copyFrom(a_bstrSrc);
|
---|
746 | }
|
---|
747 |
|
---|
748 |
|
---|
749 | HRESULT Bstr::cleanupAndCopyFromEx(const OLECHAR *a_bstrSrc) RT_NOEXCEPT
|
---|
750 | {
|
---|
751 | cleanup();
|
---|
752 |
|
---|
753 | if (a_bstrSrc && *a_bstrSrc)
|
---|
754 | {
|
---|
755 | m_bstr = ::SysAllocString(a_bstrSrc);
|
---|
756 | if (RT_LIKELY(m_bstr))
|
---|
757 | { /* likely */ }
|
---|
758 | else
|
---|
759 | return E_OUTOFMEMORY;
|
---|
760 | }
|
---|
761 | else
|
---|
762 | m_bstr = NULL;
|
---|
763 | return S_OK;
|
---|
764 | }
|
---|
765 |
|
---|
766 |
|
---|
767 |
|
---|
768 | /*********************************************************************************************************************************
|
---|
769 | * Utf8Str Implementation *
|
---|
770 | *********************************************************************************************************************************/
|
---|
771 |
|
---|
772 | /* static */
|
---|
773 | const Utf8Str Utf8Str::Empty; /* default ctor is OK */
|
---|
774 |
|
---|
775 | #if defined(VBOX_WITH_XPCOM)
|
---|
776 | void Utf8Str::cloneTo(char **pstr) const
|
---|
777 | {
|
---|
778 | size_t cb = length() + 1;
|
---|
779 | *pstr = (char *)nsMemory::Alloc(cb);
|
---|
780 | if (RT_LIKELY(*pstr))
|
---|
781 | memcpy(*pstr, c_str(), cb);
|
---|
782 | else
|
---|
783 | #ifdef RT_EXCEPTIONS_ENABLED
|
---|
784 | throw std::bad_alloc();
|
---|
785 | #else
|
---|
786 | AssertFailed();
|
---|
787 | #endif
|
---|
788 | }
|
---|
789 |
|
---|
790 | HRESULT Utf8Str::cloneToEx(char **pstr) const
|
---|
791 | {
|
---|
792 | size_t cb = length() + 1;
|
---|
793 | *pstr = (char *)nsMemory::Alloc(cb);
|
---|
794 | if (RT_LIKELY(*pstr))
|
---|
795 | {
|
---|
796 | memcpy(*pstr, c_str(), cb);
|
---|
797 | return S_OK;
|
---|
798 | }
|
---|
799 | return E_OUTOFMEMORY;
|
---|
800 | }
|
---|
801 | #endif
|
---|
802 |
|
---|
803 | HRESULT Utf8Str::cloneToEx(BSTR *pbstr) const RT_NOEXCEPT
|
---|
804 | {
|
---|
805 | if (!pbstr)
|
---|
806 | return S_OK;
|
---|
807 | Bstr bstr;
|
---|
808 | HRESULT hrc = bstr.assignEx(*this);
|
---|
809 | if (SUCCEEDED(hrc))
|
---|
810 | hrc = bstr.detachToEx(pbstr);
|
---|
811 | return hrc;
|
---|
812 | }
|
---|
813 |
|
---|
814 | Utf8Str& Utf8Str::stripTrailingSlash()
|
---|
815 | {
|
---|
816 | if (length())
|
---|
817 | {
|
---|
818 | ::RTPathStripTrailingSlash(m_psz);
|
---|
819 | jolt();
|
---|
820 | }
|
---|
821 | return *this;
|
---|
822 | }
|
---|
823 |
|
---|
824 | Utf8Str& Utf8Str::stripFilename()
|
---|
825 | {
|
---|
826 | if (length())
|
---|
827 | {
|
---|
828 | RTPathStripFilename(m_psz);
|
---|
829 | jolt();
|
---|
830 | }
|
---|
831 | return *this;
|
---|
832 | }
|
---|
833 |
|
---|
834 | Utf8Str& Utf8Str::stripPath()
|
---|
835 | {
|
---|
836 | if (length())
|
---|
837 | {
|
---|
838 | char *pszName = ::RTPathFilename(m_psz);
|
---|
839 | if (pszName)
|
---|
840 | {
|
---|
841 | size_t cchName = length() - (pszName - m_psz);
|
---|
842 | memmove(m_psz, pszName, cchName + 1);
|
---|
843 | jolt();
|
---|
844 | }
|
---|
845 | else
|
---|
846 | cleanup();
|
---|
847 | }
|
---|
848 | return *this;
|
---|
849 | }
|
---|
850 |
|
---|
851 | Utf8Str& Utf8Str::stripSuffix()
|
---|
852 | {
|
---|
853 | if (length())
|
---|
854 | {
|
---|
855 | RTPathStripSuffix(m_psz);
|
---|
856 | jolt();
|
---|
857 | }
|
---|
858 | return *this;
|
---|
859 | }
|
---|
860 |
|
---|
861 | size_t Utf8Str::parseKeyValue(Utf8Str &a_rKey, Utf8Str &a_rValue, size_t a_offStart /* = 0*/,
|
---|
862 | const Utf8Str &a_rPairSeparator /*= ","*/, const Utf8Str &a_rKeyValueSeparator /*= "="*/) const
|
---|
863 | {
|
---|
864 | /* Find the end of the next pair, skipping empty pairs.
|
---|
865 | Note! The skipping allows us to pass the return value of a parseKeyValue()
|
---|
866 | call as offStart to the next call. */
|
---|
867 | size_t offEnd;
|
---|
868 | while ( a_offStart == (offEnd = find(&a_rPairSeparator, a_offStart))
|
---|
869 | && offEnd != npos)
|
---|
870 | a_offStart++;
|
---|
871 |
|
---|
872 | /* Look for a key/value separator before the end of the pair.
|
---|
873 | ASSUMES npos value returned by find when the substring is not found is
|
---|
874 | really high. */
|
---|
875 | size_t offKeyValueSep = find(&a_rKeyValueSeparator, a_offStart);
|
---|
876 | if (offKeyValueSep < offEnd)
|
---|
877 | {
|
---|
878 | a_rKey = substr(a_offStart, offKeyValueSep - a_offStart);
|
---|
879 | if (offEnd == npos)
|
---|
880 | offEnd = m_cch; /* No confusing npos when returning strings. */
|
---|
881 | a_rValue = substr(offKeyValueSep + 1, offEnd - offKeyValueSep - 1);
|
---|
882 | }
|
---|
883 | else
|
---|
884 | {
|
---|
885 | a_rKey.setNull();
|
---|
886 | a_rValue.setNull();
|
---|
887 | }
|
---|
888 |
|
---|
889 | return offEnd;
|
---|
890 | }
|
---|
891 |
|
---|
892 | /**
|
---|
893 | * Internal function used in Utf8Str copy constructors and assignment when
|
---|
894 | * copying from a UTF-16 string.
|
---|
895 | *
|
---|
896 | * As with the RTCString::copyFrom() variants, this unconditionally sets the
|
---|
897 | * members to a copy of the given other strings and makes no assumptions about
|
---|
898 | * previous contents. This can therefore be used both in copy constructors,
|
---|
899 | * when member variables have no defined value, and in assignments after having
|
---|
900 | * called cleanup().
|
---|
901 | *
|
---|
902 | * This variant converts from a UTF-16 string, most probably from
|
---|
903 | * a Bstr assignment.
|
---|
904 | *
|
---|
905 | * @param a_pbstr The source string. The caller guarantees that this
|
---|
906 | * is valid UTF-16.
|
---|
907 | * @param a_cwcMax The number of characters to be copied. If set to RTSTR_MAX,
|
---|
908 | * the entire string will be copied.
|
---|
909 | *
|
---|
910 | * @sa RTCString::copyFromN
|
---|
911 | */
|
---|
912 | void Utf8Str::copyFrom(CBSTR a_pbstr, size_t a_cwcMax)
|
---|
913 | {
|
---|
914 | if (a_pbstr && *a_pbstr)
|
---|
915 | {
|
---|
916 | int vrc = RTUtf16ToUtf8Ex((PCRTUTF16)a_pbstr,
|
---|
917 | a_cwcMax, // size_t cwcString: translate entire string
|
---|
918 | &m_psz, // char **ppsz: output buffer
|
---|
919 | 0, // size_t cch: if 0, func allocates buffer in *ppsz
|
---|
920 | &m_cch); // size_t *pcch: receives the size of the output string, excluding the terminator.
|
---|
921 | if (RT_SUCCESS(vrc))
|
---|
922 | m_cbAllocated = m_cch + 1;
|
---|
923 | else
|
---|
924 | {
|
---|
925 | if ( vrc != VERR_NO_STR_MEMORY
|
---|
926 | && vrc != VERR_NO_MEMORY)
|
---|
927 | {
|
---|
928 | /* ASSUME: input is valid Utf-16. Fake out of memory error. */
|
---|
929 | AssertLogRelMsgFailed(("%Rrc %.*Rhxs\n", vrc, RTUtf16Len(a_pbstr) * sizeof(RTUTF16), a_pbstr));
|
---|
930 | }
|
---|
931 |
|
---|
932 | m_cch = 0;
|
---|
933 | m_cbAllocated = 0;
|
---|
934 | m_psz = NULL;
|
---|
935 |
|
---|
936 | #ifdef RT_EXCEPTIONS_ENABLED
|
---|
937 | throw std::bad_alloc();
|
---|
938 | #else
|
---|
939 | AssertFailed();
|
---|
940 | #endif
|
---|
941 | }
|
---|
942 | }
|
---|
943 | else
|
---|
944 | {
|
---|
945 | m_cch = 0;
|
---|
946 | m_cbAllocated = 0;
|
---|
947 | m_psz = NULL;
|
---|
948 | }
|
---|
949 | }
|
---|
950 |
|
---|
951 | /**
|
---|
952 | * A variant of Utf8Str::copyFrom that does not throw any exceptions but returns
|
---|
953 | * E_OUTOFMEMORY instead.
|
---|
954 | *
|
---|
955 | * @param a_pbstr The source string.
|
---|
956 | * @returns S_OK or E_OUTOFMEMORY.
|
---|
957 | */
|
---|
958 | HRESULT Utf8Str::copyFromEx(CBSTR a_pbstr)
|
---|
959 | {
|
---|
960 | if (a_pbstr && *a_pbstr)
|
---|
961 | {
|
---|
962 | int vrc = RTUtf16ToUtf8Ex((PCRTUTF16)a_pbstr,
|
---|
963 | RTSTR_MAX, // size_t cwcString: translate entire string
|
---|
964 | &m_psz, // char **ppsz: output buffer
|
---|
965 | 0, // size_t cch: if 0, func allocates buffer in *ppsz
|
---|
966 | &m_cch); // size_t *pcch: receives the size of the output string, excluding the terminator.
|
---|
967 | if (RT_SUCCESS(vrc))
|
---|
968 | m_cbAllocated = m_cch + 1;
|
---|
969 | else
|
---|
970 | {
|
---|
971 | if ( vrc != VERR_NO_STR_MEMORY
|
---|
972 | && vrc != VERR_NO_MEMORY)
|
---|
973 | {
|
---|
974 | /* ASSUME: input is valid Utf-16. Fake out of memory error. */
|
---|
975 | AssertLogRelMsgFailed(("%Rrc %.*Rhxs\n", vrc, RTUtf16Len(a_pbstr) * sizeof(RTUTF16), a_pbstr));
|
---|
976 | }
|
---|
977 |
|
---|
978 | m_cch = 0;
|
---|
979 | m_cbAllocated = 0;
|
---|
980 | m_psz = NULL;
|
---|
981 |
|
---|
982 | return E_OUTOFMEMORY;
|
---|
983 | }
|
---|
984 | }
|
---|
985 | else
|
---|
986 | {
|
---|
987 | m_cch = 0;
|
---|
988 | m_cbAllocated = 0;
|
---|
989 | m_psz = NULL;
|
---|
990 | }
|
---|
991 | return S_OK;
|
---|
992 | }
|
---|
993 |
|
---|
994 |
|
---|
995 | /**
|
---|
996 | * A variant of Utf8Str::copyFromN that does not throw any exceptions but
|
---|
997 | * returns E_OUTOFMEMORY instead.
|
---|
998 | *
|
---|
999 | * @param a_pcszSrc The source string.
|
---|
1000 | * @param a_offSrc Start offset to copy from.
|
---|
1001 | * @param a_cchSrc How much to copy
|
---|
1002 | * @returns S_OK or E_OUTOFMEMORY.
|
---|
1003 | *
|
---|
1004 | * @remarks This calls cleanup() first, so the caller doesn't have to. (Saves
|
---|
1005 | * code space.)
|
---|
1006 | */
|
---|
1007 | HRESULT Utf8Str::copyFromExNComRC(const char *a_pcszSrc, size_t a_offSrc, size_t a_cchSrc)
|
---|
1008 | {
|
---|
1009 | Assert(!a_cchSrc || !m_psz || (uintptr_t)&a_pcszSrc[a_offSrc] - (uintptr_t)m_psz >= (uintptr_t)m_cbAllocated);
|
---|
1010 | cleanup();
|
---|
1011 | if (a_cchSrc)
|
---|
1012 | {
|
---|
1013 | m_psz = RTStrAlloc(a_cchSrc + 1);
|
---|
1014 | if (RT_LIKELY(m_psz))
|
---|
1015 | {
|
---|
1016 | m_cch = a_cchSrc;
|
---|
1017 | m_cbAllocated = a_cchSrc + 1;
|
---|
1018 | memcpy(m_psz, a_pcszSrc + a_offSrc, a_cchSrc);
|
---|
1019 | m_psz[a_cchSrc] = '\0';
|
---|
1020 | }
|
---|
1021 | else
|
---|
1022 | {
|
---|
1023 | m_cch = 0;
|
---|
1024 | m_cbAllocated = 0;
|
---|
1025 | return E_OUTOFMEMORY;
|
---|
1026 | }
|
---|
1027 | }
|
---|
1028 | else
|
---|
1029 | {
|
---|
1030 | m_cch = 0;
|
---|
1031 | m_cbAllocated = 0;
|
---|
1032 | m_psz = NULL;
|
---|
1033 | }
|
---|
1034 | return S_OK;
|
---|
1035 | }
|
---|
1036 |
|
---|
1037 | } /* namespace com */
|
---|