8#include "private/qtools_p.h"
13#include "private/qnumeric_p.h"
14#include "private/qsimd_p.h"
18#include <qdatastream.h>
21#include "private/qstdweb_p.h"
27#include <qxpfunctional.h>
35#include <QtCore/q26numeric.h>
39# if !defined(QT_BOOTSTRAPPED) && (defined(QT_NO_CAST_FROM_ASCII) || defined(QT_NO_CAST_FROM_BYTEARRAY))
41# error "This file cannot be compiled with QT_NO_CAST_{TO,FROM}_ASCII, "
42 "otherwise some QByteArray functions will not get exported."
48Q_CONSTINIT
const char QByteArray::_empty =
'\0';
53 return c >=
'a' && c <=
'z' ? c & ~0x20 : c;
58 return c >=
'A' && c <=
'Z' ? c | 0x20 : c;
62
63
66
67
68
69
70
71
72
73
74const void *
qmemrchr(
const void *s,
int needle, size_t size)
noexcept
77 return memrchr(s, needle, size);
79 auto b =
static_cast<
const uchar *>(s);
80 const uchar *n = b + size;
82 if (*n == uchar(needle))
90
91
92
93
94
95
96
97
98
99
105 char *dst =
new char[strlen(src) + 1];
106 return qstrcpy(dst, src);
110
111
112
113
114
115
116
117
118
119
120
121
128 const size_t len = strlen(src);
131 if (len >= 0 && strcpy_s(dst, len+1, src) == 0)
135 return strcpy(dst, src);
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
160char *
qstrncpy(
char *dst,
const char *src, size_t len)
162 if (dst && len > 0) {
165 std::strncat(dst, src, len - 1);
167 return src ? dst :
nullptr;
171
172
173
174
175
176
177
178
179
182
183
184
185
186
187
188
189
190
191
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209int qstrcmp(
const char *str1,
const char *str2)
211 return (str1 && str2) ? strcmp(str1, str2)
212 : (str1 ? 1 : (str2 ? -1 : 0));
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
256 const uchar *s1 =
reinterpret_cast<
const uchar *>(str1);
257 const uchar *s2 =
reinterpret_cast<
const uchar *>(str2);
263 enum { Incomplete = 256 };
265 auto innerCompare = [=, &offset](qptrdiff max,
bool unlimited) {
268 uchar c = s1[offset];
269 if (
int res = QtMiscUtils::caseCompareAscii(c, s2[offset]))
274 }
while (unlimited || offset < max);
275 return int(Incomplete);
278#if defined(__SSE4_1__) && !(defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer))
279 enum { PageSize = 4096, PageMask = PageSize - 1 };
280 const __m128i zero = _mm_setzero_si128();
285 quintptr u1 = quintptr(s1 + offset);
286 quintptr u2 = quintptr(s2 + offset);
287 size_t n = PageSize - ((u1 | u2) & PageMask);
289 qptrdiff maxoffset = offset + n;
290 for ( ; offset + 16 <= maxoffset; offset +=
sizeof(__m128i)) {
292 __m128i a = _mm_loadu_si128(
reinterpret_cast<
const __m128i *>(s1 + offset));
293 __m128i b = _mm_loadu_si128(
reinterpret_cast<
const __m128i *>(s2 + offset));
296 __m128i cmp = _mm_cmpeq_epi8(a, b);
299 cmp = _mm_min_epu8(cmp, a);
300 cmp = _mm_cmpeq_epi8(cmp, zero);
303 uint mask = _mm_movemask_epi8(cmp);
306 uint start = qCountTrailingZeroBits(mask);
307 uint end =
sizeof(mask) * 8 - qCountLeadingZeroBits(mask);
308 Q_ASSERT(end >= start);
316 int res = innerCompare(n,
false);
317 if (res != Incomplete)
322 return innerCompare(-1,
true);
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
344int qstrnicmp(
const char *str1,
const char *str2, size_t len)
346 const uchar *s1 =
reinterpret_cast<
const uchar *>(str1);
347 const uchar *s2 =
reinterpret_cast<
const uchar *>(str2);
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 *str1, qsizetype len1,
const char *str2, qsizetype len2)
371 Q_ASSERT(len2 >= -1);
372 const uchar *s1 =
reinterpret_cast<
const uchar *>(str1);
373 const uchar *s2 =
reinterpret_cast<
const uchar *>(str2);
378 return (!s2 || !*s2) ? 0 : -1;
383 return len1 == 0 ? 0 : 1;
388 for (i = 0; i < len1; ++i) {
389 const uchar c = s2[i];
393 if (
int res = QtMiscUtils::caseCompareAscii(s1[i], c))
396 return s2[i] ? -1 : 0;
399 const qsizetype len = qMin(len1, len2);
400 for (qsizetype i = 0; i < len; ++i) {
401 if (
int res = QtMiscUtils::caseCompareAscii(s1[i], s2[i]))
406 return len1 < len2 ? -1 : 1;
411
412
413int QtPrivate::compareMemory(QByteArrayView lhs, QByteArrayView rhs)
415 if (!lhs.isNull() && !rhs.isNull()) {
416 int ret = memcmp(lhs.data(), rhs.data(), qMin(lhs.size(), rhs.size()));
423 return lhs.size() == rhs.size() ? 0 : lhs.size() > rhs.size() ? 1 : -1;
427
428
431 return QUtf8::isValidUtf8(s).isValidUtf8;
436static void createCRC16Table()
440 unsigned short crc_tbl[16];
441 unsigned int v0, v1, v2, v3;
442 for (i = 0; i < 16; i++) {
449#define SET_BIT(x, b, v) (x) |= (v) << (b)
464 printf(
"static const quint16 crc_tbl[16] = {\n");
465 for (
int i = 0; i < 16; i +=4)
466 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]);
472 0x0000, 0x1081, 0x2102, 0x3183,
473 0x4204, 0x5285, 0x6306, 0x7387,
474 0x8408, 0x9489, 0xa50a, 0xb58b,
475 0xc60c, 0xd68d, 0xe70e, 0xf78f
479
480
481
482
483
484
485
486
487
488
489
490
493 quint16 crc = 0x0000;
495 case Qt::ChecksumIso3309:
498 case Qt::ChecksumItuV41:
503 const uchar *p =
reinterpret_cast<
const uchar *>(data.data());
504 qsizetype len = data.size();
507 crc = ((crc >> 4) & 0x0fff) ^ crc_tbl[((crc ^ c) & 15)];
509 crc = ((crc >> 4) & 0x0fff) ^ crc_tbl[((crc ^ c) & 15)];
512 case Qt::ChecksumIso3309:
515 case Qt::ChecksumItuV41:
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
542
543
544
545
546
547
548
549
551#ifndef QT_NO_COMPRESS
552using CompressSizeHint_t = quint32;
563 Q_UNREACHABLE_RETURN(
nullptr);
567static QByteArray zlibError(ZLibOp op,
const char *what)
569 qWarning(
"%s: %s", zlibOpAsString(op), what);
574static QByteArray dataIsNull(ZLibOp op)
576 return zlibError(op,
"Data is null");
580static QByteArray lengthIsNegative(ZLibOp op)
582 return zlibError(op,
"Input length is negative");
586static QByteArray tooMuchData(ZLibOp op)
588 return zlibError(op,
"Not enough memory");
592static QByteArray invalidCompressedData()
594 return zlibError(ZLibOp::Decompression,
"Input data is corrupted");
598static QByteArray unexpectedZlibError(ZLibOp op,
int err,
const char *msg)
600 qWarning(
"%s unexpected zlib error: %s (%d)",
608 qxp::function_ref<
int(z_stream *)
const> init,
609 qxp::function_ref<
int(z_stream *, size_t)
const> processChunk,
610 qxp::function_ref<
void(z_stream *)
const> deinit)
612 if (out.data() ==
nullptr)
613 return tooMuchData(op);
614 qsizetype capacity = out.allocatedCapacity();
616 const auto initalSize = out.size;
619 zs.next_in =
reinterpret_cast<uchar *>(
const_cast<
char *>(input.data()));
620 if (
const int err = init(&zs); err != Z_OK)
621 return unexpectedZlibError(op, err, zs.msg);
622 const auto sg = qScopeGuard([&] { deinit(&zs); });
624 using ZlibChunkSize_t =
decltype(zs.avail_in);
625 static_assert(!std::is_signed_v<ZlibChunkSize_t>);
626 static_assert(std::is_same_v<ZlibChunkSize_t,
decltype(zs.avail_out)>);
627 constexpr auto MaxChunkSize =
std::numeric_limits<ZlibChunkSize_t>::max();
629 constexpr auto MaxStatisticsSize =
std::numeric_limits<
decltype(zs.total_out)>::max();
631 size_t inputLeft = size_t(input.size());
635 Q_ASSERT(out.freeSpaceAtBegin() == 0);
636 Q_ASSERT(capacity == out.allocatedCapacity());
638 if (zs.avail_out == 0) {
639 Q_ASSERT(size_t(out.size) - initalSize > MaxStatisticsSize ||
640 size_t(out.size) - initalSize == zs.total_out);
641 Q_ASSERT(out.size <= capacity);
643 qsizetype avail_out = capacity - out.size;
644 if (avail_out == 0) {
645 out->reallocateAndGrow(QArrayData::GrowsAtEnd, 1);
646 if (out.data() ==
nullptr)
647 return tooMuchData(op);
648 capacity = out.allocatedCapacity();
649 avail_out = capacity - out.size;
651 zs.next_out =
reinterpret_cast<uchar *>(out.data()) + out.size;
652 zs.avail_out = size_t(avail_out) > size_t(MaxChunkSize) ? MaxChunkSize
653 : ZlibChunkSize_t(avail_out);
654 out.size += zs.avail_out;
656 Q_ASSERT(zs.avail_out > 0);
659 if (zs.avail_in == 0) {
661 zs.avail_in = inputLeft > MaxChunkSize ? MaxChunkSize : ZlibChunkSize_t(inputLeft);
662 inputLeft -= zs.avail_in;
665 res = processChunk(&zs, inputLeft);
666 }
while (res == Z_OK);
670 out.size -= zs.avail_out;
671 Q_ASSERT(size_t(out.size) - initalSize > MaxStatisticsSize ||
672 size_t(out.size) - initalSize == zs.total_out);
673 Q_ASSERT(out.size <= out.allocatedCapacity());
674 out.data()[out.size] =
'\0';
675 return QByteArray(
std::move(out));
678 return tooMuchData(op);
686 return invalidCompressedData();
689 return unexpectedZlibError(op, res, zs.msg);
695 constexpr qsizetype HeaderSize =
sizeof(CompressSizeHint_t);
697 return QByteArray(HeaderSize,
'\0');
705 if (compressionLevel < -1 || compressionLevel > 9)
706 compressionLevel = -1;
708 QArrayDataPointer out = [&] {
709 constexpr qsizetype SingleAllocLimit = 256 * 1024;
713 qsizetype capacity = HeaderSize;
714 if (nbytes < SingleAllocLimit) {
716 capacity += compressBound(uLong(nbytes));
717 return QArrayDataPointer<
char>(capacity);
722 constexpr qsizetype MaxCompressionFactor = 1024;
725 capacity +=
std::max(qsizetype(compressBound(uLong(SingleAllocLimit))),
726 nbytes / MaxCompressionFactor);
727 return QArrayDataPointer<
char>(capacity, 0, QArrayData::Grow);
730 if (out.data() ==
nullptr)
733 qToBigEndian(q26::saturate_cast<CompressSizeHint_t>(nbytes), out.data());
734 out.size = HeaderSize;
736 return xxflate(ZLibOp::Compression, std::move(out), {data, nbytes},
737 [=] (z_stream *zs) {
return deflateInit(zs, compressionLevel); },
738 [] (z_stream *zs, size_t inputLeft) {
739 return deflate(zs, inputLeft ? Z_NO_FLUSH : Z_FINISH);
741 [] (z_stream *zs) { deflateEnd(zs); });
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
777#ifndef QT_NO_COMPRESS
779
780
781
782
783
784
793 constexpr qsizetype HeaderSize =
sizeof(CompressSizeHint_t);
794 if (nbytes < HeaderSize)
795 return invalidCompressedData();
797 const auto expectedSize = qFromBigEndian<CompressSizeHint_t>(data);
798 if (nbytes == HeaderSize) {
799 if (expectedSize != 0)
800 return invalidCompressedData();
804 constexpr auto MaxDecompressedSize = size_t(QByteArray::maxSize());
805 if constexpr (MaxDecompressedSize < std::numeric_limits<CompressSizeHint_t>::max()) {
806 if (expectedSize > MaxDecompressedSize)
812 qsizetype capacity =
std::max(qsizetype(expectedSize),
815 QArrayDataPointer<
char> d(capacity);
816 return xxflate(ZLibOp::Decompression, std::move(d), {data + HeaderSize, nbytes - HeaderSize},
817 [] (z_stream *zs) {
return inflateInit(zs); },
818 [] (z_stream *zs, size_t) {
return inflate(zs, Z_NO_FLUSH); },
819 [] (z_stream *zs) { inflateEnd(zs); });
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
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
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1120
1121
1122
1125
1126
1127
1128
1129
1130
1131
1132
1133
1136
1137
1138
1139
1140
1141
1142
1143
1146
1147
1148
1149
1150
1151
1152
1153
1156
1157
1158
1161
1162
1163
1164
1165
1166
1167
1168
1169
1172
1173
1174
1175
1176
1177
1178
1179
1182
1183
1184
1185
1186
1187
1188
1189
1190
1193
1194
1195
1198
1199
1200
1201
1202
1203
1204
1205
1206
1209
1210
1211
1212
1213
1214
1215
1216
1217
1220
1221
1222
1225
1226
1227
1228
1229
1230
1231
1232
1233
1236
1237
1238
1239
1242
1243
1244
1245
1246
1249
1250
1251
1252
1253
1256
1257
1258
1259
1260
1263
1264
1265
1266
1269
1270
1271
1272
1273
1276
1277
1278
1279
1280
1283
1284
1285
1286
1287
1290
1291
1292
1293
1294
1297
1298
1299
1300
1301
1302
1303QByteArray::iterator
QByteArray::erase(QByteArray::const_iterator first, QByteArray::const_iterator last)
1305 const auto start =
std::distance(cbegin(), first);
1306 const auto len =
std::distance(first, last);
1308 return begin() + start;
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1340
1341
1342
1343
1344
1345
1346
1349
1350
1351
1352
1353
1356
1357
1360
1361
1362
1371
1372
1373
1374
1375
1376
1377
1384 d = DataPointer::fromRawData(&_empty, 0);
1392
1393
1394
1395
1396
1397
1400
1401
1402
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1423
1424
1425
1426
1427
1428
1429
1432
1433
1434
1435
1436
1437
1438
1439
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
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
1489
1490
1491
1492
1493
1494
1495
1496
1497
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
1527
1528
1529
1530
1531
1532
1533
1534
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
1572
1573
1574
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1595
1596
1597
1600
1601
1602
1605
1606
1607
1610
1611
1612
1613
1614
1615
1616
1617
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1633
1634
1635
1636
1637
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1700
1701
1702
1703
1704
1705
1706
1709
1710
1711
1712
1713
1714
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1778
1779
1780
1781
1782
1783
1786
1787
1788
1789
1790
1791
1794
1795
1796
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1814
1815
1816
1817
1818
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1840 size = qstrlen(data);
1842 d = DataPointer::fromRawData(&_empty, 0);
1844 d = DataPointer(size, size);
1845 Q_CHECK_PTR(d.data());
1846 memcpy(d.data(), data, size);
1847 d.data()[size] =
'\0';
1853
1854
1855
1856
1861 d = DataPointer::fromRawData(&_empty, 0);
1863 d = DataPointer(size, size);
1864 Q_CHECK_PTR(d.data());
1865 memset(d.data(), ch, size);
1866 d.data()[size] =
'\0';
1871
1872
1877 d = DataPointer::fromRawData(&_empty, 0);
1879 d = DataPointer(size, size);
1880 Q_CHECK_PTR(d.data());
1881 d.data()[size] =
'\0';
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1905 const auto capacityAtEnd = capacity() - d.freeSpaceAtBegin();
1906 if (d->needsDetach() || size > capacityAtEnd)
1907 reallocData(size, QArrayData::Grow);
1909 if (d->allocatedCapacity())
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1932 const auto old = d.size;
1935 memset(d.data() + old, c, d.size - old);
1939
1940
1941
1942
1943
1944
1945
1946
1947
1954
1955
1956
1957
1958
1959
1960
1961
1965 resize(size < 0 ?
this->size() : size);
1967 memset(d.data(), ch,
this->size());
1971void QByteArray::reallocData(qsizetype alloc, QArrayData::AllocationOption option)
1974 d = DataPointer::fromRawData(&_empty, 0);
1980 const bool cannotUseReallocate = d.freeSpaceAtBegin() > 0;
1982 if (d->needsDetach() || cannotUseReallocate) {
1983 DataPointer dd(alloc, qMin(alloc, d.size), option);
1984 Q_CHECK_PTR(dd.data());
1986 ::memcpy(dd.data(), d.data(), dd.size);
1987 dd.data()[dd.size] = 0;
1990 d->reallocate(alloc, option);
1999 if (d->needsDetach()) {
2000 DataPointer dd(DataPointer::allocateGrow(d, n, QArrayData::GrowsAtEnd));
2001 Q_CHECK_PTR(dd.data());
2002 dd->copyAppend(d.data(), d.data() + d.size);
2003 dd.data()[dd.size] = 0;
2006 d->reallocate(d.constAllocatedCapacity() + n, QArrayData::Grow);
2012 resize(qMax(i + 1, size()));
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2031
2032
2033
2034
2035
2036
2037
2038
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2059
2060
2061
2062
2063
2066 if (size() == 0 && ba.size() > d.constAllocatedCapacity() && ba.d.isMutable())
2067 return (*
this = ba);
2068 return prepend(QByteArrayView(ba));
2072
2073
2074
2075
2076
2079
2080
2081
2082
2083
2084
2085
2088
2089
2090
2091
2092
2093
2096
2097
2098
2099
2100
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2130 if (Q_UNLIKELY(!ba.d.isMutable()))
2134 }
else if (ba.size()) {
2135 append(QByteArrayView(ba));
2142
2143
2144
2145
2146
2149
2150
2151
2152
2153
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2172
2173
2174
2175
2176
2177
2178
2179
2180
2183
2184
2185
2186
2190 d.detachAndGrow(QArrayData::GrowsAtEnd, 1,
nullptr,
nullptr);
2191 d->copyAppend(1, ch);
2192 d.data()[d.size] =
'\0';
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2247 const auto len = v.size();
2249 if (len <= capacity() && isDetached()) {
2250 const auto offset = d.freeSpaceAtBegin();
2252 d.setBegin(d.begin() - offset);
2253 std::memcpy(d.begin(), v.data(), len);
2255 d.data()[d.size] =
'\0';
2257 *
this = v.toByteArray();
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2284 const char *str = data.data();
2285 qsizetype size = data.size();
2286 if (i < 0 || size <= 0)
2295 DataPointer detached{};
2296 d.detachAndGrow(Data::GrowsAtEnd, (i - d.size) + size, &str, &detached);
2297 Q_CHECK_PTR(d.data());
2298 d->copyAppend(i - d->size,
' ');
2299 d->copyAppend(str, str + size);
2300 d.data()[d.size] =
'\0';
2304 if (!d->needsDetach() && QtPrivate::q_points_into_range(str, d)) {
2305 QVarLengthArray a(str, str + size);
2306 return insert(i, a);
2309 d->insert(i, str, size);
2310 d.data()[d.size] =
'\0';
2315
2316
2317
2318
2319
2320
2321
2322
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2337
2338
2339
2340
2341
2342
2343
2344
2345
2348
2349
2350
2351
2352
2353
2354
2357
2358
2359
2360
2361
2362
2363
2364
2365
2369 if (i < 0 || count <= 0)
2374 d.detachAndGrow(Data::GrowsAtEnd, (i - d.size) + count,
nullptr,
nullptr);
2375 Q_CHECK_PTR(d.data());
2376 d->copyAppend(i - d->size,
' ');
2377 d->copyAppend(count, ch);
2378 d.data()[d.size] =
'\0';
2382 d->insert(i, count, ch);
2383 d.data()[d.size] =
'\0';
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2407 if (len <= 0 || pos < 0 || size_t(pos) >= size_t(size()))
2409 if (pos + len > d->size)
2410 len = d->size - pos;
2412 const auto toRemove_start = d.begin() + pos;
2413 if (!d->isShared()) {
2414 d->erase(toRemove_start, len);
2415 d.data()[d.size] =
'\0';
2417 QByteArray copy{size() - len, Qt::Uninitialized};
2418 copy.d->copyRanges({{d.begin(), toRemove_start},
2419 {toRemove_start + len, d.end()}});
2426
2427
2428
2429
2430
2431
2432
2433
2434
2437
2438
2439
2440
2441
2442
2443
2444
2445
2447
2448
2449
2450
2451
2452
2453
2454
2455
2458
2459
2460
2461
2462
2463
2464
2465
2468
2469
2470
2471
2472
2473
2474
2475
2479 if (QtPrivate::q_points_into_range(after.data(), d)) {
2480 QVarLengthArray copy(after.data(), after.data() + after.size());
2481 return replace(pos, len, QByteArrayView{copy});
2483 if (len == after.size() && (pos + len <= size())) {
2487 memcpy(d.data() + pos, after.data(), len*
sizeof(
char));
2493 return insert(pos, after);
2498
2499
2500
2501
2502
2503
2504
2505
2508
2509
2510
2511
2512
2513
2514
2515
2518
2519
2520
2521
2522
2523
2524
2525
2526
2530 const char *b = before.data();
2531 qsizetype bsize = before.size();
2532 const char *a = after.data();
2533 qsizetype asize = after.size();
2535 if (bsize == 1 && asize == 1)
2536 return replace(*b, *a);
2538 if (isNull() || (b == a && bsize == asize))
2542 std::string pinnedNeedle, pinnedReplacement;
2543 if (QtPrivate::q_points_into_range(a, d)) {
2544 pinnedReplacement.assign(a, a + asize);
2545 a = pinnedReplacement.data();
2547 if (QtPrivate::q_points_into_range(b, d)) {
2548 pinnedNeedle.assign(b, b + bsize);
2549 b = pinnedNeedle.data();
2553 qsizetype index = 0;
2554 qsizetype len = size();
2557 if (bsize == asize) {
2559 while ((index = matcher.indexIn(*
this, index)) != -1) {
2560 memcpy(d + index, a, asize);
2564 }
else if (asize < bsize) {
2566 size_t movestart = 0;
2568 while ((index = matcher.indexIn(*
this, index)) != -1) {
2570 qsizetype msize = index - movestart;
2572 memmove(d + to, d + movestart, msize);
2579 memcpy(d + to, a, asize);
2587 qsizetype msize = len - movestart;
2589 memmove(d + to, d + movestart, msize);
2590 resize(len - num*(bsize-asize));
2595 while (index != -1) {
2596 size_t indices[4096];
2599 index = matcher.indexIn(*
this, index);
2602 indices[pos++] = index;
2612 qsizetype adjust = pos*(asize-bsize);
2616 qsizetype newlen = len + adjust;
2617 qsizetype moveend = len;
2626 qsizetype movestart = indices[pos] + bsize;
2627 qsizetype insertstart = indices[pos] + pos*(asize-bsize);
2628 qsizetype moveto = insertstart + asize;
2629 memmove(d + moveto, d + movestart, (moveend - movestart));
2631 memcpy(d + insertstart, a, asize);
2632 moveend = movestart - bsize;
2640
2641
2642
2643
2644
2645
2648
2649
2650
2651
2655 if (before != after) {
2656 if (
const auto pos = indexOf(before); pos >= 0) {
2657 if (d.needsDetach()) {
2659 auto dst = tmp.d.data();
2660 dst = std::copy(d.data(), d.data() + pos, dst);
2662 std::replace_copy(d.data() + pos + 1, d.end(), dst, before, after);
2666 d.data()[pos] = after;
2667 std::replace(d.data() + pos + 1, d.end(), before, after);
2675
2676
2677
2678
2679
2684 qsizetype start = 0;
2686 while ((end = indexOf(sep, start)) != -1) {
2687 list.append(mid(start, end - start));
2690 list.append(mid(start));
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2716 const qsizetype resultSize = times * size();
2719 result.reserve(resultSize);
2720 if (result.capacity() != resultSize)
2723 memcpy(result.d.data(), data(), size());
2725 qsizetype sizeSoFar = size();
2726 char *end = result.d.data() + sizeSoFar;
2728 const qsizetype halfResultSize = resultSize >> 1;
2729 while (sizeSoFar <= halfResultSize) {
2730 memcpy(end, result.d.data(), sizeSoFar);
2734 memcpy(end, result.d.data(), resultSize - sizeSoFar);
2735 result.d.data()[resultSize] =
'\0';
2736 result.d.size = resultSize;
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2768 qsizetype ol, qsizetype from)
2770 auto delta = l - ol;
2773 if (from < 0 || from > delta)
2778 const char *end = haystack;
2780 const qregisteruint ol_minus_1 = ol - 1;
2781 const char *n = needle + ol_minus_1;
2782 const char *h = haystack + ol_minus_1;
2783 qregisteruint hashNeedle = 0, hashHaystack = 0;
2785 for (idx = 0; idx < ol; ++idx) {
2786 hashNeedle = ((hashNeedle<<1) + *(n-idx));
2787 hashHaystack = ((hashHaystack<<1) + *(h-idx));
2789 hashHaystack -= *haystack;
2790 while (haystack >= end) {
2791 hashHaystack += *haystack;
2792 if (hashHaystack == hashNeedle && memcmp(needle, haystack, ol) == 0)
2793 return haystack - end;
2795 if (ol_minus_1 <
sizeof(ol_minus_1) * CHAR_BIT)
2796 hashHaystack -= qregisteruint(*(haystack + ol)) << ol_minus_1;
2802qsizetype
QtPrivate::lastIndexOf(QByteArrayView haystack, qsizetype from, QByteArrayView needle)
noexcept
2804 if (haystack.isEmpty()) {
2805 if (needle.isEmpty() && from == 0)
2809 const auto ol = needle.size();
2811 return QtPrivate::lastIndexOf(haystack, from, needle.front());
2813 return lastIndexOfHelper(haystack.data(), haystack.size(), needle.data(), ol, from);
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2872 for (
char ch : haystack) {
2879qsizetype
QtPrivate::count(QByteArrayView haystack, QByteArrayView needle)
noexcept
2881 if (needle.size() == 0)
2882 return haystack.size() + 1;
2884 if (needle.size() == 1)
2885 return countCharHelper(haystack, needle[0]);
2889 if (haystack.size() > 500 && needle.size() > 5) {
2891 while ((i = matcher.indexIn(haystack, i + 1)) != -1)
2894 while ((i = haystack.indexOf(needle, i + 1)) != -1)
2901
2902
2903
2904
2905
2906
2907
2910
2911
2912
2913
2914
2915
2919 return countCharHelper(*
this, ch);
2922#if QT_DEPRECATED_SINCE(6
, 4
)
2924
2925
2926
2927
2928
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2943bool QtPrivate::startsWith(QByteArrayView haystack, QByteArrayView needle)
noexcept
2945 if (haystack.size() < needle.size())
2947 if (haystack.data() == needle.data() || needle.size() == 0)
2949 return memcmp(haystack.data(), needle.data(), needle.size()) == 0;
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2965
2966
2967
2968
2969
2970
2972bool QtPrivate::endsWith(QByteArrayView haystack, QByteArrayView needle)
noexcept
2974 if (haystack.size() < needle.size())
2976 if (haystack.end() == needle.end() || needle.size() == 0)
2978 return memcmp(haystack.end() - needle.size(), needle.data(), needle.size()) == 0;
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2995
2996
2997
2998
2999
3000
3003
3004
3007 return c >=
'A' && c <=
'Z';
3011
3012
3015 return c >=
'a' && c <=
'z';
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3051
3052
3053
3054
3055
3056
3057
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3116 switch (QContainerImplHelper::mid(size(), &p, &l)) {
3117 case QContainerImplHelper::Null:
3119 case QContainerImplHelper::Empty:
3121 return QByteArray(DataPointer::fromRawData(&_empty, 0));
3123 case QContainerImplHelper::Full:
3125 case QContainerImplHelper::Subset:
3126 return sliced(p, l);
3136 switch (QContainerImplHelper::mid(size(), &p, &l)) {
3137 case QContainerImplHelper::Null:
3139 case QContainerImplHelper::Empty:
3142 case QContainerImplHelper::Full:
3143 return std::move(*
this);
3144 case QContainerImplHelper::Subset:
3145 return std::move(*
this).sliced(p, l);
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3199 return fromRawData(&_empty, 0);
3200 DataPointer d =
std::move(a.d).sliced(pos, n);
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3273template <
typename T>
3277 const char *orig_begin = input.constBegin();
3278 const char *firstBad = orig_begin;
3279 const char *e = input.constEnd();
3280 for ( ; firstBad != e ; ++firstBad) {
3281 uchar ch = uchar(*firstBad);
3282 uchar converted = lookup(ch);
3283 if (ch != converted)
3288 return std::move(input);
3292 char *b = s.begin();
3293 char *p = b + (firstBad - orig_begin);
3295 for ( ; p != e; ++p)
3296 *p =
char(lookup(uchar(*p)));
3302 return toCase_template(a, asciiLower);
3307 return toCase_template(a, asciiLower);
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3324 return toCase_template(a, asciiUpper);
3329 return toCase_template(a, asciiUpper);
3333
3334
3335
3336
3337
3344#if !defined(QT_NO_DATASTREAM)
3347
3348
3349
3350
3351
3352
3356 if (ba.isNull() && out.version() >= 6) {
3357 QDataStream::writeQSizeType(out, -1);
3360 return out.writeBytes(ba.constData(), ba.size());
3364
3365
3366
3367
3368
3369
3375 qint64 size = QDataStream::readQSizeType(in);
3376 qsizetype len = size;
3377 if (size != len || size < -1) {
3379 in.setStatus(QDataStream::SizeLimitExceeded);
3387 constexpr qsizetype Step = 1024 * 1024;
3388 qsizetype allocated = 0;
3391 qsizetype blockSize = qMin(Step, len - allocated);
3392 ba.resize(allocated + blockSize);
3393 if (in.readRawData(ba.data() + allocated, blockSize) != blockSize) {
3395 in.setStatus(QDataStream::ReadPastEnd);
3398 allocated += blockSize;
3399 }
while (allocated < len);
3406
3407
3408
3409
3410
3411
3412
3415
3416
3417
3418
3419
3420
3421
3424
3425
3426
3427
3428
3429
3430
3433
3434
3435
3436
3437
3438
3439
3442
3443
3444
3445
3446
3447
3448
3451
3452
3453
3454
3455
3456
3457
3460
3461
3462
3463
3464
3465
3466
3469
3470
3471
3472
3473
3474
3475
3478
3479
3480
3481
3482
3483
3484
3487
3488
3489
3490
3491
3492
3493
3496
3497
3498
3499
3500
3501
3502
3505
3506
3507
3508
3509
3510
3511
3514
3515
3516
3517
3518
3519
3520
3523
3524
3525
3526
3527
3528
3529
3532
3533
3534
3535
3536
3537
3538
3541
3542
3543
3544
3545
3546
3547
3550
3551
3552
3553
3554
3555
3556
3559
3560
3561
3562
3563
3564
3565
3568
3569
3570
3571
3572
3573
3574
3577
3578
3579
3580
3581
3582
3583
3586
3587
3588
3589
3590
3591
3592
3595
3596
3597
3598
3599
3600
3601
3604
3605
3606
3607
3608
3609
3610
3613
3614
3615
3616
3617
3618
3619
3620
3621
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3642 return QStringAlgorithms<
const QByteArray>::simplified_helper(a);
3647 return QStringAlgorithms<QByteArray>::simplified_helper(a);
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3671 return QStringAlgorithms<
const QByteArray>::trimmed_helper(a);
3676 return QStringAlgorithms<QByteArray>::trimmed_helper(a);
3679QByteArrayView
QtPrivate::trimmed(QByteArrayView view)
noexcept
3681 const auto [start, stop] = QStringAlgorithms<QByteArrayView>::trimmed_helper_positions(view);
3682 return QByteArrayView(start, stop);
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3706 qsizetype len = size();
3707 qsizetype padlen = width - len;
3709 result.resize(len+padlen);
3711 memcpy(result.d.data(), data(), len);
3712 memset(result.d.data()+len, fill, padlen);
3715 result = left(width);
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3743 qsizetype len = size();
3744 qsizetype padlen = width - len;
3746 result.resize(len+padlen);
3748 memcpy(result.d.data()+padlen, data(), len);
3749 memset(result.d.data(), fill, padlen);
3752 result = left(width);
3759auto QtPrivate::toSignedInteger(QByteArrayView data,
int base) -> ParsedNumber<qlonglong>
3761#if defined(QT_CHECK_RANGE)
3762 if (base != 0 && (base < 2 || base > 36)) {
3763 qWarning(
"QByteArray::toIntegral: Invalid base %d", base);
3770 const QSimpleParsedNumber r = QLocaleData::bytearrayToLongLong(data, base);
3772 return ParsedNumber(r.result);
3776auto QtPrivate::toUnsignedInteger(QByteArrayView data,
int base) -> ParsedNumber<qulonglong>
3778#if defined(QT_CHECK_RANGE)
3779 if (base != 0 && (base < 2 || base > 36)) {
3780 qWarning(
"QByteArray::toIntegral: Invalid base %d", base);
3787 const QSimpleParsedNumber r = QLocaleData::bytearrayToUnsLongLong(data, base);
3789 return ParsedNumber(r.result);
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3820 return QtPrivate::toIntegral<qlonglong>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3850 return QtPrivate::toIntegral<qulonglong>(qToByteArrayViewIgnoringNull(*
this), ok, base);
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
3882 return QtPrivate::toIntegral<
int>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3912 return QtPrivate::toIntegral<uint>(qToByteArrayViewIgnoringNull(*
this), ok, base);
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
3945 return QtPrivate::toIntegral<
long>(qToByteArrayViewIgnoringNull(*
this), ok, base);
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
3976 return QtPrivate::toIntegral<ulong>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4006 return QtPrivate::toIntegral<
short>(qToByteArrayViewIgnoringNull(*
this), ok, base);
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4036 return QtPrivate::toIntegral<ushort>(qToByteArrayViewIgnoringNull(*
this), ok, base);
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4066 return QByteArrayView(*
this).toDouble(ok);
4069auto QtPrivate::toDouble(QByteArrayView a)
noexcept -> ParsedNumber<
double>
4071 auto r = qt_asciiToDouble(a.data(), a.size(), WhitespacesAllowed);
4073 return ParsedNumber{r.result};
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4105 return QLocaleData::convertDoubleToFloat(toDouble(ok), ok);
4108auto QtPrivate::toFloat(QByteArrayView a)
noexcept -> ParsedNumber<
float>
4110 if (
const auto r = toDouble(a)) {
4114 return ParsedNumber(f);
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4132 constexpr char alphabet_base64[] =
"ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef"
4133 "ghijklmn" "opqrstuv" "wxyz0123" "456789+/";
4134 constexpr char alphabet_base64url[] =
"ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef"
4135 "ghijklmn" "opqrstuv" "wxyz0123" "456789-_";
4136 const char *
const alphabet = options & Base64UrlEncoding ? alphabet_base64url : alphabet_base64;
4137 constexpr char padchar =
'=';
4138 qsizetype padlen = 0;
4140 const qsizetype sz = size();
4142 QByteArray tmp((sz + 2) / 3 * 4, Qt::Uninitialized);
4145 char *out = tmp.data();
4149 chunk |=
int(uchar(data()[i++])) << 16;
4153 chunk |=
int(uchar(data()[i++])) << 8;
4157 chunk |=
int(uchar(data()[i++]));
4160 int j = (chunk & 0x00fc0000) >> 18;
4161 int k = (chunk & 0x0003f000) >> 12;
4162 int l = (chunk & 0x00000fc0) >> 6;
4163 int m = (chunk & 0x0000003f);
4164 *out++ = alphabet[j];
4165 *out++ = alphabet[k];
4168 if ((options & OmitTrailingEquals) == 0)
4171 *out++ = alphabet[l];
4174 if ((options & OmitTrailingEquals) == 0)
4177 *out++ = alphabet[m];
4180 Q_ASSERT((options & OmitTrailingEquals) || (out == tmp.size() + tmp.data()));
4181 if (options & OmitTrailingEquals)
4182 tmp.truncate(out - tmp.data());
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
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
4248 constexpr int buffsize = 66;
4249 char buff[buffsize];
4254 p = qulltoa2(buff + buffsize, qulonglong(-(1 + n)) + 1, base);
4257 p = qulltoa2(buff + buffsize, qulonglong(n), base);
4260 return assign(QByteArrayView{p, buff + buffsize});
4264
4265
4266
4267
4271 constexpr int buffsize = 66;
4272 char buff[buffsize];
4273 char *p = qulltoa2(buff + buffsize, n, base);
4275 return assign(QByteArrayView{p, buff + buffsize});
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4292 return *
this = QByteArray::number(n, format, precision);
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4332
4333
4334
4335
4344
4345
4346
4347
4356
4357
4358
4359
4368
4369
4370
4371
4380
4381
4382
4383
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4407 switch (QtMiscUtils::toAsciiLower(format)) {
4418#if defined(QT_CHECK_RANGE)
4419 qWarning(
"QByteArray::setNum: Invalid format char '%c'", format);
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
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4482 *
this = fromRawData(data, size);
4487struct fromBase64_helper_result {
4488 qsizetype decodedLength;
4489 QByteArray::Base64DecodingStatus status;
4492fromBase64_helper_result fromBase64_helper(
const char *input, qsizetype inputSize,
4494 QByteArray::Base64Options options)
4496 fromBase64_helper_result result{ 0, QByteArray::Base64DecodingStatus::Ok };
4498 unsigned int buf = 0;
4501 qsizetype offset = 0;
4502 for (qsizetype i = 0; i < inputSize; ++i) {
4506 if (ch >=
'A' && ch <=
'Z') {
4508 }
else if (ch >=
'a' && ch <=
'z') {
4510 }
else if (ch >=
'0' && ch <=
'9') {
4512 }
else if (ch ==
'+' && (options & QByteArray::Base64UrlEncoding) == 0) {
4514 }
else if (ch ==
'-' && (options & QByteArray::Base64UrlEncoding) != 0) {
4516 }
else if (ch ==
'/' && (options & QByteArray::Base64UrlEncoding) == 0) {
4518 }
else if (ch ==
'_' && (options & QByteArray::Base64UrlEncoding) != 0) {
4521 if (options & QByteArray::AbortOnBase64DecodingErrors) {
4525 if ((inputSize % 4) != 0) {
4526 result.status = QByteArray::Base64DecodingStatus::IllegalInputLength;
4528 }
else if ((i == inputSize - 1) ||
4529 (i == inputSize - 2 && input[++i] ==
'=')) {
4532 result.status = QByteArray::Base64DecodingStatus::IllegalPadding;
4536 result.status = QByteArray::Base64DecodingStatus::IllegalCharacter;
4545 buf = (buf << 6) | d;
4549 Q_ASSERT(offset < i);
4550 output[offset++] = buf >> nbits;
4551 buf &= (1 << nbits) - 1;
4556 result.decodedLength = offset;
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
4592 if (base64.isDetached()) {
4593 const auto base64result = fromBase64_helper(base64.data(),
4597 base64.truncate(base64result.decodedLength);
4598 return {
std::move(base64), base64result.status };
4601 return fromBase64Encoding(base64, options);
4607 const auto base64Size = base64.size();
4608 QByteArray result((base64Size * 3) / 4, Qt::Uninitialized);
4609 const auto base64result = fromBase64_helper(base64.data(),
4611 const_cast<
char *>(result.constData()),
4613 result.truncate(base64result.decodedLength);
4614 return {
std::move(result), base64result.status };
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4643 if (
auto result = fromBase64Encoding(base64, options))
4644 return std::move(result.decoded);
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4661 QByteArray res((hexEncoded.size() + 1)/ 2, Qt::Uninitialized);
4662 uchar *result = (uchar *)res.data() + res.size();
4664 bool odd_digit =
true;
4665 for (qsizetype i = hexEncoded.size() - 1; i >= 0; --i) {
4666 uchar ch = uchar(hexEncoded.at(i));
4667 int tmp = QtMiscUtils::fromHex(ch);
4675 *result |= tmp << 4;
4680 res.remove(0, result - (
const uchar *)res.constData());
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4703 const qsizetype length = separator ? (size() * 3 - 1) : (size() * 2);
4705 char *hexData = hex.data();
4706 const uchar *data = (
const uchar *)
this->data();
4707 for (qsizetype i = 0, o = 0; i < size(); ++i) {
4708 hexData[o++] = QtMiscUtils::toHexLower(data[i] >> 4);
4709 hexData[o++] = QtMiscUtils::toHexLower(data[i] & 0xf);
4711 if ((separator) && (o < length))
4712 hexData[o++] = separator;
4722 char *data = ba->data();
4723 const char *inputPtr = data;
4726 qsizetype len = ba->size();
4727 qsizetype outlen = 0;
4732 if (c == percent && i + 2 < len) {
4736 if (a >=
'0' && a <=
'9') a -=
'0';
4737 else if (a >=
'a' && a <=
'f') a = a -
'a' + 10;
4738 else if (a >=
'A' && a <=
'F') a = a -
'A' + 10;
4740 if (b >=
'0' && b <=
'9') b -=
'0';
4741 else if (b >=
'a' && b <=
'f') b = b -
'a' + 10;
4742 else if (b >=
'A' && b <=
'F') b = b -
'A' + 10;
4744 *data++ = (
char)((a << 4) | b);
4754 ba->truncate(outlen);
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4801 return input.percentDecoded(percent);
4805
4806
4807
4808
4809
4810
4813 return QByteArray(s.data(), qsizetype(s.size()));
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4830 return std::string(data(), size_t(size()));
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4865 const auto contains = [](
const QByteArray &view,
char c) {
4867 return view.size() > 0 && memchr(view.data(), c, view.size()) !=
nullptr;
4871 char *output =
nullptr;
4872 qsizetype length = 0;
4874 for (
unsigned char c : *
this) {
4875 if (
char(c) != percent
4876 && ((c >= 0x61 && c <= 0x7A)
4877 || (c >= 0x41 && c <= 0x5A)
4878 || (c >= 0x30 && c <= 0x39)
4883 || contains(exclude, c))
4884 && !contains(include, c)) {
4891 result.resize(size() * 3);
4892 output = result.data();
4894 output[length++] = percent;
4895 output[length++] = QtMiscUtils::toHexUpper((c & 0xf0) >> 4);
4896 output[length++] = QtMiscUtils::toHexUpper(c & 0xf);
4900 result.truncate(length);
4905#if defined(Q_OS_WASM) || defined(Q_QDOC)
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4930QByteArray QByteArray::fromEcmaUint8Array(emscripten::val uint8array)
4932 return qstdweb::Uint8Array(uint8array).copyToQByteArray();
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952emscripten::val QByteArray::toEcmaUint8Array()
4954 return qstdweb::Uint8Array::copyFrom(*
this).val();
4960
4961
4964
4965
4968
4969
4970
4971
4972
4975
4976
4977
4978
4979
4982
4983
4984
4985
4986
4987
4990
4991
4992
4993
4994
4995
4998
4999
5002
5003
5006
5007
5010
5011
5014
5015
5018
5019
5022
5023
5026
5027
5028
5031
5032
5033
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5055#if QT_DEPRECATED_SINCE(6
, 8
)
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5131
5132
5133
5134
5137
5138
5139
5140
5141
5144
5145
5146
5147
5148
5149
5152
5153
5154
5155
5158
5159
5160
5161
5162
5163
5164
5165
5168
5169
5170
5171
5172
5175
5176
5177size_t
qHash(
const QByteArray::FromBase64Result &key, size_t seed)
noexcept
5179 return qHashMulti(seed, key.decoded,
static_cast<
int>(key.decodingStatus));
5183
5184
5185
5186
5187
5188
5189
5190
5193
5194
5195
5196
5197
5198
5199
5200
5201
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_template(T &input, uchar(*lookup)(uchar))
static void q_fromPercentEncoding(QByteArray *ba, char percent)
int qstrnicmp(const char *str1, qsizetype len1, const char *str2, 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)