1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
|
/* $Id: string.h $ */
/** @file
* MS COM / XPCOM Abstraction Layer - Smart string classes declaration.
*/
/*
* Copyright (C) 2006-2025 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
* in the VirtualBox distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
*/
#ifndef VBOX_INCLUDED_com_string_h
#define VBOX_INCLUDED_com_string_h
#ifndef RT_WITHOUT_PRAGMA_ONCE
# pragma once
#endif
/* Make sure all the stdint.h macros are included - must come first! */
#ifndef __STDC_LIMIT_MACROS
# define __STDC_LIMIT_MACROS
#endif
#ifndef __STDC_CONSTANT_MACROS
# define __STDC_CONSTANT_MACROS
#endif
#if defined(VBOX_WITH_XPCOM)
# include <nsMemory.h>
#endif
#include "VBox/com/defs.h"
#include "VBox/com/assert.h"
#include <iprt/mem.h>
#include <iprt/utf16.h>
#include <iprt/cpp/ministring.h>
/** @defgroup grp_com_str Smart String Classes
* @ingroup grp_com
* @{
*/
namespace com
{
class Utf8Str;
// global constant in glue/string.cpp that represents an empty BSTR
extern const BSTR g_bstrEmpty;
/**
* String class used universally in Main for COM-style Utf-16 strings.
*
* Unfortunately COM on Windows uses UTF-16 everywhere, requiring conversions
* back and forth since most of VirtualBox and our libraries use UTF-8.
*
* To make things more obscure, on Windows, a COM-style BSTR is not just a
* pointer to a null-terminated wide character array, but the four bytes (32
* bits) BEFORE the memory that the pointer points to are a length DWORD. One
* must therefore avoid pointer arithmetic and always use SysAllocString and
* the like to deal with BSTR pointers, which manage that DWORD correctly.
*
* For platforms other than Windows, we provide our own versions of the Sys*
* functions in Main/xpcom/helpers.cpp which do NOT use length prefixes though
* to be compatible with how XPCOM allocates string parameters to public
* functions.
*
* The Bstr class hides all this handling behind a std::string-like interface
* and also provides automatic conversions to RTCString and Utf8Str instances.
*
* The one advantage of using the SysString* routines is that this makes it
* possible to use it as a type of member variables of COM/XPCOM components and
* pass their values to callers through component methods' output parameters
* using the #cloneTo() operation. Also, the class can adopt (take ownership
* of) string buffers returned in output parameters of COM methods using the
* #asOutParam() operation and correctly free them afterwards.
*
* Starting with VirtualBox 3.2, like Utf8Str, Bstr no longer differentiates
* between NULL strings and empty strings. In other words, Bstr("") and
* Bstr(NULL) behave the same. In both cases, Bstr allocates no memory,
* reports a zero length and zero allocated bytes for both, and returns an
* empty C wide string from raw().
*
* @note All Bstr methods ASSUMES valid UTF-16 or UTF-8 input strings.
* The VirtualBox policy in this regard is to validate strings coming
* from external sources before passing them to Bstr or Utf8Str.
*/
class Bstr
{
public:
Bstr()
: m_bstr(NULL)
{ }
Bstr(const Bstr &that)
{
copyFrom((const OLECHAR *)that.m_bstr);
}
Bstr(CBSTR that)
{
copyFrom((const OLECHAR *)that);
}
#if defined(VBOX_WITH_XPCOM)
Bstr(const wchar_t *that)
{
AssertCompile(sizeof(wchar_t) == sizeof(OLECHAR));
copyFrom((const OLECHAR *)that);
}
#endif
Bstr(const RTCString &that)
{
copyFrom(that.c_str());
}
Bstr(const char *that)
{
copyFrom(that);
}
Bstr(const char *a_pThat, size_t a_cchMax)
{
copyFromN(a_pThat, a_cchMax);
}
~Bstr()
{
setNull();
}
Bstr &operator=(const Bstr &that)
{
cleanupAndCopyFrom((const OLECHAR *)that.m_bstr);
return *this;
}
Bstr &operator=(CBSTR that)
{
cleanupAndCopyFrom((const OLECHAR *)that);
return *this;
}
#if defined(VBOX_WITH_XPCOM)
Bstr &operator=(const wchar_t *that)
{
cleanupAndCopyFrom((const OLECHAR *)that);
return *this;
}
#endif
Bstr &setNull()
{
cleanup();
return *this;
}
/**
* Extended assignment method that returns a COM status code instead of an
* exception on failure.
*
* @returns S_OK or E_OUTOFMEMORY.
* @param a_rSrcStr The source string
*/
HRESULT assignEx(const Bstr &a_rSrcStr) RT_NOEXCEPT
{
return cleanupAndCopyFromEx((const OLECHAR *)a_rSrcStr.m_bstr);
}
/**
* Extended assignment method that returns a COM status code instead of an
* exception on failure.
*
* @returns S_OK or E_OUTOFMEMORY.
* @param a_pSrcStr The source string
*/
HRESULT assignEx(CBSTR a_pSrcStr) RT_NOEXCEPT
{
return cleanupAndCopyFromEx((const OLECHAR *)a_pSrcStr);
}
/**
* Assign the value of a RTCString/Utf8Str string, no exceptions.
*
* @returns S_OK or E_OUTOFMEMORY.
* @param a_rSrcStr The source string
*/
HRESULT assignEx(RTCString const &a_rSrcStr) RT_NOEXCEPT
{
return cleanupAndCopyFromNoThrow(a_rSrcStr.c_str(), a_rSrcStr.length());
}
/**
* Assign the value of a RTCString/Utf8Str substring, no exceptions.
*
* @returns S_OK, E_OUTOFMEMORY or E_INVALIDARG.
* @param a_rSrcStr The source string
* @param a_offSrc The character (byte) offset of the substring.
* @param a_cchSrc The number of characters (bytes) to copy from the source
* string.
*/
HRESULT assignEx(RTCString const &a_rSrcStr, size_t a_offSrc, size_t a_cchSrc) RT_NOEXCEPT
{
size_t const cchTmp = a_rSrcStr.length();
if ( a_offSrc + a_cchSrc < cchTmp
&& a_offSrc < cchTmp)
return cleanupAndCopyFromNoThrow(a_rSrcStr.c_str() + a_offSrc, a_cchSrc);
return E_INVALIDARG;
}
/**
* Assign the value of a zero terminated UTF-8 string, no exceptions.
*
* @returns S_OK or E_OUTOFMEMORY.
* @param a_pszSrcStr The source string.
*/
HRESULT assignEx(const char *a_pszSrcStr) RT_NOEXCEPT
{
return cleanupAndCopyFromNoThrow(a_pszSrcStr, RTSTR_MAX);
}
/**
* Assign the value of a UTF-8 substring, no exceptions.
*
* @returns S_OK or E_OUTOFMEMORY.
* @param a_pszSrcStr The source string.
* @param a_cchSrc The number of characters (bytes) to copy from the source
* string.
*/
HRESULT assignEx(const char *a_pszSrcStr, size_t a_cchSrc) RT_NOEXCEPT
{
return cleanupAndCopyFromNoThrow(a_pszSrcStr, a_cchSrc);
}
#ifdef _MSC_VER
# if _MSC_VER >= 1400
RTMEMEF_NEW_AND_DELETE_OPERATORS();
# endif
#else
RTMEMEF_NEW_AND_DELETE_OPERATORS();
#endif
/** Case sensitivity selector. */
enum CaseSensitivity
{
CaseSensitive,
CaseInsensitive
};
/**
* Compares the member string to str.
* @param str
* @param cs Whether comparison should be case-sensitive.
* @return
*/
int compare(CBSTR str, CaseSensitivity cs = CaseSensitive) const
{
if (cs == CaseSensitive)
return ::RTUtf16Cmp((PRTUTF16)m_bstr, (PRTUTF16)str);
return ::RTUtf16LocaleICmp((PRTUTF16)m_bstr, (PRTUTF16)str);
}
int compare(BSTR str, CaseSensitivity cs = CaseSensitive) const
{
return compare((CBSTR)str, cs);
}
int compare(const Bstr &that, CaseSensitivity cs = CaseSensitive) const
{
return compare(that.m_bstr, cs);
}
bool operator==(const Bstr &that) const { return !compare(that.m_bstr); }
bool operator==(CBSTR that) const { return !compare(that); }
bool operator==(BSTR that) const { return !compare(that); }
bool operator!=(const Bstr &that) const { return !!compare(that.m_bstr); }
bool operator!=(CBSTR that) const { return !!compare(that); }
bool operator!=(BSTR that) const { return !!compare(that); }
bool operator<(const Bstr &that) const { return compare(that.m_bstr) < 0; }
bool operator<(CBSTR that) const { return compare(that) < 0; }
bool operator<(BSTR that) const { return compare(that) < 0; }
bool operator<=(const Bstr &that) const { return compare(that.m_bstr) <= 0; }
bool operator<=(CBSTR that) const { return compare(that) <= 0; }
bool operator<=(BSTR that) const { return compare(that) <= 0; }
bool operator>(const Bstr &that) const { return compare(that.m_bstr) > 0; }
bool operator>(CBSTR that) const { return compare(that) > 0; }
bool operator>(BSTR that) const { return compare(that) > 0; }
bool operator>=(const Bstr &that) const { return compare(that.m_bstr) >= 0; }
bool operator>=(CBSTR that) const { return compare(that) >= 0; }
bool operator>=(BSTR that) const { return compare(that) >= 0; }
/**
* Compares this string to an UTF-8 C style string.
*
* @retval 0 if equal
* @retval -1 if this string is smaller than the UTF-8 one.
* @retval 1 if the UTF-8 string is smaller than this.
*
* @param a_pszRight The string to compare with.
* @param a_enmCase Whether comparison should be case-sensitive.
*/
int compareUtf8(const char *a_pszRight, CaseSensitivity a_enmCase = CaseSensitive) const;
/** Java style compare method.
* @returns true if @a a_pszRight equals this string.
* @param a_pszRight The (UTF-8) string to compare with. */
bool equals(const char *a_pszRight) const { return compareUtf8(a_pszRight, CaseSensitive) == 0; }
/** Java style case-insensitive compare method.
* @returns true if @a a_pszRight equals this string.
* @param a_pszRight The (UTF-8) string to compare with. */
bool equalsIgnoreCase(const char *a_pszRight) const { return compareUtf8(a_pszRight, CaseInsensitive) == 0; }
/** Java style compare method.
* @returns true if @a a_rThat equals this string.
* @param a_rThat The other Bstr instance to compare with. */
bool equals(const Bstr &a_rThat) const { return compare(a_rThat.m_bstr, CaseSensitive) == 0; }
/** Java style case-insensitive compare method.
* @returns true if @a a_rThat equals this string.
* @param a_rThat The other Bstr instance to compare with. */
bool equalsIgnoreCase(const Bstr &a_rThat) const { return compare(a_rThat.m_bstr, CaseInsensitive) == 0; }
/** Java style compare method.
* @returns true if @a a_pThat equals this string.
* @param a_pThat The native const BSTR to compare with. */
bool equals(CBSTR a_pThat) const { return compare(a_pThat, CaseSensitive) == 0; }
/** Java style case-insensitive compare method.
* @returns true if @a a_pThat equals this string.
* @param a_pThat The native const BSTR to compare with. */
bool equalsIgnoreCase(CBSTR a_pThat) const { return compare(a_pThat, CaseInsensitive) == 0; }
/** Java style compare method.
* @returns true if @a a_pThat equals this string.
* @param a_pThat The native BSTR to compare with. */
bool equals(BSTR a_pThat) const { return compare(a_pThat, CaseSensitive) == 0; }
/** Java style case-insensitive compare method.
* @returns true if @a a_pThat equals this string.
* @param a_pThat The native BSTR to compare with. */
bool equalsIgnoreCase(BSTR a_pThat) const { return compare(a_pThat, CaseInsensitive) == 0; }
/**
* Checks if the string starts with @a a_rStart.
*/
bool startsWith(Bstr const &a_rStart) const;
/**
* Checks if the string starts with @a a_rStart.
*/
bool startsWith(RTCString const &a_rStart) const;
/**
* Checks if the string starts with @a a_pszStart.
*/
bool startsWith(const char *a_pszStart) const;
/**
* Returns true if the member string has no length.
* This is true for instances created from both NULL and "" input strings.
*
* @note Always use this method to check if an instance is empty. Do not
* use length() because that may need to run through the entire string
* (Bstr does not cache string lengths).
*/
bool isEmpty() const { return m_bstr == NULL || *m_bstr == 0; }
/**
* Returns true if the member string has a length of one or more.
*
* @returns true if not empty, false if empty (NULL or "").
*/
bool isNotEmpty() const { return m_bstr != NULL && *m_bstr != 0; }
size_t length() const { return isEmpty() ? 0 : ::RTUtf16Len((PRTUTF16)m_bstr); }
/**
* Assigns the output of the string format operation (RTStrPrintf).
*
* @param pszFormat Pointer to the format string,
* @see pg_rt_str_format.
* @param ... Ellipsis containing the arguments specified by
* the format string.
*
* @throws std::bad_alloc On allocation error. Object state is undefined.
*
* @returns Reference to the object.
*/
Bstr &printf(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
/**
* Assigns the output of the string format operation (RTStrPrintf).
*
* @param pszFormat Pointer to the format string,
* @see pg_rt_str_format.
* @param ... Ellipsis containing the arguments specified by
* the format string.
*
* @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
*/
HRESULT printfNoThrow(const char *pszFormat, ...) RT_NOEXCEPT RT_IPRT_FORMAT_ATTR(1, 2);
/**
* Assigns the output of the string format operation (RTStrPrintfV).
*
* @param pszFormat Pointer to the format string,
* @see pg_rt_str_format.
* @param va Argument vector containing the arguments
* specified by the format string.
*
* @throws std::bad_alloc On allocation error. Object state is undefined.
*
* @returns Reference to the object.
*/
Bstr &printfV(const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0);
/**
* Assigns the output of the string format operation (RTStrPrintfV).
*
* @param pszFormat Pointer to the format string,
* @see pg_rt_str_format.
* @param va Argument vector containing the arguments
* specified by the format string.
*
* @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
*/
HRESULT printfVNoThrow(const char *pszFormat, va_list va) RT_NOEXCEPT RT_IPRT_FORMAT_ATTR(1, 0);
/** @name Append methods and operators
* @{ */
/**
* Appends the string @a that to @a rThat.
*
* @param rThat The string to append.
* @throws std::bad_alloc On allocation error. The object is left unchanged.
* @returns Reference to the object.
*/
Bstr &append(const Bstr &rThat);
/**
* Appends the string @a that to @a rThat.
*
* @param rThat The string to append.
* @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
*/
HRESULT appendNoThrow(const Bstr &rThat) RT_NOEXCEPT;
/**
* Appends the UTF-8 string @a that to @a rThat.
*
* @param rThat The string to append.
* @throws std::bad_alloc On allocation error. The object is left unchanged.
* @returns Reference to the object.
*/
Bstr &append(const RTCString &rThat);
/**
* Appends the UTF-8 string @a that to @a rThat.
*
* @param rThat The string to append.
* @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
*/
HRESULT appendNoThrow(const RTCString &rThat) RT_NOEXCEPT;
/**
* Appends the UTF-16 string @a pszSrc to @a this.
*
* @param pwszSrc The C-style UTF-16 string to append.
* @throws std::bad_alloc On allocation error. The object is left unchanged.
* @returns Reference to the object.
*/
Bstr &append(CBSTR pwszSrc);
/**
* Appends the UTF-16 string @a pszSrc to @a this.
*
* @param pwszSrc The C-style UTF-16 string to append.
* @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
*/
HRESULT appendNoThrow(CBSTR pwszSrc) RT_NOEXCEPT;
/**
* Appends the UTF-8 string @a pszSrc to @a this.
*
* @param pszSrc The C-style string to append.
* @throws std::bad_alloc On allocation error. The object is left unchanged.
* @returns Reference to the object.
*/
Bstr &append(const char *pszSrc);
/**
* Appends the UTF-8 string @a pszSrc to @a this.
*
* @param pszSrc The C-style string to append.
* @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
*/
HRESULT appendNoThrow(const char *pszSrc) RT_NOEXCEPT;
/**
* Appends the a substring from @a rThat to @a this.
*
* @param rThat The string to append a substring from.
* @param offStart The start of the substring to append (UTF-16
* offset, not codepoint).
* @param cwcMax The maximum number of UTF-16 units to append.
* @throws std::bad_alloc On allocation error. The object is left unchanged.
* @returns Reference to the object.
*/
Bstr &append(const Bstr &rThat, size_t offStart, size_t cwcMax = RTSTR_MAX);
/**
* Appends the a substring from @a rThat to @a this.
*
* @param rThat The string to append a substring from.
* @param offStart The start of the substring to append (UTF-16
* offset, not codepoint).
* @param cwcMax The maximum number of UTF-16 units to append.
* @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
*/
HRESULT appendNoThrow(const Bstr &rThat, size_t offStart, size_t cwcMax = RTSTR_MAX) RT_NOEXCEPT;
/**
* Appends the a substring from UTF-8 @a rThat to @a this.
*
* @param rThat The string to append a substring from.
* @param offStart The start of the substring to append (byte offset,
* not codepoint).
* @param cchMax The maximum number of bytes to append.
* @throws std::bad_alloc On allocation error. The object is left unchanged.
* @returns Reference to the object.
*/
Bstr &append(const RTCString &rThat, size_t offStart, size_t cchMax = RTSTR_MAX);
/**
* Appends the a substring from UTF-8 @a rThat to @a this.
*
* @param rThat The string to append a substring from.
* @param offStart The start of the substring to append (byte offset,
* not codepoint).
* @param cchMax The maximum number of bytes to append.
* @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
*/
HRESULT appendNoThrow(const RTCString &rThat, size_t offStart, size_t cchMax = RTSTR_MAX) RT_NOEXCEPT;
/**
* Appends the first @a cchMax chars from UTF-16 string @a pszThat to @a this.
*
* @param pwszThat The C-style UTF-16 string to append.
* @param cchMax The maximum number of bytes to append.
* @throws std::bad_alloc On allocation error. The object is left unchanged.
* @returns Reference to the object.
*/
Bstr &append(CBSTR pwszThat, size_t cchMax);
/**
* Appends the first @a cchMax chars from UTF-16 string @a pszThat to @a this.
*
* @param pwszThat The C-style UTF-16 string to append.
* @param cchMax The maximum number of bytes to append.
* @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
*/
HRESULT appendNoThrow(CBSTR pwszThat, size_t cchMax) RT_NOEXCEPT;
/**
* Appends the first @a cchMax chars from string @a pszThat to @a this.
*
* @param pszThat The C-style string to append.
* @param cchMax The maximum number of bytes to append.
* @throws std::bad_alloc On allocation error. The object is left unchanged.
* @returns Reference to the object.
*/
Bstr &append(const char *pszThat, size_t cchMax);
/**
* Appends the first @a cchMax chars from string @a pszThat to @a this.
*
* @param pszThat The C-style string to append.
* @param cchMax The maximum number of bytes to append.
* @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
*/
HRESULT appendNoThrow(const char *pszThat, size_t cchMax) RT_NOEXCEPT;
/**
* Appends the given character to @a this.
*
* @param ch The character to append.
* @throws std::bad_alloc On allocation error. The object is left unchanged.
* @returns Reference to the object.
*/
Bstr &append(char ch);
/**
* Appends the given character to @a this.
*
* @param ch The character to append.
* @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
*/
HRESULT appendNoThrow(char ch) RT_NOEXCEPT;
/**
* Appends the given unicode code point to @a this.
*
* @param uc The unicode code point to append.
* @throws std::bad_alloc On allocation error. The object is left unchanged.
* @returns Reference to the object.
*/
Bstr &appendCodePoint(RTUNICP uc);
/**
* Appends the given unicode code point to @a this.
*
* @param uc The unicode code point to append.
* @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
*/
HRESULT appendCodePointNoThrow(RTUNICP uc) RT_NOEXCEPT;
/**
* Appends the output of the string format operation (RTStrPrintf).
*
* @param pszFormat Pointer to the format string,
* @see pg_rt_str_format.
* @param ... Ellipsis containing the arguments specified by
* the format string.
*
* @throws std::bad_alloc On allocation error. Object state is undefined.
*
* @returns Reference to the object.
*/
Bstr &appendPrintf(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
/**
* Appends the output of the string format operation (RTStrPrintf).
*
* @param pszFormat Pointer to the format string,
* @see pg_rt_str_format.
* @param ... Ellipsis containing the arguments specified by
* the format string.
*
* @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
*/
HRESULT appendPrintfNoThrow(const char *pszFormat, ...) RT_NOEXCEPT RT_IPRT_FORMAT_ATTR(1, 2);
/**
* Appends the output of the string format operation (RTStrPrintfV).
*
* @param pszFormat Pointer to the format string,
* @see pg_rt_str_format.
* @param va Argument vector containing the arguments
* specified by the format string.
*
* @throws std::bad_alloc On allocation error. Object state is undefined.
*
* @returns Reference to the object.
*/
Bstr &appendPrintfV(const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0);
/**
* Appends the output of the string format operation (RTStrPrintfV).
*
* @param pszFormat Pointer to the format string,
* @see pg_rt_str_format.
* @param va Argument vector containing the arguments
* specified by the format string.
*
* @returns S_OK, E_OUTOFMEMORY or E_INVAL (bad encoding).
*/
HRESULT appendPrintfVNoThrow(const char *pszFormat, va_list va) RT_NOEXCEPT RT_IPRT_FORMAT_ATTR(1, 0);
/**
* Shortcut to append(), Bstr variant.
*
* @param rThat The string to append.
* @returns Reference to the object.
*/
Bstr &operator+=(const Bstr &rThat)
{
return append(rThat);
}
/**
* Shortcut to append(), RTCString variant.
*
* @param rThat The string to append.
* @returns Reference to the object.
*/
Bstr &operator+=(const RTCString &rThat)
{
return append(rThat);
}
/**
* Shortcut to append(), CBSTR variant.
*
* @param pwszThat The C-style string to append.
* @returns Reference to the object.
*/
Bstr &operator+=(CBSTR pwszThat)
{
return append(pwszThat);
}
/**
* Shortcut to append(), const char * variant.
*
* @param pszThat The C-style string to append.
* @returns Reference to the object.
*/
Bstr &operator+=(const char *pszThat)
{
return append(pszThat);
}
/**
* Shortcut to append(), char variant.
*
* @param ch The character to append.
*
* @returns Reference to the object.
*/
Bstr &operator+=(char ch)
{
return append(ch);
}
/** @} */
/**
* Erases a sequence from the string.
*
* @returns Reference to the object.
* @param offStart Where in @a this string to start erasing (UTF-16
* units, not codepoints).
* @param cwcLength How much following @a offStart to erase (UTF-16
* units, not codepoints).
*/
Bstr &erase(size_t offStart = 0, size_t cwcLength = RTSTR_MAX) RT_NOEXCEPT;
/** @name BASE64 related methods
* @{ */
/**
* Encodes the given data as BASE64.
*
* @returns S_OK or E_OUTOFMEMORY.
* @param pvData Pointer to the data to encode.
* @param cbData Number of bytes to encode.
* @param fLineBreaks Whether to add line breaks (true) or just encode it
* as a continuous string.
* @sa RTBase64EncodeUtf16
*/
HRESULT base64Encode(const void *pvData, size_t cbData, bool fLineBreaks = false);
/**
* Decodes the string as BASE64.
*
* @returns IPRT status code, see RTBase64DecodeUtf16Ex.
* @param pvData Where to return the decoded bytes.
* @param cbData Size of the @a pvData return buffer.
* @param pcbActual Where to return number of bytes actually decoded.
* This is optional and if not specified, the request
* will fail unless @a cbData matches the data size
* exactly.
* @param ppwszEnd Where to return pointer to the first non-base64
* character following the encoded data. This is
* optional and if NULL, the request will fail if there
* are anything trailing the encoded bytes in the
* string.
* @sa base64DecodedSize, RTBase64DecodeUtf16
*/
int base64Decode(void *pvData, size_t cbData, size_t *pcbActual = NULL, PRTUTF16 *ppwszEnd = NULL);
/**
* Determins the size of the BASE64 encoded data in the string.
*
* @returns The length in bytes. -1 if the encoding is bad.
*
* @param ppwszEnd If not NULL, this will point to the first char
* following the Base64 encoded text block. If
* NULL the entire string is assumed to be Base64.
* @sa base64Decode, RTBase64DecodedUtf16Size
*/
ssize_t base64DecodedSize(PRTUTF16 *ppwszEnd = NULL);
/** @} */
#if defined(VBOX_WITH_XPCOM)
/**
* Returns a pointer to the raw member UTF-16 string. If the member string is empty,
* returns a pointer to a global variable containing an empty BSTR with a proper zero
* length prefix so that Windows is happy.
*/
CBSTR raw() const
{
if (m_bstr)
return m_bstr;
return g_bstrEmpty;
}
#else
/**
* Windows-only hack, as the automatically generated headers use BSTR.
* So if we don't want to cast like crazy we have to be more loose than
* on XPCOM.
*
* Returns a pointer to the raw member UTF-16 string. If the member string is empty,
* returns a pointer to a global variable containing an empty BSTR with a proper zero
* length prefix so that Windows is happy.
*/
BSTR raw() const
{
if (m_bstr)
return m_bstr;
return g_bstrEmpty;
}
#endif
/**
* Returns a non-const raw pointer that allows modifying the string directly.
*
* @note As opposed to raw(), this DOES return NULL if the member string is
* empty because we cannot return a mutable pointer to the global variable
* with the empty string.
*
* @note If modifying the string size (only shrinking it is allows), #jolt() or
* #joltNoThrow() must be called!
*
* @note Do not modify memory beyond the #length() of the string!
*
* @sa joltNoThrow(), mutalbleRaw(), reserve(), reserveNoThrow()
*/
BSTR mutableRaw() { return m_bstr; }
/**
* Correct the embedded length after using mutableRaw().
*
* This is needed on COM (Windows) to update the embedded string length. It is
* a stub on hosts using XPCOM.
*
* @param cwcNew The new string length, if handy, otherwise a negative
* number.
* @sa joltNoThrow(), mutalbleRaw(), reserve(), reserveNoThrow()
*/
#ifndef VBOX_WITH_XPCOM
void jolt(ssize_t cwcNew = -1);
#else
void jolt(ssize_t cwcNew = -1)
{
Assert(cwcNew < 0 || (cwcNew == 0 && !m_bstr) || m_bstr[cwcNew] == '\0'); RT_NOREF(cwcNew);
}
#endif
/**
* Correct the embedded length after using mutableRaw().
*
* This is needed on COM (Windows) to update the embedded string length. It is
* a stub on hosts using XPCOM.
*
* @returns S_OK on success, E_OUTOFMEMORY if shrinking the string failed.
* @param cwcNew The new string length, if handy, otherwise a negative
* number.
* @sa jolt(), mutalbleRaw(), reserve(), reserveNoThrow()
*/
#ifndef VBOX_WITH_XPCOM
HRESULT joltNoThrow(ssize_t cwcNew = -1) RT_NOEXCEPT;
#else
HRESULT joltNoThrow(ssize_t cwcNew = -1) RT_NOEXCEPT
{
Assert(cwcNew < 0 || (cwcNew == 0 && !m_bstr) || m_bstr[cwcNew] == '\0'); RT_NOREF(cwcNew);
return S_OK;
}
#endif
/**
* Make sure at that least @a cwc of buffer space is reserved.
*
* Requests that the contained memory buffer have at least cb bytes allocated.
* This may expand or shrink the string's storage, but will never truncate the
* contained string. In other words, cb will be ignored if it's smaller than
* length() + 1.
*
* @param cwcMin The new minimum string length that the can be stored. This
* does not include the terminator.
* @param fForce Force this size.
*
* @throws std::bad_alloc On allocation error. The object is left unchanged.
*/
void reserve(size_t cwcMin, bool fForce = false);
/**
* A C like version of the #reserve() method, i.e. return code instead of throw.
*
* @returns S_OK or E_OUTOFMEMORY.
* @param cwcMin The new minimum string length that the can be stored. This
* does not include the terminator.
* @param fForce Force this size.
*/
HRESULT reserveNoThrow(size_t cwcMin, bool fForce = false) RT_NOEXCEPT;
/**
* Intended to assign copies of instances to |BSTR| out parameters from
* within the interface method. Transfers the ownership of the duplicated
* string to the caller.
*
* If the member string is empty, this allocates an empty BSTR in *pstr
* (i.e. makes it point to a new buffer with a null byte).
*
* @deprecated Use cloneToEx instead to avoid throwing exceptions.
*/
void cloneTo(BSTR *pstr) const
{
if (pstr)
{
*pstr = ::SysAllocString((const OLECHAR *)raw()); // raw() returns a pointer to "" if empty
#ifdef RT_EXCEPTIONS_ENABLED
if (!*pstr)
throw std::bad_alloc();
#endif
}
}
/**
* A version of cloneTo that does not throw any out of memory exceptions, but
* returns E_OUTOFMEMORY intead.
* @returns S_OK or E_OUTOFMEMORY.
*/
HRESULT cloneToEx(BSTR *pstr) const
{
if (!pstr)
return S_OK;
*pstr = ::SysAllocString((const OLECHAR *)raw()); // raw() returns a pointer to "" if empty
return pstr ? S_OK : E_OUTOFMEMORY;
}
/**
* Intended to assign instances to |BSTR| out parameters from within the
* interface method. Transfers the ownership of the original string to the
* caller and resets the instance to null.
*
* As opposed to cloneTo(), this method doesn't create a copy of the
* string.
*
* If the member string is empty, this allocates an empty BSTR in *pstr
* (i.e. makes it point to a new buffer with a null byte).
*
* @param pbstrDst The BSTR variable to detach the string to.
*
* @throws std::bad_alloc if we failed to allocate a new empty string.
*/
void detachTo(BSTR *pbstrDst)
{
if (m_bstr)
{
*pbstrDst = m_bstr;
m_bstr = NULL;
}
else
{
// allocate null BSTR
*pbstrDst = ::SysAllocString((const OLECHAR *)g_bstrEmpty);
#ifdef RT_EXCEPTIONS_ENABLED
if (!*pbstrDst)
throw std::bad_alloc();
#endif
}
}
/**
* A version of detachTo that does not throw exceptions on out-of-memory
* conditions, but instead returns E_OUTOFMEMORY.
*
* @param pbstrDst The BSTR variable to detach the string to.
* @returns S_OK or E_OUTOFMEMORY.
*/
HRESULT detachToEx(BSTR *pbstrDst)
{
if (m_bstr)
{
*pbstrDst = m_bstr;
m_bstr = NULL;
}
else
{
// allocate null BSTR
*pbstrDst = ::SysAllocString((const OLECHAR *)g_bstrEmpty);
if (!*pbstrDst)
return E_OUTOFMEMORY;
}
return S_OK;
}
/**
* Intended to pass instances as |BSTR| out parameters to methods.
* Takes the ownership of the returned data.
*/
BSTR *asOutParam()
{
cleanup();
return &m_bstr;
}
/**
* Static immutable empty-string object. May be used for comparison purposes.
*/
static const Bstr Empty;
protected:
void cleanup();
/**
* Protected internal helper to copy a string. This ignores the previous object
* state, so either call this from a constructor or call cleanup() first.
*
* This variant copies from a zero-terminated UTF-16 string (which need not
* be a BSTR, i.e. need not have a length prefix).
*
* If the source is empty, this sets the member string to NULL.
*
* @param a_bstrSrc The source string. The caller guarantees
* that this is valid UTF-16.
*
* @throws std::bad_alloc - the object is representing an empty string.
*/
void copyFrom(const OLECHAR *a_bstrSrc);
/** cleanup() + copyFrom() - for assignment operators. */
void cleanupAndCopyFrom(const OLECHAR *a_bstrSrc);
/**
* Protected internal helper to copy a string, implying cleanup().
*
* This variant copies from a zero-terminated UTF-16 string (which need not be a
* BSTR, i.e. need not have a length prefix).
*
* If the source is empty, this sets the member string to NULL.
*
* @param a_bstrSrc The source string. The caller guarantees
* that this is valid UTF-16.
* @returns S_OK or E_OUTOFMEMORY
*/
HRESULT cleanupAndCopyFromEx(const OLECHAR *a_bstrSrc) RT_NOEXCEPT;
/**
* Protected internal helper to copy a string. This ignores the previous object
* state, so either call this from a constructor or call cleanup() first.
*
* This variant copies and converts from a zero-terminated UTF-8 string.
*
* If the source is empty, this sets the member string to NULL.
*
* @param a_pszSrc The source string. The caller guarantees
* that this is valid UTF-8.
*
* @throws std::bad_alloc - the object is representing an empty string.
*/
void copyFrom(const char *a_pszSrc)
{
copyFromN(a_pszSrc, RTSTR_MAX);
}
/**
* Variant of copyFrom for sub-string constructors.
*
* @param a_pszSrc The source string. The caller guarantees
* that this is valid UTF-8.
* @param a_cchSrc The maximum number of chars (not codepoints) to
* copy. If you pass RTSTR_MAX it'll be exactly
* like copyFrom().
*
* @throws std::bad_alloc - the object is representing an empty string.
*/
void copyFromN(const char *a_pszSrc, size_t a_cchSrc);
/** cleanup() + non-throwing copyFromN(). */
HRESULT cleanupAndCopyFromNoThrow(const char *a_pszSrc, size_t a_cchMax) RT_NOEXCEPT;
Bstr &appendWorkerUtf16(PCRTUTF16 pwszSrc, size_t cwcSrc);
Bstr &appendWorkerUtf8(const char *pszSrc, size_t cchSrc);
HRESULT appendWorkerUtf16NoThrow(PCRTUTF16 pwszSrc, size_t cwcSrc) RT_NOEXCEPT;
HRESULT appendWorkerUtf8NoThrow(const char *pszSrc, size_t cchSrc) RT_NOEXCEPT;
static DECLCALLBACK(size_t) printfOutputCallbackNoThrow(void *pvArg, const char *pachChars, size_t cbChars) RT_NOEXCEPT;
BSTR m_bstr;
friend class Utf8Str; /* to access our raw_copy() */
};
/* symmetric compare operators */
inline bool operator==(CBSTR l, const Bstr &r) { return r.operator==(l); }
inline bool operator!=(CBSTR l, const Bstr &r) { return r.operator!=(l); }
inline bool operator==(BSTR l, const Bstr &r) { return r.operator==(l); }
inline bool operator!=(BSTR l, const Bstr &r) { return r.operator!=(l); }
/**
* String class used universally in Main for UTF-8 strings.
*
* This is based on RTCString, to which some functionality has been
* moved. Here we keep things that are specific to Main, such as conversions
* with UTF-16 strings (Bstr).
*
* Like RTCString, Utf8Str does not differentiate between NULL strings
* and empty strings. In other words, Utf8Str("") and Utf8Str(NULL) behave the
* same. In both cases, RTCString allocates no memory, reports
* a zero length and zero allocated bytes for both, and returns an empty
* C string from c_str().
*
* @note All Utf8Str methods ASSUMES valid UTF-8 or UTF-16 input strings.
* The VirtualBox policy in this regard is to validate strings coming
* from external sources before passing them to Utf8Str or Bstr.
*/
class Utf8Str : public RTCString
{
public:
Utf8Str() {}
Utf8Str(const RTCString &that)
: RTCString(that)
{}
Utf8Str(const char *that)
: RTCString(that)
{}
Utf8Str(const Bstr &that)
{
copyFrom(that.raw());
}
Utf8Str(CBSTR that, size_t a_cwcSize = RTSTR_MAX)
{
copyFrom(that, a_cwcSize);
}
Utf8Str(const char *a_pszSrc, size_t a_cchSrc)
: RTCString(a_pszSrc, a_cchSrc)
{
}
/**
* Constructs a new string given the format string and the list of the
* arguments for the format string.
*
* @param a_pszFormat Pointer to the format string (UTF-8),
* @see pg_rt_str_format.
* @param a_va Argument vector containing the arguments
* specified by the format string.
* @sa RTCString::printfV
*/
Utf8Str(const char *a_pszFormat, va_list a_va) RT_IPRT_FORMAT_ATTR(1, 0)
: RTCString(a_pszFormat, a_va)
{
}
Utf8Str& operator=(const RTCString &that)
{
RTCString::operator=(that);
return *this;
}
Utf8Str& operator=(const char *that)
{
RTCString::operator=(that);
return *this;
}
Utf8Str& operator=(const Bstr &that)
{
cleanup();
copyFrom(that.raw());
return *this;
}
Utf8Str& operator=(CBSTR that)
{
cleanup();
copyFrom(that);
return *this;
}
/**
* Extended assignment method that returns a COM status code instead of an
* exception on failure.
*
* @returns S_OK or E_OUTOFMEMORY.
* @param a_rSrcStr The source string
*/
HRESULT assignEx(Utf8Str const &a_rSrcStr)
{
return copyFromExNComRC(a_rSrcStr.m_psz, 0, a_rSrcStr.m_cch);
}
/**
* Extended assignment method that returns a COM status code instead of an
* exception on failure.
*
* @returns S_OK, E_OUTOFMEMORY or E_INVALIDARG.
* @param a_rSrcStr The source string
* @param a_offSrc The character (byte) offset of the substring.
* @param a_cchSrc The number of characters (bytes) to copy from the source
* string.
*/
HRESULT assignEx(Utf8Str const &a_rSrcStr, size_t a_offSrc, size_t a_cchSrc)
{
if ( a_offSrc + a_cchSrc > a_rSrcStr.m_cch
|| a_offSrc > a_rSrcStr.m_cch)
return E_INVALIDARG;
return copyFromExNComRC(a_rSrcStr.m_psz, a_offSrc, a_cchSrc);
}
/**
* Extended assignment method that returns a COM status code instead of an
* exception on failure.
*
* @returns S_OK or E_OUTOFMEMORY.
* @param a_pcszSrc The source string
*/
HRESULT assignEx(const char *a_pcszSrc)
{
return copyFromExNComRC(a_pcszSrc, 0, a_pcszSrc ? strlen(a_pcszSrc) : 0);
}
/**
* Extended assignment method that returns a COM status code instead of an
* exception on failure.
*
* @returns S_OK or E_OUTOFMEMORY.
* @param a_pcszSrc The source string
* @param a_cchSrc The number of characters (bytes) to copy from the source
* string.
*/
HRESULT assignEx(const char *a_pcszSrc, size_t a_cchSrc)
{
return copyFromExNComRC(a_pcszSrc, 0, a_cchSrc);
}
/** Resolve compiler confusion. */
Utf8Str &assign(const char *a_pszSrc)
{
RTCString::assign(a_pszSrc);
return *this;
}
/** Resolve compiler confusion. */
Utf8Str &assign(const char *a_pszSrc, size_t a_cchSrc)
{
RTCString::assign(a_pszSrc, a_cchSrc);
return *this;
}
/** Resolve compiler confusion. */
RTCString &assign(const RTCString &a_rSrc)
{
RTCString::assign(a_rSrc);
return *this;
}
/** Resolve compiler confusion. */
RTCString &assign(const RTCString &a_rSrc, size_t a_offSrc, size_t a_cchSrc = npos)
{
RTCString::assign(a_rSrc, a_offSrc, a_cchSrc);
return *this;
}
/** Resolve compiler confusion. */
RTCString &assign(size_t a_cTimes, char a_ch)
{
RTCString::assign(a_cTimes, a_ch);
return *this;
}
/**
* Assignment method for UTF-16 strings.
*
* @throws std::bad_alloc if we failed to allocate a new empty string.
*/
Utf8Str &assign(CBSTR that, size_t a_cwcSize = RTSTR_MAX)
{
copyFrom(that, a_cwcSize);
return *this;
}
RTMEMEF_NEW_AND_DELETE_OPERATORS();
#if defined(VBOX_WITH_XPCOM)
/**
* Intended to assign instances to |char *| out parameters from within the
* interface method. Transfers the ownership of the duplicated string to the
* caller.
*
* This allocates a single 0 byte in the target if the member string is empty.
*
* This uses XPCOM memory allocation and thus only works on XPCOM. MSCOM doesn't
* like char* strings anyway.
*/
void cloneTo(char **pstr) const;
/**
* A version of cloneTo that does not throw allocation errors but returns
* E_OUTOFMEMORY instead.
* @returns S_OK or E_OUTOFMEMORY (COM status codes).
*/
HRESULT cloneToEx(char **pstr) const;
#endif
/**
* Intended to assign instances to |BSTR| out parameters from within the
* interface method. Transfers the ownership of the duplicated string to the
* caller.
*/
void cloneTo(BSTR *pstr) const
{
if (pstr)
{
Bstr bstr(*this);
bstr.cloneTo(pstr);
}
}
/**
* A version of cloneTo that does not throw allocation errors but returns
* E_OUTOFMEMORY instead.
*
* @param pbstr Where to store a clone of the string.
* @returns S_OK or E_OUTOFMEMORY (COM status codes).
*/
HRESULT cloneToEx(BSTR *pbstr) const RT_NOEXCEPT;
/**
* Safe assignment from BSTR.
*
* @param pbstrSrc The source string.
* @returns S_OK or E_OUTOFMEMORY (COM status codes).
*/
HRESULT cloneEx(CBSTR pbstrSrc)
{
cleanup();
return copyFromEx(pbstrSrc);
}
/**
* Removes a trailing slash from the member string, if present.
* Calls RTPathStripTrailingSlash() without having to mess with mutableRaw().
*/
Utf8Str& stripTrailingSlash();
/**
* Removes a trailing filename from the member string, if present.
* Calls RTPathStripFilename() without having to mess with mutableRaw().
*/
Utf8Str& stripFilename();
/**
* Removes the path component from the member string, if present.
* Calls RTPathFilename() without having to mess with mutableRaw().
*/
Utf8Str& stripPath();
/**
* Removes a trailing file name suffix from the member string, if present.
* Calls RTPathStripSuffix() without having to mess with mutableRaw().
*/
Utf8Str& stripSuffix();
/**
* Parses key=value pairs.
*
* @returns offset of the @a a_rPairSeparator following the returned value.
* @retval npos is returned if there are no more key/value pairs.
*
* @param a_rKey Reference to variable that should receive
* the key substring. This is set to null if
* no key/value found. (It's also possible the
* key is an empty string, so be careful.)
* @param a_rValue Reference to variable that should receive
* the value substring. This is set to null if
* no key/value found. (It's also possible the
* value is an empty string, so be careful.)
* @param a_offStart The offset to start searching from. This is
* typically 0 for the first call, and the
* return value of the previous call for the
* subsequent ones.
* @param a_rPairSeparator The pair separator string. If this is an
* empty string, the whole string will be
* considered as a single key/value pair.
* @param a_rKeyValueSeparator The key/value separator string.
*/
size_t parseKeyValue(Utf8Str &a_rKey, Utf8Str &a_rValue, size_t a_offStart = 0,
const Utf8Str &a_rPairSeparator = ",", const Utf8Str &a_rKeyValueSeparator = "=") const;
/**
* Static immutable empty-string object. May be used for comparison purposes.
*/
static const Utf8Str Empty;
protected:
void copyFrom(CBSTR a_pbstr, size_t a_cwcMax = RTSTR_MAX);
HRESULT copyFromEx(CBSTR a_pbstr);
HRESULT copyFromExNComRC(const char *a_pcszSrc, size_t a_offSrc, size_t a_cchSrc);
friend class Bstr; /* to access our raw_copy() */
};
/**
* Class with RTCString::printf as constructor for your convenience.
*
* Constructing a Utf8Str string object from a format string and a variable
* number of arguments can easily be confused with the other Utf8Str
* constructures, thus this child class.
*
* The usage of this class is like the following:
* @code
Utf8StrFmt strName("program name = %s", argv[0]);
@endcode
*
* @note Do not use in assignments to Utf8Str variables. Instead use
* RTCString::printf directly on the variable! This avoid an extra
* temporary Utf8Str instance and assignment operation.
*/
class Utf8StrFmt : public Utf8Str
{
public:
/**
* Constructs a new string given the format string and the list of the
* arguments for the format string.
*
* @param a_pszFormat Pointer to the format string (UTF-8),
* @see pg_rt_str_format.
* @param ... Ellipsis containing the arguments specified by
* the format string.
*/
explicit Utf8StrFmt(const char *a_pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2)
{
va_list va;
va_start(va, a_pszFormat);
printfV(a_pszFormat, va);
va_end(va);
}
RTMEMEF_NEW_AND_DELETE_OPERATORS();
protected:
Utf8StrFmt()
{ }
private:
};
/**
* Class with Bstr::printf as constructor for your convenience.
*/
class BstrFmt : public Bstr
{
public:
/**
* Constructs a new string given the format string and the list of the
* arguments for the format string.
*
* @param a_pszFormat printf-like format string (in UTF-8 encoding), see
* iprt/string.h for details.
* @param ... List of the arguments for the format string.
*/
explicit BstrFmt(const char *a_pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2)
{
va_list va;
va_start(va, a_pszFormat);
printfV(a_pszFormat, va);
va_end(va);
}
RTMEMEF_NEW_AND_DELETE_OPERATORS();
protected:
BstrFmt()
{ }
};
/**
* Class with Bstr::printfV as constructor for your convenience.
*/
class BstrFmtVA : public Bstr
{
public:
/**
* Constructs a new string given the format string and the list of the
* arguments for the format string.
*
* @param a_pszFormat printf-like format string (in UTF-8 encoding), see
* iprt/string.h for details.
* @param a_va List of arguments for the format string
*/
BstrFmtVA(const char *a_pszFormat, va_list a_va) RT_IPRT_FORMAT_ATTR(1, 0)
{
printfV(a_pszFormat, a_va);
}
RTMEMEF_NEW_AND_DELETE_OPERATORS();
protected:
BstrFmtVA()
{ }
};
} /* namespace com */
/** @} */
#endif /* !VBOX_INCLUDED_com_string_h */
|