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