9#include "private/qtools_p.h"
14#include "private/qnumeric_p.h"
15#include "private/qsimd_p.h"
19#include <qdatastream.h>
22#include "private/qstdweb_p.h"
24#include <QtCore/private/qtclasshelper_p.h>
29#include <qxpfunctional.h>
37#include <QtCore/q26numeric.h>
41# if !defined(QT_BOOTSTRAPPED) && (defined(QT_NO_CAST_FROM_ASCII) || defined(QT_NO_CAST_FROM_BYTEARRAY))
43# error "This file cannot be compiled with QT_NO_CAST_{TO,FROM}_ASCII, "
44 "otherwise some QByteArray functions will not get exported."
50Q_CONSTINIT
const char QByteArray::_empty =
'\0';
55 return c >=
'a' && c <=
'z' ? c & ~0x20 : c;
60 return c >=
'A' && c <=
'Z' ? c | 0x20 : c;
64
65
68
69
70
71
72
73
74
75
76const void *
qmemrchr(
const void *s,
int needle, size_t size)
noexcept
79 return memrchr(s, needle, size);
81 auto b =
static_cast<
const uchar *>(s);
82 const uchar *n = b + size;
84 if (*n == uchar(needle))
92
93
94
95
96
97
98
99
100
101
107 char *dst =
new char[strlen(src) + 1];
108 return qstrcpy(dst, src);
112
113
114
115
116
117
118
119
120
121
122
123
130 const size_t len = strlen(src);
133 if (len >= 0 && strcpy_s(dst, len+1, src) == 0)
137 return strcpy(dst, src);
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
162char *
qstrncpy(
char *dst,
const char *src, size_t len)
164 if (dst && len > 0) {
167 std::strncat(dst, src, len - 1);
169 return src ? dst :
nullptr;
173
174
175
176
177
178
179
180
181
184
185
186
187
188
189
190
191
192
193
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211int qstrcmp(
const char *str1,
const char *str2)
213 return (str1 && str2) ? strcmp(str1, str2)
214 : (str1 ? 1 : (str2 ? -1 : 0));
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
258 const uchar *s1 =
reinterpret_cast<
const uchar *>(str1);
259 const uchar *s2 =
reinterpret_cast<
const uchar *>(str2);
265 enum { Incomplete = 256 };
267 auto innerCompare = [=, &offset](qptrdiff max,
bool unlimited) {
270 uchar c = s1[offset];
271 if (
int res = QtMiscUtils::caseCompareAscii(c, s2[offset]))
276 }
while (unlimited || offset < max);
277 return int(Incomplete);
280#if defined(__SSE4_1__) && !(defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer))
281 enum { PageSize = 4096, PageMask = PageSize - 1 };
282 const __m128i zero = _mm_setzero_si128();
287 quintptr u1 = quintptr(s1 + offset);
288 quintptr u2 = quintptr(s2 + offset);
289 size_t n = PageSize - ((u1 | u2) & PageMask);
291 qptrdiff maxoffset = offset + n;
292 for ( ; offset + 16 <= maxoffset; offset +=
sizeof(__m128i)) {
294 __m128i a = _mm_loadu_si128(
reinterpret_cast<
const __m128i *>(s1 + offset));
295 __m128i b = _mm_loadu_si128(
reinterpret_cast<
const __m128i *>(s2 + offset));
298 __m128i cmp = _mm_cmpeq_epi8(a, b);
301 cmp = _mm_min_epu8(cmp, a);
302 cmp = _mm_cmpeq_epi8(cmp, zero);
305 uint mask = _mm_movemask_epi8(cmp);
308 uint start = qCountTrailingZeroBits(mask);
309 uint end =
sizeof(mask) * 8 - qCountLeadingZeroBits(mask);
310 Q_ASSERT(end >= start);
318 int res = innerCompare(n,
false);
319 if (res != Incomplete)
324 return innerCompare(-1,
true);
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
346int qstrnicmp(
const char *s1,
const char *s2, size_t len)
349 return s1 ? 1 : (s2 ? -1 : 0);
350 for (; len--; ++s1, ++s2) {
352 if (
int res = QtMiscUtils::caseCompareAscii(c, *s2))
361
362
363
364
365
366
367
368int qstrnicmp(
const char *s1, qsizetype len1,
const char *s2, qsizetype len2)
371 Q_ASSERT(len2 >= -1);
376 return (!s2 || !*s2) ? 0 : -1;
381 return len1 == 0 ? 0 : 1;
386 for (i = 0; i < len1; ++i) {
387 const char c = s2[i];
391 if (
int res = QtMiscUtils::caseCompareAscii(s1[i], c))
394 return s2[i] ? -1 : 0;
397 const qsizetype len = qMin(len1, len2);
398 for (qsizetype i = 0; i < len; ++i) {
399 if (
int res = QtMiscUtils::caseCompareAscii(s1[i], s2[i]))
404 return len1 < len2 ? -1 : 1;
409
410
411int QtPrivate::compareMemory(QByteArrayView lhs, QByteArrayView rhs)
413 if (!lhs.isNull() && !rhs.isNull()) {
414 int ret = memcmp(lhs.data(), rhs.data(), qMin(lhs.size(), rhs.size()));
421 return lhs.size() == rhs.size() ? 0 : lhs.size() > rhs.size() ? 1 : -1;
425
426
429 return QUtf8::isValidUtf8(s).isValidUtf8;
434static void createCRC16Table()
438 unsigned short crc_tbl[16];
439 unsigned int v0, v1, v2, v3;
440 for (i = 0; i < 16; i++) {
447#define SET_BIT(x, b, v) (x) |= (v) << (b)
462 printf(
"static const quint16 crc_tbl[16] = {\n");
463 for (
int i = 0; i < 16; i +=4)
464 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]);
470 0x0000, 0x1081, 0x2102, 0x3183,
471 0x4204, 0x5285, 0x6306, 0x7387,
472 0x8408, 0x9489, 0xa50a, 0xb58b,
473 0xc60c, 0xd68d, 0xe70e, 0xf78f
477
478
479
480
481
482
483
484
485
486
487
488
491 quint16 crc = 0x0000;
493 case Qt::ChecksumIso3309:
496 case Qt::ChecksumItuV41:
501 const uchar *p =
reinterpret_cast<
const uchar *>(data.data());
502 qsizetype len = data.size();
505 crc = ((crc >> 4) & 0x0fff) ^ crc_tbl[((crc ^ c) & 15)];
507 crc = ((crc >> 4) & 0x0fff) ^ crc_tbl[((crc ^ c) & 15)];
510 case Qt::ChecksumIso3309:
513 case Qt::ChecksumItuV41:
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
540
541
542
543
544
545
546
547
549#ifndef QT_NO_COMPRESS
550using CompressSizeHint_t = quint32;
561 Q_UNREACHABLE_RETURN(
nullptr);
565static QByteArray zlibError(ZLibOp op,
const char *what)
567 qWarning(
"%s: %s", zlibOpAsString(op), what);
572static QByteArray dataIsNull(ZLibOp op)
574 return zlibError(op,
"Data is null");
578static QByteArray lengthIsNegative(ZLibOp op)
580 return zlibError(op,
"Input length is negative");
584static QByteArray tooMuchData(ZLibOp op)
586 return zlibError(op,
"Not enough memory");
590static QByteArray invalidCompressedData()
592 return zlibError(ZLibOp::Decompression,
"Input data is corrupted");
596static QByteArray unexpectedZlibError(ZLibOp op,
int err,
const char *msg)
598 qWarning(
"%s unexpected zlib error: %s (%d)",
610 if (out.data() ==
nullptr)
611 return tooMuchData(op);
612 qsizetype capacity = out.allocatedCapacity();
614 const auto initalSize = out.size;
617 zs.next_in =
reinterpret_cast<uchar *>(
const_cast<
char *>(input.data()));
618 if (
const int err = init(&zs); err != Z_OK)
619 return unexpectedZlibError(op, err, zs.msg);
620 const auto sg = qScopeGuard([&] { deinit(&zs); });
622 using ZlibChunkSize_t =
decltype(zs.avail_in);
623 static_assert(!std::is_signed_v<ZlibChunkSize_t>);
624 static_assert(std::is_same_v<ZlibChunkSize_t,
decltype(zs.avail_out)>);
625 constexpr auto MaxChunkSize =
std::numeric_limits<ZlibChunkSize_t>::max();
627 constexpr auto MaxStatisticsSize =
std::numeric_limits<
decltype(zs.total_out)>::max();
629 size_t inputLeft = size_t(input.size());
633 Q_ASSERT(out.freeSpaceAtBegin() == 0);
634 Q_ASSERT(capacity == out.allocatedCapacity());
636 if (zs.avail_out == 0) {
637 Q_ASSERT(size_t(out.size) - initalSize > MaxStatisticsSize ||
638 size_t(out.size) - initalSize == zs.total_out);
639 Q_ASSERT(out.size <= capacity);
641 qsizetype avail_out = capacity - out.size;
642 if (avail_out == 0) {
643 out->reallocateAndGrow(QArrayData::GrowsAtEnd, 1);
644 if (out.data() ==
nullptr)
645 return tooMuchData(op);
646 capacity = out.allocatedCapacity();
647 avail_out = capacity - out.size;
649 zs.next_out =
reinterpret_cast<uchar *>(out.data()) + out.size;
650 zs.avail_out = size_t(avail_out) > size_t(MaxChunkSize) ? MaxChunkSize
651 : ZlibChunkSize_t(avail_out);
652 out.size += zs.avail_out;
654 Q_ASSERT(zs.avail_out > 0);
657 if (zs.avail_in == 0) {
659 zs.avail_in = inputLeft > MaxChunkSize ? MaxChunkSize : ZlibChunkSize_t(inputLeft);
660 inputLeft -= zs.avail_in;
663 res = processChunk(&zs, inputLeft);
664 }
while (res == Z_OK);
668 out.size -= zs.avail_out;
669 Q_ASSERT(size_t(out.size) - initalSize > MaxStatisticsSize ||
670 size_t(out.size) - initalSize == zs.total_out);
671 Q_ASSERT(out.size <= out.allocatedCapacity());
672 out.data()[out.size] =
'\0';
673 return QByteArray(
std::move(out));
676 return tooMuchData(op);
684 return invalidCompressedData();
687 return unexpectedZlibError(op, res, zs.msg);
693 constexpr qsizetype HeaderSize =
sizeof(CompressSizeHint_t);
695 return QByteArray(HeaderSize,
'\0');
703 if (compressionLevel < -1 || compressionLevel > 9)
704 compressionLevel = -1;
706 QArrayDataPointer out = [&] {
707 constexpr qsizetype SingleAllocLimit = 256 * 1024;
711 qsizetype capacity = HeaderSize;
712 if (nbytes < SingleAllocLimit) {
714 capacity += compressBound(uLong(nbytes));
715 return QArrayDataPointer<
char>(capacity);
720 constexpr qsizetype MaxCompressionFactor = 1024;
723 capacity +=
std::max(qsizetype(compressBound(uLong(SingleAllocLimit))),
724 nbytes / MaxCompressionFactor);
725 return QArrayDataPointer<
char>(capacity, 0, QArrayData::Grow);
728 if (out.data() ==
nullptr)
731 qToBigEndian(q26::saturating_cast<CompressSizeHint_t>(nbytes), out.data());
732 out.size = HeaderSize;
734 return xxflate(ZLibOp::Compression, std::move(out), {data, nbytes},
735 [=] (z_stream *zs) {
return deflateInit(zs, compressionLevel); },
736 [] (z_stream *zs, size_t inputLeft) {
737 return deflate(zs, inputLeft ? Z_NO_FLUSH : Z_FINISH);
739 [] (z_stream *zs) { deflateEnd(zs); });
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
775#ifndef QT_NO_COMPRESS
777
778
779
780
781
782
791 constexpr qsizetype HeaderSize =
sizeof(CompressSizeHint_t);
792 if (nbytes < HeaderSize)
793 return invalidCompressedData();
795 const auto expectedSize = qFromBigEndian<CompressSizeHint_t>(data);
796 if (nbytes == HeaderSize) {
797 if (expectedSize != 0)
798 return invalidCompressedData();
802 constexpr auto MaxDecompressedSize = size_t(QByteArray::maxSize());
803 if constexpr (MaxDecompressedSize < std::numeric_limits<CompressSizeHint_t>::max()) {
804 if (expectedSize > MaxDecompressedSize)
810 qsizetype capacity =
std::max(qsizetype(expectedSize),
813 QArrayDataPointer<
char> d(capacity);
814 return xxflate(ZLibOp::Decompression, std::move(d), {data + HeaderSize, nbytes - HeaderSize},
815 [] (z_stream *zs) {
return inflateInit(zs); },
816 [] (z_stream *zs, size_t) {
return inflate(zs, Z_NO_FLUSH); },
817 [] (z_stream *zs) { inflateEnd(zs); });
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
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
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1118
1119
1120
1123
1124
1125
1126
1127
1128
1129
1130
1131
1134
1135
1136
1137
1138
1139
1140
1141
1144
1145
1146
1147
1148
1149
1150
1151
1154
1155
1156
1159
1160
1161
1162
1163
1164
1165
1166
1167
1170
1171
1172
1173
1174
1175
1176
1177
1180
1181
1182
1183
1184
1185
1186
1187
1188
1191
1192
1193
1196
1197
1198
1199
1200
1201
1202
1203
1204
1207
1208
1209
1210
1211
1212
1213
1214
1215
1218
1219
1220
1223
1224
1225
1226
1227
1228
1229
1230
1231
1234
1235
1236
1237
1240
1241
1242
1243
1244
1247
1248
1249
1250
1251
1254
1255
1256
1257
1258
1261
1262
1263
1264
1267
1268
1269
1270
1271
1274
1275
1276
1277
1278
1281
1282
1283
1284
1285
1288
1289
1290
1291
1292
1295
1296
1297
1298
1299
1300
1301QByteArray::iterator
QByteArray::erase(QByteArray::const_iterator first, QByteArray::const_iterator last)
1303 const auto start =
std::distance(cbegin(), first);
1304 const auto len =
std::distance(first, last);
1306 return begin() + start;
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1338
1339
1340
1341
1342
1343
1344
1347
1348
1349
1350
1351
1354
1355
1358
1359
1360
1369
1370
1371
1372
1373
1374
1375
1382 d = DataPointer::fromRawData(&_empty, 0);
1390
1391
1392
1393
1394
1395
1398
1399
1400
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1421
1422
1423
1424
1425
1426
1427
1430
1431
1432
1433
1434
1435
1436
1437
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
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
1487
1488
1489
1490
1491
1492
1493
1494
1495
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
1525
1526
1527
1528
1529
1530
1531
1532
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
1570
1571
1572
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1593
1594
1595
1598
1599
1600
1603
1604
1605
1608
1609
1610
1611
1612
1613
1614
1615
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1631
1632
1633
1634
1635
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1698
1699
1700
1701
1702
1703
1704
1707
1708
1709
1710
1711
1712
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1776
1777
1778
1779
1780
1781
1784
1785
1786
1787
1788
1789
1792
1793
1794
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1812
1813
1814
1815
1816
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1838 size = qstrlen(data);
1840 d = DataPointer::fromRawData(&_empty, 0);
1842 d = DataPointer(size, size);
1843 Q_CHECK_PTR(d.data());
1844 memcpy(d.data(), data, size);
1845 d.data()[size] =
'\0';
1851
1852
1853
1854
1859 d = DataPointer::fromRawData(&_empty, 0);
1861 d = DataPointer(size, size);
1862 Q_CHECK_PTR(d.data());
1863 memset(d.data(), ch, size);
1864 d.data()[size] =
'\0';
1869
1870
1871
1872
1873
1874
1875
1880 d = DataPointer::fromRawData(&_empty, 0);
1882 d = DataPointer(size, size);
1883 Q_CHECK_PTR(d.data());
1884 d.data()[size] =
'\0';
1889
1890
1891
1892
1893
1894
1895
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1917 const auto capacityAtEnd = capacity() - d.freeSpaceAtBegin();
1918 if (d->needsDetach() || size > capacityAtEnd)
1919 reallocData(size, QArrayData::Grow);
1921 if (d->allocatedCapacity())
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1944 const auto old = d.size;
1947 memset(d.data() + old, c, d.size - old);
1951
1952
1953
1954
1955
1956
1957
1958
1959
1966
1967
1968
1969
1970
1971
1972
1973
1977 resize(size < 0 ?
this->size() : size);
1979 memset(d.data(), ch,
this->size());
1983void QByteArray::reallocData(qsizetype alloc, QArrayData::AllocationOption option)
1986 d = DataPointer::fromRawData(&_empty, 0);
1992 const bool cannotUseReallocate = d.freeSpaceAtBegin() > 0;
1994 if (d->needsDetach() || cannotUseReallocate) {
1995 DataPointer dd(alloc, qMin(alloc, d.size), option);
1996 Q_CHECK_PTR(dd.data());
1998 ::memcpy(dd.data(), d.data(), dd.size);
1999 dd.data()[dd.size] = 0;
2002 d->reallocate(alloc, option);
2011 if (d->needsDetach()) {
2012 DataPointer dd(DataPointer::allocateGrow(d, n, QArrayData::GrowsAtEnd));
2013 Q_CHECK_PTR(dd.data());
2014 dd->copyAppend(d.data(), d.data() + d.size);
2015 dd.data()[dd.size] = 0;
2018 d->reallocate(d.constAllocatedCapacity() + n, QArrayData::Grow);
2024 resize(qMax(i + 1, size()));
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2049
2050
2051
2052
2053
2054
2055
2056
2057
2069 return std::move(*
this);
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2091
2092
2093
2094
2095
2098 if (size() == 0 && ba.size() > d.constAllocatedCapacity() && ba.d.isMutable())
2099 return (*
this = ba);
2100 return prepend(QByteArrayView(ba));
2104
2105
2106
2107
2108
2111
2112
2113
2114
2115
2116
2117
2120
2121
2122
2123
2124
2125
2128
2129
2130
2131
2132
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2162 if (Q_UNLIKELY(!ba.d.isMutable()))
2166 }
else if (ba.size()) {
2167 append(QByteArrayView(ba));
2174
2175
2176
2177
2178
2181
2182
2183
2184
2185
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2204
2205
2206
2207
2208
2209
2210
2211
2212
2215
2216
2217
2218
2222 d.detachAndGrow(QArrayData::GrowsAtEnd, 1,
nullptr,
nullptr);
2223 d->copyAppend(1, ch);
2224 d.data()[d.size] =
'\0';
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2279 const auto len = v.size();
2281 if (len <= capacity() && isDetached()) {
2282 const auto offset = d.freeSpaceAtBegin();
2284 d.setBegin(d.begin() - offset);
2285 std::memcpy(d.begin(), v.data(), len);
2287 d.data()[d.size] =
'\0';
2289 *
this = v.toByteArray();
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2316 const char *str = data.data();
2317 qsizetype size = data.size();
2318 if (i < 0 || size <= 0)
2327 DataPointer detached{};
2328 d.detachAndGrow(Data::GrowsAtEnd, (i - d.size) + size, &str, &detached);
2329 Q_CHECK_PTR(d.data());
2330 d->copyAppend(i - d->size,
' ');
2331 d->copyAppend(str, str + size);
2332 d.data()[d.size] =
'\0';
2336 if (!d->needsDetach() && QtPrivate::q_points_into_range(str, d)) {
2337 QVarLengthArray a(str, str + size);
2338 return insert(i, a);
2341 d->insert(i, str, size);
2342 d.data()[d.size] =
'\0';
2347
2348
2349
2350
2351
2352
2353
2354
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2369
2370
2371
2372
2373
2374
2375
2376
2377
2380
2381
2382
2383
2384
2385
2386
2389
2390
2391
2392
2393
2394
2395
2396
2397
2401 if (i < 0 || count <= 0)
2406 d.detachAndGrow(Data::GrowsAtEnd, (i - d.size) + count,
nullptr,
nullptr);
2407 Q_CHECK_PTR(d.data());
2408 d->copyAppend(i - d->size,
' ');
2409 d->copyAppend(count, ch);
2410 d.data()[d.size] =
'\0';
2414 d->insert(i, count, ch);
2415 d.data()[d.size] =
'\0';
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2439 if (len <= 0 || pos < 0 || size_t(pos) >= size_t(size()))
2441 if (pos + len > d->size)
2442 len = d->size - pos;
2444 const auto toRemove_start = d.begin() + pos;
2445 if (!d->isShared()) {
2446 d->erase(toRemove_start, len);
2447 d.data()[d.size] =
'\0';
2449 QByteArray copy{size() - len, Qt::Uninitialized};
2450 copy.d->copyRanges({{d.begin(), toRemove_start},
2451 {toRemove_start + len, d.end()}});
2458
2459
2460
2461
2462
2463
2464
2465
2466
2469
2470
2471
2472
2473
2474
2475
2476
2477
2479
2480
2481
2482
2483
2484
2485
2486
2487
2490
2491
2492
2493
2494
2495
2496
2497
2500
2501
2502
2503
2504
2505
2506
2507
2511 if (size_t(pos) > size_t(
this->size()))
2513 if (len >
this->size() - pos)
2514 len =
this->size() - pos;
2519 return insert(pos, after);
2521 if (after.isEmpty())
2522 return remove(pos, len);
2524 using A = QStringAlgorithms<QByteArray>;
2525 const qsizetype newlen = A::newSize(*
this, len, after, {pos});
2526 if (data_ptr().needsDetach() || A::needsReallocate(*
this, newlen)) {
2527 A::replace_into_copy(*
this, len, after, {pos}, newlen);
2532 char *
const begin = data_ptr().data();
2533 char *
const before = begin + pos;
2534 const char *beforeEnd = before + len;
2535 if (len >= after.size()) {
2536 memmove(before , after.cbegin(), after.size());
2538 if (len > after.size()) {
2539 memmove(before + after.size(), beforeEnd, d.size - (beforeEnd - begin));
2540 A::setSize(*
this, newlen);
2543 char *oldEnd = begin + d.size;
2544 const qsizetype adjust = newlen - d.size;
2545 A::setSize(*
this, newlen);
2547 QByteArrayView tail{beforeEnd, oldEnd};
2548 QByteArrayView prefix = after;
2549 QByteArrayView suffix;
2550 if (QtPrivate::q_points_into_range(after.cend() - 1, tail)) {
2551 if (QtPrivate::q_points_into_range(after.cbegin(), tail)) {
2554 suffix = QByteArrayView{after.cbegin(), after.cend()};
2556 prefix = QByteArrayView{after.cbegin(), beforeEnd};
2557 suffix = QByteArrayView{beforeEnd, after.cend()};
2560 memmove(before + after.size(), tail.cbegin(), tail.size());
2561 if (!prefix.isEmpty())
2562 memmove(before, prefix.cbegin(), prefix.size());
2563 if (!suffix.isEmpty())
2564 memcpy(before + prefix.size(), suffix.cbegin() + adjust, suffix.size());
2570
2571
2572
2573
2574
2575
2576
2577
2580
2581
2582
2583
2584
2585
2586
2587
2590
2591
2592
2593
2594
2595
2596
2597
2598
2602 const char *b = before.data();
2603 qsizetype bsize = before.size();
2604 const char *a = after.data();
2605 qsizetype asize = after.size();
2611 if (b == a && bsize == asize)
2614 if (asize == 0 && bsize == 0)
2617 if (bsize == 1 && asize == 1)
2618 return replace(*b, *a);
2621 std::string pinnedReplacement;
2622 if (QtPrivate::q_points_into_range(a, d)) {
2623 pinnedReplacement.assign(a, a + asize);
2624 after = pinnedReplacement;
2632 QVarLengthArray<qsizetype> indices;
2633 qsizetype index = 0;
2634 while ((index = matcher.indexIn(*
this, index)) != -1) {
2635 indices.push_back(index);
2642 QStringAlgorithms<QByteArray>::replace_helper(*
this, bsize, after, indices);
2647
2648
2649
2650
2651
2652
2655
2656
2657
2658
2662 if (before != after) {
2663 if (
const auto pos = indexOf(before); pos >= 0) {
2664 if (d.needsDetach()) {
2666 auto dst = tmp.d.data();
2667 dst =
std::copy(d.data(), d.data() + pos, dst);
2669 std::replace_copy(d.data() + pos + 1, d.end(), dst, before, after);
2673 d.data()[pos] = after;
2674 std::replace(d.data() + pos + 1, d.end(), before, after);
2682
2683
2684
2685
2686
2691 qsizetype start = 0;
2693 while ((end = indexOf(sep, start)) != -1) {
2694 list.append(mid(start, end - start));
2697 list.append(mid(start));
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2723 const qsizetype resultSize = times * size();
2726 result.reserve(resultSize);
2727 if (result.capacity() != resultSize)
2730 memcpy(result.d.data(), data(), size());
2732 qsizetype sizeSoFar = size();
2733 char *end = result.d.data() + sizeSoFar;
2735 const qsizetype halfResultSize = resultSize >> 1;
2736 while (sizeSoFar <= halfResultSize) {
2737 memcpy(end, result.d.data(), sizeSoFar);
2741 memcpy(end, result.d.data(), resultSize - sizeSoFar);
2742 result.d.data()[resultSize] =
'\0';
2743 result.d.size = resultSize;
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2775 qsizetype ol, qsizetype from)
2777 auto delta = l - ol;
2780 if (from < 0 || from > delta)
2785 const char *end = haystack;
2787 const qregisteruint ol_minus_1 = ol - 1;
2788 const char *n = needle + ol_minus_1;
2789 const char *h = haystack + ol_minus_1;
2790 qregisteruint hashNeedle = 0, hashHaystack = 0;
2792 for (idx = 0; idx < ol; ++idx) {
2793 hashNeedle = ((hashNeedle<<1) + *(n-idx));
2794 hashHaystack = ((hashHaystack<<1) + *(h-idx));
2796 hashHaystack -= *haystack;
2797 while (haystack >= end) {
2798 hashHaystack += *haystack;
2799 if (hashHaystack == hashNeedle && memcmp(needle, haystack, ol) == 0)
2800 return haystack - end;
2802 if (ol_minus_1 <
sizeof(ol_minus_1) * CHAR_BIT)
2803 hashHaystack -= qregisteruint(*(haystack + ol)) << ol_minus_1;
2809qsizetype
QtPrivate::lastIndexOf(QByteArrayView haystack, qsizetype from, QByteArrayView needle)
noexcept
2811 if (haystack.isEmpty()) {
2812 if (needle.isEmpty() && from == 0)
2816 const auto ol = needle.size();
2818 return QtPrivate::lastIndexOf(haystack, from, needle.front());
2820 return lastIndexOfHelper(haystack.data(), haystack.size(), needle.data(), ol, from);
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2879 for (
char ch : haystack) {
2886qsizetype
QtPrivate::count(QByteArrayView haystack, QByteArrayView needle)
noexcept
2888 if (needle.size() == 0)
2889 return haystack.size() + 1;
2891 if (needle.size() == 1)
2892 return countCharHelper(haystack, needle[0]);
2896 if (haystack.size() > 500 && needle.size() > 5) {
2898 while ((i = matcher.indexIn(haystack, i + 1)) != -1)
2901 while ((i = haystack.indexOf(needle, i + 1)) != -1)
2908
2909
2910
2911
2912
2913
2914
2917
2918
2919
2920
2921
2922
2926 return countCharHelper(*
this, ch);
2929#if QT_DEPRECATED_SINCE(6
, 4
)
2931
2932
2933
2934
2935
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2950bool QtPrivate::startsWith(QByteArrayView haystack, QByteArrayView needle)
noexcept
2952 if (haystack.size() < needle.size())
2954 if (haystack.data() == needle.data() || needle.size() == 0)
2956 return memcmp(haystack.data(), needle.data(), needle.size()) == 0;
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2972
2973
2974
2975
2976
2977
2979bool QtPrivate::endsWith(QByteArrayView haystack, QByteArrayView needle)
noexcept
2981 if (haystack.size() < needle.size())
2983 if (haystack.end() == needle.end() || needle.size() == 0)
2985 return memcmp(haystack.end() - needle.size(), needle.data(), needle.size()) == 0;
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3002
3003
3004
3005
3006
3007
3010
3011
3014 return c >=
'A' && c <=
'Z';
3018
3019
3022 return c >=
'a' && c <=
'z';
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3058
3059
3060
3061
3062
3063
3064
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3123 switch (QContainerImplHelper::mid(size(), &p, &l)) {
3124 case QContainerImplHelper::Null:
3126 case QContainerImplHelper::Empty:
3128 return QByteArray(DataPointer::fromRawData(&_empty, 0));
3130 case QContainerImplHelper::Full:
3132 case QContainerImplHelper::Subset:
3133 return sliced(p, l);
3143 switch (QContainerImplHelper::mid(size(), &p, &l)) {
3144 case QContainerImplHelper::Null:
3146 case QContainerImplHelper::Empty:
3149 case QContainerImplHelper::Full:
3150 return std::move(*
this);
3151 case QContainerImplHelper::Subset:
3152 return std::move(*
this).sliced(p, l);
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3206 return fromRawData(&_empty, 0);
3207 DataPointer d =
std::move(a.d).sliced(pos, n);
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3283 const char *orig_begin = input.constBegin();
3284 const char *firstBad = orig_begin;
3285 const char *e = input.constEnd();
3286 for ( ; firstBad != e ; ++firstBad) {
3287 uchar ch = uchar(*firstBad);
3288 uchar converted = lookup(ch);
3289 if (ch != converted)
3294 return q_choose_copy_move(input, rvalue);
3297 QByteArray s = q_choose_copy_move(input, rvalue);
3298 char *b = s.begin();
3299 char *p = b + (firstBad - orig_begin);
3301 for ( ; p != e; ++p)
3302 *p =
char(lookup(uchar(*p)));
3308 return toCase(a,
nullptr, asciiLower);
3313 return toCase(a, &a, asciiLower);
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3330 return toCase(a,
nullptr, asciiUpper);
3335 return toCase(a, &a, asciiUpper);
3339
3340
3341
3342
3343
3350#if !defined(QT_NO_DATASTREAM)
3353
3354
3355
3356
3357
3358
3362 if (ba.isNull() && out.version() >= 6) {
3363 QDataStream::writeQSizeType(out, -1);
3366 return out.writeBytes(ba.constData(), ba.size());
3370
3371
3372
3373
3374
3375
3381 qint64 size = QDataStream::readQSizeType(in);
3382 qsizetype len = size;
3383 if (size != len || size < -1) {
3385 in.setStatus(QDataStream::SizeLimitExceeded);
3393 constexpr qsizetype Step = 1024 * 1024;
3394 qsizetype allocated = 0;
3397 qsizetype blockSize = qMin(Step, len - allocated);
3398 ba.resize(allocated + blockSize);
3399 if (in.readRawData(ba.data() + allocated, blockSize) != blockSize) {
3401 in.setStatus(QDataStream::ReadPastEnd);
3404 allocated += blockSize;
3405 }
while (allocated < len);
3412
3413
3414
3415
3416
3417
3418
3421
3422
3423
3424
3425
3426
3427
3430
3431
3432
3433
3434
3435
3436
3439
3440
3441
3442
3443
3444
3445
3448
3449
3450
3451
3452
3453
3454
3457
3458
3459
3460
3461
3462
3463
3466
3467
3468
3469
3470
3471
3472
3475
3476
3477
3478
3479
3480
3481
3484
3485
3486
3487
3488
3489
3490
3493
3494
3495
3496
3497
3498
3499
3502
3503
3504
3505
3506
3507
3508
3511
3512
3513
3514
3515
3516
3517
3520
3521
3522
3523
3524
3525
3526
3529
3530
3531
3532
3533
3534
3535
3538
3539
3540
3541
3542
3543
3544
3547
3548
3549
3550
3551
3552
3553
3556
3557
3558
3559
3560
3561
3562
3565
3566
3567
3568
3569
3570
3571
3574
3575
3576
3577
3578
3579
3580
3583
3584
3585
3586
3587
3588
3589
3592
3593
3594
3595
3596
3597
3598
3601
3602
3603
3604
3605
3606
3607
3610
3611
3612
3613
3614
3615
3616
3619
3620
3621
3622
3623
3624
3625
3626
3627
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3648 return QStringAlgorithms<
const QByteArray>::simplified_helper(a);
3653 return QStringAlgorithms<QByteArray>::simplified_helper(a);
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3677 return QStringAlgorithms<
const QByteArray>::trimmed_helper(a);
3682 return QStringAlgorithms<QByteArray>::trimmed_helper(a);
3685QByteArrayView
QtPrivate::trimmed(QByteArrayView view)
noexcept
3687 const auto [start, stop] = QStringAlgorithms<QByteArrayView>::trimmed_helper_positions(view);
3688 return QByteArrayView(start, stop);
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3712 qsizetype len = size();
3713 qsizetype padlen = width - len;
3715 result.resize(len+padlen);
3717 memcpy(result.d.data(), data(), len);
3718 memset(result.d.data()+len, fill, padlen);
3721 result = left(width);
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3749 qsizetype len = size();
3750 qsizetype padlen = width - len;
3752 result.resize(len+padlen);
3754 memcpy(result.d.data()+padlen, data(), len);
3755 memset(result.d.data(), fill, padlen);
3758 result = left(width);
3765auto QtPrivate::toSignedInteger(QByteArrayView data,
int base) -> ParsedNumber<qlonglong>
3767#if defined(QT_CHECK_RANGE)
3768 if (base != 0 && (base < 2 || base > 36)) {
3769 qWarning(
"QByteArray::toIntegral: Invalid base %d", base);
3776 const QSimpleParsedNumber r = QLocaleData::bytearrayToLongLong(data, base);
3778 return ParsedNumber(r.result);
3782auto QtPrivate::toUnsignedInteger(QByteArrayView data,
int base) -> ParsedNumber<qulonglong>
3784#if defined(QT_CHECK_RANGE)
3785 if (base != 0 && (base < 2 || base > 36)) {
3786 qWarning(
"QByteArray::toIntegral: Invalid base %d", base);
3793 const QSimpleParsedNumber r = QLocaleData::bytearrayToUnsLongLong(data, base);
3795 return ParsedNumber(r.result);
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3826 return QtPrivate::toIntegral<qlonglong>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3856 return QtPrivate::toIntegral<qulonglong>(qToByteArrayViewIgnoringNull(*
this), ok, base);
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
3888 return QtPrivate::toIntegral<
int>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3918 return QtPrivate::toIntegral<uint>(qToByteArrayViewIgnoringNull(*
this), ok, base);
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
3951 return QtPrivate::toIntegral<
long>(qToByteArrayViewIgnoringNull(*
this), ok, base);
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
3982 return QtPrivate::toIntegral<ulong>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4012 return QtPrivate::toIntegral<
short>(qToByteArrayViewIgnoringNull(*
this), ok, base);
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4042 return QtPrivate::toIntegral<ushort>(qToByteArrayViewIgnoringNull(*
this), ok, base);
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4072 return QByteArrayView(*
this).toDouble(ok);
4075auto QtPrivate::toDouble(QByteArrayView a)
noexcept -> ParsedNumber<
double>
4078 auto r = qt_asciiToDouble(a.data(), a.size());
4080 return ParsedNumber{r.result};
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4115auto QtPrivate::toFloat(QByteArrayView a)
noexcept -> ParsedNumber<
float>
4117 if (
const auto r = toDouble(a)) {
4121 return ParsedNumber(f);
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4139 constexpr char alphabet_base64[] =
"ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef"
4140 "ghijklmn" "opqrstuv" "wxyz0123" "456789+/";
4141 constexpr char alphabet_base64url[] =
"ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef"
4142 "ghijklmn" "opqrstuv" "wxyz0123" "456789-_";
4143 const char *
const alphabet = options & Base64UrlEncoding ? alphabet_base64url : alphabet_base64;
4144 constexpr char padchar =
'=';
4145 qsizetype padlen = 0;
4147 const qsizetype sz = size();
4149 QByteArray tmp((sz + 2) / 3 * 4, Qt::Uninitialized);
4152 char *out = tmp.data();
4156 chunk |=
int(uchar(data()[i++])) << 16;
4160 chunk |=
int(uchar(data()[i++])) << 8;
4164 chunk |=
int(uchar(data()[i++]));
4167 int j = (chunk & 0x00fc0000) >> 18;
4168 int k = (chunk & 0x0003f000) >> 12;
4169 int l = (chunk & 0x00000fc0) >> 6;
4170 int m = (chunk & 0x0000003f);
4171 *out++ = alphabet[j];
4172 *out++ = alphabet[k];
4175 if ((options & OmitTrailingEquals) == 0)
4178 *out++ = alphabet[l];
4181 if ((options & OmitTrailingEquals) == 0)
4184 *out++ = alphabet[m];
4187 Q_ASSERT((options & OmitTrailingEquals) || (out == tmp.size() + tmp.data()));
4188 if (options & OmitTrailingEquals)
4189 tmp.truncate(out - tmp.data());
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4214
4215
4216
4217
4218
4221
4222
4223
4224
4225
4228
4229
4230
4231
4232
4235
4236
4237
4238
4239
4242
4243
4244
4245
4246
4249
4250
4251
4252
4255 constexpr int buffsize = 66;
4256 char buff[buffsize];
4261 p = qulltoa2(buff + buffsize, qulonglong(-(1 + n)) + 1, base);
4264 p = qulltoa2(buff + buffsize, qulonglong(n), base);
4267 return assign(QByteArrayView{p, buff + buffsize});
4271
4272
4273
4274
4278 constexpr int buffsize = 66;
4279 char buff[buffsize];
4280 char *p = qulltoa2(buff + buffsize, n, base);
4282 return assign(QByteArrayView{p, buff + buffsize});
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4299 return *
this = QByteArray::number(n, format, precision);
4303
4304
4305
4306
4307
4308
4309
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4335
4336
4337
4338
4347
4348
4349
4350
4359
4360
4361
4362
4371
4372
4373
4374
4383
4384
4385
4386
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4410 switch (QtMiscUtils::toAsciiLower(format)) {
4421#if defined(QT_CHECK_RANGE)
4422 qWarning(
"QByteArray::setNum: Invalid format char '%c'", format);
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
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4485 *
this = fromRawData(data, size);
4490struct fromBase64_helper_result {
4491 qsizetype decodedLength;
4492 QByteArray::Base64DecodingStatus status;
4495fromBase64_helper_result fromBase64_helper(
const char *input, qsizetype inputSize,
4497 QByteArray::Base64Options options)
4499 fromBase64_helper_result result{ 0, QByteArray::Base64DecodingStatus::Ok };
4501 unsigned int buf = 0;
4504 qsizetype offset = 0;
4505 for (qsizetype i = 0; i < inputSize; ++i) {
4509 if (ch >=
'A' && ch <=
'Z') {
4511 }
else if (ch >=
'a' && ch <=
'z') {
4513 }
else if (ch >=
'0' && ch <=
'9') {
4515 }
else if (ch ==
'+' && (options & QByteArray::Base64UrlEncoding) == 0) {
4517 }
else if (ch ==
'-' && (options & QByteArray::Base64UrlEncoding) != 0) {
4519 }
else if (ch ==
'/' && (options & QByteArray::Base64UrlEncoding) == 0) {
4521 }
else if (ch ==
'_' && (options & QByteArray::Base64UrlEncoding) != 0) {
4524 if (options & QByteArray::AbortOnBase64DecodingErrors) {
4528 if ((inputSize % 4) != 0) {
4529 result.status = QByteArray::Base64DecodingStatus::IllegalInputLength;
4531 }
else if ((i == inputSize - 1) ||
4532 (i == inputSize - 2 && input[++i] ==
'=')) {
4535 result.status = QByteArray::Base64DecodingStatus::IllegalPadding;
4539 result.status = QByteArray::Base64DecodingStatus::IllegalCharacter;
4548 buf = (buf << 6) | d;
4552 Q_ASSERT(offset < i);
4553 output[offset++] = buf >> nbits;
4554 buf &= (1 << nbits) - 1;
4559 result.decodedLength = offset;
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
4595 if (base64.isDetached()) {
4596 const auto base64result = fromBase64_helper(base64.data(),
4600 base64.truncate(base64result.decodedLength);
4601 return {
std::move(base64), base64result.status };
4604 return fromBase64Encoding(base64, options);
4610 const auto base64Size = base64.size();
4611 QByteArray result((base64Size * 3) / 4, Qt::Uninitialized);
4612 const auto base64result = fromBase64_helper(base64.data(),
4614 const_cast<
char *>(result.constData()),
4616 result.truncate(base64result.decodedLength);
4617 return {
std::move(result), base64result.status };
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4646 if (
auto result = fromBase64Encoding(base64, options))
4647 return std::move(result.decoded);
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4664 QByteArray res((hexEncoded.size() + 1)/ 2, Qt::Uninitialized);
4665 uchar *result = (uchar *)res.data() + res.size();
4667 bool odd_digit =
true;
4668 for (qsizetype i = hexEncoded.size() - 1; i >= 0; --i) {
4669 uchar ch = uchar(hexEncoded.at(i));
4670 int tmp = QtMiscUtils::fromHex(ch);
4678 *result |= tmp << 4;
4683 res.remove(0, result - (
const uchar *)res.constData());
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4706 const qsizetype length = separator ? (size() * 3 - 1) : (size() * 2);
4708 char *hexData = hex.data();
4709 const uchar *data = (
const uchar *)
this->data();
4710 for (qsizetype i = 0, o = 0; i < size(); ++i) {
4711 hexData[o++] = QtMiscUtils::toHexLower(data[i] >> 4);
4712 hexData[o++] = QtMiscUtils::toHexLower(data[i] & 0xf);
4714 if ((separator) && (o < length))
4715 hexData[o++] = separator;
4722 char *data = buffer.begin();
4723 const char *inputPtr = src.begin();
4726 const qsizetype len = src.size();
4728 char c = inputPtr[i];
4729 if (c == percent && i + 2 < len) {
4730 if (
int a = QtMiscUtils::fromHex(uchar(inputPtr[++i])); a != -1)
4732 if (
int b = QtMiscUtils::fromHex(uchar(inputPtr[++i])); b != -1)
4741 return data - buffer.begin();
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4765
4766
4767
4768
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4786 if (input.isEmpty())
4789 QByteArray out{input.size(), Qt::Uninitialized};
4790 qsizetype len = q_fromPercentEncoding(input, percent, out);
4796
4797
4798
4801 if (input.d->needsDetach())
4802 return fromPercentEncoding(input, percent);
4804 if (input.isEmpty())
4805 return std::move(input);
4807 qsizetype len = q_fromPercentEncoding(input, percent, input);
4808 input.truncate(len);
4809 return std::move(input);
4813
4814
4815
4816
4817
4818
4821 return QByteArray(s.data(), qsizetype(s.size()));
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4838 return std::string(data(), size_t(size()));
4842
4843
4844
4845
4846
4847
4848
4849
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4883 const auto contains = [](
const QByteArray &view,
char c) {
4885 return view.size() > 0 && memchr(view.data(), c, view.size()) !=
nullptr;
4889 char *output =
nullptr;
4890 qsizetype length = 0;
4892 for (
unsigned char c : *
this) {
4893 if (
char(c) != percent
4894 && ((c >= 0x61 && c <= 0x7A)
4895 || (c >= 0x41 && c <= 0x5A)
4896 || (c >= 0x30 && c <= 0x39)
4901 || contains(exclude, c))
4902 && !contains(include, c)) {
4909 result.resize(size() * 3);
4910 output = result.data();
4912 output[length++] = percent;
4913 output[length++] = QtMiscUtils::toHexUpper((c & 0xf0) >> 4);
4914 output[length++] = QtMiscUtils::toHexUpper(c & 0xf);
4918 result.truncate(length);
4923#if defined(Q_OS_WASM) || defined(Q_QDOC)
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4948QByteArray QByteArray::fromEcmaUint8Array(emscripten::val uint8array)
4950 return qstdweb::Uint8Array(uint8array).copyToQByteArray();
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970emscripten::val QByteArray::toEcmaUint8Array()
4972 return qstdweb::Uint8Array::copyFrom(*
this).val();
4978
4979
4982
4983
4986
4987
4988
4989
4990
4993
4994
4995
4996
4997
5000
5001
5002
5003
5004
5005
5008
5009
5010
5011
5012
5013
5016
5017
5020
5021
5024
5025
5028
5029
5032
5033
5036
5037
5040
5041
5044
5045
5046
5049
5050
5051
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5073#if QT_DEPRECATED_SINCE(6
, 8
)
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5149
5150
5151
5152
5155
5156
5157
5158
5159
5162
5163
5164
5165
5166
5167
5170
5171
5172
5173
5174
5175
5176
5179
5180
5181
5182
5183
5184
5185
5186
5189
5190
5191
5192
5193
5196
5197
5198size_t
qHash(
const QByteArray::FromBase64Result &key, size_t seed)
noexcept
5200 return qHashMulti(seed, key.decoded,
static_cast<
int>(key.decodingStatus));
5204
5205
5206
5207
5208
5209
5210
5211
5214
5215
5216
5217
5218
5219
5220
5221
5222
QDataStream & operator>>(QDataStream &in, QByteArray &ba)
Reads a byte array into ba from the stream in and returns a reference to the stream.
quint16 qChecksum(QByteArrayView data, Qt::ChecksumType standard)
static constexpr bool isLowerCaseAscii(char c)
static const quint16 crc_tbl[16]
QByteArray qCompress(const uchar *data, qsizetype nbytes, int compressionLevel)
static Q_DECL_COLD_FUNCTION const char * zlibOpAsString(ZLibOp op)
static QByteArray toCase(const QByteArray &input, QByteArray *rvalue, uchar(*lookup)(uchar))
static qsizetype q_fromPercentEncoding(QByteArrayView src, char percent, QSpan< char > buffer)
int qstrnicmp(const char *s1, qsizetype len1, const char *s2, qsizetype len2)
static qsizetype lastIndexOfHelper(const char *haystack, qsizetype l, const char *needle, qsizetype ol, qsizetype from)
static constexpr bool isUpperCaseAscii(char c)
static QByteArray xxflate(ZLibOp op, QArrayDataPointer< char > out, QByteArrayView input, qxp::function_ref< int(z_stream *) const > init, qxp::function_ref< int(z_stream *, size_t) const > processChunk, qxp::function_ref< void(z_stream *) const > deinit)
static constexpr uchar asciiLower(uchar c)
static qsizetype countCharHelper(QByteArrayView haystack, char needle) noexcept
static constexpr uchar asciiUpper(uchar c)
Q_CORE_EXPORT char * qstrncpy(char *dst, const char *src, size_t len)
Q_CORE_EXPORT int qstrnicmp(const char *, const char *, size_t len)
Q_CORE_EXPORT int qstricmp(const char *, const char *)
Q_CORE_EXPORT char * qstrdup(const char *)
Q_CORE_EXPORT char * qstrcpy(char *dst, const char *src)
Q_DECL_PURE_FUNCTION Q_CORE_EXPORT const void * qmemrchr(const void *s, int needle, size_t n) noexcept
Q_CORE_EXPORT int qstrcmp(const char *str1, const char *str2)
constexpr size_t qHash(const QSize &s, size_t seed=0) noexcept
static float convertDoubleToFloat(double d, bool *ok)