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 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231
|
/****************************************************************************
**
** Copyright (C) 2019 The Qt Company Ltd.
** Copyright (C) 2016 Intel Corporation.
** Copyright (C) 2019 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qbytearray.h"
#include "qbytearraymatcher.h"
#include "private/qtools_p.h"
#include "qhashfunctions.h"
#include "qstring.h"
#include "qlist.h"
#include "qlocale.h"
#include "qlocale_p.h"
#include "qlocale_tools_p.h"
#include "private/qnumeric_p.h"
#include "private/qsimd_p.h"
#include "qstringalgorithms_p.h"
#include "qscopedpointer.h"
#include "qbytearray_p.h"
#include <qdatastream.h>
#include <qmath.h>
#ifndef QT_NO_COMPRESS
#include <zconf.h>
#include <zlib.h>
#endif
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#define IS_RAW_DATA(d) ((d)->offset != sizeof(QByteArrayData))
QT_BEGIN_NAMESPACE
// Latin 1 case system, used by QByteArray::to{Upper,Lower}() and qstr(n)icmp():
/*
#!/usr/bin/perl -l
use feature "unicode_strings";
for (0..255) {
$up = uc(chr($_));
$up = chr($_) if ord($up) > 0x100 || length $up > 1;
printf "0x%02x,", ord($up);
print "" if ($_ & 0xf) == 0xf;
}
*/
static const uchar latin1_uppercased[256] = {
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,
0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,
0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,
0x60,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,
0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x7b,0x7c,0x7d,0x7e,0x7f,
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,
0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,
0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,
0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,
0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf,
0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,
0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xf7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xff
};
/*
#!/usr/bin/perl -l
use feature "unicode_strings";
for (0..255) {
$up = lc(chr($_));
$up = chr($_) if ord($up) > 0x100 || length $up > 1;
printf "0x%02x,", ord($up);
print "" if ($_ & 0xf) == 0xf;
}
*/
static const uchar latin1_lowercased[256] = {
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,
0x40,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,
0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x5b,0x5c,0x5d,0x5e,0x5f,
0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,
0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f,
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,
0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,
0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,
0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef,
0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xd7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xdf,
0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef,
0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
};
int qFindByteArray(
const char *haystack0, int haystackLen, int from,
const char *needle0, int needleLen);
/*****************************************************************************
Safe and portable C string functions; extensions to standard string.h
*****************************************************************************/
/*! \relates QByteArray
Returns a duplicate string.
Allocates space for a copy of \a src, copies it, and returns a
pointer to the copy. If \a src is \nullptr, it immediately returns
\nullptr.
Ownership is passed to the caller, so the returned string must be
deleted using \c delete[].
*/
char *qstrdup(const char *src)
{
if (!src)
return nullptr;
char *dst = new char[strlen(src) + 1];
return qstrcpy(dst, src);
}
/*! \relates QByteArray
Copies all the characters up to and including the '\\0' from \a
src into \a dst and returns a pointer to \a dst. If \a src is
\nullptr, it immediately returns \nullptr.
This function assumes that \a dst is large enough to hold the
contents of \a src.
\note If \a dst and \a src overlap, the behavior is undefined.
\sa qstrncpy()
*/
char *qstrcpy(char *dst, const char *src)
{
if (!src)
return nullptr;
#ifdef Q_CC_MSVC
const int len = int(strlen(src));
// This is actually not secure!!! It will be fixed
// properly in a later release!
if (len >= 0 && strcpy_s(dst, len+1, src) == 0)
return dst;
return nullptr;
#else
return strcpy(dst, src);
#endif
}
/*! \relates QByteArray
A safe \c strncpy() function.
Copies at most \a len bytes from \a src (stopping at \a len or the
terminating '\\0' whichever comes first) into \a dst and returns a
pointer to \a dst. Guarantees that \a dst is '\\0'-terminated. If
\a src or \a dst is \nullptr, returns \nullptr immediately.
This function assumes that \a dst is at least \a len characters
long.
\note If \a dst and \a src overlap, the behavior is undefined.
\note When compiling with Visual C++ compiler version 14.00
(Visual C++ 2005) or later, internally the function strncpy_s
will be used.
\sa qstrcpy()
*/
char *qstrncpy(char *dst, const char *src, uint len)
{
if (!src || !dst)
return nullptr;
if (len > 0) {
#ifdef Q_CC_MSVC
strncpy_s(dst, len, src, len - 1);
#else
strncpy(dst, src, len);
#endif
dst[len-1] = '\0';
}
return dst;
}
/*! \fn uint qstrlen(const char *str)
\relates QByteArray
A safe \c strlen() function.
Returns the number of characters that precede the terminating '\\0',
or 0 if \a str is \nullptr.
\sa qstrnlen()
*/
/*! \fn uint qstrnlen(const char *str, uint maxlen)
\relates QByteArray
\since 4.2
A safe \c strnlen() function.
Returns the number of characters that precede the terminating '\\0', but
at most \a maxlen. If \a str is \nullptr, returns 0.
\sa qstrlen()
*/
/*!
\relates QByteArray
A safe \c strcmp() function.
Compares \a str1 and \a str2. Returns a negative value if \a str1
is less than \a str2, 0 if \a str1 is equal to \a str2 or a
positive value if \a str1 is greater than \a str2.
Special case 1: Returns 0 if \a str1 and \a str2 are both \nullptr.
Special case 2: Returns an arbitrary non-zero value if \a str1 is
\nullptr or \a str2 is \nullptr (but not both).
\sa qstrncmp(), qstricmp(), qstrnicmp(), {8-bit Character Comparisons},
QByteArray::compare()
*/
int qstrcmp(const char *str1, const char *str2)
{
return (str1 && str2) ? strcmp(str1, str2)
: (str1 ? 1 : (str2 ? -1 : 0));
}
/*! \fn int qstrncmp(const char *str1, const char *str2, uint len);
\relates QByteArray
A safe \c strncmp() function.
Compares at most \a len bytes of \a str1 and \a str2.
Returns a negative value if \a str1 is less than \a str2, 0 if \a
str1 is equal to \a str2 or a positive value if \a str1 is greater
than \a str2.
Special case 1: Returns 0 if \a str1 and \a str2 are both \nullptr.
Special case 2: Returns a random non-zero value if \a str1 is \nullptr
or \a str2 is \nullptr (but not both).
\sa qstrcmp(), qstricmp(), qstrnicmp(), {8-bit Character Comparisons},
QByteArray::compare()
*/
/*! \relates QByteArray
A safe \c stricmp() function.
Compares \a str1 and \a str2 ignoring the case of the
characters. The encoding of the strings is assumed to be Latin-1.
Returns a negative value if \a str1 is less than \a str2, 0 if \a
str1 is equal to \a str2 or a positive value if \a str1 is greater
than \a str2.
Special case 1: Returns 0 if \a str1 and \a str2 are both \nullptr.
Special case 2: Returns a random non-zero value if \a str1 is \nullptr
or \a str2 is \nullptr (but not both).
\sa qstrcmp(), qstrncmp(), qstrnicmp(), {8-bit Character Comparisons},
QByteArray::compare()
*/
int qstricmp(const char *str1, const char *str2)
{
const uchar *s1 = reinterpret_cast<const uchar *>(str1);
const uchar *s2 = reinterpret_cast<const uchar *>(str2);
if (!s1)
return s2 ? -1 : 0;
if (!s2)
return 1;
enum { Incomplete = 256 };
qptrdiff offset = 0;
auto innerCompare = [=, &offset](qptrdiff max, bool unlimited) {
max += offset;
do {
uchar c = latin1_lowercased[s1[offset]];
int res = c - latin1_lowercased[s2[offset]];
if (Q_UNLIKELY(res))
return res;
if (Q_UNLIKELY(!c))
return 0;
++offset;
} while (unlimited || offset < max);
return int(Incomplete);
};
#if defined(__SSE4_1__) && !(defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer))
enum { PageSize = 4096, PageMask = PageSize - 1 };
const __m128i zero = _mm_setzero_si128();
forever {
// Calculate how many bytes we can load until we cross a page boundary
// for either source. This isn't an exact calculation, just something
// very quick.
quintptr u1 = quintptr(s1 + offset);
quintptr u2 = quintptr(s2 + offset);
uint n = PageSize - ((u1 | u2) & PageMask);
qptrdiff maxoffset = offset + n;
for ( ; offset + 16 <= maxoffset; offset += sizeof(__m128i)) {
// load 16 bytes from either source
__m128i a = _mm_loadu_si128(reinterpret_cast<const __m128i *>(s1 + offset));
__m128i b = _mm_loadu_si128(reinterpret_cast<const __m128i *>(s2 + offset));
// compare the two against each oher
__m128i cmp = _mm_cmpeq_epi8(a, b);
// find NUL terminators too
cmp = _mm_min_epu8(cmp, a);
cmp = _mm_cmpeq_epi8(cmp, zero);
// was there any difference or a NUL?
uint mask = _mm_movemask_epi8(cmp);
if (mask) {
// yes, find out where
uint start = qCountTrailingZeroBits(mask);
uint end = sizeof(mask) * 8 - qCountLeadingZeroBits(mask);
Q_ASSUME(end >= start);
offset += start;
n = end - start;
break;
}
}
// using SIMD could cause a page fault, so iterate byte by byte
int res = innerCompare(n, false);
if (res != Incomplete)
return res;
}
#endif
return innerCompare(-1, true);
}
/*! \relates QByteArray
A safe \c strnicmp() function.
Compares at most \a len bytes of \a str1 and \a str2 ignoring the
case of the characters. The encoding of the strings is assumed to
be Latin-1.
Returns a negative value if \a str1 is less than \a str2, 0 if \a str1
is equal to \a str2 or a positive value if \a str1 is greater than \a
str2.
Special case 1: Returns 0 if \a str1 and \a str2 are both \nullptr.
Special case 2: Returns a random non-zero value if \a str1 is \nullptr
or \a str2 is \nullptr (but not both).
\sa qstrcmp(), qstrncmp(), qstricmp(), {8-bit Character Comparisons},
QByteArray::compare()
*/
int qstrnicmp(const char *str1, const char *str2, uint len)
{
const uchar *s1 = reinterpret_cast<const uchar *>(str1);
const uchar *s2 = reinterpret_cast<const uchar *>(str2);
int res;
uchar c;
if (!s1 || !s2)
return s1 ? 1 : (s2 ? -1 : 0);
for (; len--; s1++, s2++) {
if ((res = (c = latin1_lowercased[*s1]) - latin1_lowercased[*s2]))
return res;
if (!c) // strings are equal
break;
}
return 0;
}
/*!
\internal
\since 5.12
A helper for QByteArray::compare. Compares \a len1 bytes from \a str1 to \a
len2 bytes from \a str2. If \a len2 is -1, then \a str2 is expected to be
'\\0'-terminated.
*/
int qstrnicmp(const char *str1, qsizetype len1, const char *str2, qsizetype len2)
{
Q_ASSERT(str1);
Q_ASSERT(len1 >= 0);
Q_ASSERT(len2 >= -1);
const uchar *s1 = reinterpret_cast<const uchar *>(str1);
const uchar *s2 = reinterpret_cast<const uchar *>(str2);
if (!s2)
return len1 == 0 ? 0 : 1;
int res;
uchar c;
if (len2 == -1) {
// null-terminated str2
qsizetype i;
for (i = 0; i < len1; ++i) {
c = latin1_lowercased[s2[i]];
if (!c)
return 1;
res = latin1_lowercased[s1[i]] - c;
if (res)
return res;
}
c = latin1_lowercased[s2[i]];
return c ? -1 : 0;
} else {
// not null-terminated
for (qsizetype i = 0; i < qMin(len1, len2); ++i) {
c = latin1_lowercased[s2[i]];
res = latin1_lowercased[s1[i]] - c;
if (res)
return res;
}
if (len1 == len2)
return 0;
return len1 < len2 ? -1 : 1;
}
}
/*!
\internal
### Qt6: replace the QByteArray parameter with [pointer,len] pair
*/
int qstrcmp(const QByteArray &str1, const char *str2)
{
if (!str2)
return str1.isEmpty() ? 0 : +1;
const char *str1data = str1.constData();
const char *str1end = str1data + str1.length();
for ( ; str1data < str1end && *str2; ++str1data, ++str2) {
int diff = int(uchar(*str1data)) - uchar(*str2);
if (diff)
// found a difference
return diff;
}
// Why did we stop?
if (*str2 != '\0')
// not the null, so we stopped because str1 is shorter
return -1;
if (str1data < str1end)
// we haven't reached the end, so str1 must be longer
return +1;
return 0;
}
/*!
\internal
### Qt6: replace the QByteArray parameter with [pointer,len] pair
*/
int qstrcmp(const QByteArray &str1, const QByteArray &str2)
{
int l1 = str1.length();
int l2 = str2.length();
int ret = memcmp(str1.constData(), str2.constData(), qMin(l1, l2));
if (ret != 0)
return ret;
// they matched qMin(l1, l2) bytes
// so the longer one is lexically after the shorter one
return l1 - l2;
}
// the CRC table below is created by the following piece of code
#if 0
static void createCRC16Table() // build CRC16 lookup table
{
unsigned int i;
unsigned int j;
unsigned short crc_tbl[16];
unsigned int v0, v1, v2, v3;
for (i = 0; i < 16; i++) {
v0 = i & 1;
v1 = (i >> 1) & 1;
v2 = (i >> 2) & 1;
v3 = (i >> 3) & 1;
j = 0;
#undef SET_BIT
#define SET_BIT(x, b, v) (x) |= (v) << (b)
SET_BIT(j, 0, v0);
SET_BIT(j, 7, v0);
SET_BIT(j, 12, v0);
SET_BIT(j, 1, v1);
SET_BIT(j, 8, v1);
SET_BIT(j, 13, v1);
SET_BIT(j, 2, v2);
SET_BIT(j, 9, v2);
SET_BIT(j, 14, v2);
SET_BIT(j, 3, v3);
SET_BIT(j, 10, v3);
SET_BIT(j, 15, v3);
crc_tbl[i] = j;
}
printf("static const quint16 crc_tbl[16] = {\n");
for (int i = 0; i < 16; i +=4)
printf(" 0x%04x, 0x%04x, 0x%04x, 0x%04x,\n", crc_tbl[i], crc_tbl[i+1], crc_tbl[i+2], crc_tbl[i+3]);
printf("};\n");
}
#endif
static const quint16 crc_tbl[16] = {
0x0000, 0x1081, 0x2102, 0x3183,
0x4204, 0x5285, 0x6306, 0x7387,
0x8408, 0x9489, 0xa50a, 0xb58b,
0xc60c, 0xd68d, 0xe70e, 0xf78f
};
/*!
\relates QByteArray
Returns the CRC-16 checksum of the first \a len bytes of \a data.
The checksum is independent of the byte order (endianness) and will be
calculated accorded to the algorithm published in ISO 3309 (Qt::ChecksumIso3309).
\note This function is a 16-bit cache conserving (16 entry table)
implementation of the CRC-16-CCITT algorithm.
*/
quint16 qChecksum(const char *data, uint len)
{
return qChecksum(data, len, Qt::ChecksumIso3309);
}
/*!
\relates QByteArray
\since 5.9
Returns the CRC-16 checksum of the first \a len bytes of \a data.
The checksum is independent of the byte order (endianness) and will
be calculated accorded to the algorithm published in \a standard.
\note This function is a 16-bit cache conserving (16 entry table)
implementation of the CRC-16-CCITT algorithm.
*/
quint16 qChecksum(const char *data, uint len, Qt::ChecksumType standard)
{
quint16 crc = 0x0000;
switch (standard) {
case Qt::ChecksumIso3309:
crc = 0xffff;
break;
case Qt::ChecksumItuV41:
crc = 0x6363;
break;
}
uchar c;
const uchar *p = reinterpret_cast<const uchar *>(data);
while (len--) {
c = *p++;
crc = ((crc >> 4) & 0x0fff) ^ crc_tbl[((crc ^ c) & 15)];
c >>= 4;
crc = ((crc >> 4) & 0x0fff) ^ crc_tbl[((crc ^ c) & 15)];
}
switch (standard) {
case Qt::ChecksumIso3309:
crc = ~crc;
break;
case Qt::ChecksumItuV41:
break;
}
return crc & 0xffff;
}
/*!
\fn QByteArray qCompress(const QByteArray& data, int compressionLevel)
\relates QByteArray
Compresses the \a data byte array and returns the compressed data
in a new byte array.
The \a compressionLevel parameter specifies how much compression
should be used. Valid values are between 0 and 9, with 9
corresponding to the greatest compression (i.e. smaller compressed
data) at the cost of using a slower algorithm. Smaller values (8,
7, ..., 1) provide successively less compression at slightly
faster speeds. The value 0 corresponds to no compression at all.
The default value is -1, which specifies zlib's default
compression.
\sa qUncompress()
*/
/*! \relates QByteArray
\overload
Compresses the first \a nbytes of \a data at compression level
\a compressionLevel and returns the compressed data in a new byte array.
*/
#ifndef QT_NO_COMPRESS
QByteArray qCompress(const uchar* data, int nbytes, int compressionLevel)
{
if (nbytes == 0) {
return QByteArray(4, '\0');
}
if (!data) {
qWarning("qCompress: Data is null");
return QByteArray();
}
if (compressionLevel < -1 || compressionLevel > 9)
compressionLevel = -1;
ulong len = nbytes + nbytes / 100 + 13;
QByteArray bazip;
int res;
do {
bazip.resize(len + 4);
res = ::compress2((uchar*)bazip.data()+4, &len, data, nbytes, compressionLevel);
switch (res) {
case Z_OK:
bazip.resize(len + 4);
bazip[0] = (nbytes & 0xff000000) >> 24;
bazip[1] = (nbytes & 0x00ff0000) >> 16;
bazip[2] = (nbytes & 0x0000ff00) >> 8;
bazip[3] = (nbytes & 0x000000ff);
break;
case Z_MEM_ERROR:
qWarning("qCompress: Z_MEM_ERROR: Not enough memory");
bazip.resize(0);
break;
case Z_BUF_ERROR:
len *= 2;
break;
}
} while (res == Z_BUF_ERROR);
return bazip;
}
#endif
/*!
\fn QByteArray qUncompress(const QByteArray &data)
\relates QByteArray
Uncompresses the \a data byte array and returns a new byte array
with the uncompressed data.
Returns an empty QByteArray if the input data was corrupt.
This function will uncompress data compressed with qCompress()
from this and any earlier Qt version, back to Qt 3.1 when this
feature was added.
\b{Note:} If you want to use this function to uncompress external
data that was compressed using zlib, you first need to prepend a four
byte header to the byte array containing the data. The header must
contain the expected length (in bytes) of the uncompressed data,
expressed as an unsigned, big-endian, 32-bit integer.
\sa qCompress()
*/
#ifndef QT_NO_COMPRESS
namespace {
struct QByteArrayDataDeleter
{
static inline void cleanup(QTypedArrayData<char> *d)
{ if (d) QTypedArrayData<char>::deallocate(d); }
};
}
static QByteArray invalidCompressedData()
{
qWarning("qUncompress: Input data is corrupted");
return QByteArray();
}
/*! \relates QByteArray
\overload
Uncompresses the first \a nbytes of \a data and returns a new byte
array with the uncompressed data.
*/
QByteArray qUncompress(const uchar* data, int nbytes)
{
if (!data) {
qWarning("qUncompress: Data is null");
return QByteArray();
}
if (nbytes <= 4) {
if (nbytes < 4 || (data[0]!=0 || data[1]!=0 || data[2]!=0 || data[3]!=0))
qWarning("qUncompress: Input data is corrupted");
return QByteArray();
}
ulong expectedSize = uint((data[0] << 24) | (data[1] << 16) |
(data[2] << 8) | (data[3] ));
ulong len = qMax(expectedSize, 1ul);
const ulong maxPossibleSize = MaxAllocSize - sizeof(QByteArray::Data);
if (Q_UNLIKELY(len >= maxPossibleSize)) {
// QByteArray does not support that huge size anyway.
return invalidCompressedData();
}
QScopedPointer<QByteArray::Data, QByteArrayDataDeleter> d(QByteArray::Data::allocate(expectedSize + 1));
if (Q_UNLIKELY(d.data() == nullptr))
return invalidCompressedData();
d->size = expectedSize;
forever {
ulong alloc = len;
int res = ::uncompress((uchar*)d->data(), &len,
data+4, nbytes-4);
switch (res) {
case Z_OK:
Q_ASSERT(len <= alloc);
Q_UNUSED(alloc);
d->size = len;
d->data()[len] = 0;
{
QByteArrayDataPtr dataPtr = { d.take() };
return QByteArray(dataPtr);
}
case Z_MEM_ERROR:
qWarning("qUncompress: Z_MEM_ERROR: Not enough memory");
return QByteArray();
case Z_BUF_ERROR:
len *= 2;
if (Q_UNLIKELY(len >= maxPossibleSize)) {
// QByteArray does not support that huge size anyway.
return invalidCompressedData();
} else {
// grow the block
QByteArray::Data *p = QByteArray::Data::reallocateUnaligned(d.data(), len + 1);
if (Q_UNLIKELY(p == nullptr))
return invalidCompressedData();
d.take(); // don't free
d.reset(p);
}
continue;
case Z_DATA_ERROR:
qWarning("qUncompress: Z_DATA_ERROR: Input data is corrupted");
return QByteArray();
}
}
}
#endif
/*!
\class QByteArray
\inmodule QtCore
\brief The QByteArray class provides an array of bytes.
\ingroup tools
\ingroup shared
\ingroup string-processing
\reentrant
QByteArray can be used to store both raw bytes (including '\\0's)
and traditional 8-bit '\\0'-terminated strings. Using QByteArray
is much more convenient than using \c{const char *}. Behind the
scenes, it always ensures that the data is followed by a '\\0'
terminator, and uses \l{implicit sharing} (copy-on-write) to
reduce memory usage and avoid needless copying of data.
In addition to QByteArray, Qt also provides the QString class to
store string data. For most purposes, QString is the class you
want to use. It stores 16-bit Unicode characters, making it easy
to store non-ASCII/non-Latin-1 characters in your application.
Furthermore, QString is used throughout in the Qt API. The two
main cases where QByteArray is appropriate are when you need to
store raw binary data, and when memory conservation is critical
(e.g., with Qt for Embedded Linux).
One way to initialize a QByteArray is simply to pass a \c{const
char *} to its constructor. For example, the following code
creates a byte array of size 5 containing the data "Hello":
\snippet code/src_corelib_tools_qbytearray.cpp 0
Although the size() is 5, the byte array also maintains an extra
'\\0' character at the end so that if a function is used that
asks for a pointer to the underlying data (e.g. a call to
data()), the data pointed to is guaranteed to be
'\\0'-terminated.
QByteArray makes a deep copy of the \c{const char *} data, so you
can modify it later without experiencing side effects. (If for
performance reasons you don't want to take a deep copy of the
character data, use QByteArray::fromRawData() instead.)
Another approach is to set the size of the array using resize()
and to initialize the data byte per byte. QByteArray uses 0-based
indexes, just like C++ arrays. To access the byte at a particular
index position, you can use operator[](). On non-const byte
arrays, operator[]() returns a reference to a byte that can be
used on the left side of an assignment. For example:
\snippet code/src_corelib_tools_qbytearray.cpp 1
For read-only access, an alternative syntax is to use at():
\snippet code/src_corelib_tools_qbytearray.cpp 2
at() can be faster than operator[](), because it never causes a
\l{deep copy} to occur.
To extract many bytes at a time, use left(), right(), or mid().
A QByteArray can embed '\\0' bytes. The size() function always
returns the size of the whole array, including embedded '\\0'
bytes, but excluding the terminating '\\0' added by QByteArray.
For example:
\snippet code/src_corelib_tools_qbytearray.cpp 48
If you want to obtain the length of the data up to and
excluding the first '\\0' character, call qstrlen() on the byte
array.
After a call to resize(), newly allocated bytes have undefined
values. To set all the bytes to a particular value, call fill().
To obtain a pointer to the actual character data, call data() or
constData(). These functions return a pointer to the beginning of the data.
The pointer is guaranteed to remain valid until a non-const function is
called on the QByteArray. It is also guaranteed that the data ends with a
'\\0' byte unless the QByteArray was created from a \l{fromRawData()}{raw
data}. This '\\0' byte is automatically provided by QByteArray and is not
counted in size().
QByteArray provides the following basic functions for modifying
the byte data: append(), prepend(), insert(), replace(), and
remove(). For example:
\snippet code/src_corelib_tools_qbytearray.cpp 3
The replace() and remove() functions' first two arguments are the
position from which to start erasing and the number of bytes that
should be erased.
When you append() data to a non-empty array, the array will be
reallocated and the new data copied to it. You can avoid this
behavior by calling reserve(), which preallocates a certain amount
of memory. You can also call capacity() to find out how much
memory QByteArray actually allocated. Data appended to an empty
array is not copied.
A frequent requirement is to remove whitespace characters from a
byte array ('\\n', '\\t', ' ', etc.). If you want to remove
whitespace from both ends of a QByteArray, use trimmed(). If you
want to remove whitespace from both ends and replace multiple
consecutive whitespaces with a single space character within the
byte array, use simplified().
If you want to find all occurrences of a particular character or
substring in a QByteArray, use indexOf() or lastIndexOf(). The
former searches forward starting from a given index position, the
latter searches backward. Both return the index position of the
character or substring if they find it; otherwise, they return -1.
For example, here's a typical loop that finds all occurrences of a
particular substring:
\snippet code/src_corelib_tools_qbytearray.cpp 4
If you simply want to check whether a QByteArray contains a
particular character or substring, use contains(). If you want to
find out how many times a particular character or substring
occurs in the byte array, use count(). If you want to replace all
occurrences of a particular value with another, use one of the
two-parameter replace() overloads.
\l{QByteArray}s can be compared using overloaded operators such as
operator<(), operator<=(), operator==(), operator>=(), and so on.
The comparison is based exclusively on the numeric values
of the characters and is very fast, but is not what a human would
expect. QString::localeAwareCompare() is a better choice for
sorting user-interface strings.
For historical reasons, QByteArray distinguishes between a null
byte array and an empty byte array. A \e null byte array is a
byte array that is initialized using QByteArray's default
constructor or by passing (const char *)0 to the constructor. An
\e empty byte array is any byte array with size 0. A null byte
array is always empty, but an empty byte array isn't necessarily
null:
\snippet code/src_corelib_tools_qbytearray.cpp 5
All functions except isNull() treat null byte arrays the same as
empty byte arrays. For example, data() returns a valid pointer
(\e not nullptr) to a '\\0' character for a byte array
and QByteArray() compares equal to QByteArray(""). We recommend
that you always use isEmpty() and avoid isNull().
\section1 Maximum size and out-of-memory conditions
The current version of QByteArray is limited to just under 2 GB (2^31
bytes) in size. The exact value is architecture-dependent, since it depends
on the overhead required for managing the data block, but is no more than
32 bytes. Raw data blocks are also limited by the use of \c int type in the
current version to 2 GB minus 1 byte.
In case memory allocation fails, QByteArray will throw a \c std::bad_alloc
exception. Out of memory conditions in the Qt containers are the only case
where Qt will throw exceptions.
Note that the operating system may impose further limits on applications
holding a lot of allocated memory, especially large, contiguous blocks.
Such considerations, the configuration of such behavior or any mitigation
are outside the scope of the QByteArray API.
\section1 Notes on Locale
\section2 Number-String Conversions
Functions that perform conversions between numeric data types and
strings are performed in the C locale, irrespective of the user's
locale settings. Use QString to perform locale-aware conversions
between numbers and strings.
\section2 8-bit Character Comparisons
In QByteArray, the notion of uppercase and lowercase and of which
character is greater than or less than another character is done
in the Latin-1 locale. This affects functions that support a case
insensitive option or that compare or lowercase or uppercase
their arguments. Case insensitive operations and comparisons will
be accurate if both strings contain only Latin-1 characters.
Functions that this affects include contains(), indexOf(),
lastIndexOf(), operator<(), operator<=(), operator>(),
operator>=(), isLower(), isUpper(), toLower() and toUpper().
This issue does not apply to \l{QString}s since they represent
characters using Unicode.
\sa QString, QBitArray
*/
/*!
\enum QByteArray::Base64Option
\since 5.2
This enum contains the options available for encoding and decoding Base64.
Base64 is defined by \l{RFC 4648}, with the following options:
\value Base64Encoding (default) The regular Base64 alphabet, called simply "base64"
\value Base64UrlEncoding An alternate alphabet, called "base64url", which replaces two
characters in the alphabet to be more friendly to URLs.
\value KeepTrailingEquals (default) Keeps the trailing padding equal signs at the end
of the encoded data, so the data is always a size multiple of
four.
\value OmitTrailingEquals Omits adding the padding equal signs at the end of the encoded
data.
\value IgnoreBase64DecodingErrors When decoding Base64-encoded data, ignores errors
in the input; invalid characters are simply skipped.
This enum value has been added in Qt 5.15.
\value AbortOnBase64DecodingErrors When decoding Base64-encoded data, stops at the first
decoding error.
This enum value has been added in Qt 5.15.
QByteArray::fromBase64Encoding() and QByteArray::fromBase64()
ignore the KeepTrailingEquals and OmitTrailingEquals options. If
the IgnoreBase64DecodingErrors option is specified, they will not
flag errors in case trailing equal signs are missing or if there
are too many of them. If instead the AbortOnBase64DecodingErrors is
specified, then the input must either have no padding or have the
correct amount of equal signs.
*/
/*! \fn QByteArray::iterator QByteArray::begin()
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first character in
the byte-array.
\sa constBegin(), end()
*/
/*! \fn QByteArray::const_iterator QByteArray::begin() const
\overload begin()
*/
/*! \fn QByteArray::const_iterator QByteArray::cbegin() const
\since 5.0
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first character
in the byte-array.
\sa begin(), cend()
*/
/*! \fn QByteArray::const_iterator QByteArray::constBegin() const
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first character
in the byte-array.
\sa begin(), constEnd()
*/
/*! \fn QByteArray::iterator QByteArray::end()
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary character
after the last character in the byte-array.
\sa begin(), constEnd()
*/
/*! \fn QByteArray::const_iterator QByteArray::end() const
\overload end()
*/
/*! \fn QByteArray::const_iterator QByteArray::cend() const
\since 5.0
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary
character after the last character in the list.
\sa cbegin(), end()
*/
/*! \fn QByteArray::const_iterator QByteArray::constEnd() const
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary
character after the last character in the list.
\sa constBegin(), end()
*/
/*! \fn QByteArray::reverse_iterator QByteArray::rbegin()
\since 5.6
Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to the first
character in the byte-array, in reverse order.
\sa begin(), crbegin(), rend()
*/
/*! \fn QByteArray::const_reverse_iterator QByteArray::rbegin() const
\since 5.6
\overload
*/
/*! \fn QByteArray::const_reverse_iterator QByteArray::crbegin() const
\since 5.6
Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to the first
character in the byte-array, in reverse order.
\sa begin(), rbegin(), rend()
*/
/*! \fn QByteArray::reverse_iterator QByteArray::rend()
\since 5.6
Returns a \l{STL-style iterators}{STL-style} reverse iterator pointing to one past
the last character in the byte-array, in reverse order.
\sa end(), crend(), rbegin()
*/
/*! \fn QByteArray::const_reverse_iterator QByteArray::rend() const
\since 5.6
\overload
*/
/*! \fn QByteArray::const_reverse_iterator QByteArray::crend() const
\since 5.6
Returns a const \l{STL-style iterators}{STL-style} reverse iterator pointing to one
past the last character in the byte-array, in reverse order.
\sa end(), rend(), rbegin()
*/
/*! \fn void QByteArray::push_back(const QByteArray &other)
This function is provided for STL compatibility. It is equivalent
to append(\a other).
*/
/*! \fn void QByteArray::push_back(const char *str)
\overload
Same as append(\a str).
*/
/*! \fn void QByteArray::push_back(char ch)
\overload
Same as append(\a ch).
*/
/*! \fn void QByteArray::push_front(const QByteArray &other)
This function is provided for STL compatibility. It is equivalent
to prepend(\a other).
*/
/*! \fn void QByteArray::push_front(const char *str)
\overload
Same as prepend(\a str).
*/
/*! \fn void QByteArray::push_front(char ch)
\overload
Same as prepend(\a ch).
*/
/*! \fn void QByteArray::shrink_to_fit()
\since 5.10
This function is provided for STL compatibility. It is equivalent to
squeeze().
*/
/*! \fn QByteArray::QByteArray(const QByteArray &other)
Constructs a copy of \a other.
This operation takes \l{constant time}, because QByteArray is
\l{implicitly shared}. This makes returning a QByteArray from a
function very fast. If a shared instance is modified, it will be
copied (copy-on-write), taking \l{linear time}.
\sa operator=()
*/
/*!
\fn QByteArray::QByteArray(QByteArray &&other)
Move-constructs a QByteArray instance, making it point at the same
object that \a other was pointing to.
\since 5.2
*/
/*! \fn QByteArray::QByteArray(QByteArrayDataPtr dd)
\internal
Constructs a byte array pointing to the same data as \a dd.
*/
/*! \fn QByteArray::~QByteArray()
Destroys the byte array.
*/
/*!
Assigns \a other to this byte array and returns a reference to
this byte array.
*/
QByteArray &QByteArray::operator=(const QByteArray & other) noexcept
{
other.d->ref.ref();
if (!d->ref.deref())
Data::deallocate(d);
d = other.d;
return *this;
}
/*!
\overload
Assigns \a str to this byte array.
*/
QByteArray &QByteArray::operator=(const char *str)
{
Data *x;
if (!str) {
x = Data::sharedNull();
} else if (!*str) {
x = Data::allocate(0);
} else {
const int len = int(strlen(str));
const uint fullLen = len + 1;
if (d->ref.isShared() || fullLen > d->alloc
|| (len < d->size && fullLen < uint(d->alloc >> 1)))
reallocData(fullLen, d->detachFlags());
x = d;
memcpy(x->data(), str, fullLen); // include null terminator
x->size = len;
}
x->ref.ref();
if (!d->ref.deref())
Data::deallocate(d);
d = x;
return *this;
}
/*!
\fn QByteArray &QByteArray::operator=(QByteArray &&other)
Move-assigns \a other to this QByteArray instance.
\since 5.2
*/
/*! \fn void QByteArray::swap(QByteArray &other)
\since 4.8
Swaps byte array \a other with this byte array. This operation is very
fast and never fails.
*/
/*! \fn int QByteArray::size() const
Returns the number of bytes in this byte array.
The last byte in the byte array is at position size() - 1. In addition,
QByteArray ensures that the byte at position size() is always '\\0', so
that you can use the return value of data() and constData() as arguments to
functions that expect '\\0'-terminated strings. If the QByteArray object
was created from a \l{fromRawData()}{raw data} that didn't include the
trailing null-termination character then QByteArray doesn't add it
automaticall unless the \l{deep copy} is created.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 6
\sa isEmpty(), resize()
*/
/*! \fn bool QByteArray::isEmpty() const
Returns \c true if the byte array has size 0; otherwise returns \c false.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 7
\sa size()
*/
/*! \fn int QByteArray::capacity() const
Returns the maximum number of bytes that can be stored in the
byte array without forcing a reallocation.
The sole purpose of this function is to provide a means of fine
tuning QByteArray's memory usage. In general, you will rarely
ever need to call this function. If you want to know how many
bytes are in the byte array, call size().
\sa reserve(), squeeze()
*/
/*! \fn void QByteArray::reserve(int size)
Attempts to allocate memory for at least \a size bytes. If you
know in advance how large the byte array will be, you can call
this function, and if you call resize() often you are likely to
get better performance. If \a size is an underestimate, the worst
that will happen is that the QByteArray will be a bit slower.
The sole purpose of this function is to provide a means of fine
tuning QByteArray's memory usage. In general, you will rarely
ever need to call this function. If you want to change the size
of the byte array, call resize().
\sa squeeze(), capacity()
*/
/*! \fn void QByteArray::squeeze()
Releases any memory not required to store the array's data.
The sole purpose of this function is to provide a means of fine
tuning QByteArray's memory usage. In general, you will rarely
ever need to call this function.
\sa reserve(), capacity()
*/
/*! \fn QByteArray::operator const char *() const
\fn QByteArray::operator const void *() const
\obsolete Use constData() instead.
Returns a pointer to the data stored in the byte array. The
pointer can be used to access the bytes that compose the array.
The data is '\\0'-terminated. The pointer remains valid as long
as the array isn't reallocated or destroyed.
This operator is mostly useful to pass a byte array to a function
that accepts a \c{const char *}.
You can disable this operator by defining \c
QT_NO_CAST_FROM_BYTEARRAY when you compile your applications.
Note: A QByteArray can store any byte values including '\\0's,
but most functions that take \c{char *} arguments assume that the
data ends at the first '\\0' they encounter.
\sa constData()
*/
/*!
\macro QT_NO_CAST_FROM_BYTEARRAY
\relates QByteArray
Disables automatic conversions from QByteArray to
const char * or const void *.
\sa QT_NO_CAST_TO_ASCII, QT_NO_CAST_FROM_ASCII
*/
/*! \fn char *QByteArray::data()
Returns a pointer to the data stored in the byte array. The
pointer can be used to access and modify the bytes that compose
the array. The data is '\\0'-terminated, i.e. the number of
bytes in the returned character string is size() + 1 for the
'\\0' terminator.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 8
The pointer remains valid as long as the byte array isn't
reallocated or destroyed. For read-only access, constData() is
faster because it never causes a \l{deep copy} to occur.
This function is mostly useful to pass a byte array to a function
that accepts a \c{const char *}.
The following example makes a copy of the char* returned by
data(), but it will corrupt the heap and cause a crash because it
does not allocate a byte for the '\\0' at the end:
\snippet code/src_corelib_tools_qbytearray.cpp 46
This one allocates the correct amount of space:
\snippet code/src_corelib_tools_qbytearray.cpp 47
Note: A QByteArray can store any byte values including '\\0's,
but most functions that take \c{char *} arguments assume that the
data ends at the first '\\0' they encounter.
\sa constData(), operator[]()
*/
/*! \fn const char *QByteArray::data() const
\overload
*/
/*! \fn const char *QByteArray::constData() const
Returns a pointer to the data stored in the byte array. The pointer can be
used to access the bytes that compose the array. The data is
'\\0'-terminated unless the QByteArray object was created from raw data.
The pointer remains valid as long as the byte array isn't reallocated or
destroyed.
This function is mostly useful to pass a byte array to a function
that accepts a \c{const char *}.
Note: A QByteArray can store any byte values including '\\0's,
but most functions that take \c{char *} arguments assume that the
data ends at the first '\\0' they encounter.
\sa data(), operator[](), fromRawData()
*/
/*! \fn void QByteArray::detach()
\internal
*/
/*! \fn bool QByteArray::isDetached() const
\internal
*/
/*! \fn bool QByteArray::isSharedWith(const QByteArray &other) const
\internal
*/
/*! \fn char QByteArray::at(int i) const
Returns the character at index position \a i in the byte array.
\a i must be a valid index position in the byte array (i.e., 0 <=
\a i < size()).
\sa operator[]()
*/
/*! \fn QByteRef QByteArray::operator[](int i)
Returns the byte at index position \a i as a modifiable reference.
If an assignment is made beyond the end of the byte array, the
array is extended with resize() before the assignment takes
place.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 9
The return value is of type QByteRef, a helper class for
QByteArray. When you get an object of type QByteRef, you can use
it as if it were a char &. If you assign to it, the assignment
will apply to the character in the QByteArray from which you got
the reference.
\note Before Qt 5.14 it was possible to use this operator to access
a character at an out-of-bounds position in the byte array, and
then assign to such a position, causing the byte array to be
automatically resized. Furthermore, assigning a value to the
returned QByteRef would cause a detach of the byte array, even if the
byte array has been copied in the meanwhile (and the QByteRef kept
alive while the copy was taken). These behaviors are deprecated,
and will be changed in a future version of Qt.
\sa at()
*/
/*! \fn char QByteArray::operator[](int i) const
\overload
Same as at(\a i).
*/
/*! \fn QByteRef QByteArray::operator[](uint i)
\overload
*/
/*! \fn char QByteArray::operator[](uint i) const
\overload
*/
/*!
\fn char QByteArray::front() const
\since 5.10
Returns the first character in the byte array.
Same as \c{at(0)}.
This function is provided for STL compatibility.
\warning Calling this function on an empty byte array constitutes
undefined behavior.
\sa back(), at(), operator[]()
*/
/*!
\fn char QByteArray::back() const
\since 5.10
Returns the last character in the byte array.
Same as \c{at(size() - 1)}.
This function is provided for STL compatibility.
\warning Calling this function on an empty byte array constitutes
undefined behavior.
\sa front(), at(), operator[]()
*/
/*!
\fn QByteRef QByteArray::front()
\since 5.10
Returns a reference to the first character in the byte array.
Same as \c{operator[](0)}.
This function is provided for STL compatibility.
\warning Calling this function on an empty byte array constitutes
undefined behavior.
\sa back(), at(), operator[]()
*/
/*!
\fn QByteRef QByteArray::back()
\since 5.10
Returns a reference to the last character in the byte array.
Same as \c{operator[](size() - 1)}.
This function is provided for STL compatibility.
\warning Calling this function on an empty byte array constitutes
undefined behavior.
\sa front(), at(), operator[]()
*/
/*! \fn bool QByteArray::contains(const QByteArray &ba) const
Returns \c true if the byte array contains an occurrence of the byte
array \a ba; otherwise returns \c false.
\sa indexOf(), count()
*/
/*! \fn bool QByteArray::contains(const char *str) const
\overload
Returns \c true if the byte array contains the string \a str;
otherwise returns \c false.
*/
/*! \fn bool QByteArray::contains(char ch) const
\overload
Returns \c true if the byte array contains the character \a ch;
otherwise returns \c false.
*/
/*!
Truncates the byte array at index position \a pos.
If \a pos is beyond the end of the array, nothing happens.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 10
\sa chop(), resize(), left()
*/
void QByteArray::truncate(int pos)
{
if (pos < d->size)
resize(pos);
}
/*!
Removes \a n bytes from the end of the byte array.
If \a n is greater than size(), the result is an empty byte
array.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 11
\sa truncate(), resize(), left()
*/
void QByteArray::chop(int n)
{
if (n > 0)
resize(d->size - n);
}
/*! \fn QByteArray &QByteArray::operator+=(const QByteArray &ba)
Appends the byte array \a ba onto the end of this byte array and
returns a reference to this byte array.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 12
Note: QByteArray is an \l{implicitly shared} class. Consequently,
if you append to an empty byte array, then the byte array will just
share the data held in \a ba. In this case, no copying of data is done,
taking \l{constant time}. If a shared instance is modified, it will
be copied (copy-on-write), taking \l{linear time}.
If the byte array being appended to is not empty, a deep copy of the
data is performed, taking \l{linear time}.
This operation typically does not suffer from allocation overhead,
because QByteArray preallocates extra space at the end of the data
so that it may grow without reallocating for each append operation.
\sa append(), prepend()
*/
/*! \fn QByteArray &QByteArray::operator+=(const QString &str)
\overload
\obsolete
Appends the string \a str onto the end of this byte array and
returns a reference to this byte array. The Unicode data is
converted into 8-bit characters using QString::toUtf8().
You can disable this function by defining \c QT_NO_CAST_TO_ASCII when you
compile your applications. You then need to call QString::toUtf8() (or
QString::toLatin1() or QString::toLocal8Bit()) explicitly if you want to
convert the data to \c{const char *}.
*/
/*! \fn QByteArray &QByteArray::operator+=(const char *str)
\overload
Appends the string \a str onto the end of this byte array and
returns a reference to this byte array.
*/
/*! \fn QByteArray &QByteArray::operator+=(char ch)
\overload
Appends the character \a ch onto the end of this byte array and
returns a reference to this byte array.
*/
/*! \fn int QByteArray::length() const
Same as size().
*/
/*! \fn bool QByteArray::isNull() const
Returns \c true if this byte array is null; otherwise returns \c false.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 13
Qt makes a distinction between null byte arrays and empty byte
arrays for historical reasons. For most applications, what
matters is whether or not a byte array contains any data,
and this can be determined using isEmpty().
\sa isEmpty()
*/
/*! \fn QByteArray::QByteArray()
Constructs an empty byte array.
\sa isEmpty()
*/
/*!
Constructs a byte array containing the first \a size bytes of
array \a data.
If \a data is 0, a null byte array is constructed.
If \a size is negative, \a data is assumed to point to a
'\\0'-terminated string and its length is determined dynamically.
The terminating \\0 character is not considered part of the
byte array.
QByteArray makes a deep copy of the string data.
\sa fromRawData()
*/
QByteArray::QByteArray(const char *data, int size)
{
if (!data) {
d = Data::sharedNull();
} else {
if (size < 0)
size = int(strlen(data));
if (!size) {
d = Data::allocate(0);
} else {
d = Data::allocate(uint(size) + 1u);
Q_CHECK_PTR(d);
d->size = size;
memcpy(d->data(), data, size);
d->data()[size] = '\0';
}
}
}
/*!
Constructs a byte array of size \a size with every byte set to
character \a ch.
\sa fill()
*/
QByteArray::QByteArray(int size, char ch)
{
if (size <= 0) {
d = Data::allocate(0);
} else {
d = Data::allocate(uint(size) + 1u);
Q_CHECK_PTR(d);
d->size = size;
memset(d->data(), ch, size);
d->data()[size] = '\0';
}
}
/*!
\internal
Constructs a byte array of size \a size with uninitialized contents.
*/
QByteArray::QByteArray(int size, Qt::Initialization)
{
d = Data::allocate(uint(size) + 1u);
Q_CHECK_PTR(d);
d->size = size;
d->data()[size] = '\0';
}
/*!
Sets the size of the byte array to \a size bytes.
If \a size is greater than the current size, the byte array is
extended to make it \a size bytes with the extra bytes added to
the end. The new bytes are uninitialized.
If \a size is less than the current size, bytes are removed from
the end.
\sa size(), truncate()
*/
void QByteArray::resize(int size)
{
if (size < 0)
size = 0;
if (IS_RAW_DATA(d) && !d->ref.isShared() && size < d->size) {
d->size = size;
return;
}
if (d->size == 0 && d->ref.isStatic()) {
//
// Optimize the idiom:
// QByteArray a;
// a.resize(sz);
// ...
// which is used in place of the Qt 3 idiom:
// QByteArray a(sz);
//
Data *x = Data::allocate(uint(size) + 1u);
Q_CHECK_PTR(x);
x->size = size;
x->data()[size] = '\0';
d = x;
} else {
if (d->ref.isShared() || uint(size) + 1u > d->alloc)
reallocData(uint(size) + 1u, d->detachFlags() | Data::Grow);
if (d->alloc) {
d->size = size;
d->data()[size] = '\0';
}
}
}
/*!
Sets every byte in the byte array to character \a ch. If \a size
is different from -1 (the default), the byte array is resized to
size \a size beforehand.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 14
\sa resize()
*/
QByteArray &QByteArray::fill(char ch, int size)
{
resize(size < 0 ? d->size : size);
if (d->size)
memset(d->data(), ch, d->size);
return *this;
}
void QByteArray::reallocData(uint alloc, Data::AllocationOptions options)
{
if (d->ref.isShared() || IS_RAW_DATA(d)) {
Data *x = Data::allocate(alloc, options);
Q_CHECK_PTR(x);
x->size = qMin(int(alloc) - 1, d->size);
::memcpy(x->data(), d->data(), x->size);
x->data()[x->size] = '\0';
if (!d->ref.deref())
Data::deallocate(d);
d = x;
} else {
Data *x = Data::reallocateUnaligned(d, alloc, options);
Q_CHECK_PTR(x);
d = x;
}
}
void QByteArray::expand(int i)
{
resize(qMax(i + 1, d->size));
}
/*!
\internal
Return a QByteArray that is sure to be '\\0'-terminated.
By default, all QByteArray have an extra NUL at the end,
guaranteeing that assumption. However, if QByteArray::fromRawData
is used, then the NUL is there only if the user put it there. We
can't be sure.
*/
QByteArray QByteArray::nulTerminated() const
{
// is this fromRawData?
if (!IS_RAW_DATA(d))
return *this; // no, then we're sure we're zero terminated
QByteArray copy(*this);
copy.detach();
return copy;
}
/*!
Prepends the byte array \a ba to this byte array and returns a
reference to this byte array.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 15
This is the same as insert(0, \a ba).
Note: QByteArray is an \l{implicitly shared} class. Consequently,
if you prepend to an empty byte array, then the byte array will just
share the data held in \a ba. In this case, no copying of data is done,
taking \l{constant time}. If a shared instance is modified, it will
be copied (copy-on-write), taking \l{linear time}.
If the byte array being prepended to is not empty, a deep copy of the
data is performed, taking \l{linear time}.
\sa append(), insert()
*/
QByteArray &QByteArray::prepend(const QByteArray &ba)
{
if (d->size == 0 && d->ref.isStatic() && !IS_RAW_DATA(ba.d)) {
*this = ba;
} else if (ba.d->size != 0) {
QByteArray tmp = *this;
*this = ba;
append(tmp);
}
return *this;
}
/*!
\overload
Prepends the string \a str to this byte array.
*/
QByteArray &QByteArray::prepend(const char *str)
{
return prepend(str, qstrlen(str));
}
/*!
\overload
\since 4.6
Prepends \a len bytes of the string \a str to this byte array.
*/
QByteArray &QByteArray::prepend(const char *str, int len)
{
if (str) {
if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc)
reallocData(uint(d->size + len) + 1u, d->detachFlags() | Data::Grow);
memmove(d->data()+len, d->data(), d->size);
memcpy(d->data(), str, len);
d->size += len;
d->data()[d->size] = '\0';
}
return *this;
}
/*! \fn QByteArray &QByteArray::prepend(int count, char ch)
\overload
\since 5.7
Prepends \a count copies of character \a ch to this byte array.
*/
/*!
\overload
Prepends the character \a ch to this byte array.
*/
QByteArray &QByteArray::prepend(char ch)
{
if (d->ref.isShared() || uint(d->size) + 2u > d->alloc)
reallocData(uint(d->size) + 2u, d->detachFlags() | Data::Grow);
memmove(d->data()+1, d->data(), d->size);
d->data()[0] = ch;
++d->size;
d->data()[d->size] = '\0';
return *this;
}
/*!
Appends the byte array \a ba onto the end of this byte array.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 16
This is the same as insert(size(), \a ba).
Note: QByteArray is an \l{implicitly shared} class. Consequently,
if you append to an empty byte array, then the byte array will just
share the data held in \a ba. In this case, no copying of data is done,
taking \l{constant time}. If a shared instance is modified, it will
be copied (copy-on-write), taking \l{linear time}.
If the byte array being appended to is not empty, a deep copy of the
data is performed, taking \l{linear time}.
This operation typically does not suffer from allocation overhead,
because QByteArray preallocates extra space at the end of the data
so that it may grow without reallocating for each append operation.
\sa operator+=(), prepend(), insert()
*/
QByteArray &QByteArray::append(const QByteArray &ba)
{
if (d->size == 0 && d->ref.isStatic() && !IS_RAW_DATA(ba.d)) {
*this = ba;
} else if (ba.d->size != 0) {
if (d->ref.isShared() || uint(d->size + ba.d->size) + 1u > d->alloc)
reallocData(uint(d->size + ba.d->size) + 1u, d->detachFlags() | Data::Grow);
memcpy(d->data() + d->size, ba.d->data(), ba.d->size);
d->size += ba.d->size;
d->data()[d->size] = '\0';
}
return *this;
}
/*! \fn QByteArray &QByteArray::append(const QString &str)
\overload
\obsolete
Appends the string \a str to this byte array. The Unicode data is
converted into 8-bit characters using QString::toUtf8().
You can disable this function by defining \c QT_NO_CAST_TO_ASCII when you
compile your applications. You then need to call QString::toUtf8() (or
QString::toLatin1() or QString::toLocal8Bit()) explicitly if you want to
convert the data to \c{const char *}.
*/
/*!
\overload
Appends the string \a str to this byte array.
*/
QByteArray& QByteArray::append(const char *str)
{
if (str) {
const int len = int(strlen(str));
if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc)
reallocData(uint(d->size + len) + 1u, d->detachFlags() | Data::Grow);
memcpy(d->data() + d->size, str, len + 1); // include null terminator
d->size += len;
}
return *this;
}
/*!
\overload append()
Appends the first \a len characters of the string \a str to this byte
array and returns a reference to this byte array.
If \a len is negative, the length of the string will be determined
automatically using qstrlen(). If \a len is zero or \a str is
null, nothing is appended to the byte array. Ensure that \a len is
\e not longer than \a str.
*/
QByteArray &QByteArray::append(const char *str, int len)
{
if (len < 0)
len = qstrlen(str);
if (str && len) {
if (d->ref.isShared() || uint(d->size + len) + 1u > d->alloc)
reallocData(uint(d->size + len) + 1u, d->detachFlags() | Data::Grow);
memcpy(d->data() + d->size, str, len); // include null terminator
d->size += len;
d->data()[d->size] = '\0';
}
return *this;
}
/*! \fn QByteArray &QByteArray::append(int count, char ch)
\overload
\since 5.7
Appends \a count copies of character \a ch to this byte
array and returns a reference to this byte array.
If \a count is negative or zero nothing is appended to the byte array.
*/
/*!
\overload
Appends the character \a ch to this byte array.
*/
QByteArray& QByteArray::append(char ch)
{
if (d->ref.isShared() || uint(d->size) + 2u > d->alloc)
reallocData(uint(d->size) + 2u, d->detachFlags() | Data::Grow);
d->data()[d->size++] = ch;
d->data()[d->size] = '\0';
return *this;
}
/*!
\internal
Inserts \a len bytes from the array \a arr at position \a pos and returns a
reference the modified byte array.
*/
static inline QByteArray &qbytearray_insert(QByteArray *ba,
int pos, const char *arr, int len)
{
Q_ASSERT(pos >= 0);
if (pos < 0 || len <= 0 || arr == nullptr)
return *ba;
int oldsize = ba->size();
ba->resize(qMax(pos, oldsize) + len);
char *dst = ba->data();
if (pos > oldsize)
::memset(dst + oldsize, 0x20, pos - oldsize);
else
::memmove(dst + pos + len, dst + pos, oldsize - pos);
memcpy(dst + pos, arr, len);
return *ba;
}
/*!
Inserts the byte array \a ba at index position \a i and returns a
reference to this byte array.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 17
\sa append(), prepend(), replace(), remove()
*/
QByteArray &QByteArray::insert(int i, const QByteArray &ba)
{
QByteArray copy(ba);
return qbytearray_insert(this, i, copy.d->data(), copy.d->size);
}
/*!
\fn QByteArray &QByteArray::insert(int i, const QString &str)
\overload
\obsolete
Inserts the string \a str at index position \a i in the byte
array. The Unicode data is converted into 8-bit characters using
QString::toUtf8().
If \a i is greater than size(), the array is first extended using
resize().
You can disable this function by defining \c QT_NO_CAST_TO_ASCII when you
compile your applications. You then need to call QString::toUtf8() (or
QString::toLatin1() or QString::toLocal8Bit()) explicitly if you want to
convert the data to \c{const char *}.
*/
/*!
\overload
Inserts the string \a str at position \a i in the byte array.
If \a i is greater than size(), the array is first extended using
resize().
*/
QByteArray &QByteArray::insert(int i, const char *str)
{
return qbytearray_insert(this, i, str, qstrlen(str));
}
/*!
\overload
\since 4.6
Inserts \a len bytes of the string \a str at position
\a i in the byte array.
If \a i is greater than size(), the array is first extended using
resize().
*/
QByteArray &QByteArray::insert(int i, const char *str, int len)
{
return qbytearray_insert(this, i, str, len);
}
/*!
\overload
Inserts character \a ch at index position \a i in the byte array.
If \a i is greater than size(), the array is first extended using
resize().
*/
QByteArray &QByteArray::insert(int i, char ch)
{
return qbytearray_insert(this, i, &ch, 1);
}
/*! \fn QByteArray &QByteArray::insert(int i, int count, char ch)
\overload
\since 5.7
Inserts \a count copies of character \a ch at index position \a i in the
byte array.
If \a i is greater than size(), the array is first extended using resize().
*/
QByteArray &QByteArray::insert(int i, int count, char ch)
{
if (i < 0 || count <= 0)
return *this;
int oldsize = size();
resize(qMax(i, oldsize) + count);
char *dst = d->data();
if (i > oldsize)
::memset(dst + oldsize, 0x20, i - oldsize);
else if (i < oldsize)
::memmove(dst + i + count, dst + i, oldsize - i);
::memset(dst + i, ch, count);
return *this;
}
/*!
Removes \a len bytes from the array, starting at index position \a
pos, and returns a reference to the array.
If \a pos is out of range, nothing happens. If \a pos is valid,
but \a pos + \a len is larger than the size of the array, the
array is truncated at position \a pos.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 18
\sa insert(), replace()
*/
QByteArray &QByteArray::remove(int pos, int len)
{
if (len <= 0 || uint(pos) >= uint(d->size))
return *this;
detach();
if (len >= d->size - pos) {
resize(pos);
} else {
memmove(d->data() + pos, d->data() + pos + len, d->size - pos - len);
resize(d->size - len);
}
return *this;
}
/*!
Replaces \a len bytes from index position \a pos with the byte
array \a after, and returns a reference to this byte array.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 19
\sa insert(), remove()
*/
QByteArray &QByteArray::replace(int pos, int len, const QByteArray &after)
{
if (len == after.d->size && (pos + len <= d->size)) {
detach();
memmove(d->data() + pos, after.d->data(), len*sizeof(char));
return *this;
} else {
QByteArray copy(after);
// ### optimize me
remove(pos, len);
return insert(pos, copy);
}
}
/*! \fn QByteArray &QByteArray::replace(int pos, int len, const char *after)
\overload
Replaces \a len bytes from index position \a pos with the
'\\0'-terminated string \a after.
Notice: this can change the length of the byte array.
*/
QByteArray &QByteArray::replace(int pos, int len, const char *after)
{
return replace(pos,len,after,qstrlen(after));
}
/*! \fn QByteArray &QByteArray::replace(int pos, int len, const char *after, int alen)
\overload
Replaces \a len bytes from index position \a pos with \a alen bytes
from the string \a after. \a after is allowed to have '\\0' characters.
\since 4.7
*/
QByteArray &QByteArray::replace(int pos, int len, const char *after, int alen)
{
if (len == alen && (pos + len <= d->size)) {
detach();
memcpy(d->data() + pos, after, len*sizeof(char));
return *this;
} else {
remove(pos, len);
return qbytearray_insert(this, pos, after, alen);
}
}
// ### optimize all other replace method, by offering
// QByteArray::replace(const char *before, int blen, const char *after, int alen)
/*!
\overload
Replaces every occurrence of the byte array \a before with the
byte array \a after.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 20
*/
QByteArray &QByteArray::replace(const QByteArray &before, const QByteArray &after)
{
if (isNull() || before.d == after.d)
return *this;
QByteArray aft = after;
if (after.d == d)
aft.detach();
return replace(before.constData(), before.size(), aft.constData(), aft.size());
}
/*!
\fn QByteArray &QByteArray::replace(const char *before, const QByteArray &after)
\overload
Replaces every occurrence of the string \a before with the
byte array \a after.
*/
QByteArray &QByteArray::replace(const char *c, const QByteArray &after)
{
QByteArray aft = after;
if (after.d == d)
aft.detach();
return replace(c, qstrlen(c), aft.constData(), aft.size());
}
/*!
\fn QByteArray &QByteArray::replace(const char *before, int bsize, const char *after, int asize)
\overload
Replaces every occurrence of the string \a before with the string \a after.
Since the sizes of the strings are given by \a bsize and \a asize, they
may contain zero characters and do not need to be '\\0'-terminated.
*/
QByteArray &QByteArray::replace(const char *before, int bsize, const char *after, int asize)
{
if (isNull() || (before == after && bsize == asize))
return *this;
// protect against before or after being part of this
const char *a = after;
const char *b = before;
if (after >= d->data() && after < d->data() + d->size) {
char *copy = (char *)malloc(asize);
Q_CHECK_PTR(copy);
memcpy(copy, after, asize);
a = copy;
}
if (before >= d->data() && before < d->data() + d->size) {
char *copy = (char *)malloc(bsize);
Q_CHECK_PTR(copy);
memcpy(copy, before, bsize);
b = copy;
}
QByteArrayMatcher matcher(before, bsize);
int index = 0;
int len = d->size;
char *d = data();
if (bsize == asize) {
if (bsize) {
while ((index = matcher.indexIn(*this, index)) != -1) {
memcpy(d + index, after, asize);
index += bsize;
}
}
} else if (asize < bsize) {
uint to = 0;
uint movestart = 0;
uint num = 0;
while ((index = matcher.indexIn(*this, index)) != -1) {
if (num) {
int msize = index - movestart;
if (msize > 0) {
memmove(d + to, d + movestart, msize);
to += msize;
}
} else {
to = index;
}
if (asize) {
memcpy(d + to, after, asize);
to += asize;
}
index += bsize;
movestart = index;
num++;
}
if (num) {
int msize = len - movestart;
if (msize > 0)
memmove(d + to, d + movestart, msize);
resize(len - num*(bsize-asize));
}
} else {
// the most complex case. We don't want to lose performance by doing repeated
// copies and reallocs of the string.
while (index != -1) {
uint indices[4096];
uint pos = 0;
while(pos < 4095) {
index = matcher.indexIn(*this, index);
if (index == -1)
break;
indices[pos++] = index;
index += bsize;
// avoid infinite loop
if (!bsize)
index++;
}
if (!pos)
break;
// we have a table of replacement positions, use them for fast replacing
int adjust = pos*(asize-bsize);
// index has to be adjusted in case we get back into the loop above.
if (index != -1)
index += adjust;
int newlen = len + adjust;
int moveend = len;
if (newlen > len) {
resize(newlen);
len = newlen;
}
d = this->d->data();
while(pos) {
pos--;
int movestart = indices[pos] + bsize;
int insertstart = indices[pos] + pos*(asize-bsize);
int moveto = insertstart + asize;
memmove(d + moveto, d + movestart, (moveend - movestart));
if (asize)
memcpy(d + insertstart, after, asize);
moveend = movestart - bsize;
}
}
}
if (a != after)
::free(const_cast<char *>(a));
if (b != before)
::free(const_cast<char *>(b));
return *this;
}
/*!
\fn QByteArray &QByteArray::replace(const QByteArray &before, const char *after)
\overload
Replaces every occurrence of the byte array \a before with the
string \a after.
*/
/*! \fn QByteArray &QByteArray::replace(const QString &before, const QByteArray &after)
\overload
\obsolete
Replaces every occurrence of the string \a before with the byte
array \a after. The Unicode data is converted into 8-bit
characters using QString::toUtf8().
You can disable this function by defining \c QT_NO_CAST_TO_ASCII when you
compile your applications. You then need to call QString::toUtf8() (or
QString::toLatin1() or QString::toLocal8Bit()) explicitly if you want to
convert the data to \c{const char *}.
*/
/*! \fn QByteArray &QByteArray::replace(const QString &before, const char *after)
\overload
\obsolete
Replaces every occurrence of the string \a before with the string
\a after.
*/
/*! \fn QByteArray &QByteArray::replace(const char *before, const char *after)
\overload
Replaces every occurrence of the string \a before with the string
\a after.
*/
/*!
\overload
Replaces every occurrence of the character \a before with the
byte array \a after.
*/
QByteArray &QByteArray::replace(char before, const QByteArray &after)
{
char b[2] = { before, '\0' };
QByteArray cb = fromRawData(b, 1);
return replace(cb, after);
}
/*! \fn QByteArray &QByteArray::replace(char before, const QString &after)
\overload
\obsolete
Replaces every occurrence of the character \a before with the
string \a after. The Unicode data is converted into 8-bit
characters using QString::toUtf8().
You can disable this function by defining \c QT_NO_CAST_TO_ASCII when you
compile your applications. You then need to call QString::toUtf8() (or
QString::toLatin1() or QString::toLocal8Bit()) explicitly if you want to
convert the data to \c{const char *}.
*/
/*! \fn QByteArray &QByteArray::replace(char before, const char *after)
\overload
Replaces every occurrence of the character \a before with the
string \a after.
*/
/*!
\overload
Replaces every occurrence of the character \a before with the
character \a after.
*/
QByteArray &QByteArray::replace(char before, char after)
{
if (d->size) {
char *i = data();
char *e = i + d->size;
for (; i != e; ++i)
if (*i == before)
* i = after;
}
return *this;
}
/*!
Splits the byte array into subarrays wherever \a sep occurs, and
returns the list of those arrays. If \a sep does not match
anywhere in the byte array, split() returns a single-element list
containing this byte array.
*/
QList<QByteArray> QByteArray::split(char sep) const
{
QList<QByteArray> list;
int start = 0;
int end;
while ((end = indexOf(sep, start)) != -1) {
list.append(mid(start, end - start));
start = end + 1;
}
list.append(mid(start));
return list;
}
/*!
\since 4.5
Returns a copy of this byte array repeated the specified number of \a times.
If \a times is less than 1, an empty byte array is returned.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 49
*/
QByteArray QByteArray::repeated(int times) const
{
if (d->size == 0)
return *this;
if (times <= 1) {
if (times == 1)
return *this;
return QByteArray();
}
const int resultSize = times * d->size;
QByteArray result;
result.reserve(resultSize);
if (result.d->alloc != uint(resultSize) + 1u)
return QByteArray(); // not enough memory
memcpy(result.d->data(), d->data(), d->size);
int sizeSoFar = d->size;
char *end = result.d->data() + sizeSoFar;
const int halfResultSize = resultSize >> 1;
while (sizeSoFar <= halfResultSize) {
memcpy(end, result.d->data(), sizeSoFar);
end += sizeSoFar;
sizeSoFar <<= 1;
}
memcpy(end, result.d->data(), resultSize - sizeSoFar);
result.d->data()[resultSize] = '\0';
result.d->size = resultSize;
return result;
}
#define REHASH(a) \
if (ol_minus_1 < sizeof(uint) * CHAR_BIT) \
hashHaystack -= (a) << ol_minus_1; \
hashHaystack <<= 1
/*!
Returns the index position of the first occurrence of the byte
array \a ba in this byte array, searching forward from index
position \a from. Returns -1 if \a ba could not be found.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 21
\sa lastIndexOf(), contains(), count()
*/
int QByteArray::indexOf(const QByteArray &ba, int from) const
{
const int ol = ba.d->size;
if (ol == 0)
return from;
if (ol == 1)
return indexOf(*ba.d->data(), from);
const int l = d->size;
if (from > d->size || ol + from > l)
return -1;
return qFindByteArray(d->data(), d->size, from, ba.d->data(), ol);
}
/*! \fn int QByteArray::indexOf(const QString &str, int from) const
\overload
\obsolete
Returns the index position of the first occurrence of the string
\a str in the byte array, searching forward from index position
\a from. Returns -1 if \a str could not be found.
The Unicode data is converted into 8-bit characters using
QString::toUtf8().
You can disable this function by defining \c QT_NO_CAST_TO_ASCII when you
compile your applications. You then need to call QString::toUtf8() (or
QString::toLatin1() or QString::toLocal8Bit()) explicitly if you want to
convert the data to \c{const char *}.
*/
/*! \fn int QByteArray::indexOf(const char *str, int from) const
\overload
Returns the index position of the first occurrence of the string
\a str in the byte array, searching forward from index position \a
from. Returns -1 if \a str could not be found.
*/
int QByteArray::indexOf(const char *c, int from) const
{
const int ol = qstrlen(c);
if (ol == 1)
return indexOf(*c, from);
const int l = d->size;
if (from > d->size || ol + from > l)
return -1;
if (ol == 0)
return from;
return qFindByteArray(d->data(), d->size, from, c, ol);
}
/*!
\overload
Returns the index position of the first occurrence of the
character \a ch in the byte array, searching forward from index
position \a from. Returns -1 if \a ch could not be found.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 22
\sa lastIndexOf(), contains()
*/
int QByteArray::indexOf(char ch, int from) const
{
if (from < 0)
from = qMax(from + d->size, 0);
if (from < d->size) {
const char *n = d->data() + from - 1;
const char *e = d->data() + d->size;
while (++n != e)
if (*n == ch)
return n - d->data();
}
return -1;
}
static int lastIndexOfHelper(const char *haystack, int l, const char *needle, int ol, int from)
{
int delta = l - ol;
if (from < 0)
from = delta;
if (from < 0 || from > l)
return -1;
if (from > delta)
from = delta;
const char *end = haystack;
haystack += from;
const uint ol_minus_1 = ol - 1;
const char *n = needle + ol_minus_1;
const char *h = haystack + ol_minus_1;
uint hashNeedle = 0, hashHaystack = 0;
int idx;
for (idx = 0; idx < ol; ++idx) {
hashNeedle = ((hashNeedle<<1) + *(n-idx));
hashHaystack = ((hashHaystack<<1) + *(h-idx));
}
hashHaystack -= *haystack;
while (haystack >= end) {
hashHaystack += *haystack;
if (hashHaystack == hashNeedle && memcmp(needle, haystack, ol) == 0)
return haystack - end;
--haystack;
REHASH(*(haystack + ol));
}
return -1;
}
/*!
\fn int QByteArray::lastIndexOf(const QByteArray &ba, int from) const
Returns the index position of the last occurrence of the byte
array \a ba in this byte array, searching backward from index
position \a from. If \a from is -1 (the default), the search
starts at the last byte. Returns -1 if \a ba could not be found.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 23
\sa indexOf(), contains(), count()
*/
int QByteArray::lastIndexOf(const QByteArray &ba, int from) const
{
const int ol = ba.d->size;
if (ol == 1)
return lastIndexOf(*ba.d->data(), from);
return lastIndexOfHelper(d->data(), d->size, ba.d->data(), ol, from);
}
/*! \fn int QByteArray::lastIndexOf(const QString &str, int from) const
\overload
\obsolete
Returns the index position of the last occurrence of the string \a
str in the byte array, searching backward from index position \a
from. If \a from is -1 (the default), the search starts at the
last (size() - 1) byte. Returns -1 if \a str could not be found.
The Unicode data is converted into 8-bit characters using
QString::toUtf8().
You can disable this function by defining \c QT_NO_CAST_TO_ASCII when you
compile your applications. You then need to call QString::toUtf8() (or
QString::toLatin1() or QString::toLocal8Bit()) explicitly if you want to
convert the data to \c{const char *}.
*/
/*! \fn int QByteArray::lastIndexOf(const char *str, int from) const
\overload
Returns the index position of the last occurrence of the string \a
str in the byte array, searching backward from index position \a
from. If \a from is -1 (the default), the search starts at the
last (size() - 1) byte. Returns -1 if \a str could not be found.
*/
int QByteArray::lastIndexOf(const char *str, int from) const
{
const int ol = qstrlen(str);
if (ol == 1)
return lastIndexOf(*str, from);
return lastIndexOfHelper(d->data(), d->size, str, ol, from);
}
/*!
\overload
Returns the index position of the last occurrence of character \a
ch in the byte array, searching backward from index position \a
from. If \a from is -1 (the default), the search starts at the
last (size() - 1) byte. Returns -1 if \a ch could not be found.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 24
\sa indexOf(), contains()
*/
int QByteArray::lastIndexOf(char ch, int from) const
{
if (from < 0)
from += d->size;
else if (from > d->size)
from = d->size-1;
if (from >= 0) {
const char *b = d->data();
const char *n = d->data() + from + 1;
while (n-- != b)
if (*n == ch)
return n - b;
}
return -1;
}
/*!
Returns the number of (potentially overlapping) occurrences of
byte array \a ba in this byte array.
\sa contains(), indexOf()
*/
int QByteArray::count(const QByteArray &ba) const
{
int num = 0;
int i = -1;
if (d->size > 500 && ba.d->size > 5) {
QByteArrayMatcher matcher(ba);
while ((i = matcher.indexIn(*this, i + 1)) != -1)
++num;
} else {
while ((i = indexOf(ba, i + 1)) != -1)
++num;
}
return num;
}
/*!
\overload
Returns the number of (potentially overlapping) occurrences of
string \a str in the byte array.
*/
int QByteArray::count(const char *str) const
{
return count(fromRawData(str, qstrlen(str)));
}
/*!
\overload
Returns the number of occurrences of character \a ch in the byte
array.
\sa contains(), indexOf()
*/
int QByteArray::count(char ch) const
{
int num = 0;
const char *i = d->data() + d->size;
const char *b = d->data();
while (i != b)
if (*--i == ch)
++num;
return num;
}
/*! \fn int QByteArray::count() const
\overload
Same as size().
*/
/*!
\fn int QByteArray::compare(const char *c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
\since 5.12
Returns an integer less than, equal to, or greater than zero depending on
whether this QByteArray sorts before, at the same position, or after the
string pointed to by \a c. The comparison is performed according to case
sensitivity \a cs.
\sa operator==
*/
/*!
\fn int QByteArray::compare(const QByteArray &a, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
\overload
\since 5.12
Returns an integer less than, equal to, or greater than zero depending on
whether this QByteArray sorts before, at the same position, or after the
QByteArray \a a. The comparison is performed according to case sensitivity
\a cs.
\sa operator==
*/
/*!
Returns \c true if this byte array starts with byte array \a ba;
otherwise returns \c false.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 25
\sa endsWith(), left()
*/
bool QByteArray::startsWith(const QByteArray &ba) const
{
if (d == ba.d || ba.d->size == 0)
return true;
if (d->size < ba.d->size)
return false;
return memcmp(d->data(), ba.d->data(), ba.d->size) == 0;
}
/*! \overload
Returns \c true if this byte array starts with string \a str;
otherwise returns \c false.
*/
bool QByteArray::startsWith(const char *str) const
{
if (!str || !*str)
return true;
const int len = int(strlen(str));
if (d->size < len)
return false;
return qstrncmp(d->data(), str, len) == 0;
}
/*! \overload
Returns \c true if this byte array starts with character \a ch;
otherwise returns \c false.
*/
bool QByteArray::startsWith(char ch) const
{
if (d->size == 0)
return false;
return d->data()[0] == ch;
}
/*!
Returns \c true if this byte array ends with byte array \a ba;
otherwise returns \c false.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 26
\sa startsWith(), right()
*/
bool QByteArray::endsWith(const QByteArray &ba) const
{
if (d == ba.d || ba.d->size == 0)
return true;
if (d->size < ba.d->size)
return false;
return memcmp(d->data() + d->size - ba.d->size, ba.d->data(), ba.d->size) == 0;
}
/*! \overload
Returns \c true if this byte array ends with string \a str; otherwise
returns \c false.
*/
bool QByteArray::endsWith(const char *str) const
{
if (!str || !*str)
return true;
const int len = int(strlen(str));
if (d->size < len)
return false;
return qstrncmp(d->data() + d->size - len, str, len) == 0;
}
/*
Returns true if \a c is an uppercase Latin1 letter.
\note The multiplication sign 0xD7 and the sz ligature 0xDF are not
treated as uppercase Latin1.
*/
static inline bool isUpperCaseLatin1(char c)
{
if (c >= 'A' && c <= 'Z')
return true;
return (uchar(c) >= 0xC0 && uchar(c) <= 0xDE && uchar(c) != 0xD7);
}
/*!
Returns \c true if this byte array contains only uppercase letters,
otherwise returns \c false. The byte array is interpreted as a Latin-1
encoded string.
\since 5.12
\sa isLower(), toUpper()
*/
bool QByteArray::isUpper() const
{
if (isEmpty())
return false;
const char *d = data();
for (int i = 0, max = size(); i < max; ++i) {
if (!isUpperCaseLatin1(d[i]))
return false;
}
return true;
}
/*
Returns true if \a c is an lowercase Latin1 letter.
\note The division sign 0xF7 is not treated as lowercase Latin1,
but the small y dieresis 0xFF is.
*/
static inline bool isLowerCaseLatin1(char c)
{
if (c >= 'a' && c <= 'z')
return true;
return (uchar(c) >= 0xD0 && uchar(c) != 0xF7);
}
/*!
Returns \c true if this byte array contains only lowercase letters,
otherwise returns \c false. The byte array is interpreted as a Latin-1
encoded string.
\since 5.12
\sa isUpper(), toLower()
*/
bool QByteArray::isLower() const
{
if (isEmpty())
return false;
const char *d = data();
for (int i = 0, max = size(); i < max; ++i) {
if (!isLowerCaseLatin1(d[i]))
return false;
}
return true;
}
/*! \overload
Returns \c true if this byte array ends with character \a ch;
otherwise returns \c false.
*/
bool QByteArray::endsWith(char ch) const
{
if (d->size == 0)
return false;
return d->data()[d->size - 1] == ch;
}
/*!
Returns a byte array that contains the leftmost \a len bytes of
this byte array.
The entire byte array is returned if \a len is greater than
size().
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 27
\sa startsWith(), right(), mid(), chopped(), chop(), truncate()
*/
QByteArray QByteArray::left(int len) const
{
if (len >= d->size)
return *this;
if (len < 0)
len = 0;
return QByteArray(d->data(), len);
}
/*!
Returns a byte array that contains the rightmost \a len bytes of
this byte array.
The entire byte array is returned if \a len is greater than
size().
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 28
\sa endsWith(), left(), mid(), chopped(), chop(), truncate()
*/
QByteArray QByteArray::right(int len) const
{
if (len >= d->size)
return *this;
if (len < 0)
len = 0;
return QByteArray(d->data() + d->size - len, len);
}
/*!
Returns a byte array containing \a len bytes from this byte array,
starting at position \a pos.
If \a len is -1 (the default), or \a pos + \a len >= size(),
returns a byte array containing all bytes starting at position \a
pos until the end of the byte array.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 29
\sa left(), right(), chopped(), chop(), truncate()
*/
QByteArray QByteArray::mid(int pos, int len) const
{
using namespace QtPrivate;
switch (QContainerImplHelper::mid(size(), &pos, &len)) {
case QContainerImplHelper::Null:
return QByteArray();
case QContainerImplHelper::Empty:
{
QByteArrayDataPtr empty = { Data::allocate(0) };
return QByteArray(empty);
}
case QContainerImplHelper::Full:
return *this;
case QContainerImplHelper::Subset:
return QByteArray(d->data() + pos, len);
}
Q_UNREACHABLE();
return QByteArray();
}
/*!
\fn QByteArray::chopped(int len) const
\since 5.10
Returns a byte array that contains the leftmost size() - \a len bytes of
this byte array.
\note The behavior is undefined if \a len is negative or greater than size().
\sa endsWith(), left(), right(), mid(), chop(), truncate()
*/
/*!
\fn QByteArray QByteArray::toLower() const
Returns a lowercase copy of the byte array. The bytearray is
interpreted as a Latin-1 encoded string.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 30
\sa isLower(), toUpper(), {8-bit Character Comparisons}
*/
// prevent the compiler from inlining the function in each of
// toLower and toUpper when the only difference is the table being used
// (even with constant propagation, there's no gain in performance).
template <typename T>
Q_NEVER_INLINE
static QByteArray toCase_template(T &input, const uchar * table)
{
// find the first bad character in input
const char *orig_begin = input.constBegin();
const char *firstBad = orig_begin;
const char *e = input.constEnd();
for ( ; firstBad != e ; ++firstBad) {
uchar ch = uchar(*firstBad);
uchar converted = table[ch];
if (ch != converted)
break;
}
if (firstBad == e)
return std::move(input);
// transform the rest
QByteArray s = std::move(input); // will copy if T is const QByteArray
char *b = s.begin(); // will detach if necessary
char *p = b + (firstBad - orig_begin);
e = b + s.size();
for ( ; p != e; ++p) {
*p = char(uchar(table[uchar(*p)]));
}
return s;
}
QByteArray QByteArray::toLower_helper(const QByteArray &a)
{
return toCase_template(a, latin1_lowercased);
}
QByteArray QByteArray::toLower_helper(QByteArray &a)
{
return toCase_template(a, latin1_lowercased);
}
/*!
\fn QByteArray QByteArray::toUpper() const
Returns an uppercase copy of the byte array. The bytearray is
interpreted as a Latin-1 encoded string.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 31
\sa isUpper(), toLower(), {8-bit Character Comparisons}
*/
QByteArray QByteArray::toUpper_helper(const QByteArray &a)
{
return toCase_template(a, latin1_uppercased);
}
QByteArray QByteArray::toUpper_helper(QByteArray &a)
{
return toCase_template(a, latin1_uppercased);
}
/*! \fn void QByteArray::clear()
Clears the contents of the byte array and makes it null.
\sa resize(), isNull()
*/
void QByteArray::clear()
{
if (!d->ref.deref())
Data::deallocate(d);
d = Data::sharedNull();
}
#if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE))
/*! \relates QByteArray
Writes byte array \a ba to the stream \a out and returns a reference
to the stream.
\sa {Serializing Qt Data Types}
*/
QDataStream &operator<<(QDataStream &out, const QByteArray &ba)
{
if (ba.isNull() && out.version() >= 6) {
out << (quint32)0xffffffff;
return out;
}
return out.writeBytes(ba.constData(), ba.size());
}
/*! \relates QByteArray
Reads a byte array into \a ba from the stream \a in and returns a
reference to the stream.
\sa {Serializing Qt Data Types}
*/
QDataStream &operator>>(QDataStream &in, QByteArray &ba)
{
ba.clear();
quint32 len;
in >> len;
if (len == 0xffffffff)
return in;
const quint32 Step = 1024 * 1024;
quint32 allocated = 0;
do {
int blockSize = qMin(Step, len - allocated);
ba.resize(allocated + blockSize);
if (in.readRawData(ba.data() + allocated, blockSize) != blockSize) {
ba.clear();
in.setStatus(QDataStream::ReadPastEnd);
return in;
}
allocated += blockSize;
} while (allocated < len);
return in;
}
#endif // QT_NO_DATASTREAM
/*! \fn bool QByteArray::operator==(const QString &str) const
Returns \c true if this byte array is equal to string \a str;
otherwise returns \c false.
The Unicode data is converted into 8-bit characters using
QString::toUtf8().
The comparison is case sensitive.
You can disable this operator by defining \c
QT_NO_CAST_FROM_ASCII when you compile your applications. You
then need to call QString::fromUtf8(), QString::fromLatin1(),
or QString::fromLocal8Bit() explicitly if you want to convert the byte
array to a QString before doing the comparison.
*/
/*! \fn bool QByteArray::operator!=(const QString &str) const
Returns \c true if this byte array is not equal to string \a str;
otherwise returns \c false.
The Unicode data is converted into 8-bit characters using
QString::toUtf8().
The comparison is case sensitive.
You can disable this operator by defining \c
QT_NO_CAST_FROM_ASCII when you compile your applications. You
then need to call QString::fromUtf8(), QString::fromLatin1(),
or QString::fromLocal8Bit() explicitly if you want to convert the byte
array to a QString before doing the comparison.
*/
/*! \fn bool QByteArray::operator<(const QString &str) const
Returns \c true if this byte array is lexically less than string \a
str; otherwise returns \c false.
The Unicode data is converted into 8-bit characters using
QString::toUtf8().
The comparison is case sensitive.
You can disable this operator by defining \c
QT_NO_CAST_FROM_ASCII when you compile your applications. You
then need to call QString::fromUtf8(), QString::fromLatin1(),
or QString::fromLocal8Bit() explicitly if you want to convert the byte
array to a QString before doing the comparison.
*/
/*! \fn bool QByteArray::operator>(const QString &str) const
Returns \c true if this byte array is lexically greater than string
\a str; otherwise returns \c false.
The Unicode data is converted into 8-bit characters using
QString::toUtf8().
The comparison is case sensitive.
You can disable this operator by defining \c
QT_NO_CAST_FROM_ASCII when you compile your applications. You
then need to call QString::fromUtf8(), QString::fromLatin1(),
or QString::fromLocal8Bit() explicitly if you want to convert the byte
array to a QString before doing the comparison.
*/
/*! \fn bool QByteArray::operator<=(const QString &str) const
Returns \c true if this byte array is lexically less than or equal
to string \a str; otherwise returns \c false.
The Unicode data is converted into 8-bit characters using
QString::toUtf8().
The comparison is case sensitive.
You can disable this operator by defining \c
QT_NO_CAST_FROM_ASCII when you compile your applications. You
then need to call QString::fromUtf8(), QString::fromLatin1(),
or QString::fromLocal8Bit() explicitly if you want to convert the byte
array to a QString before doing the comparison.
*/
/*! \fn bool QByteArray::operator>=(const QString &str) const
Returns \c true if this byte array is greater than or equal to string
\a str; otherwise returns \c false.
The Unicode data is converted into 8-bit characters using
QString::toUtf8().
The comparison is case sensitive.
You can disable this operator by defining \c
QT_NO_CAST_FROM_ASCII when you compile your applications. You
then need to call QString::fromUtf8(), QString::fromLatin1(),
or QString::fromLocal8Bit() explicitly if you want to convert the byte
array to a QString before doing the comparison.
*/
/*! \fn bool operator==(const QByteArray &a1, const QByteArray &a2)
\relates QByteArray
\overload
Returns \c true if byte array \a a1 is equal to byte array \a a2;
otherwise returns \c false.
\sa QByteArray::compare()
*/
/*! \fn bool operator==(const QByteArray &a1, const char *a2)
\relates QByteArray
\overload
Returns \c true if byte array \a a1 is equal to string \a a2;
otherwise returns \c false.
\sa QByteArray::compare()
*/
/*! \fn bool operator==(const char *a1, const QByteArray &a2)
\relates QByteArray
\overload
Returns \c true if string \a a1 is equal to byte array \a a2;
otherwise returns \c false.
\sa QByteArray::compare()
*/
/*! \fn bool operator!=(const QByteArray &a1, const QByteArray &a2)
\relates QByteArray
\overload
Returns \c true if byte array \a a1 is not equal to byte array \a a2;
otherwise returns \c false.
\sa QByteArray::compare()
*/
/*! \fn bool operator!=(const QByteArray &a1, const char *a2)
\relates QByteArray
\overload
Returns \c true if byte array \a a1 is not equal to string \a a2;
otherwise returns \c false.
\sa QByteArray::compare()
*/
/*! \fn bool operator!=(const char *a1, const QByteArray &a2)
\relates QByteArray
\overload
Returns \c true if string \a a1 is not equal to byte array \a a2;
otherwise returns \c false.
\sa QByteArray::compare()
*/
/*! \fn bool operator<(const QByteArray &a1, const QByteArray &a2)
\relates QByteArray
\overload
Returns \c true if byte array \a a1 is lexically less than byte array
\a a2; otherwise returns \c false.
\sa QByteArray::compare()
*/
/*! \fn inline bool operator<(const QByteArray &a1, const char *a2)
\relates QByteArray
\overload
Returns \c true if byte array \a a1 is lexically less than string
\a a2; otherwise returns \c false.
\sa QByteArray::compare()
*/
/*! \fn bool operator<(const char *a1, const QByteArray &a2)
\relates QByteArray
\overload
Returns \c true if string \a a1 is lexically less than byte array
\a a2; otherwise returns \c false.
\sa QByteArray::compare()
*/
/*! \fn bool operator<=(const QByteArray &a1, const QByteArray &a2)
\relates QByteArray
\overload
Returns \c true if byte array \a a1 is lexically less than or equal
to byte array \a a2; otherwise returns \c false.
\sa QByteArray::compare()
*/
/*! \fn bool operator<=(const QByteArray &a1, const char *a2)
\relates QByteArray
\overload
Returns \c true if byte array \a a1 is lexically less than or equal
to string \a a2; otherwise returns \c false.
\sa QByteArray::compare()
*/
/*! \fn bool operator<=(const char *a1, const QByteArray &a2)
\relates QByteArray
\overload
Returns \c true if string \a a1 is lexically less than or equal
to byte array \a a2; otherwise returns \c false.
\sa QByteArray::compare()
*/
/*! \fn bool operator>(const QByteArray &a1, const QByteArray &a2)
\relates QByteArray
\overload
Returns \c true if byte array \a a1 is lexically greater than byte
array \a a2; otherwise returns \c false.
\sa QByteArray::compare()
*/
/*! \fn bool operator>(const QByteArray &a1, const char *a2)
\relates QByteArray
\overload
Returns \c true if byte array \a a1 is lexically greater than string
\a a2; otherwise returns \c false.
\sa QByteArray::compare()
*/
/*! \fn bool operator>(const char *a1, const QByteArray &a2)
\relates QByteArray
\overload
Returns \c true if string \a a1 is lexically greater than byte array
\a a2; otherwise returns \c false.
\sa QByteArray::compare()
*/
/*! \fn bool operator>=(const QByteArray &a1, const QByteArray &a2)
\relates QByteArray
\overload
Returns \c true if byte array \a a1 is lexically greater than or
equal to byte array \a a2; otherwise returns \c false.
\sa QByteArray::compare()
*/
/*! \fn bool operator>=(const QByteArray &a1, const char *a2)
\relates QByteArray
\overload
Returns \c true if byte array \a a1 is lexically greater than or
equal to string \a a2; otherwise returns \c false.
\sa QByteArray::compare()
*/
/*! \fn bool operator>=(const char *a1, const QByteArray &a2)
\relates QByteArray
\overload
Returns \c true if string \a a1 is lexically greater than or
equal to byte array \a a2; otherwise returns \c false.
\sa QByteArray::compare()
*/
/*! \fn const QByteArray operator+(const QByteArray &a1, const QByteArray &a2)
\relates QByteArray
Returns a byte array that is the result of concatenating byte
array \a a1 and byte array \a a2.
\sa QByteArray::operator+=()
*/
/*! \fn const QByteArray operator+(const QByteArray &a1, const char *a2)
\relates QByteArray
\overload
Returns a byte array that is the result of concatenating byte
array \a a1 and string \a a2.
*/
/*! \fn const QByteArray operator+(const QByteArray &a1, char a2)
\relates QByteArray
\overload
Returns a byte array that is the result of concatenating byte
array \a a1 and character \a a2.
*/
/*! \fn const QByteArray operator+(const char *a1, const QByteArray &a2)
\relates QByteArray
\overload
Returns a byte array that is the result of concatenating string
\a a1 and byte array \a a2.
*/
/*! \fn const QByteArray operator+(char a1, const QByteArray &a2)
\relates QByteArray
\overload
Returns a byte array that is the result of concatenating character
\a a1 and byte array \a a2.
*/
/*!
\fn QByteArray QByteArray::simplified() const
Returns a byte array that has whitespace removed from the start
and the end, and which has each sequence of internal whitespace
replaced with a single space.
Whitespace means any character for which the standard C++
\c isspace() function returns \c true in the C locale. This includes the ASCII
isspace() function returns \c true in the C locale. This includes the ASCII
characters '\\t', '\\n', '\\v', '\\f', '\\r', and ' '.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 32
\sa trimmed()
*/
QByteArray QByteArray::simplified_helper(const QByteArray &a)
{
return QStringAlgorithms<const QByteArray>::simplified_helper(a);
}
QByteArray QByteArray::simplified_helper(QByteArray &a)
{
return QStringAlgorithms<QByteArray>::simplified_helper(a);
}
/*!
\fn QByteArray QByteArray::trimmed() const
Returns a byte array that has whitespace removed from the start
and the end.
Whitespace means any character for which the standard C++
\c isspace() function returns \c true in the C locale. This includes the ASCII
characters '\\t', '\\n', '\\v', '\\f', '\\r', and ' '.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 33
Unlike simplified(), \l {QByteArray::trimmed()}{trimmed()} leaves internal whitespace alone.
\sa simplified()
*/
QByteArray QByteArray::trimmed_helper(const QByteArray &a)
{
return QStringAlgorithms<const QByteArray>::trimmed_helper(a);
}
QByteArray QByteArray::trimmed_helper(QByteArray &a)
{
return QStringAlgorithms<QByteArray>::trimmed_helper(a);
}
/*!
Returns a byte array of size \a width that contains this byte
array padded by the \a fill character.
If \a truncate is false and the size() of the byte array is more
than \a width, then the returned byte array is a copy of this byte
array.
If \a truncate is true and the size() of the byte array is more
than \a width, then any bytes in a copy of the byte array
after position \a width are removed, and the copy is returned.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 34
\sa rightJustified()
*/
QByteArray QByteArray::leftJustified(int width, char fill, bool truncate) const
{
QByteArray result;
int len = d->size;
int padlen = width - len;
if (padlen > 0) {
result.resize(len+padlen);
if (len)
memcpy(result.d->data(), d->data(), len);
memset(result.d->data()+len, fill, padlen);
} else {
if (truncate)
result = left(width);
else
result = *this;
}
return result;
}
/*!
Returns a byte array of size \a width that contains the \a fill
character followed by this byte array.
If \a truncate is false and the size of the byte array is more
than \a width, then the returned byte array is a copy of this byte
array.
If \a truncate is true and the size of the byte array is more
than \a width, then the resulting byte array is truncated at
position \a width.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 35
\sa leftJustified()
*/
QByteArray QByteArray::rightJustified(int width, char fill, bool truncate) const
{
QByteArray result;
int len = d->size;
int padlen = width - len;
if (padlen > 0) {
result.resize(len+padlen);
if (len)
memcpy(result.d->data()+padlen, data(), len);
memset(result.d->data(), fill, padlen);
} else {
if (truncate)
result = left(width);
else
result = *this;
}
return result;
}
bool QByteArray::isNull() const { return d == QArrayData::sharedNull(); }
static qlonglong toIntegral_helper(const char *data, bool *ok, int base, qlonglong)
{
return QLocaleData::bytearrayToLongLong(data, base, ok);
}
static qulonglong toIntegral_helper(const char *data, bool *ok, int base, qulonglong)
{
return QLocaleData::bytearrayToUnsLongLong(data, base, ok);
}
template <typename T> static inline
T toIntegral_helper(const char *data, bool *ok, int base)
{
using Int64 = typename std::conditional<std::is_unsigned<T>::value, qulonglong, qlonglong>::type;
#if defined(QT_CHECK_RANGE)
if (base != 0 && (base < 2 || base > 36)) {
qWarning("QByteArray::toIntegral: Invalid base %d", base);
base = 10;
}
#endif
// we select the right overload by the last, unused parameter
Int64 val = toIntegral_helper(data, ok, base, Int64());
if (T(val) != val) {
if (ok)
*ok = false;
val = 0;
}
return T(val);
}
/*!
Returns the byte array converted to a \c {long long} using base \a
base, which is 10 by default and must be between 2 and 36, or 0.
If \a base is 0, the base is determined automatically using the
following rules: If the byte array begins with "0x", it is assumed to
be hexadecimal; if it begins with "0", it is assumed to be octal;
otherwise it is assumed to be decimal.
Returns 0 if the conversion fails.
If \a ok is not \nullptr, failure is reported by setting *\a{ok}
to \c false, and success by setting *\a{ok} to \c true.
\note The conversion of the number is performed in the default C locale,
irrespective of the user's locale.
\sa number()
*/
qlonglong QByteArray::toLongLong(bool *ok, int base) const
{
return toIntegral_helper<qlonglong>(nulTerminated().constData(), ok, base);
}
/*!
Returns the byte array converted to an \c {unsigned long long}
using base \a base, which is 10 by default and must be between 2
and 36, or 0.
If \a base is 0, the base is determined automatically using the
following rules: If the byte array begins with "0x", it is assumed to
be hexadecimal; if it begins with "0", it is assumed to be octal;
otherwise it is assumed to be decimal.
Returns 0 if the conversion fails.
If \a ok is not \nullptr, failure is reported by setting *\a{ok}
to \c false, and success by setting *\a{ok} to \c true.
\note The conversion of the number is performed in the default C locale,
irrespective of the user's locale.
\sa number()
*/
qulonglong QByteArray::toULongLong(bool *ok, int base) const
{
return toIntegral_helper<qulonglong>(nulTerminated().constData(), ok, base);
}
/*!
Returns the byte array converted to an \c int using base \a
base, which is 10 by default and must be between 2 and 36, or 0.
If \a base is 0, the base is determined automatically using the
following rules: If the byte array begins with "0x", it is assumed to
be hexadecimal; if it begins with "0", it is assumed to be octal;
otherwise it is assumed to be decimal.
Returns 0 if the conversion fails.
If \a ok is not \nullptr, failure is reported by setting *\a{ok}
to \c false, and success by setting *\a{ok} to \c true.
\snippet code/src_corelib_tools_qbytearray.cpp 36
\note The conversion of the number is performed in the default C locale,
irrespective of the user's locale.
\sa number()
*/
int QByteArray::toInt(bool *ok, int base) const
{
return toIntegral_helper<int>(nulTerminated().constData(), ok, base);
}
/*!
Returns the byte array converted to an \c {unsigned int} using base \a
base, which is 10 by default and must be between 2 and 36, or 0.
If \a base is 0, the base is determined automatically using the
following rules: If the byte array begins with "0x", it is assumed to
be hexadecimal; if it begins with "0", it is assumed to be octal;
otherwise it is assumed to be decimal.
Returns 0 if the conversion fails.
If \a ok is not \nullptr, failure is reported by setting *\a{ok}
to \c false, and success by setting *\a{ok} to \c true.
\note The conversion of the number is performed in the default C locale,
irrespective of the user's locale.
\sa number()
*/
uint QByteArray::toUInt(bool *ok, int base) const
{
return toIntegral_helper<uint>(nulTerminated().constData(), ok, base);
}
/*!
\since 4.1
Returns the byte array converted to a \c long int using base \a
base, which is 10 by default and must be between 2 and 36, or 0.
If \a base is 0, the base is determined automatically using the
following rules: If the byte array begins with "0x", it is assumed to
be hexadecimal; if it begins with "0", it is assumed to be octal;
otherwise it is assumed to be decimal.
Returns 0 if the conversion fails.
If \a ok is not \nullptr, failure is reported by setting *\a{ok}
to \c false, and success by setting *\a{ok} to \c true.
\snippet code/src_corelib_tools_qbytearray.cpp 37
\note The conversion of the number is performed in the default C locale,
irrespective of the user's locale.
\sa number()
*/
long QByteArray::toLong(bool *ok, int base) const
{
return toIntegral_helper<long>(nulTerminated().constData(), ok, base);
}
/*!
\since 4.1
Returns the byte array converted to an \c {unsigned long int} using base \a
base, which is 10 by default and must be between 2 and 36, or 0.
If \a base is 0, the base is determined automatically using the
following rules: If the byte array begins with "0x", it is assumed to
be hexadecimal; if it begins with "0", it is assumed to be octal;
otherwise it is assumed to be decimal.
Returns 0 if the conversion fails.
If \a ok is not \nullptr, failure is reported by setting *\a{ok}
to \c false, and success by setting *\a{ok} to \c true.
\note The conversion of the number is performed in the default C locale,
irrespective of the user's locale.
\sa number()
*/
ulong QByteArray::toULong(bool *ok, int base) const
{
return toIntegral_helper<ulong>(nulTerminated().constData(), ok, base);
}
/*!
Returns the byte array converted to a \c short using base \a
base, which is 10 by default and must be between 2 and 36, or 0.
If \a base is 0, the base is determined automatically using the
following rules: If the byte array begins with "0x", it is assumed to
be hexadecimal; if it begins with "0", it is assumed to be octal;
otherwise it is assumed to be decimal.
Returns 0 if the conversion fails.
If \a ok is not \nullptr, failure is reported by setting *\a{ok}
to \c false, and success by setting *\a{ok} to \c true.
\note The conversion of the number is performed in the default C locale,
irrespective of the user's locale.
\sa number()
*/
short QByteArray::toShort(bool *ok, int base) const
{
return toIntegral_helper<short>(nulTerminated().constData(), ok, base);
}
/*!
Returns the byte array converted to an \c {unsigned short} using base \a
base, which is 10 by default and must be between 2 and 36, or 0.
If \a base is 0, the base is determined automatically using the
following rules: If the byte array begins with "0x", it is assumed to
be hexadecimal; if it begins with "0", it is assumed to be octal;
otherwise it is assumed to be decimal.
Returns 0 if the conversion fails.
If \a ok is not \nullptr, failure is reported by setting *\a{ok}
to \c false, and success by setting *\a{ok} to \c true.
\note The conversion of the number is performed in the default C locale,
irrespective of the user's locale.
\sa number()
*/
ushort QByteArray::toUShort(bool *ok, int base) const
{
return toIntegral_helper<ushort>(nulTerminated().constData(), ok, base);
}
/*!
Returns the byte array converted to a \c double value.
Returns an infinity if the conversion overflows or 0.0 if the
conversion fails for other reasons (e.g. underflow).
If \a ok is not \nullptr, failure is reported by setting *\a{ok}
to \c false, and success by setting *\a{ok} to \c true.
\snippet code/src_corelib_tools_qbytearray.cpp 38
\warning The QByteArray content may only contain valid numerical characters
which includes the plus/minus sign, the character e used in scientific
notation, and the decimal point. Including the unit or additional characters
leads to a conversion error.
\note The conversion of the number is performed in the default C locale,
irrespective of the user's locale.
This function ignores leading and trailing whitespace.
\sa number()
*/
double QByteArray::toDouble(bool *ok) const
{
bool nonNullOk = false;
int processed = 0;
double d = qt_asciiToDouble(constData(), size(),
nonNullOk, processed, WhitespacesAllowed);
if (ok)
*ok = nonNullOk;
return d;
}
/*!
Returns the byte array converted to a \c float value.
Returns an infinity if the conversion overflows or 0.0 if the
conversion fails for other reasons (e.g. underflow).
If \a ok is not \nullptr, failure is reported by setting *\a{ok}
to \c false, and success by setting *\a{ok} to \c true.
\snippet code/src_corelib_tools_qbytearray.cpp 38float
\warning The QByteArray content may only contain valid numerical characters
which includes the plus/minus sign, the character e used in scientific
notation, and the decimal point. Including the unit or additional characters
leads to a conversion error.
\note The conversion of the number is performed in the default C locale,
irrespective of the user's locale.
This function ignores leading and trailing whitespace.
\sa number()
*/
float QByteArray::toFloat(bool *ok) const
{
return QLocaleData::convertDoubleToFloat(toDouble(ok), ok);
}
/*!
Returns a copy of the byte array, encoded as Base64.
\snippet code/src_corelib_tools_qbytearray.cpp 39
The algorithm used to encode Base64-encoded data is defined in \l{RFC 4648}.
\sa fromBase64()
*/
QByteArray QByteArray::toBase64() const
{
return toBase64(Base64Encoding);
}
/*!
\since 5.2
\overload
Returns a copy of the byte array, encoded using the options \a options.
\snippet code/src_corelib_tools_qbytearray.cpp 39bis
The algorithm used to encode Base64-encoded data is defined in \l{RFC 4648}.
\sa fromBase64()
*/
QByteArray QByteArray::toBase64(Base64Options options) const
{
const char alphabet_base64[] = "ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef"
"ghijklmn" "opqrstuv" "wxyz0123" "456789+/";
const char alphabet_base64url[] = "ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef"
"ghijklmn" "opqrstuv" "wxyz0123" "456789-_";
const char *const alphabet = options & Base64UrlEncoding ? alphabet_base64url : alphabet_base64;
const char padchar = '=';
int padlen = 0;
QByteArray tmp((d->size + 2) / 3 * 4, Qt::Uninitialized);
int i = 0;
char *out = tmp.data();
while (i < d->size) {
// encode 3 bytes at a time
int chunk = 0;
chunk |= int(uchar(d->data()[i++])) << 16;
if (i == d->size) {
padlen = 2;
} else {
chunk |= int(uchar(d->data()[i++])) << 8;
if (i == d->size)
padlen = 1;
else
chunk |= int(uchar(data()[i++]));
}
int j = (chunk & 0x00fc0000) >> 18;
int k = (chunk & 0x0003f000) >> 12;
int l = (chunk & 0x00000fc0) >> 6;
int m = (chunk & 0x0000003f);
*out++ = alphabet[j];
*out++ = alphabet[k];
if (padlen > 1) {
if ((options & OmitTrailingEquals) == 0)
*out++ = padchar;
} else {
*out++ = alphabet[l];
}
if (padlen > 0) {
if ((options & OmitTrailingEquals) == 0)
*out++ = padchar;
} else {
*out++ = alphabet[m];
}
}
Q_ASSERT((options & OmitTrailingEquals) || (out == tmp.size() + tmp.data()));
if (options & OmitTrailingEquals)
tmp.truncate(out - tmp.data());
return tmp;
}
/*!
\fn QByteArray &QByteArray::setNum(int n, int base)
Sets the byte array to the printed value of \a n in base \a base (10
by default) and returns a reference to the byte array. The \a base can
be any value between 2 and 36. For bases other than 10, n is treated
as an unsigned integer.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 40
\note The format of the number is not localized; the default C locale
is used irrespective of the user's locale.
\sa number(), toInt()
*/
/*!
\fn QByteArray &QByteArray::setNum(uint n, int base)
\overload
\sa toUInt()
*/
/*!
\fn QByteArray &QByteArray::setNum(short n, int base)
\overload
\sa toShort()
*/
/*!
\fn QByteArray &QByteArray::setNum(ushort n, int base)
\overload
\sa toUShort()
*/
static char *qulltoa2(char *p, qulonglong n, int base)
{
#if defined(QT_CHECK_RANGE)
if (base < 2 || base > 36) {
qWarning("QByteArray::setNum: Invalid base %d", base);
base = 10;
}
#endif
const char b = 'a' - 10;
do {
const int c = n % base;
n /= base;
*--p = c + (c < 10 ? '0' : b);
} while (n);
return p;
}
/*!
\overload
\sa toLongLong()
*/
QByteArray &QByteArray::setNum(qlonglong n, int base)
{
const int buffsize = 66; // big enough for MAX_ULLONG in base 2
char buff[buffsize];
char *p;
if (n < 0 && base == 10) {
p = qulltoa2(buff + buffsize, qulonglong(-(1 + n)) + 1, base);
*--p = '-';
} else {
p = qulltoa2(buff + buffsize, qulonglong(n), base);
}
clear();
append(p, buffsize - (p - buff));
return *this;
}
/*!
\overload
\sa toULongLong()
*/
QByteArray &QByteArray::setNum(qulonglong n, int base)
{
const int buffsize = 66; // big enough for MAX_ULLONG in base 2
char buff[buffsize];
char *p = qulltoa2(buff + buffsize, n, base);
clear();
append(p, buffsize - (p - buff));
return *this;
}
/*!
\overload
Sets the byte array to the printed value of \a n, formatted in format
\a f with precision \a prec, and returns a reference to the
byte array.
The format \a f can be any of the following:
\table
\header \li Format \li Meaning
\row \li \c e \li format as [-]9.9e[+|-]999
\row \li \c E \li format as [-]9.9E[+|-]999
\row \li \c f \li format as [-]9.9
\row \li \c g \li use \c e or \c f format, whichever is the most concise
\row \li \c G \li use \c E or \c f format, whichever is the most concise
\endtable
With 'e', 'E', and 'f', \a prec is the number of digits after the
decimal point. With 'g' and 'G', \a prec is the maximum number of
significant digits (trailing zeroes are omitted).
\note The format of the number is not localized; the default C locale
is used irrespective of the user's locale.
\sa toDouble()
*/
QByteArray &QByteArray::setNum(double n, char f, int prec)
{
QLocaleData::DoubleForm form = QLocaleData::DFDecimal;
uint flags = QLocaleData::ZeroPadExponent;
char lower = latin1_lowercased[uchar(f)];
if (f != lower)
flags |= QLocaleData::CapitalEorX;
f = lower;
switch (f) {
case 'f':
form = QLocaleData::DFDecimal;
break;
case 'e':
form = QLocaleData::DFExponent;
break;
case 'g':
form = QLocaleData::DFSignificantDigits;
break;
default:
#if defined(QT_CHECK_RANGE)
qWarning("QByteArray::setNum: Invalid format char '%c'", f);
#endif
break;
}
*this = QLocaleData::c()->doubleToString(n, prec, form, -1, flags).toLatin1();
return *this;
}
/*!
\fn QByteArray &QByteArray::setNum(float n, char f, int prec)
\overload
Sets the byte array to the printed value of \a n, formatted in format
\a f with precision \a prec, and returns a reference to the
byte array.
\note The format of the number is not localized; the default C locale
is used irrespective of the user's locale.
\sa toFloat()
*/
/*!
Returns a byte array containing the string equivalent of the
number \a n to base \a base (10 by default). The \a base can be
any value between 2 and 36.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 41
\note The format of the number is not localized; the default C locale
is used irrespective of the user's locale.
\sa setNum(), toInt()
*/
QByteArray QByteArray::number(int n, int base)
{
QByteArray s;
s.setNum(n, base);
return s;
}
/*!
\overload
\sa toUInt()
*/
QByteArray QByteArray::number(uint n, int base)
{
QByteArray s;
s.setNum(n, base);
return s;
}
/*!
\overload
\sa toLongLong()
*/
QByteArray QByteArray::number(qlonglong n, int base)
{
QByteArray s;
s.setNum(n, base);
return s;
}
/*!
\overload
\sa toULongLong()
*/
QByteArray QByteArray::number(qulonglong n, int base)
{
QByteArray s;
s.setNum(n, base);
return s;
}
/*!
\overload
Returns a byte array that contains the printed value of \a n,
formatted in format \a f with precision \a prec.
Argument \a n is formatted according to the \a f format specified,
which is \c g by default, and can be any of the following:
\table
\header \li Format \li Meaning
\row \li \c e \li format as [-]9.9e[+|-]999
\row \li \c E \li format as [-]9.9E[+|-]999
\row \li \c f \li format as [-]9.9
\row \li \c g \li use \c e or \c f format, whichever is the most concise
\row \li \c G \li use \c E or \c f format, whichever is the most concise
\endtable
With 'e', 'E', and 'f', \a prec is the number of digits after the
decimal point. With 'g' and 'G', \a prec is the maximum number of
significant digits (trailing zeroes are omitted).
\snippet code/src_corelib_tools_qbytearray.cpp 42
\note The format of the number is not localized; the default C locale
is used irrespective of the user's locale.
\sa toDouble()
*/
QByteArray QByteArray::number(double n, char f, int prec)
{
QByteArray s;
s.setNum(n, f, prec);
return s;
}
/*!
Constructs a QByteArray that uses the first \a size bytes of the
\a data array. The bytes are \e not copied. The QByteArray will
contain the \a data pointer. The caller guarantees that \a data
will not be deleted or modified as long as this QByteArray and any
copies of it exist that have not been modified. In other words,
because QByteArray is an \l{implicitly shared} class and the
instance returned by this function contains the \a data pointer,
the caller must not delete \a data or modify it directly as long
as the returned QByteArray and any copies exist. However,
QByteArray does not take ownership of \a data, so the QByteArray
destructor will never delete the raw \a data, even when the
last QByteArray referring to \a data is destroyed.
A subsequent attempt to modify the contents of the returned
QByteArray or any copy made from it will cause it to create a deep
copy of the \a data array before doing the modification. This
ensures that the raw \a data array itself will never be modified
by QByteArray.
Here is an example of how to read data using a QDataStream on raw
data in memory without copying the raw data into a QByteArray:
\snippet code/src_corelib_tools_qbytearray.cpp 43
\warning A byte array created with fromRawData() is \e not
'\\0'-terminated, unless the raw data contains a 0 character at
position \a size. While that does not matter for QDataStream or
functions like indexOf(), passing the byte array to a function
accepting a \c{const char *} expected to be '\\0'-terminated will
fail.
\sa setRawData(), data(), constData()
*/
QByteArray QByteArray::fromRawData(const char *data, int size)
{
Data *x;
if (!data) {
x = Data::sharedNull();
} else if (!size) {
x = Data::allocate(0);
} else {
x = Data::fromRawData(data, size);
Q_CHECK_PTR(x);
}
QByteArrayDataPtr dataPtr = { x };
return QByteArray(dataPtr);
}
/*!
\since 4.7
Resets the QByteArray to use the first \a size bytes of the
\a data array. The bytes are \e not copied. The QByteArray will
contain the \a data pointer. The caller guarantees that \a data
will not be deleted or modified as long as this QByteArray and any
copies of it exist that have not been modified.
This function can be used instead of fromRawData() to re-use
existing QByteArray objects to save memory re-allocations.
\sa fromRawData(), data(), constData()
*/
QByteArray &QByteArray::setRawData(const char *data, uint size)
{
if (d->ref.isShared() || d->alloc) {
*this = fromRawData(data, size);
} else {
if (data) {
d->size = size;
d->offset = data - reinterpret_cast<char *>(d);
} else {
d->offset = sizeof(QByteArrayData);
d->size = 0;
}
}
return *this;
}
namespace {
struct fromBase64_helper_result {
qsizetype decodedLength;
QByteArray::Base64DecodingStatus status;
};
fromBase64_helper_result fromBase64_helper(const char *input, qsizetype inputSize,
char *output /* may alias input */,
QByteArray::Base64Options options)
{
fromBase64_helper_result result{ 0, QByteArray::Base64DecodingStatus::Ok };
unsigned int buf = 0;
int nbits = 0;
qsizetype offset = 0;
for (qsizetype i = 0; i < inputSize; ++i) {
int ch = input[i];
int d;
if (ch >= 'A' && ch <= 'Z') {
d = ch - 'A';
} else if (ch >= 'a' && ch <= 'z') {
d = ch - 'a' + 26;
} else if (ch >= '0' && ch <= '9') {
d = ch - '0' + 52;
} else if (ch == '+' && (options & QByteArray::Base64UrlEncoding) == 0) {
d = 62;
} else if (ch == '-' && (options & QByteArray::Base64UrlEncoding) != 0) {
d = 62;
} else if (ch == '/' && (options & QByteArray::Base64UrlEncoding) == 0) {
d = 63;
} else if (ch == '_' && (options & QByteArray::Base64UrlEncoding) != 0) {
d = 63;
} else {
if (options & QByteArray::AbortOnBase64DecodingErrors) {
if (ch == '=') {
// can have 1 or 2 '=' signs, in both cases padding base64Size to
// a multiple of 4. Any other case is illegal.
if ((inputSize % 4) != 0) {
result.status = QByteArray::Base64DecodingStatus::IllegalInputLength;
return result;
} else if ((i == inputSize - 1) ||
(i == inputSize - 2 && input[++i] == '=')) {
d = -1; // ... and exit the loop, normally
} else {
result.status = QByteArray::Base64DecodingStatus::IllegalPadding;
return result;
}
} else {
result.status = QByteArray::Base64DecodingStatus::IllegalCharacter;
return result;
}
} else {
d = -1;
}
}
if (d != -1) {
buf = (buf << 6) | d;
nbits += 6;
if (nbits >= 8) {
nbits -= 8;
Q_ASSERT(offset < i);
output[offset++] = buf >> nbits;
buf &= (1 << nbits) - 1;
}
}
}
result.decodedLength = offset;
return result;
}
} // anonymous namespace
/*!
\fn QByteArray::FromBase64Result QByteArray::fromBase64Encoding(QByteArray &&base64, Base64Options options)
\fn QByteArray::FromBase64Result QByteArray::fromBase64Encoding(const QByteArray &base64, Base64Options options)
\since 5.15
\overload
Decodes the Base64 array \a base64, using the options
defined by \a options. If \a options contains \c{IgnoreBase64DecodingErrors}
(the default), the input is not checked for validity; invalid
characters in the input are skipped, enabling the decoding process to
continue with subsequent characters. If \a options contains
\c{AbortOnBase64DecodingErrors}, then decoding will stop at the first
invalid character.
For example:
\snippet code/src_corelib_tools_qbytearray.cpp 44ter
The algorithm used to decode Base64-encoded data is defined in \l{RFC 4648}.
Returns a QByteArrayFromBase64Result object, containing the decoded
data and a flag telling whether decoding was successful. If the
\c{AbortOnBase64DecodingErrors} option was passed and the input
data was invalid, it is unspecified what the decoded data contains.
\sa toBase64()
*/
QByteArray::FromBase64Result QByteArray::fromBase64Encoding(QByteArray &&base64, Base64Options options)
{
// try to avoid a detach when calling data(), as it would over-allocate
// (we need less space when decoding than the one required by the full copy)
if (base64.isDetached()) {
const auto base64result = fromBase64_helper(base64.data(),
base64.size(),
base64.data(), // in-place
options);
base64.truncate(int(base64result.decodedLength));
return { std::move(base64), base64result.status };
}
return fromBase64Encoding(base64, options);
}
QByteArray::FromBase64Result QByteArray::fromBase64Encoding(const QByteArray &base64, Base64Options options)
{
const auto base64Size = base64.size();
QByteArray result((base64Size * 3) / 4, Qt::Uninitialized);
const auto base64result = fromBase64_helper(base64.data(),
base64Size,
const_cast<char *>(result.constData()),
options);
result.truncate(int(base64result.decodedLength));
return { std::move(result), base64result.status };
}
/*!
\overload
Returns a decoded copy of the Base64 array \a base64. Input is not checked
for validity; invalid characters in the input are skipped, enabling the
decoding process to continue with subsequent characters.
For example:
\snippet code/src_corelib_tools_qbytearray.cpp 44
The algorithm used to decode Base64-encoded data is defined in \l{RFC 4648}.
\note The fromBase64Encoding() function is recommended in new code.
\sa toBase64(), fromBase64Encoding()
*/
QByteArray QByteArray::fromBase64(const QByteArray &base64)
{
if (auto result = fromBase64Encoding(base64, Base64Encoding))
return std::move(result.decoded);
return QByteArray();
}
/*!
\since 5.2
\overload
Returns a decoded copy of the Base64 array \a base64, using the options
defined by \a options. If \a options contains \c{IgnoreBase64DecodingErrors}
(the default), the input is not checked for validity; invalid
characters in the input are skipped, enabling the decoding process to
continue with subsequent characters. If \a options contains
\c{AbortOnBase64DecodingErrors}, then decoding will stop at the first
invalid character.
For example:
\snippet code/src_corelib_tools_qbytearray.cpp 44bis
The algorithm used to decode Base64-encoded data is defined in \l{RFC 4648}.
Returns the decoded data, or, if the \c{AbortOnBase64DecodingErrors}
option was passed and the input data was invalid, an empty byte array.
\note The fromBase64Encoding() function is recommended in new code.
\sa toBase64(), fromBase64Encoding()
*/
QByteArray QByteArray::fromBase64(const QByteArray &base64, Base64Options options)
{
if (auto result = fromBase64Encoding(base64, options))
return std::move(result.decoded);
return QByteArray();
}
/*!
Returns a decoded copy of the hex encoded array \a hexEncoded. Input is not checked
for validity; invalid characters in the input are skipped, enabling the
decoding process to continue with subsequent characters.
For example:
\snippet code/src_corelib_tools_qbytearray.cpp 45
\sa toHex()
*/
QByteArray QByteArray::fromHex(const QByteArray &hexEncoded)
{
QByteArray res((hexEncoded.size() + 1)/ 2, Qt::Uninitialized);
uchar *result = (uchar *)res.data() + res.size();
bool odd_digit = true;
for (int i = hexEncoded.size() - 1; i >= 0; --i) {
uchar ch = uchar(hexEncoded.at(i));
int tmp = QtMiscUtils::fromHex(ch);
if (tmp == -1)
continue;
if (odd_digit) {
--result;
*result = tmp;
odd_digit = false;
} else {
*result |= tmp << 4;
odd_digit = true;
}
}
res.remove(0, result - (const uchar *)res.constData());
return res;
}
/*!
Returns a hex encoded copy of the byte array. The hex encoding uses the numbers 0-9 and
the letters a-f.
\sa fromHex()
*/
QByteArray QByteArray::toHex() const
{
return toHex('\0');
}
/*! \overload
\since 5.9
Returns a hex encoded copy of the byte array. The hex encoding uses the numbers 0-9 and
the letters a-f.
If \a separator is not '\0', the separator character is inserted between the hex bytes.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 50
\sa fromHex()
*/
QByteArray QByteArray::toHex(char separator) const
{
if (!d->size)
return QByteArray();
const int length = separator ? (d->size * 3 - 1) : (d->size * 2);
QByteArray hex(length, Qt::Uninitialized);
char *hexData = hex.data();
const uchar *data = (const uchar *)d->data();
for (int i = 0, o = 0; i < d->size; ++i) {
hexData[o++] = QtMiscUtils::toHexLower(data[i] >> 4);
hexData[o++] = QtMiscUtils::toHexLower(data[i] & 0xf);
if ((separator) && (o < length))
hexData[o++] = separator;
}
return hex;
}
static void q_fromPercentEncoding(QByteArray *ba, char percent)
{
if (ba->isEmpty())
return;
char *data = ba->data();
const char *inputPtr = data;
int i = 0;
int len = ba->count();
int outlen = 0;
int a, b;
char c;
while (i < len) {
c = inputPtr[i];
if (c == percent && i + 2 < len) {
a = inputPtr[++i];
b = inputPtr[++i];
if (a >= '0' && a <= '9') a -= '0';
else if (a >= 'a' && a <= 'f') a = a - 'a' + 10;
else if (a >= 'A' && a <= 'F') a = a - 'A' + 10;
if (b >= '0' && b <= '9') b -= '0';
else if (b >= 'a' && b <= 'f') b = b - 'a' + 10;
else if (b >= 'A' && b <= 'F') b = b - 'A' + 10;
*data++ = (char)((a << 4) | b);
} else {
*data++ = c;
}
++i;
++outlen;
}
if (outlen != len)
ba->truncate(outlen);
}
void q_fromPercentEncoding(QByteArray *ba)
{
q_fromPercentEncoding(ba, '%');
}
/*!
\since 4.4
Returns a decoded copy of the URI/URL-style percent-encoded \a input.
The \a percent parameter allows you to replace the '%' character for
another (for instance, '_' or '=').
For example:
\snippet code/src_corelib_tools_qbytearray.cpp 51
\note Given invalid input (such as a string containing the sequence "%G5",
which is not a valid hexadecimal number) the output will be invalid as
well. As an example: the sequence "%G5" could be decoded to 'W'.
\sa toPercentEncoding(), QUrl::fromPercentEncoding()
*/
QByteArray QByteArray::fromPercentEncoding(const QByteArray &input, char percent)
{
if (input.isNull())
return QByteArray(); // preserve null
if (input.isEmpty())
return QByteArray(input.data(), 0);
QByteArray tmp = input;
q_fromPercentEncoding(&tmp, percent);
return tmp;
}
/*! \fn QByteArray QByteArray::fromStdString(const std::string &str)
\since 5.4
Returns a copy of the \a str string as a QByteArray.
\sa toStdString(), QString::fromStdString()
*/
/*!
\fn std::string QByteArray::toStdString() const
\since 5.4
Returns a std::string object with the data contained in this
QByteArray.
This operator is mostly useful to pass a QByteArray to a function
that accepts a std::string object.
\sa fromStdString(), QString::toStdString()
*/
static inline bool q_strchr(const char str[], char chr)
{
if (!str) return false;
const char *ptr = str;
char c;
while ((c = *ptr++))
if (c == chr)
return true;
return false;
}
static void q_toPercentEncoding(QByteArray *ba, const char *dontEncode, const char *alsoEncode, char percent)
{
if (ba->isEmpty())
return;
QByteArray input = *ba;
int len = input.count();
const char *inputData = input.constData();
char *output = nullptr;
int length = 0;
for (int i = 0; i < len; ++i) {
unsigned char c = *inputData++;
if (((c >= 0x61 && c <= 0x7A) // ALPHA
|| (c >= 0x41 && c <= 0x5A) // ALPHA
|| (c >= 0x30 && c <= 0x39) // DIGIT
|| c == 0x2D // -
|| c == 0x2E // .
|| c == 0x5F // _
|| c == 0x7E // ~
|| q_strchr(dontEncode, c))
&& !q_strchr(alsoEncode, c)) {
if (output)
output[length] = c;
++length;
} else {
if (!output) {
// detach now
ba->resize(len*3); // worst case
output = ba->data();
}
output[length++] = percent;
output[length++] = QtMiscUtils::toHexUpper((c & 0xf0) >> 4);
output[length++] = QtMiscUtils::toHexUpper(c & 0xf);
}
}
if (output)
ba->truncate(length);
}
void q_toPercentEncoding(QByteArray *ba, const char *exclude, const char *include)
{
q_toPercentEncoding(ba, exclude, include, '%');
}
void q_normalizePercentEncoding(QByteArray *ba, const char *exclude)
{
q_fromPercentEncoding(ba, '%');
q_toPercentEncoding(ba, exclude, nullptr, '%');
}
/*!
\since 4.4
Returns a URI/URL-style percent-encoded copy of this byte array. The
\a percent parameter allows you to override the default '%'
character for another.
By default, this function will encode all characters that are not
one of the following:
ALPHA ("a" to "z" and "A" to "Z") / DIGIT (0 to 9) / "-" / "." / "_" / "~"
To prevent characters from being encoded pass them to \a
exclude. To force characters to be encoded pass them to \a
include. The \a percent character is always encoded.
Example:
\snippet code/src_corelib_tools_qbytearray.cpp 52
The hex encoding uses the numbers 0-9 and the uppercase letters A-F.
\sa fromPercentEncoding(), QUrl::toPercentEncoding()
*/
QByteArray QByteArray::toPercentEncoding(const QByteArray &exclude, const QByteArray &include,
char percent) const
{
if (isNull())
return QByteArray(); // preserve null
if (isEmpty())
return QByteArray(data(), 0);
QByteArray include2 = include;
if (percent != '%') // the default
if ((percent >= 0x61 && percent <= 0x7A) // ALPHA
|| (percent >= 0x41 && percent <= 0x5A) // ALPHA
|| (percent >= 0x30 && percent <= 0x39) // DIGIT
|| percent == 0x2D // -
|| percent == 0x2E // .
|| percent == 0x5F // _
|| percent == 0x7E) // ~
include2 += percent;
QByteArray result = *this;
q_toPercentEncoding(&result, exclude.nulTerminated().constData(), include2.nulTerminated().constData(), percent);
return result;
}
/*! \typedef QByteArray::ConstIterator
\internal
*/
/*! \typedef QByteArray::Iterator
\internal
*/
/*! \typedef QByteArray::const_iterator
This typedef provides an STL-style const iterator for QByteArray.
\sa QByteArray::const_reverse_iterator, QByteArray::iterator
*/
/*! \typedef QByteArray::iterator
This typedef provides an STL-style non-const iterator for QByteArray.
\sa QByteArray::reverse_iterator, QByteArray::const_iterator
*/
/*! \typedef QByteArray::const_reverse_iterator
\since 5.6
This typedef provides an STL-style const reverse iterator for QByteArray.
\sa QByteArray::reverse_iterator, QByteArray::const_iterator
*/
/*! \typedef QByteArray::reverse_iterator
\since 5.6
This typedef provides an STL-style non-const reverse iterator for QByteArray.
\sa QByteArray::const_reverse_iterator, QByteArray::iterator
*/
/*! \typedef QByteArray::size_type
\internal
*/
/*! \typedef QByteArray::difference_type
\internal
*/
/*! \typedef QByteArray::const_reference
\internal
*/
/*! \typedef QByteArray::reference
\internal
*/
/*! \typedef QByteArray::const_pointer
\internal
*/
/*! \typedef QByteArray::pointer
\internal
*/
/*! \typedef QByteArray::value_type
\internal
*/
/*!
\fn DataPtr &QByteArray::data_ptr()
\internal
*/
/*!
\typedef QByteArray::DataPtr
\internal
*/
/*!
\macro QByteArrayLiteral(ba)
\relates QByteArray
The macro generates the data for a QByteArray out of the string literal
\a ba at compile time. Creating a QByteArray from it is free in this case, and
the generated byte array data is stored in the read-only segment of the
compiled object file.
For instance:
\snippet code/src_corelib_tools_qbytearray.cpp 53
Using QByteArrayLiteral instead of a double quoted plain C++ string literal
can significantly speed up creation of QByteArray instances from data known
at compile time.
\sa QStringLiteral
*/
namespace QtPrivate {
namespace DeprecatedRefClassBehavior {
void warn(WarningType w, EmittingClass c)
{
const char *deprecatedBehaviorString =
"The corresponding behavior is deprecated, and will be changed"
" in a future version of Qt.";
const char *emittingClassName = nullptr;
switch (c) {
case EmittingClass::QByteRef:
emittingClassName = "QByteRef";
break;
case EmittingClass::QCharRef:
emittingClassName = "QCharRef";
break;
}
const char *containerClassName = nullptr;
switch (c) {
case EmittingClass::QByteRef:
containerClassName = "QByteArray";
break;
case EmittingClass::QCharRef:
containerClassName = "QString";
break;
}
switch (w) {
case WarningType::OutOfRange:
qWarning("Using %s with an index pointing outside the valid range of a %s. %s",
emittingClassName, containerClassName, deprecatedBehaviorString);
break;
case WarningType::DelayedDetach:
qWarning("Using %s on a %s that is not already detached. %s",
emittingClassName, containerClassName, deprecatedBehaviorString);
break;
}
}
} // namespace DeprecatedRefClassBehavior
} // namespace QtPrivate
/*!
\class QByteArray::FromBase64Result
\inmodule QtCore
\ingroup tools
\since 5.15
\brief The QByteArray::FromBase64Result class holds the result of
a call to QByteArray::fromBase64Encoding.
Objects of this class can be used to check whether the conversion
was successful, and if so, retrieve the decoded QByteArray. The
conversion operators defined for QByteArray::FromBase64Result make
its usage straightforward:
\snippet code/src_corelib_tools_qbytearray.cpp 44ter
Alternatively, it is possible to access the conversion status
and the decoded data directly:
\snippet code/src_corelib_tools_qbytearray.cpp 44quater
\sa QByteArray::fromBase64
*/
/*!
\variable QByteArray::FromBase64Result::decoded
Contains the decoded byte array.
*/
/*!
\variable QByteArray::FromBase64Result::decodingStatus
Contains whether the decoding was successful, expressed as a value
of type QByteArray::Base64DecodingStatus.
*/
/*!
\fn QByteArray::FromBase64Result::operator bool() const
Returns whether the decoding was successful. This is equivalent
to checking whether the \c{decodingStatus} member is equal to
QByteArray::Base64DecodingStatus::Ok.
*/
/*!
\fn QByteArray &QByteArray::FromBase64Result::operator*() const
Returns the decoded byte array.
*/
/*!
\fn bool operator==(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
\relates QByteArray::FromBase64Result
Returns \c true if \a lhs and \a rhs are equal, otherwise returns \c false.
\a lhs and \a rhs are equal if and only if they contain the same decoding
status and, if the status is QByteArray::Base64DecodingStatus::Ok, if and
only if they contain the same decoded data.
*/
/*!
\fn bool operator!=(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
\relates QByteArray::FromBase64Result
Returns \c true if \a lhs and \a rhs are different, otherwise returns \c false.
*/
/*!
\relates QByteArray::FromBase64Result
Returns the hash value for \a key, using
\a seed to seed the calculation.
*/
uint qHash(const QByteArray::FromBase64Result &key, uint seed) noexcept
{
QtPrivate::QHashCombine hash;
seed = hash(seed, key.decoded);
seed = hash(seed, static_cast<int>(key.decodingStatus));
return seed;
}
QT_END_NAMESPACE
|