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"
28#include <qxpfunctional.h>
36#include <QtCore/q26numeric.h>
40# if !defined(QT_BOOTSTRAPPED) && (defined(QT_NO_CAST_FROM_ASCII) || defined(QT_NO_CAST_FROM_BYTEARRAY))
42# error "This file cannot be compiled with QT_NO_CAST_{TO,FROM}_ASCII, "
43 "otherwise some QByteArray functions will not get exported."
49Q_CONSTINIT
const char QByteArray::_empty =
'\0';
54 return c >=
'a' && c <=
'z' ? c & ~0x20 : c;
59 return c >=
'A' && c <=
'Z' ? c | 0x20 : c;
63
64
67
68
69
70
71
72
73
74
75const void *
qmemrchr(
const void *s,
int needle, size_t size)
noexcept
78 return memrchr(s, needle, size);
80 auto b =
static_cast<
const uchar *>(s);
81 const uchar *n = b + size;
83 if (*n == uchar(needle))
91
92
93
94
95
96
97
98
99
100
106 char *dst =
new char[strlen(src) + 1];
107 return qstrcpy(dst, src);
111
112
113
114
115
116
117
118
119
120
121
122
129 const size_t len = strlen(src);
132 if (len >= 0 && strcpy_s(dst, len+1, src) == 0)
136 return strcpy(dst, src);
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
161char *
qstrncpy(
char *dst,
const char *src, size_t len)
163 if (dst && len > 0) {
166 std::strncat(dst, src, len - 1);
168 return src ? dst :
nullptr;
172
173
174
175
176
177
178
179
180
183
184
185
186
187
188
189
190
191
192
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210int qstrcmp(
const char *str1,
const char *str2)
212 return (str1 && str2) ? strcmp(str1, str2)
213 : (str1 ? 1 : (str2 ? -1 : 0));
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
257 const uchar *s1 =
reinterpret_cast<
const uchar *>(str1);
258 const uchar *s2 =
reinterpret_cast<
const uchar *>(str2);
264 enum { Incomplete = 256 };
266 auto innerCompare = [=, &offset](qptrdiff max,
bool unlimited) {
269 uchar c = s1[offset];
270 if (
int res = QtMiscUtils::caseCompareAscii(c, s2[offset]))
275 }
while (unlimited || offset < max);
276 return int(Incomplete);
279#if defined(__SSE4_1__) && !(defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer))
280 enum { PageSize = 4096, PageMask = PageSize - 1 };
281 const __m128i zero = _mm_setzero_si128();
286 quintptr u1 = quintptr(s1 + offset);
287 quintptr u2 = quintptr(s2 + offset);
288 size_t n = PageSize - ((u1 | u2) & PageMask);
290 qptrdiff maxoffset = offset + n;
291 for ( ; offset + 16 <= maxoffset; offset +=
sizeof(__m128i)) {
293 __m128i a = _mm_loadu_si128(
reinterpret_cast<
const __m128i *>(s1 + offset));
294 __m128i b = _mm_loadu_si128(
reinterpret_cast<
const __m128i *>(s2 + offset));
297 __m128i cmp = _mm_cmpeq_epi8(a, b);
300 cmp = _mm_min_epu8(cmp, a);
301 cmp = _mm_cmpeq_epi8(cmp, zero);
304 uint mask = _mm_movemask_epi8(cmp);
307 uint start = qCountTrailingZeroBits(mask);
308 uint end =
sizeof(mask) * 8 - qCountLeadingZeroBits(mask);
309 Q_ASSERT(end >= start);
317 int res = innerCompare(n,
false);
318 if (res != Incomplete)
323 return innerCompare(-1,
true);
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
345int qstrnicmp(
const char *str1,
const char *str2, size_t len)
347 const uchar *s1 =
reinterpret_cast<
const uchar *>(str1);
348 const uchar *s2 =
reinterpret_cast<
const uchar *>(str2);
350 return s1 ? 1 : (s2 ? -1 : 0);
351 for (; len--; ++s1, ++s2) {
353 if (
int res = QtMiscUtils::caseCompareAscii(c, *s2))
362
363
364
365
366
367
368
369int qstrnicmp(
const char *str1, qsizetype len1,
const char *str2, qsizetype len2)
372 Q_ASSERT(len2 >= -1);
373 const uchar *s1 =
reinterpret_cast<
const uchar *>(str1);
374 const uchar *s2 =
reinterpret_cast<
const uchar *>(str2);
379 return (!s2 || !*s2) ? 0 : -1;
384 return len1 == 0 ? 0 : 1;
389 for (i = 0; i < len1; ++i) {
390 const uchar c = s2[i];
394 if (
int res = QtMiscUtils::caseCompareAscii(s1[i], c))
397 return s2[i] ? -1 : 0;
400 const qsizetype len = qMin(len1, len2);
401 for (qsizetype i = 0; i < len; ++i) {
402 if (
int res = QtMiscUtils::caseCompareAscii(s1[i], s2[i]))
407 return len1 < len2 ? -1 : 1;
412
413
414int QtPrivate::compareMemory(QByteArrayView lhs, QByteArrayView rhs)
416 if (!lhs.isNull() && !rhs.isNull()) {
417 int ret = memcmp(lhs.data(), rhs.data(), qMin(lhs.size(), rhs.size()));
424 return lhs.size() == rhs.size() ? 0 : lhs.size() > rhs.size() ? 1 : -1;
428
429
432 return QUtf8::isValidUtf8(s).isValidUtf8;
437static void createCRC16Table()
441 unsigned short crc_tbl[16];
442 unsigned int v0, v1, v2, v3;
443 for (i = 0; i < 16; i++) {
450#define SET_BIT(x, b, v) (x) |= (v) << (b)
465 printf(
"static const quint16 crc_tbl[16] = {\n");
466 for (
int i = 0; i < 16; i +=4)
467 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]);
473 0x0000, 0x1081, 0x2102, 0x3183,
474 0x4204, 0x5285, 0x6306, 0x7387,
475 0x8408, 0x9489, 0xa50a, 0xb58b,
476 0xc60c, 0xd68d, 0xe70e, 0xf78f
480
481
482
483
484
485
486
487
488
489
490
491
494 quint16 crc = 0x0000;
496 case Qt::ChecksumIso3309:
499 case Qt::ChecksumItuV41:
504 const uchar *p =
reinterpret_cast<
const uchar *>(data.data());
505 qsizetype len = data.size();
508 crc = ((crc >> 4) & 0x0fff) ^ crc_tbl[((crc ^ c) & 15)];
510 crc = ((crc >> 4) & 0x0fff) ^ crc_tbl[((crc ^ c) & 15)];
513 case Qt::ChecksumIso3309:
516 case Qt::ChecksumItuV41:
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
543
544
545
546
547
548
549
550
552#ifndef QT_NO_COMPRESS
553using CompressSizeHint_t = quint32;
564 Q_UNREACHABLE_RETURN(
nullptr);
568static QByteArray zlibError(ZLibOp op,
const char *what)
570 qWarning(
"%s: %s", zlibOpAsString(op), what);
575static QByteArray dataIsNull(ZLibOp op)
577 return zlibError(op,
"Data is null");
581static QByteArray lengthIsNegative(ZLibOp op)
583 return zlibError(op,
"Input length is negative");
587static QByteArray tooMuchData(ZLibOp op)
589 return zlibError(op,
"Not enough memory");
593static QByteArray invalidCompressedData()
595 return zlibError(ZLibOp::Decompression,
"Input data is corrupted");
599static QByteArray unexpectedZlibError(ZLibOp op,
int err,
const char *msg)
601 qWarning(
"%s unexpected zlib error: %s (%d)",
613 if (out.data() ==
nullptr)
614 return tooMuchData(op);
615 qsizetype capacity = out.allocatedCapacity();
617 const auto initalSize = out.size;
620 zs.next_in =
reinterpret_cast<uchar *>(
const_cast<
char *>(input.data()));
621 if (
const int err = init(&zs); err != Z_OK)
622 return unexpectedZlibError(op, err, zs.msg);
623 const auto sg = qScopeGuard([&] { deinit(&zs); });
625 using ZlibChunkSize_t =
decltype(zs.avail_in);
626 static_assert(!std::is_signed_v<ZlibChunkSize_t>);
627 static_assert(std::is_same_v<ZlibChunkSize_t,
decltype(zs.avail_out)>);
628 constexpr auto MaxChunkSize =
std::numeric_limits<ZlibChunkSize_t>::max();
630 constexpr auto MaxStatisticsSize =
std::numeric_limits<
decltype(zs.total_out)>::max();
632 size_t inputLeft = size_t(input.size());
636 Q_ASSERT(out.freeSpaceAtBegin() == 0);
637 Q_ASSERT(capacity == out.allocatedCapacity());
639 if (zs.avail_out == 0) {
640 Q_ASSERT(size_t(out.size) - initalSize > MaxStatisticsSize ||
641 size_t(out.size) - initalSize == zs.total_out);
642 Q_ASSERT(out.size <= capacity);
644 qsizetype avail_out = capacity - out.size;
645 if (avail_out == 0) {
646 out->reallocateAndGrow(QArrayData::GrowsAtEnd, 1);
647 if (out.data() ==
nullptr)
648 return tooMuchData(op);
649 capacity = out.allocatedCapacity();
650 avail_out = capacity - out.size;
652 zs.next_out =
reinterpret_cast<uchar *>(out.data()) + out.size;
653 zs.avail_out = size_t(avail_out) > size_t(MaxChunkSize) ? MaxChunkSize
654 : ZlibChunkSize_t(avail_out);
655 out.size += zs.avail_out;
657 Q_ASSERT(zs.avail_out > 0);
660 if (zs.avail_in == 0) {
662 zs.avail_in = inputLeft > MaxChunkSize ? MaxChunkSize : ZlibChunkSize_t(inputLeft);
663 inputLeft -= zs.avail_in;
666 res = processChunk(&zs, inputLeft);
667 }
while (res == Z_OK);
671 out.size -= zs.avail_out;
672 Q_ASSERT(size_t(out.size) - initalSize > MaxStatisticsSize ||
673 size_t(out.size) - initalSize == zs.total_out);
674 Q_ASSERT(out.size <= out.allocatedCapacity());
675 out.data()[out.size] =
'\0';
676 return QByteArray(
std::move(out));
679 return tooMuchData(op);
687 return invalidCompressedData();
690 return unexpectedZlibError(op, res, zs.msg);
696 constexpr qsizetype HeaderSize =
sizeof(CompressSizeHint_t);
698 return QByteArray(HeaderSize,
'\0');
706 if (compressionLevel < -1 || compressionLevel > 9)
707 compressionLevel = -1;
709 QArrayDataPointer out = [&] {
710 constexpr qsizetype SingleAllocLimit = 256 * 1024;
714 qsizetype capacity = HeaderSize;
715 if (nbytes < SingleAllocLimit) {
717 capacity += compressBound(uLong(nbytes));
718 return QArrayDataPointer<
char>(capacity);
723 constexpr qsizetype MaxCompressionFactor = 1024;
726 capacity +=
std::max(qsizetype(compressBound(uLong(SingleAllocLimit))),
727 nbytes / MaxCompressionFactor);
728 return QArrayDataPointer<
char>(capacity, 0, QArrayData::Grow);
731 if (out.data() ==
nullptr)
734 qToBigEndian(q26::saturate_cast<CompressSizeHint_t>(nbytes), out.data());
735 out.size = HeaderSize;
737 return xxflate(ZLibOp::Compression, std::move(out), {data, nbytes},
738 [=] (z_stream *zs) {
return deflateInit(zs, compressionLevel); },
739 [] (z_stream *zs, size_t inputLeft) {
740 return deflate(zs, inputLeft ? Z_NO_FLUSH : Z_FINISH);
742 [] (z_stream *zs) { deflateEnd(zs); });
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
778#ifndef QT_NO_COMPRESS
780
781
782
783
784
785
794 constexpr qsizetype HeaderSize =
sizeof(CompressSizeHint_t);
795 if (nbytes < HeaderSize)
796 return invalidCompressedData();
798 const auto expectedSize = qFromBigEndian<CompressSizeHint_t>(data);
799 if (nbytes == HeaderSize) {
800 if (expectedSize != 0)
801 return invalidCompressedData();
805 constexpr auto MaxDecompressedSize = size_t(QByteArray::maxSize());
806 if constexpr (MaxDecompressedSize < std::numeric_limits<CompressSizeHint_t>::max()) {
807 if (expectedSize > MaxDecompressedSize)
813 qsizetype capacity =
std::max(qsizetype(expectedSize),
816 QArrayDataPointer<
char> d(capacity);
817 return xxflate(ZLibOp::Decompression, std::move(d), {data + HeaderSize, nbytes - HeaderSize},
818 [] (z_stream *zs) {
return inflateInit(zs); },
819 [] (z_stream *zs, size_t) {
return inflate(zs, Z_NO_FLUSH); },
820 [] (z_stream *zs) { inflateEnd(zs); });
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
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
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1121
1122
1123
1126
1127
1128
1129
1130
1131
1132
1133
1134
1137
1138
1139
1140
1141
1142
1143
1144
1147
1148
1149
1150
1151
1152
1153
1154
1157
1158
1159
1162
1163
1164
1165
1166
1167
1168
1169
1170
1173
1174
1175
1176
1177
1178
1179
1180
1183
1184
1185
1186
1187
1188
1189
1190
1191
1194
1195
1196
1199
1200
1201
1202
1203
1204
1205
1206
1207
1210
1211
1212
1213
1214
1215
1216
1217
1218
1221
1222
1223
1226
1227
1228
1229
1230
1231
1232
1233
1234
1237
1238
1239
1240
1243
1244
1245
1246
1247
1250
1251
1252
1253
1254
1257
1258
1259
1260
1261
1264
1265
1266
1267
1270
1271
1272
1273
1274
1277
1278
1279
1280
1281
1284
1285
1286
1287
1288
1291
1292
1293
1294
1295
1298
1299
1300
1301
1302
1303
1304QByteArray::iterator
QByteArray::erase(QByteArray::const_iterator first, QByteArray::const_iterator last)
1306 const auto start =
std::distance(cbegin(), first);
1307 const auto len =
std::distance(first, last);
1309 return begin() + start;
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1341
1342
1343
1344
1345
1346
1347
1350
1351
1352
1353
1354
1357
1358
1361
1362
1363
1372
1373
1374
1375
1376
1377
1378
1385 d = DataPointer::fromRawData(&_empty, 0);
1393
1394
1395
1396
1397
1398
1401
1402
1403
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1424
1425
1426
1427
1428
1429
1430
1433
1434
1435
1436
1437
1438
1439
1440
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
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
1490
1491
1492
1493
1494
1495
1496
1497
1498
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
1528
1529
1530
1531
1532
1533
1534
1535
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
1573
1574
1575
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1596
1597
1598
1601
1602
1603
1606
1607
1608
1611
1612
1613
1614
1615
1616
1617
1618
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1634
1635
1636
1637
1638
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1701
1702
1703
1704
1705
1706
1707
1710
1711
1712
1713
1714
1715
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1779
1780
1781
1782
1783
1784
1787
1788
1789
1790
1791
1792
1795
1796
1797
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1815
1816
1817
1818
1819
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1841 size = qstrlen(data);
1843 d = DataPointer::fromRawData(&_empty, 0);
1845 d = DataPointer(size, size);
1846 Q_CHECK_PTR(d.data());
1847 memcpy(d.data(), data, size);
1848 d.data()[size] =
'\0';
1854
1855
1856
1857
1862 d = DataPointer::fromRawData(&_empty, 0);
1864 d = DataPointer(size, size);
1865 Q_CHECK_PTR(d.data());
1866 memset(d.data(), ch, size);
1867 d.data()[size] =
'\0';
1872
1873
1874
1875
1876
1877
1878
1883 d = DataPointer::fromRawData(&_empty, 0);
1885 d = DataPointer(size, size);
1886 Q_CHECK_PTR(d.data());
1887 d.data()[size] =
'\0';
1892
1893
1894
1895
1896
1897
1898
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1920 const auto capacityAtEnd = capacity() - d.freeSpaceAtBegin();
1921 if (d->needsDetach() || size > capacityAtEnd)
1922 reallocData(size, QArrayData::Grow);
1924 if (d->allocatedCapacity())
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1947 const auto old = d.size;
1950 memset(d.data() + old, c, d.size - old);
1954
1955
1956
1957
1958
1959
1960
1961
1962
1969
1970
1971
1972
1973
1974
1975
1976
1980 resize(size < 0 ?
this->size() : size);
1982 memset(d.data(), ch,
this->size());
1986void QByteArray::reallocData(qsizetype alloc, QArrayData::AllocationOption option)
1989 d = DataPointer::fromRawData(&_empty, 0);
1995 const bool cannotUseReallocate = d.freeSpaceAtBegin() > 0;
1997 if (d->needsDetach() || cannotUseReallocate) {
1998 DataPointer dd(alloc, qMin(alloc, d.size), option);
1999 Q_CHECK_PTR(dd.data());
2001 ::memcpy(dd.data(), d.data(), dd.size);
2002 dd.data()[dd.size] = 0;
2005 d->reallocate(alloc, option);
2014 if (d->needsDetach()) {
2015 DataPointer dd(DataPointer::allocateGrow(d, n, QArrayData::GrowsAtEnd));
2016 Q_CHECK_PTR(dd.data());
2017 dd->copyAppend(d.data(), d.data() + d.size);
2018 dd.data()[dd.size] = 0;
2021 d->reallocate(d.constAllocatedCapacity() + n, QArrayData::Grow);
2027 resize(qMax(i + 1, size()));
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2052
2053
2054
2055
2056
2057
2058
2059
2060
2072 return std::move(*
this);
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2094
2095
2096
2097
2098
2101 if (size() == 0 && ba.size() > d.constAllocatedCapacity() && ba.d.isMutable())
2102 return (*
this = ba);
2103 return prepend(QByteArrayView(ba));
2107
2108
2109
2110
2111
2114
2115
2116
2117
2118
2119
2120
2123
2124
2125
2126
2127
2128
2131
2132
2133
2134
2135
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2165 if (Q_UNLIKELY(!ba.d.isMutable()))
2169 }
else if (ba.size()) {
2170 append(QByteArrayView(ba));
2177
2178
2179
2180
2181
2184
2185
2186
2187
2188
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2207
2208
2209
2210
2211
2212
2213
2214
2215
2218
2219
2220
2221
2225 d.detachAndGrow(QArrayData::GrowsAtEnd, 1,
nullptr,
nullptr);
2226 d->copyAppend(1, ch);
2227 d.data()[d.size] =
'\0';
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2282 const auto len = v.size();
2284 if (len <= capacity() && isDetached()) {
2285 const auto offset = d.freeSpaceAtBegin();
2287 d.setBegin(d.begin() - offset);
2288 std::memcpy(d.begin(), v.data(), len);
2290 d.data()[d.size] =
'\0';
2292 *
this = v.toByteArray();
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2319 const char *str = data.data();
2320 qsizetype size = data.size();
2321 if (i < 0 || size <= 0)
2330 DataPointer detached{};
2331 d.detachAndGrow(Data::GrowsAtEnd, (i - d.size) + size, &str, &detached);
2332 Q_CHECK_PTR(d.data());
2333 d->copyAppend(i - d->size,
' ');
2334 d->copyAppend(str, str + size);
2335 d.data()[d.size] =
'\0';
2339 if (!d->needsDetach() && QtPrivate::q_points_into_range(str, d)) {
2340 QVarLengthArray a(str, str + size);
2341 return insert(i, a);
2344 d->insert(i, str, size);
2345 d.data()[d.size] =
'\0';
2350
2351
2352
2353
2354
2355
2356
2357
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2372
2373
2374
2375
2376
2377
2378
2379
2380
2383
2384
2385
2386
2387
2388
2389
2392
2393
2394
2395
2396
2397
2398
2399
2400
2404 if (i < 0 || count <= 0)
2409 d.detachAndGrow(Data::GrowsAtEnd, (i - d.size) + count,
nullptr,
nullptr);
2410 Q_CHECK_PTR(d.data());
2411 d->copyAppend(i - d->size,
' ');
2412 d->copyAppend(count, ch);
2413 d.data()[d.size] =
'\0';
2417 d->insert(i, count, ch);
2418 d.data()[d.size] =
'\0';
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2442 if (len <= 0 || pos < 0 || size_t(pos) >= size_t(size()))
2444 if (pos + len > d->size)
2445 len = d->size - pos;
2447 const auto toRemove_start = d.begin() + pos;
2448 if (!d->isShared()) {
2449 d->erase(toRemove_start, len);
2450 d.data()[d.size] =
'\0';
2452 QByteArray copy{size() - len, Qt::Uninitialized};
2453 copy.d->copyRanges({{d.begin(), toRemove_start},
2454 {toRemove_start + len, d.end()}});
2461
2462
2463
2464
2465
2466
2467
2468
2469
2472
2473
2474
2475
2476
2477
2478
2479
2480
2482
2483
2484
2485
2486
2487
2488
2489
2490
2493
2494
2495
2496
2497
2498
2499
2500
2503
2504
2505
2506
2507
2508
2509
2510
2514 if (size_t(pos) > size_t(
this->size()))
2516 if (len >
this->size() - pos)
2517 len =
this->size() - pos;
2522 return insert(pos, after);
2524 if (after.isEmpty())
2525 return remove(pos, len);
2527 using A = QStringAlgorithms<QByteArray>;
2528 const qsizetype newlen = A::newSize(*
this, len, after, {pos});
2529 if (data_ptr().needsDetach() || A::needsReallocate(*
this, newlen)) {
2530 A::replace_into_copy(*
this, len, after, {pos}, newlen);
2535 char *
const begin = data_ptr().data();
2536 char *
const before = begin + pos;
2537 const char *beforeEnd = before + len;
2538 if (len >= after.size()) {
2539 memmove(before , after.cbegin(), after.size());
2541 if (len > after.size()) {
2542 memmove(before + after.size(), beforeEnd, d.size - (beforeEnd - begin));
2543 A::setSize(*
this, newlen);
2546 char *oldEnd = begin + d.size;
2547 const qsizetype adjust = newlen - d.size;
2548 A::setSize(*
this, newlen);
2550 QByteArrayView tail{beforeEnd, oldEnd};
2551 QByteArrayView prefix = after;
2552 QByteArrayView suffix;
2553 if (QtPrivate::q_points_into_range(after.cend() - 1, tail)) {
2554 if (QtPrivate::q_points_into_range(after.cbegin(), tail)) {
2557 suffix = QByteArrayView{after.cbegin(), after.cend()};
2559 prefix = QByteArrayView{after.cbegin(), beforeEnd};
2560 suffix = QByteArrayView{beforeEnd, after.cend()};
2563 memmove(before + after.size(), tail.cbegin(), tail.size());
2564 if (!prefix.isEmpty())
2565 memmove(before, prefix.cbegin(), prefix.size());
2566 if (!suffix.isEmpty())
2567 memcpy(before + prefix.size(), suffix.cbegin() + adjust, suffix.size());
2573
2574
2575
2576
2577
2578
2579
2580
2583
2584
2585
2586
2587
2588
2589
2590
2593
2594
2595
2596
2597
2598
2599
2600
2601
2605 const char *b = before.data();
2606 qsizetype bsize = before.size();
2607 const char *a = after.data();
2608 qsizetype asize = after.size();
2614 if (b == a && bsize == asize)
2617 if (asize == 0 && bsize == 0)
2620 if (bsize == 1 && asize == 1)
2621 return replace(*b, *a);
2624 std::string pinnedReplacement;
2625 if (QtPrivate::q_points_into_range(a, d)) {
2626 pinnedReplacement.assign(a, a + asize);
2627 after = pinnedReplacement;
2635 QVarLengthArray<qsizetype> indices;
2636 qsizetype index = 0;
2637 while ((index = matcher.indexIn(*
this, index)) != -1) {
2638 indices.push_back(index);
2645 QStringAlgorithms<QByteArray>::replace_helper(*
this, bsize, after, indices);
2650
2651
2652
2653
2654
2655
2658
2659
2660
2661
2665 if (before != after) {
2666 if (
const auto pos = indexOf(before); pos >= 0) {
2667 if (d.needsDetach()) {
2669 auto dst = tmp.d.data();
2670 dst = std::copy(d.data(), d.data() + pos, dst);
2672 std::replace_copy(d.data() + pos + 1, d.end(), dst, before, after);
2676 d.data()[pos] = after;
2677 std::replace(d.data() + pos + 1, d.end(), before, after);
2685
2686
2687
2688
2689
2694 qsizetype start = 0;
2696 while ((end = indexOf(sep, start)) != -1) {
2697 list.append(mid(start, end - start));
2700 list.append(mid(start));
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2726 const qsizetype resultSize = times * size();
2729 result.reserve(resultSize);
2730 if (result.capacity() != resultSize)
2733 memcpy(result.d.data(), data(), size());
2735 qsizetype sizeSoFar = size();
2736 char *end = result.d.data() + sizeSoFar;
2738 const qsizetype halfResultSize = resultSize >> 1;
2739 while (sizeSoFar <= halfResultSize) {
2740 memcpy(end, result.d.data(), sizeSoFar);
2744 memcpy(end, result.d.data(), resultSize - sizeSoFar);
2745 result.d.data()[resultSize] =
'\0';
2746 result.d.size = resultSize;
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2778 qsizetype ol, qsizetype from)
2780 auto delta = l - ol;
2783 if (from < 0 || from > delta)
2788 const char *end = haystack;
2790 const qregisteruint ol_minus_1 = ol - 1;
2791 const char *n = needle + ol_minus_1;
2792 const char *h = haystack + ol_minus_1;
2793 qregisteruint hashNeedle = 0, hashHaystack = 0;
2795 for (idx = 0; idx < ol; ++idx) {
2796 hashNeedle = ((hashNeedle<<1) + *(n-idx));
2797 hashHaystack = ((hashHaystack<<1) + *(h-idx));
2799 hashHaystack -= *haystack;
2800 while (haystack >= end) {
2801 hashHaystack += *haystack;
2802 if (hashHaystack == hashNeedle && memcmp(needle, haystack, ol) == 0)
2803 return haystack - end;
2805 if (ol_minus_1 <
sizeof(ol_minus_1) * CHAR_BIT)
2806 hashHaystack -= qregisteruint(*(haystack + ol)) << ol_minus_1;
2812qsizetype
QtPrivate::lastIndexOf(QByteArrayView haystack, qsizetype from, QByteArrayView needle)
noexcept
2814 if (haystack.isEmpty()) {
2815 if (needle.isEmpty() && from == 0)
2819 const auto ol = needle.size();
2821 return QtPrivate::lastIndexOf(haystack, from, needle.front());
2823 return lastIndexOfHelper(haystack.data(), haystack.size(), needle.data(), ol, from);
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2882 for (
char ch : haystack) {
2889qsizetype
QtPrivate::count(QByteArrayView haystack, QByteArrayView needle)
noexcept
2891 if (needle.size() == 0)
2892 return haystack.size() + 1;
2894 if (needle.size() == 1)
2895 return countCharHelper(haystack, needle[0]);
2899 if (haystack.size() > 500 && needle.size() > 5) {
2901 while ((i = matcher.indexIn(haystack, i + 1)) != -1)
2904 while ((i = haystack.indexOf(needle, i + 1)) != -1)
2911
2912
2913
2914
2915
2916
2917
2920
2921
2922
2923
2924
2925
2929 return countCharHelper(*
this, ch);
2932#if QT_DEPRECATED_SINCE(6
, 4
)
2934
2935
2936
2937
2938
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2953bool QtPrivate::startsWith(QByteArrayView haystack, QByteArrayView needle)
noexcept
2955 if (haystack.size() < needle.size())
2957 if (haystack.data() == needle.data() || needle.size() == 0)
2959 return memcmp(haystack.data(), needle.data(), needle.size()) == 0;
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2975
2976
2977
2978
2979
2980
2982bool QtPrivate::endsWith(QByteArrayView haystack, QByteArrayView needle)
noexcept
2984 if (haystack.size() < needle.size())
2986 if (haystack.end() == needle.end() || needle.size() == 0)
2988 return memcmp(haystack.end() - needle.size(), needle.data(), needle.size()) == 0;
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3005
3006
3007
3008
3009
3010
3013
3014
3017 return c >=
'A' && c <=
'Z';
3021
3022
3025 return c >=
'a' && c <=
'z';
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3061
3062
3063
3064
3065
3066
3067
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3126 switch (QContainerImplHelper::mid(size(), &p, &l)) {
3127 case QContainerImplHelper::Null:
3129 case QContainerImplHelper::Empty:
3131 return QByteArray(DataPointer::fromRawData(&_empty, 0));
3133 case QContainerImplHelper::Full:
3135 case QContainerImplHelper::Subset:
3136 return sliced(p, l);
3146 switch (QContainerImplHelper::mid(size(), &p, &l)) {
3147 case QContainerImplHelper::Null:
3149 case QContainerImplHelper::Empty:
3152 case QContainerImplHelper::Full:
3153 return std::move(*
this);
3154 case QContainerImplHelper::Subset:
3155 return std::move(*
this).sliced(p, l);
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3209 return fromRawData(&_empty, 0);
3210 DataPointer d =
std::move(a.d).sliced(pos, n);
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3283template <
typename T>
3287 const char *orig_begin = input.constBegin();
3288 const char *firstBad = orig_begin;
3289 const char *e = input.constEnd();
3290 for ( ; firstBad != e ; ++firstBad) {
3291 uchar ch = uchar(*firstBad);
3292 uchar converted = lookup(ch);
3293 if (ch != converted)
3298 return std::move(input);
3302 char *b = s.begin();
3303 char *p = b + (firstBad - orig_begin);
3305 for ( ; p != e; ++p)
3306 *p =
char(lookup(uchar(*p)));
3312 return toCase_template(a, asciiLower);
3317 return toCase_template(a, asciiLower);
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3334 return toCase_template(a, asciiUpper);
3339 return toCase_template(a, asciiUpper);
3343
3344
3345
3346
3347
3354#if !defined(QT_NO_DATASTREAM)
3357
3358
3359
3360
3361
3362
3366 if (ba.isNull() && out.version() >= 6) {
3367 QDataStream::writeQSizeType(out, -1);
3370 return out.writeBytes(ba.constData(), ba.size());
3374
3375
3376
3377
3378
3379
3385 qint64 size = QDataStream::readQSizeType(in);
3386 qsizetype len = size;
3387 if (size != len || size < -1) {
3389 in.setStatus(QDataStream::SizeLimitExceeded);
3397 constexpr qsizetype Step = 1024 * 1024;
3398 qsizetype allocated = 0;
3401 qsizetype blockSize = qMin(Step, len - allocated);
3402 ba.resize(allocated + blockSize);
3403 if (in.readRawData(ba.data() + allocated, blockSize) != blockSize) {
3405 in.setStatus(QDataStream::ReadPastEnd);
3408 allocated += blockSize;
3409 }
while (allocated < len);
3416
3417
3418
3419
3420
3421
3422
3425
3426
3427
3428
3429
3430
3431
3434
3435
3436
3437
3438
3439
3440
3443
3444
3445
3446
3447
3448
3449
3452
3453
3454
3455
3456
3457
3458
3461
3462
3463
3464
3465
3466
3467
3470
3471
3472
3473
3474
3475
3476
3479
3480
3481
3482
3483
3484
3485
3488
3489
3490
3491
3492
3493
3494
3497
3498
3499
3500
3501
3502
3503
3506
3507
3508
3509
3510
3511
3512
3515
3516
3517
3518
3519
3520
3521
3524
3525
3526
3527
3528
3529
3530
3533
3534
3535
3536
3537
3538
3539
3542
3543
3544
3545
3546
3547
3548
3551
3552
3553
3554
3555
3556
3557
3560
3561
3562
3563
3564
3565
3566
3569
3570
3571
3572
3573
3574
3575
3578
3579
3580
3581
3582
3583
3584
3587
3588
3589
3590
3591
3592
3593
3596
3597
3598
3599
3600
3601
3602
3605
3606
3607
3608
3609
3610
3611
3614
3615
3616
3617
3618
3619
3620
3623
3624
3625
3626
3627
3628
3629
3630
3631
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3652 return QStringAlgorithms<
const QByteArray>::simplified_helper(a);
3657 return QStringAlgorithms<QByteArray>::simplified_helper(a);
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3681 return QStringAlgorithms<
const QByteArray>::trimmed_helper(a);
3686 return QStringAlgorithms<QByteArray>::trimmed_helper(a);
3689QByteArrayView
QtPrivate::trimmed(QByteArrayView view)
noexcept
3691 const auto [start, stop] = QStringAlgorithms<QByteArrayView>::trimmed_helper_positions(view);
3692 return QByteArrayView(start, stop);
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3716 qsizetype len = size();
3717 qsizetype padlen = width - len;
3719 result.resize(len+padlen);
3721 memcpy(result.d.data(), data(), len);
3722 memset(result.d.data()+len, fill, padlen);
3725 result = left(width);
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3753 qsizetype len = size();
3754 qsizetype padlen = width - len;
3756 result.resize(len+padlen);
3758 memcpy(result.d.data()+padlen, data(), len);
3759 memset(result.d.data(), fill, padlen);
3762 result = left(width);
3769auto QtPrivate::toSignedInteger(QByteArrayView data,
int base) -> ParsedNumber<qlonglong>
3771#if defined(QT_CHECK_RANGE)
3772 if (base != 0 && (base < 2 || base > 36)) {
3773 qWarning(
"QByteArray::toIntegral: Invalid base %d", base);
3780 const QSimpleParsedNumber r = QLocaleData::bytearrayToLongLong(data, base);
3782 return ParsedNumber(r.result);
3786auto QtPrivate::toUnsignedInteger(QByteArrayView data,
int base) -> ParsedNumber<qulonglong>
3788#if defined(QT_CHECK_RANGE)
3789 if (base != 0 && (base < 2 || base > 36)) {
3790 qWarning(
"QByteArray::toIntegral: Invalid base %d", base);
3797 const QSimpleParsedNumber r = QLocaleData::bytearrayToUnsLongLong(data, base);
3799 return ParsedNumber(r.result);
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3830 return QtPrivate::toIntegral<qlonglong>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3860 return QtPrivate::toIntegral<qulonglong>(qToByteArrayViewIgnoringNull(*
this), ok, base);
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
3892 return QtPrivate::toIntegral<
int>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3922 return QtPrivate::toIntegral<uint>(qToByteArrayViewIgnoringNull(*
this), ok, base);
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
3955 return QtPrivate::toIntegral<
long>(qToByteArrayViewIgnoringNull(*
this), ok, base);
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
3986 return QtPrivate::toIntegral<ulong>(qToByteArrayViewIgnoringNull(*
this), ok, base);
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4016 return QtPrivate::toIntegral<
short>(qToByteArrayViewIgnoringNull(*
this), ok, base);
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4046 return QtPrivate::toIntegral<ushort>(qToByteArrayViewIgnoringNull(*
this), ok, base);
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4076 return QByteArrayView(*
this).toDouble(ok);
4079auto QtPrivate::toDouble(QByteArrayView a)
noexcept -> ParsedNumber<
double>
4082 auto r = qt_asciiToDouble(a.data(), a.size());
4084 return ParsedNumber{r.result};
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4116 return QLocaleData::convertDoubleToFloat(toDouble(ok), ok);
4119auto QtPrivate::toFloat(QByteArrayView a)
noexcept -> ParsedNumber<
float>
4121 if (
const auto r = toDouble(a)) {
4125 return ParsedNumber(f);
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4143 constexpr char alphabet_base64[] =
"ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef"
4144 "ghijklmn" "opqrstuv" "wxyz0123" "456789+/";
4145 constexpr char alphabet_base64url[] =
"ABCDEFGH" "IJKLMNOP" "QRSTUVWX" "YZabcdef"
4146 "ghijklmn" "opqrstuv" "wxyz0123" "456789-_";
4147 const char *
const alphabet = options & Base64UrlEncoding ? alphabet_base64url : alphabet_base64;
4148 constexpr char padchar =
'=';
4149 qsizetype padlen = 0;
4151 const qsizetype sz = size();
4153 QByteArray tmp((sz + 2) / 3 * 4, Qt::Uninitialized);
4156 char *out = tmp.data();
4160 chunk |=
int(uchar(data()[i++])) << 16;
4164 chunk |=
int(uchar(data()[i++])) << 8;
4168 chunk |=
int(uchar(data()[i++]));
4171 int j = (chunk & 0x00fc0000) >> 18;
4172 int k = (chunk & 0x0003f000) >> 12;
4173 int l = (chunk & 0x00000fc0) >> 6;
4174 int m = (chunk & 0x0000003f);
4175 *out++ = alphabet[j];
4176 *out++ = alphabet[k];
4179 if ((options & OmitTrailingEquals) == 0)
4182 *out++ = alphabet[l];
4185 if ((options & OmitTrailingEquals) == 0)
4188 *out++ = alphabet[m];
4191 Q_ASSERT((options & OmitTrailingEquals) || (out == tmp.size() + tmp.data()));
4192 if (options & OmitTrailingEquals)
4193 tmp.truncate(out - tmp.data());
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4218
4219
4220
4221
4222
4225
4226
4227
4228
4229
4232
4233
4234
4235
4236
4239
4240
4241
4242
4243
4246
4247
4248
4249
4250
4253
4254
4255
4256
4259 constexpr int buffsize = 66;
4260 char buff[buffsize];
4265 p = qulltoa2(buff + buffsize, qulonglong(-(1 + n)) + 1, base);
4268 p = qulltoa2(buff + buffsize, qulonglong(n), base);
4271 return assign(QByteArrayView{p, buff + buffsize});
4275
4276
4277
4278
4282 constexpr int buffsize = 66;
4283 char buff[buffsize];
4284 char *p = qulltoa2(buff + buffsize, n, base);
4286 return assign(QByteArrayView{p, buff + buffsize});
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4303 return *
this = QByteArray::number(n, format, precision);
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4343
4344
4345
4346
4355
4356
4357
4358
4367
4368
4369
4370
4379
4380
4381
4382
4391
4392
4393
4394
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4418 switch (QtMiscUtils::toAsciiLower(format)) {
4429#if defined(QT_CHECK_RANGE)
4430 qWarning(
"QByteArray::setNum: Invalid format char '%c'", format);
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
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4493 *
this = fromRawData(data, size);
4498struct fromBase64_helper_result {
4499 qsizetype decodedLength;
4500 QByteArray::Base64DecodingStatus status;
4503fromBase64_helper_result fromBase64_helper(
const char *input, qsizetype inputSize,
4505 QByteArray::Base64Options options)
4507 fromBase64_helper_result result{ 0, QByteArray::Base64DecodingStatus::Ok };
4509 unsigned int buf = 0;
4512 qsizetype offset = 0;
4513 for (qsizetype i = 0; i < inputSize; ++i) {
4517 if (ch >=
'A' && ch <=
'Z') {
4519 }
else if (ch >=
'a' && ch <=
'z') {
4521 }
else if (ch >=
'0' && ch <=
'9') {
4523 }
else if (ch ==
'+' && (options & QByteArray::Base64UrlEncoding) == 0) {
4525 }
else if (ch ==
'-' && (options & QByteArray::Base64UrlEncoding) != 0) {
4527 }
else if (ch ==
'/' && (options & QByteArray::Base64UrlEncoding) == 0) {
4529 }
else if (ch ==
'_' && (options & QByteArray::Base64UrlEncoding) != 0) {
4532 if (options & QByteArray::AbortOnBase64DecodingErrors) {
4536 if ((inputSize % 4) != 0) {
4537 result.status = QByteArray::Base64DecodingStatus::IllegalInputLength;
4539 }
else if ((i == inputSize - 1) ||
4540 (i == inputSize - 2 && input[++i] ==
'=')) {
4543 result.status = QByteArray::Base64DecodingStatus::IllegalPadding;
4547 result.status = QByteArray::Base64DecodingStatus::IllegalCharacter;
4556 buf = (buf << 6) | d;
4560 Q_ASSERT(offset < i);
4561 output[offset++] = buf >> nbits;
4562 buf &= (1 << nbits) - 1;
4567 result.decodedLength = offset;
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
4603 if (base64.isDetached()) {
4604 const auto base64result = fromBase64_helper(base64.data(),
4608 base64.truncate(base64result.decodedLength);
4609 return {
std::move(base64), base64result.status };
4612 return fromBase64Encoding(base64, options);
4618 const auto base64Size = base64.size();
4619 QByteArray result((base64Size * 3) / 4, Qt::Uninitialized);
4620 const auto base64result = fromBase64_helper(base64.data(),
4622 const_cast<
char *>(result.constData()),
4624 result.truncate(base64result.decodedLength);
4625 return {
std::move(result), base64result.status };
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4654 if (
auto result = fromBase64Encoding(base64, options))
4655 return std::move(result.decoded);
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4672 QByteArray res((hexEncoded.size() + 1)/ 2, Qt::Uninitialized);
4673 uchar *result = (uchar *)res.data() + res.size();
4675 bool odd_digit =
true;
4676 for (qsizetype i = hexEncoded.size() - 1; i >= 0; --i) {
4677 uchar ch = uchar(hexEncoded.at(i));
4678 int tmp = QtMiscUtils::fromHex(ch);
4686 *result |= tmp << 4;
4691 res.remove(0, result - (
const uchar *)res.constData());
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4714 const qsizetype length = separator ? (size() * 3 - 1) : (size() * 2);
4716 char *hexData = hex.data();
4717 const uchar *data = (
const uchar *)
this->data();
4718 for (qsizetype i = 0, o = 0; i < size(); ++i) {
4719 hexData[o++] = QtMiscUtils::toHexLower(data[i] >> 4);
4720 hexData[o++] = QtMiscUtils::toHexLower(data[i] & 0xf);
4722 if ((separator) && (o < length))
4723 hexData[o++] = separator;
4733 char *data = ba->data();
4734 const char *inputPtr = data;
4737 qsizetype len = ba->size();
4738 qsizetype outlen = 0;
4743 if (c == percent && i + 2 < len) {
4747 if (a >=
'0' && a <=
'9') a -=
'0';
4748 else if (a >=
'a' && a <=
'f') a = a -
'a' + 10;
4749 else if (a >=
'A' && a <=
'F') a = a -
'A' + 10;
4751 if (b >=
'0' && b <=
'9') b -=
'0';
4752 else if (b >=
'a' && b <=
'f') b = b -
'a' + 10;
4753 else if (b >=
'A' && b <=
'F') b = b -
'A' + 10;
4755 *data++ = (
char)((a << 4) | b);
4765 ba->truncate(outlen);
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4812 return input.percentDecoded(percent);
4816
4817
4818
4819
4820
4821
4824 return QByteArray(s.data(), qsizetype(s.size()));
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4841 return std::string(data(), size_t(size()));
4845
4846
4847
4848
4849
4850
4851
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4885 const auto contains = [](
const QByteArray &view,
char c) {
4887 return view.size() > 0 && memchr(view.data(), c, view.size()) !=
nullptr;
4891 char *output =
nullptr;
4892 qsizetype length = 0;
4894 for (
unsigned char c : *
this) {
4895 if (
char(c) != percent
4896 && ((c >= 0x61 && c <= 0x7A)
4897 || (c >= 0x41 && c <= 0x5A)
4898 || (c >= 0x30 && c <= 0x39)
4903 || contains(exclude, c))
4904 && !contains(include, c)) {
4911 result.resize(size() * 3);
4912 output = result.data();
4914 output[length++] = percent;
4915 output[length++] = QtMiscUtils::toHexUpper((c & 0xf0) >> 4);
4916 output[length++] = QtMiscUtils::toHexUpper(c & 0xf);
4920 result.truncate(length);
4925#if defined(Q_OS_WASM) || defined(Q_QDOC)
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4950QByteArray QByteArray::fromEcmaUint8Array(emscripten::val uint8array)
4952 return qstdweb::Uint8Array(uint8array).copyToQByteArray();
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972emscripten::val QByteArray::toEcmaUint8Array()
4974 return qstdweb::Uint8Array::copyFrom(*
this).val();
4980
4981
4984
4985
4988
4989
4990
4991
4992
4995
4996
4997
4998
4999
5002
5003
5004
5005
5006
5007
5010
5011
5012
5013
5014
5015
5018
5019
5022
5023
5026
5027
5030
5031
5034
5035
5038
5039
5042
5043
5046
5047
5048
5051
5052
5053
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5075#if QT_DEPRECATED_SINCE(6
, 8
)
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5151
5152
5153
5154
5157
5158
5159
5160
5161
5164
5165
5166
5167
5168
5169
5172
5173
5174
5175
5178
5179
5180
5181
5182
5183
5184
5185
5188
5189
5190
5191
5192
5195
5196
5197size_t
qHash(
const QByteArray::FromBase64Result &key, size_t seed)
noexcept
5199 return qHashMulti(seed, key.decoded,
static_cast<
int>(key.decodingStatus));
5203
5204
5205
5206
5207
5208
5209
5210
5213
5214
5215
5216
5217
5218
5219
5220
5221
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)