summaryrefslogtreecommitdiffstats
path: root/src/corelib/doc/snippets
diff options
context:
space:
mode:
authorOleksii Zbykovskyi <Oleksii.Zbykovskyi@qt.io>2025-07-25 14:57:33 +0200
committerOleksii Zbykovskyi <Oleksii.Zbykovskyi@qt.io>2025-07-25 20:36:21 +0200
commit96caebed5085b2d6a4ceec2d08cbd57939e4669c (patch)
tree45df7071cc7749f9a6c7632219eba7a569ec2632 /src/corelib/doc/snippets
parent72dcdf9b66bf5f66e4bd392b27fc24510b2ee298 (diff)
Fix files under text prefix and add to the build system
Made each file under the text prefix compilable. Task-number: QTBUG-137566 Change-Id: I285ff866ac057c2997345742515e8d2cf21cec4b Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/corelib/doc/snippets')
-rw-r--r--src/corelib/doc/snippets/code/CMakeLists.txt12
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_text_qanystringview.cpp15
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_text_qbytearray.cpp1033
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_text_qbytearraymatcher.cpp6
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_text_qbytearrayview.cpp47
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_text_qcollator.cpp3
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_text_qlocale.cpp152
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_text_qregularexpression.cpp660
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_text_qstaticlatin1stringmatcher.cpp15
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_text_qstring.cpp212
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_text_qstringconverter.cpp123
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_text_qstringview.cpp4
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_text_qutf8stringview.cpp2
13 files changed, 1251 insertions, 1033 deletions
diff --git a/src/corelib/doc/snippets/code/CMakeLists.txt b/src/corelib/doc/snippets/code/CMakeLists.txt
index a90562d7425..e75affd2bd7 100644
--- a/src/corelib/doc/snippets/code/CMakeLists.txt
+++ b/src/corelib/doc/snippets/code/CMakeLists.txt
@@ -53,6 +53,18 @@ add_library(corelib_snippets_code OBJECT
src_corelib_serialization_qcborvalue.cpp
src_corelib_serialization_qdatastream.cpp
src_corelib_serialization_qjsonobject.cpp
+ src_corelib_text_qanystringview.cpp
+ src_corelib_text_qbytearray.cpp
+ src_corelib_text_qbytearraymatcher.cpp
+ src_corelib_text_qbytearrayview.cpp
+ src_corelib_text_qcollator.cpp
+ src_corelib_text_qlocale.cpp
+ src_corelib_text_qregularexpression.cpp
+ src_corelib_text_qstaticlatin1stringmatcher.cpp
+ src_corelib_text_qstring.cpp
+ src_corelib_text_qstringconverter.cpp
+ src_corelib_text_qstringview.cpp
+ src_corelib_text_qutf8stringview.cpp
)
target_link_libraries(corelib_snippets_code PRIVATE
diff --git a/src/corelib/doc/snippets/code/src_corelib_text_qanystringview.cpp b/src/corelib/doc/snippets/code/src_corelib_text_qanystringview.cpp
index 370c6fd3e00..6685fff62b9 100644
--- a/src/corelib/doc/snippets/code/src_corelib_text_qanystringview.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_text_qanystringview.cpp
@@ -1,12 +1,19 @@
// Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+#include <QAnyStringView>
+
//! [0]
- void myfun1(QAnyStringView sv); // preferred
- void myfun2(const QAnyStringView &sv); // compiles and works, but slower
+void myfun1(QAnyStringView sv); // preferred
+void myfun2(const QAnyStringView &sv); // compiles and works, but slower
//! [0]
-//! [2]
+void examples()
+{
+ char array[] = "Hello, world!";
+
+ //! [2]
auto sv1 = QAnyStringView{std::begin(array), std::end(array) - 1}; // using C++11 std::begin()/std::end()
auto sv2 = QAnyStringView(array, std::size(array) - 1); // using C++17 std::size()
-//! [2]
+ //! [2]
+}
diff --git a/src/corelib/doc/snippets/code/src_corelib_text_qbytearray.cpp b/src/corelib/doc/snippets/code/src_corelib_text_qbytearray.cpp
index ff51d27d05e..12e03b01679 100644
--- a/src/corelib/doc/snippets/code/src_corelib_text_qbytearray.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_text_qbytearray.cpp
@@ -1,481 +1,572 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+#include <QByteArray>
+#include <QDataStream>
+#include <QIODevice>
+#include <iostream>
+
+using namespace std;
void wrapInFunction()
{
+ {
+ //! [0]
+ QByteArray ba("Hello");
+ //! [0]
+ }
+
+ {
+ //! [1]
+ QByteArray ba;
+ ba.resize(5);
+ ba[0] = 0x3c;
+ ba[1] = 0xb8;
+ ba[2] = 0x64;
+ ba[3] = 0x18;
+ ba[4] = 0xca;
+ //! [1]
+ }
+
+ {
+ QByteArray ba("");
+
+ //! [2]
+ for (qsizetype i = 0; i < ba.size(); ++i) {
+ if (ba.at(i) >= 'a' && ba.at(i) <= 'f')
+ cout << "Found character in range [a-f]" << endl;
+ }
+ //! [2]
+ }
+
+ {
+ //! [3]
+ QByteArray x("and");
+ x.prepend("rock "); // x == "rock and"
+ x.append(" roll"); // x == "rock and roll"
+ x.replace(5, 3, "&"); // x == "rock & roll"
+ //! [3]
+ }
+
+ {
+ //! [4]
+ QByteArray ba("We must be <b>bold</b>, very <b>bold</b>");
+ qsizetype j = 0;
+ while ((j = ba.indexOf("<b>", j)) != -1) {
+ cout << "Found <b> tag at index position " << j << endl;
+ ++j;
+ }
+ //! [4]
+ }
+
+ {
+ //! [5]
+ QByteArray().isNull(); // returns true
+ QByteArray().isEmpty(); // returns true
+
+ QByteArray("").isNull(); // returns false
+ QByteArray("").isEmpty(); // returns true
+
+ QByteArray("abc").isNull(); // returns false
+ QByteArray("abc").isEmpty(); // returns false
+ //! [5]
+ }
+
+ {
+ //! [6]
+ QByteArray ba("Hello");
+ qsizetype n = ba.size(); // n == 5
+ ba.data()[0]; // returns 'H'
+ ba.data()[4]; // returns 'o'
+ ba.data()[5]; // returns '\0'
+ //! [6]
+ }
+
+ {
+ //! [7]
+ QByteArray().isEmpty(); // returns true
+ QByteArray("").isEmpty(); // returns true
+ QByteArray("abc").isEmpty(); // returns false
+ //! [7]
+ }
+
+ {
+ //! [8]
+ QByteArray ba("Hello world");
+ char *data = ba.data();
+ while (*data) {
+ cout << "[" << *data << "]" << endl;
+ ++data;
+ }
+ //! [8]
+ }
+
+ {
+ //! [9]
+ QByteArray ba("Hello, world");
+ cout << ba[0]; // prints H
+ ba[7] = 'W';
+ // ba == "Hello, World"
+ //! [9]
+ }
+
+ {
+ //! [10]
+ QByteArray ba("Stockholm");
+ ba.truncate(5); // ba == "Stock"
+ //! [10]
+ }
+
+ {
+ //! [11]
+ QByteArray ba("STARTTLS\r\n");
+ ba.chop(2); // ba == "STARTTLS"
+ //! [11]
+ }
+
+ {
+ //! [12]
+ QByteArray x("free");
+ QByteArray y("dom");
+ x += y;
+ // x == "freedom"
+ //! [12]
+ }
+
+ {
+ //! [13]
+ QByteArray().isNull(); // returns true
+ QByteArray("").isNull(); // returns false
+ QByteArray("abc").isNull(); // returns false
+ //! [13]
+ }
+
+ {
+ //! [14]
+ QByteArray ba("Istambul");
+ ba.fill('o');
+ // ba == "oooooooo"
+
+ ba.fill('X', 2);
+ // ba == "XX"
+ //! [14]
+ }
+
+ {
+ //! [15]
+ QByteArray x("ship");
+ QByteArray y("air");
+ x.prepend(y);
+ // x == "airship"
+ //! [15]
+ }
+
+ {
+ //! [16]
+ QByteArray x("free");
+ QByteArray y("dom");
+ x.append(y);
+ // x == "freedom"
+ //! [16]
+ }
+
+ {
+ //! [17]
+ QByteArray ba("Meal");
+ ba.insert(1, QByteArrayView("ontr"));
+ // ba == "Montreal"
+ //! [17]
+ }
+
+ {
+ //! [18]
+ QByteArray ba("Montreal");
+ ba.remove(1, 4);
+ // ba == "Meal"
+ //! [18]
+ }
+
+ {
+ //! [19]
+ QByteArray x("Say yes!");
+ QByteArray y("no");
+ x.replace(4, 3, y);
+ // x == "Say no!"
+ //! [19]
+ }
+
+ {
+ //! [20]
+ QByteArray ba("colour behaviour flavour neighbour");
+ ba.replace(QByteArray("ou"), QByteArray("o"));
+ // ba == "color behavior flavor neighbor"
+ //! [20]
+ }
+
+ {
+ //! [21]
+ QByteArray x("sticky question");
+ QByteArrayView y("sti");
+ x.indexOf(y); // returns 0
+ x.indexOf(y, 1); // returns 10
+ x.indexOf(y, 10); // returns 10
+ x.indexOf(y, 11); // returns -1
+ //! [21]
+ }
+
+ {
+ //! [22]
+ QByteArray ba("ABCBA");
+ ba.indexOf("B"); // returns 1
+ ba.indexOf("B", 1); // returns 1
+ ba.indexOf("B", 2); // returns 3
+ ba.indexOf("X"); // returns -1
+ //! [22]
+ }
+
+ {
+ //! [23]
+ QByteArray x("crazy azimuths");
+ QByteArrayView y("az");
+ x.lastIndexOf(y); // returns 6
+ x.lastIndexOf(y, 6); // returns 6
+ x.lastIndexOf(y, 5); // returns 2
+ x.lastIndexOf(y, 1); // returns -1
+ //! [23]
+ }
+
+ {
+ //! [24]
+ QByteArray ba("ABCBA");
+ ba.lastIndexOf("B"); // returns 3
+ ba.lastIndexOf("B", 3); // returns 3
+ ba.lastIndexOf("B", 2); // returns 1
+ ba.lastIndexOf("X"); // returns -1
+ //! [24]
+ }
+
+ {
+ //! [25]
+ QByteArray url("ftp://ftp.qt-project.org/");
+ if (url.startsWith("ftp:"))
+ {/*...*/}
+ //! [25]
+ }
+
+ {
+ //! [26]
+ QByteArray url("http://qt-project.org/doc/qt-5.0/qtdoc/index.html");
+ if (url.endsWith(".html"))
+ {/*...*/}
+ //! [26]
+ }
+
+ {
+ //! [27]
+ QByteArray x("Pineapple");
+ QByteArray y = x.first(4);
+ // y == "Pine"
+ //! [27]
+ }
+
+ {
+ //! [28]
+ QByteArray x("Pineapple");
+ QByteArray y = x.last(5);
+ // y == "apple"
+ //! [28]
+ }
+
+ {
+ //! [29]
+ QByteArray x("Five pineapples");
+ QByteArray y = x.sliced(5, 4); // y == "pine"
+ QByteArray z = x.sliced(5); // z == "pineapples"
+ //! [29]
+ }
+
+ {
+ //! [30]
+ QByteArray x("Qt by THE QT COMPANY");
+ QByteArray y = x.toLower();
+ // y == "qt by the qt company"
+ //! [30]
+ }
+
+ {
+ //! [31]
+ QByteArray x("Qt by THE QT COMPANY");
+ QByteArray y = x.toUpper();
+ // y == "QT BY THE QT COMPANY"
+ //! [31]
+ }
+
+ {
+ //! [32]
+ QByteArray ba(" lots\t of\nwhitespace\r\n ");
+ ba = ba.simplified();
+ // ba == "lots of whitespace";
+ //! [32]
+ }
+
+ {
+ //! [33]
+ QByteArray ba(" lots\t of\nwhitespace\r\n ");
+ ba = ba.trimmed();
+ // ba == "lots\t of\nwhitespace";
+ //! [33]
+ }
+
+ {
+ //! [34]
+ QByteArray x("apple");
+ QByteArray y = x.leftJustified(8, '.'); // y == "apple..."
+ //! [34]
+ }
+
+ {
+ //! [35]
+ QByteArray x("apple");
+ QByteArray y = x.rightJustified(8, '.'); // y == "...apple"
+ //! [35]
+ }
+
+ {
+ //! [36]
+ QByteArray str("FF");
+ bool ok;
+ int hex = str.toInt(&ok, 16); // hex == 255, ok == true
+ int dec = str.toInt(&ok, 10); // dec == 0, ok == false
+ //! [36]
+ }
+
+ {
+ //! [37]
+ QByteArray str("FF");
+ bool ok;
+ long hex = str.toLong(&ok, 16); // hex == 255, ok == true
+ long dec = str.toLong(&ok, 10); // dec == 0, ok == false
+ //! [37]
+ }
+
+ {
+ //! [38]
+ QByteArray string("1234.56");
+ bool ok;
+ double a = string.toDouble(&ok); // a == 1234.56, ok == true
+
+ string = "1234.56 Volt";
+ a = string.toDouble(&ok); // a == 0, ok == false
+ //! [38]
+ }
+
+ {
+ //! [38float]
+ QByteArray string("1234.56");
+ bool ok;
+ float a = string.toFloat(&ok); // a == 1234.56, ok == true
+
+ string = "1234.56 Volt";
+ a = string.toFloat(&ok); // a == 0, ok == false
+ //! [38float]
+ }
+
+ {
+ //! [39]
+ QByteArray text("Qt is great!");
+ text.toBase64(); // returns "UXQgaXMgZ3JlYXQh"
+
+ QByteArray hello("<p>Hello?</p>");
+ hello.toBase64(QByteArray::Base64Encoding | QByteArray::OmitTrailingEquals); // returns "PHA+SGVsbG8/PC9wPg"
+ hello.toBase64(QByteArray::Base64Encoding); // returns "PHA+SGVsbG8/PC9wPg=="
+ hello.toBase64(QByteArray::Base64UrlEncoding); // returns "PHA-SGVsbG8_PC9wPg=="
+ hello.toBase64(QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals); // returns "PHA-SGVsbG8_PC9wPg"
+ //! [39]
+ }
+
+ {
+ //! [40]
+ QByteArray ba;
+ int n = 63;
+ ba.setNum(n); // ba == "63"
+ ba.setNum(n, 16); // ba == "3f"
+ //! [40]
+ }
+
+ {
+ //! [41]
+ int n = 63;
+ QByteArray::number(n); // returns "63"
+ QByteArray::number(n, 16); // returns "3f"
+ QByteArray::number(n, 16).toUpper(); // returns "3F"
+ //! [41]
+ }
+
+ {
+ //! [42]
+ QByteArray ba = QByteArray::number(12.3456, 'E', 3);
+ // ba == 1.235E+01
+ //! [42]
+ }
+
+ {
+ //! [43]
+ static const char mydata[] = {
+ '\x00', '\x00', '\x03', '\x84', '\x78', '\x9c', '\x3b', '\x76',
+ '\xec', '\x18', '\xc3', '\x31', '\x0a', '\xf1', '\xcc', '\x99',
+ //...
+ '\x6d', '\x5b'
+ };
+
+ QByteArray data = QByteArray::fromRawData(mydata, sizeof(mydata));
+ QDataStream in(&data, QIODevice::ReadOnly);
+ //...
+ //! [43]
+ }
+
+ {
+ //! [44]
+ QByteArray text = QByteArray::fromBase64("UXQgaXMgZ3JlYXQh");
+ text.data(); // returns "Qt is great!"
+
+ QByteArray::fromBase64("PHA+SGVsbG8/PC9wPg==", QByteArray::Base64Encoding); // returns "<p>Hello?</p>"
+ QByteArray::fromBase64("PHA-SGVsbG8_PC9wPg==", QByteArray::Base64UrlEncoding); // returns "<p>Hello?</p>"
+ //! [44]
+ }
+
+ QByteArray encodedData;
+
+ {
+ //! [44ter]
+ void process(const QByteArray &);
+
+ if (auto result = QByteArray::fromBase64Encoding(encodedData))
+ process(*result);
+ //! [44ter]
+
+ //! [44quater]
+ auto result = QByteArray::fromBase64Encoding(encodedData);
+ if (result.decodingStatus == QByteArray::Base64DecodingStatus::Ok)
+ process(result.decoded);
+ //! [44quater]
+ }
+
+ {
+ //! [45]
+ QByteArray text = QByteArray::fromHex("517420697320677265617421");
+ text.data(); // returns "Qt is great!"
+ //! [45]
+ }
+
+ {
+ //! [46]
+ QString tmp = "test";
+ QByteArray text = tmp.toLocal8Bit();
+ char *data = new char[text.size()];
+ strcpy(data, text.data());
+ delete [] data;
+ //! [46]
+ }
+
+ {
+ //! [47]
+ QString tmp = "test";
+ QByteArray text = tmp.toLocal8Bit();
+ char *data = new char[text.size() + 1];
+ strcpy(data, text.data());
+ delete [] data;
+ //! [47]
+ }
+
+ {
+ //! [48]
+ QByteArray ba1("ca\0r\0t");
+ ba1.size(); // Returns 2.
+ ba1.constData(); // Returns "ca" with terminating \0.
+
+ QByteArray ba2("ca\0r\0t", 3);
+ ba2.size(); // Returns 3.
+ ba2.constData(); // Returns "ca\0" with terminating \0.
+
+ QByteArray ba3("ca\0r\0t", 4);
+ ba3.size(); // Returns 4.
+ ba3.constData(); // Returns "ca\0r" with terminating \0.
+
+ const char cart[] = {'c', 'a', '\0', 'r', '\0', 't'};
+ QByteArray ba4(QByteArray::fromRawData(cart, 6));
+ ba4.size(); // Returns 6.
+ ba4.constData(); // Returns "ca\0r\0t" without terminating \0.
+ //! [48]
+ }
+
+ {
+ //! [49]
+ QByteArray ba("ab");
+ ba.repeated(4); // returns "abababab"
+ //! [49]
+ }
+
+ {
+ //! [50]
+ QByteArray macAddress = QByteArray::fromHex("123456abcdef");
+ macAddress.toHex(':'); // returns "12:34:56:ab:cd:ef"
+ macAddress.toHex(0); // returns "123456abcdef"
+ //! [50]
+ }
+
+ {
+ //! [51]
+ QByteArray text = QByteArray::fromPercentEncoding("Qt%20is%20great%33");
+ qDebug("%s", text.data()); // reports "Qt is great!"
+ //! [51]
+ }
+
+ {
+ //! [52]
+ QByteArray text = "{a fishy string?}";
+ QByteArray ba = text.toPercentEncoding("{}", "s");
+ qDebug("%s", ba.constData());
+ // prints "{a fi%73hy %73tring%3F}"
+ //! [52]
+ }
+
+ {
+ //! [53]
+ QByteArray ba = QByteArrayLiteral("byte array contents");
+ //! [53]
+ }
+
+ {
+ //! [54]
+ QByteArray encoded("Qt%20is%20great%33");
+ QByteArray decoded = encoded.percentDecoded(); // Set to "Qt is great!"
+ //! [54]
+ }
+
+ using namespace Qt::StringLiterals;
+
+ {
+ //! [57]
+ QByteArray x = "Five pineapples"_ba;
+ x.slice(5); // x == "pineapples"
+ x.slice(4, 3); // x == "app"
+ //! [57]
+ }
-//! [0]
-QByteArray ba("Hello");
-//! [0]
-
-
-//! [1]
-QByteArray ba;
-ba.resize(5);
-ba[0] = 0x3c;
-ba[1] = 0xb8;
-ba[2] = 0x64;
-ba[3] = 0x18;
-ba[4] = 0xca;
-//! [1]
-
-
-//! [2]
-for (qsizetype i = 0; i < ba.size(); ++i) {
- if (ba.at(i) >= 'a' && ba.at(i) <= 'f')
- cout << "Found character in range [a-f]" << endl;
-}
-//! [2]
-
-
-//! [3]
-QByteArray x("and");
-x.prepend("rock "); // x == "rock and"
-x.append(" roll"); // x == "rock and roll"
-x.replace(5, 3, "&"); // x == "rock & roll"
-//! [3]
-
-
-//! [4]
-QByteArray ba("We must be <b>bold</b>, very <b>bold</b>");
-qsizetype j = 0;
-while ((j = ba.indexOf("<b>", j)) != -1) {
- cout << "Found <b> tag at index position " << j << endl;
- ++j;
-}
-//! [4]
-
-
-//! [5]
-QByteArray().isNull(); // returns true
-QByteArray().isEmpty(); // returns true
-
-QByteArray("").isNull(); // returns false
-QByteArray("").isEmpty(); // returns true
-
-QByteArray("abc").isNull(); // returns false
-QByteArray("abc").isEmpty(); // returns false
-//! [5]
-
-
-//! [6]
-QByteArray ba("Hello");
-qsizetype n = ba.size(); // n == 5
-ba.data()[0]; // returns 'H'
-ba.data()[4]; // returns 'o'
-ba.data()[5]; // returns '\0'
-//! [6]
-
-
-//! [7]
-QByteArray().isEmpty(); // returns true
-QByteArray("").isEmpty(); // returns true
-QByteArray("abc").isEmpty(); // returns false
-//! [7]
-
-
-//! [8]
-QByteArray ba("Hello world");
-char *data = ba.data();
-while (*data) {
- cout << "[" << *data << "]" << endl;
- ++data;
}
-//! [8]
-
-
-//! [9]
-QByteArray ba("Hello, world");
-cout << ba[0]; // prints H
-ba[7] = 'W';
-// ba == "Hello, World"
-//! [9]
-
-
-//! [10]
-QByteArray ba("Stockholm");
-ba.truncate(5); // ba == "Stock"
-//! [10]
-
-
-//! [11]
-QByteArray ba("STARTTLS\r\n");
-ba.chop(2); // ba == "STARTTLS"
-//! [11]
-
-
-//! [12]
-QByteArray x("free");
-QByteArray y("dom");
-x += y;
-// x == "freedom"
-//! [12]
-
-
-//! [13]
-QByteArray().isNull(); // returns true
-QByteArray("").isNull(); // returns false
-QByteArray("abc").isNull(); // returns false
-//! [13]
-
-
-//! [14]
-QByteArray ba("Istambul");
-ba.fill('o');
-// ba == "oooooooo"
-
-ba.fill('X', 2);
-// ba == "XX"
-//! [14]
-
-
-//! [15]
-QByteArray x("ship");
-QByteArray y("air");
-x.prepend(y);
-// x == "airship"
-//! [15]
-
-
-//! [16]
-QByteArray x("free");
-QByteArray y("dom");
-x.append(y);
-// x == "freedom"
-//! [16]
-
-
-//! [17]
-QByteArray ba("Meal");
-ba.insert(1, QByteArrayView("ontr"));
-// ba == "Montreal"
-//! [17]
-
-
-//! [18]
-QByteArray ba("Montreal");
-ba.remove(1, 4);
-// ba == "Meal"
-//! [18]
-
-
-//! [19]
-QByteArray x("Say yes!");
-QByteArray y("no");
-x.replace(4, 3, y);
-// x == "Say no!"
-//! [19]
-
-
-//! [20]
-QByteArray ba("colour behaviour flavour neighbour");
-ba.replace(QByteArray("ou"), QByteArray("o"));
-// ba == "color behavior flavor neighbor"
-//! [20]
-
-
-//! [21]
-QByteArray x("sticky question");
-QByteArrayView y("sti");
-x.indexOf(y); // returns 0
-x.indexOf(y, 1); // returns 10
-x.indexOf(y, 10); // returns 10
-x.indexOf(y, 11); // returns -1
-//! [21]
-
-
-//! [22]
-QByteArray ba("ABCBA");
-ba.indexOf("B"); // returns 1
-ba.indexOf("B", 1); // returns 1
-ba.indexOf("B", 2); // returns 3
-ba.indexOf("X"); // returns -1
-//! [22]
-
-
-//! [23]
-QByteArray x("crazy azimuths");
-QByteArrayView y("az");
-x.lastIndexOf(y); // returns 6
-x.lastIndexOf(y, 6); // returns 6
-x.lastIndexOf(y, 5); // returns 2
-x.lastIndexOf(y, 1); // returns -1
-//! [23]
-
-
-//! [24]
-QByteArray ba("ABCBA");
-ba.lastIndexOf("B"); // returns 3
-ba.lastIndexOf("B", 3); // returns 3
-ba.lastIndexOf("B", 2); // returns 1
-ba.lastIndexOf("X"); // returns -1
-//! [24]
-
-
-//! [25]
-QByteArray url("ftp://ftp.qt-project.org/");
-if (url.startsWith("ftp:"))
- ...
-//! [25]
-
-
-//! [26]
-QByteArray url("http://qt-project.org/doc/qt-5.0/qtdoc/index.html");
-if (url.endsWith(".html"))
- ...
-//! [26]
-
-
-//! [27]
-QByteArray x("Pineapple");
-QByteArray y = x.first(4);
-// y == "Pine"
-//! [27]
-
-
-//! [28]
-QByteArray x("Pineapple");
-QByteArray y = x.last(5);
-// y == "apple"
-//! [28]
-
-
-//! [29]
-QByteArray x("Five pineapples");
-QByteArray y = x.sliced(5, 4); // y == "pine"
-QByteArray z = x.sliced(5); // z == "pineapples"
-//! [29]
-
-
-//! [30]
-QByteArray x("Qt by THE QT COMPANY");
-QByteArray y = x.toLower();
-// y == "qt by the qt company"
-//! [30]
-
-
-//! [31]
-QByteArray x("Qt by THE QT COMPANY");
-QByteArray y = x.toUpper();
-// y == "QT BY THE QT COMPANY"
-//! [31]
-
-
-//! [32]
-QByteArray ba(" lots\t of\nwhitespace\r\n ");
-ba = ba.simplified();
-// ba == "lots of whitespace";
-//! [32]
-
-
-//! [33]
-QByteArray ba(" lots\t of\nwhitespace\r\n ");
-ba = ba.trimmed();
-// ba == "lots\t of\nwhitespace";
-//! [33]
-
-
-//! [34]
-QByteArray x("apple");
-QByteArray y = x.leftJustified(8, '.'); // y == "apple..."
-//! [34]
-
-
-//! [35]
-QByteArray x("apple");
-QByteArray y = x.rightJustified(8, '.'); // y == "...apple"
-//! [35]
-
-
-//! [36]
-QByteArray str("FF");
-bool ok;
-int hex = str.toInt(&ok, 16); // hex == 255, ok == true
-int dec = str.toInt(&ok, 10); // dec == 0, ok == false
-//! [36]
-
-
-//! [37]
-QByteArray str("FF");
-bool ok;
-long hex = str.toLong(&ok, 16); // hex == 255, ok == true
-long dec = str.toLong(&ok, 10); // dec == 0, ok == false
-//! [37]
-
-
-//! [38]
-QByteArray string("1234.56");
-bool ok;
-double a = string.toDouble(&ok); // a == 1234.56, ok == true
-
-string = "1234.56 Volt";
-a = str.toDouble(&ok); // a == 0, ok == false
-//! [38]
-
-//! [38float]
-QByteArray string("1234.56");
-bool ok;
-float a = string.toFloat(&ok); // a == 1234.56, ok == true
-
-string = "1234.56 Volt";
-a = str.toFloat(&ok); // a == 0, ok == false
-//! [38float]
-
-//! [39]
-QByteArray text("Qt is great!");
-text.toBase64(); // returns "UXQgaXMgZ3JlYXQh"
-
-QByteArray text("<p>Hello?</p>");
-text.toBase64(QByteArray::Base64Encoding | QByteArray::OmitTrailingEquals); // returns "PHA+SGVsbG8/PC9wPg"
-text.toBase64(QByteArray::Base64Encoding); // returns "PHA+SGVsbG8/PC9wPg=="
-text.toBase64(QByteArray::Base64UrlEncoding); // returns "PHA-SGVsbG8_PC9wPg=="
-text.toBase64(QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals); // returns "PHA-SGVsbG8_PC9wPg"
-//! [39]
-
-//! [40]
-QByteArray ba;
-int n = 63;
-ba.setNum(n); // ba == "63"
-ba.setNum(n, 16); // ba == "3f"
-//! [40]
-
-
-//! [41]
-int n = 63;
-QByteArray::number(n); // returns "63"
-QByteArray::number(n, 16); // returns "3f"
-QByteArray::number(n, 16).toUpper(); // returns "3F"
-//! [41]
-
-
-//! [42]
-QByteArray ba = QByteArray::number(12.3456, 'E', 3);
-// ba == 1.235E+01
-//! [42]
-
-
-//! [43]
- static const char mydata[] = {
- '\x00', '\x00', '\x03', '\x84', '\x78', '\x9c', '\x3b', '\x76',
- '\xec', '\x18', '\xc3', '\x31', '\x0a', '\xf1', '\xcc', '\x99',
- ...
- '\x6d', '\x5b'
-};
-
-QByteArray data = QByteArray::fromRawData(mydata, sizeof(mydata));
-QDataStream in(&data, QIODevice::ReadOnly);
-...
-//! [43]
-
-
-//! [44]
-QByteArray text = QByteArray::fromBase64("UXQgaXMgZ3JlYXQh");
-text.data(); // returns "Qt is great!"
-
-QByteArray::fromBase64("PHA+SGVsbG8/PC9wPg==", QByteArray::Base64Encoding); // returns "<p>Hello?</p>"
-QByteArray::fromBase64("PHA-SGVsbG8_PC9wPg==", QByteArray::Base64UrlEncoding); // returns "<p>Hello?</p>"
-//! [44]
-
-//! [44ter]
-void process(const QByteArray &);
-
-if (auto result = QByteArray::fromBase64Encoding(encodedData))
- process(*result);
-//! [44ter]
-
-//! [44quater]
-auto result = QByteArray::fromBase64Encoding(encodedData);
-if (result.decodingStatus == QByteArray::Base64DecodingStatus::Ok)
- process(result.decoded);
-//! [44quater]
-
-//! [45]
-QByteArray text = QByteArray::fromHex("517420697320677265617421");
-text.data(); // returns "Qt is great!"
-//! [45]
-
-//! [46]
-QString tmp = "test";
-QByteArray text = tmp.toLocal8Bit();
-char *data = new char[text.size()];
-strcpy(data, text.data());
-delete [] data;
-//! [46]
-
-//! [47]
-QString tmp = "test";
-QByteArray text = tmp.toLocal8Bit();
-char *data = new char[text.size() + 1];
-strcpy(data, text.data());
-delete [] data;
-//! [47]
-
-//! [48]
-QByteArray ba1("ca\0r\0t");
-ba1.size(); // Returns 2.
-ba1.constData(); // Returns "ca" with terminating \0.
-
-QByteArray ba2("ca\0r\0t", 3);
-ba2.size(); // Returns 3.
-ba2.constData(); // Returns "ca\0" with terminating \0.
-
-QByteArray ba3("ca\0r\0t", 4);
-ba3.size(); // Returns 4.
-ba3.constData(); // Returns "ca\0r" with terminating \0.
-
-const char cart[] = {'c', 'a', '\0', 'r', '\0', 't'};
-QByteArray ba4(QByteArray::fromRawData(cart, 6));
-ba4.size(); // Returns 6.
-ba4.constData(); // Returns "ca\0r\0t" without terminating \0.
-//! [48]
-
-//! [49]
-QByteArray ba("ab");
-ba.repeated(4); // returns "abababab"
-//! [49]
-
-//! [50]
-QByteArray macAddress = QByteArray::fromHex("123456abcdef");
-macAddress.toHex(':'); // returns "12:34:56:ab:cd:ef"
-macAddress.toHex(0); // returns "123456abcdef"
-//! [50]
-
-//! [51]
-QByteArray text = QByteArray::fromPercentEncoding("Qt%20is%20great%33");
-qDebug("%s", text.data()); // reports "Qt is great!"
-//! [51]
-
-//! [52]
-QByteArray text = "{a fishy string?}";
-QByteArray ba = text.toPercentEncoding("{}", "s");
-qDebug("%s", ba.constData());
-// prints "{a fi%73hy %73tring%3F}"
-//! [52]
-
-//! [53]
-QByteArray ba = QByteArrayLiteral("byte array contents");
-//! [53]
-
-//! [54]
-QByteArray encoded("Qt%20is%20great%33");
-QByteArray decoded = encoded.percentDecoded(); // Set to "Qt is great!"
-//! [54]
-
-//! [55]
-emscripten::val uint8array = emscripten::val::global("g_uint8array");
-QByteArray byteArray = QByteArray::fromEcmaUint8Array(uint8array);
-//! [55]
-
-//! [56]
-QByteArray byteArray = "test";
-emscripten::val uint8array = byteArray.toEcmaUint8Array();
-//! [56]
-
-//! [57]
-QByteArray x = "Five pineapples"_ba;
-x.slice(5); // x == "pineapples"
-x.slice(4, 3); // x == "app"
-//! [57]
+# ifdef EMSCRIPTEN
+#include <emscripten/val.h>
+void emscripten_examples()
+{
+ //! [55]
+ emscripten::val uint8array = emscripten::val::global("g_uint8array");
+ QByteArray byteArray = QByteArray::fromEcmaUint8Array(uint8array);
+ //! [55]
+
+ //! [56]
+ QByteArray byteArray = "test";
+ emscripten::val uint8array = byteArray.toEcmaUint8Array();
+ //! [56]
}
+# endif
diff --git a/src/corelib/doc/snippets/code/src_corelib_text_qbytearraymatcher.cpp b/src/corelib/doc/snippets/code/src_corelib_text_qbytearraymatcher.cpp
index 3d750c44ff8..c142c127dd9 100644
--- a/src/corelib/doc/snippets/code/src_corelib_text_qbytearraymatcher.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_text_qbytearraymatcher.cpp
@@ -1,10 +1,8 @@
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+#include <QByteArrayMatcher>
+
//! [0]
static const auto matcher = qMakeStaticByteArrayMatcher("needle");
//! [0]
-
-//! [1]
-static const auto matcher = qMakeStaticByteArrayMatcher("needle");
-//! [1]
diff --git a/src/corelib/doc/snippets/code/src_corelib_text_qbytearrayview.cpp b/src/corelib/doc/snippets/code/src_corelib_text_qbytearrayview.cpp
index 4457a71ae59..10295c44f0a 100644
--- a/src/corelib/doc/snippets/code/src_corelib_text_qbytearrayview.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_text_qbytearrayview.cpp
@@ -1,6 +1,6 @@
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-#include <QtCore/qbytearrayview.h>
+#include <QByteArrayView>
//! [0]
void myfun1(QByteArrayView bv); // preferred
@@ -12,23 +12,32 @@
void fun(char ch) { fun(QByteArrayView(&ch, 1)); }
//! [1]
-//! [2]
-QByteArrayView str("FF");
-bool ok;
-int hex = str.toInt(&ok, 16); // hex == 255, ok == true
-int dec = str.toInt(&ok, 10); // dec == 0, ok == false
-//! [2]
+void examples()
+{
+ {
+ //! [2]
+ QByteArrayView str("FF");
+ bool ok;
+ int hex = str.toInt(&ok, 16); // hex == 255, ok == true
+ int dec = str.toInt(&ok, 10); // dec == 0, ok == false
+ //! [2]
+ }
-//! [3]
-QByteArrayView str("FF");
-bool ok;
-long hex = str.toLong(&ok, 16); // hex == 255, ok == true
-long dec = str.toLong(&ok, 10); // dec == 0, ok == false
-//! [3]
+ {
+ //! [3]
+ QByteArrayView str("FF");
+ bool ok;
+ long hex = str.toLong(&ok, 16); // hex == 255, ok == true
+ long dec = str.toLong(&ok, 10); // dec == 0, ok == false
+ //! [3]
+ }
-//! [4]
-QByteArrayView string("1234.56 Volt");
-bool ok;
-float a = str.toFloat(&ok); // a == 0, ok == false
-a = string.first(7).toFloat(&ok); // a == 1234.56, ok == true
-//! [4]
+ {
+ //! [4]
+ QByteArrayView string("1234.56 Volt");
+ bool ok;
+ float a = string.toFloat(&ok); // a == 0, ok == false
+ a = string.first(7).toFloat(&ok); // a == 1234.56, ok == true
+ //! [4]
+ }
+}
diff --git a/src/corelib/doc/snippets/code/src_corelib_text_qcollator.cpp b/src/corelib/doc/snippets/code/src_corelib_text_qcollator.cpp
index 91da052bf75..a1d2a7f421b 100644
--- a/src/corelib/doc/snippets/code/src_corelib_text_qcollator.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_text_qcollator.cpp
@@ -1,6 +1,9 @@
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+#include <QStringList>
+#include <QCollator>
+
//! [0]
QStringList sortedStrings(QStringList seq)
{
diff --git a/src/corelib/doc/snippets/code/src_corelib_text_qlocale.cpp b/src/corelib/doc/snippets/code/src_corelib_text_qlocale.cpp
index c3c41b250bb..ce76bbfcc4a 100644
--- a/src/corelib/doc/snippets/code/src_corelib_text_qlocale.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_text_qlocale.cpp
@@ -1,73 +1,85 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-//! [0]
-QLocale egyptian(QLocale::Arabic, QLocale::Egypt);
-QString s1 = egyptian.toString(1.571429E+07, 'e');
-QString s2 = egyptian.toString(10);
-
-double d = egyptian.toDouble(s1);
-int i = egyptian.toInt(s2);
-//! [0]
-
-
-//! [1]
-bool ok;
-double d;
-
-QLocale::setDefault(QLocale::C); // uses '.' as a decimal point
-QLocale cLocale; // default-constructed C locale
-d = cLocale.toDouble("1234,56", &ok); // ok == false, d == 0
-d = cLocale.toDouble("1234.56", &ok); // ok == true, d == 1234.56
-
-QLocale::setDefault(QLocale::German); // uses ',' as a decimal point
-QLocale german; // default-constructed German locale
-d = german.toDouble("1234,56", &ok); // ok == true, d == 1234.56
-d = german.toDouble("1234.56", &ok); // ok == false, d == 0
-
-QLocale::setDefault(QLocale::English);
-// Default locale now uses ',' as a group separator.
-QString str = QString("%1 %L2 %L3").arg(12345).arg(12345).arg(12345, 0, 16);
-// str == "12345 12,345 3039"
-//! [1]
-
-
-//! [2]
-QLocale korean("ko");
-QLocale swiss("de_CH");
-//! [2]
-
-
-//! [3]
-bool ok;
-double d;
-
-QLocale c(QLocale::C);
-d = c.toDouble("1234.56", &ok); // ok == true, d == 1234.56
-d = c.toDouble("1,234.56", &ok); // ok == true, d == 1234.56
-d = c.toDouble("1234,56", &ok); // ok == false, d == 0
-
-QLocale german(QLocale::German);
-d = german.toDouble("1234,56", &ok); // ok == true, d == 1234.56
-d = german.toDouble("1.234,56", &ok); // ok == true, d == 1234.56
-d = german.toDouble("1234.56", &ok); // ok == false, d == 0
-
-d = german.toDouble("1.234", &ok); // ok == true, d == 1234.0
-//! [3]
-
-//! [3-qstringview]
-bool ok;
-double d;
-
-QLocale c(QLocale::C);
-d = c.toDouble(u"1234.56", &ok); // ok == true, d == 1234.56
-d = c.toDouble(u"1,234.56", &ok); // ok == true, d == 1234.56
-d = c.toDouble(u"1234,56", &ok); // ok == false, d == 0
-
-QLocale german(QLocale::German);
-d = german.toDouble(u"1234,56", &ok); // ok == true, d == 1234.56
-d = german.toDouble(u"1.234,56", &ok); // ok == true, d == 1234.56
-d = german.toDouble(u"1234.56", &ok); // ok == false, d == 0
-
-d = german.toDouble(u"1.234", &ok); // ok == true, d == 1234.0
-//! [3-qstringview]
+#include <QLocale>
+
+void wrapInFunction()
+{
+ {
+ //! [0]
+ QLocale egyptian(QLocale::Arabic, QLocale::Egypt);
+ QString s1 = egyptian.toString(1.571429E+07, 'e');
+ QString s2 = egyptian.toString(10);
+
+ double d = egyptian.toDouble(s1);
+ int i = egyptian.toInt(s2);
+ //! [0]
+ }
+
+ {
+ //! [1]
+ bool ok;
+ double d;
+
+ QLocale::setDefault(QLocale::C); // uses '.' as a decimal point
+ QLocale cLocale; // default-constructed C locale
+ d = cLocale.toDouble("1234,56", &ok); // ok == false, d == 0
+ d = cLocale.toDouble("1234.56", &ok); // ok == true, d == 1234.56
+
+ QLocale::setDefault(QLocale::German); // uses ',' as a decimal point
+ QLocale german; // default-constructed German locale
+ d = german.toDouble("1234,56", &ok); // ok == true, d == 1234.56
+ d = german.toDouble("1234.56", &ok); // ok == false, d == 0
+
+ QLocale::setDefault(QLocale::English);
+ // Default locale now uses ',' as a group separator.
+ QString str = QString("%1 %L2 %L3").arg(12345).arg(12345).arg(12345, 0, 16);
+ // str == "12345 12,345 3039"
+ //! [1]
+ }
+
+ {
+ //! [2]
+ QLocale korean("ko");
+ QLocale swiss("de_CH");
+ //! [2]
+ }
+
+ {
+ //! [3]
+ bool ok;
+ double d;
+
+ QLocale c(QLocale::C);
+ d = c.toDouble("1234.56", &ok); // ok == true, d == 1234.56
+ d = c.toDouble("1,234.56", &ok); // ok == true, d == 1234.56
+ d = c.toDouble("1234,56", &ok); // ok == false, d == 0
+
+ QLocale german(QLocale::German);
+ d = german.toDouble("1234,56", &ok); // ok == true, d == 1234.56
+ d = german.toDouble("1.234,56", &ok); // ok == true, d == 1234.56
+ d = german.toDouble("1234.56", &ok); // ok == false, d == 0
+
+ d = german.toDouble("1.234", &ok); // ok == true, d == 1234.0
+ //! [3]
+ }
+
+ {
+ //! [3-qstringview]
+ bool ok;
+ double d;
+
+ QLocale c(QLocale::C);
+ d = c.toDouble(u"1234.56", &ok); // ok == true, d == 1234.56
+ d = c.toDouble(u"1,234.56", &ok); // ok == true, d == 1234.56
+ d = c.toDouble(u"1234,56", &ok); // ok == false, d == 0
+
+ QLocale german(QLocale::German);
+ d = german.toDouble(u"1234,56", &ok); // ok == true, d == 1234.56
+ d = german.toDouble(u"1.234,56", &ok); // ok == true, d == 1234.56
+ d = german.toDouble(u"1234.56", &ok); // ok == false, d == 0
+
+ d = german.toDouble(u"1.234", &ok); // ok == true, d == 1234.0
+ //! [3-qstringview]
+ }
+}
diff --git a/src/corelib/doc/snippets/code/src_corelib_text_qregularexpression.cpp b/src/corelib/doc/snippets/code/src_corelib_text_qregularexpression.cpp
index 03fdda99879..23fc24042cc 100644
--- a/src/corelib/doc/snippets/code/src_corelib_text_qregularexpression.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_text_qregularexpression.cpp
@@ -9,334 +9,336 @@
int main() {
-{
-//! [0]
-QRegularExpression re("a pattern");
-//! [0]
-}
-
-{
-//! [1]
-QRegularExpression re;
-re.setPattern("another pattern");
-//! [1]
-}
-
-{
-//! [2]
-// matches two digits followed by a space and a word
-QRegularExpression re("\\d\\d \\w+");
-
-// matches a backslash
-QRegularExpression re2("\\\\");
-//! [2]
-}
-
-{
-//! [3]
-QRegularExpression re("a third pattern");
-QString pattern = re.pattern(); // pattern == "a third pattern"
-//! [3]
-}
-
-{
-//! [4]
-// matches "Qt rocks", but also "QT rocks", "QT ROCKS", "qT rOcKs", etc.
-QRegularExpression re("Qt rocks", QRegularExpression::CaseInsensitiveOption);
-//! [4]
-}
-
-{
-//! [5]
-QRegularExpression re("^\\d+$");
-re.setPatternOptions(QRegularExpression::MultilineOption);
-// re matches any line in the subject string that contains only digits (but at least one)
-//! [5]
-}
-
-{
-//! [6]
-QRegularExpression re = QRegularExpression("^two.*words$", QRegularExpression::MultilineOption
- | QRegularExpression::DotMatchesEverythingOption);
-
-QRegularExpression::PatternOptions options = re.patternOptions();
-// options == QRegularExpression::MultilineOption | QRegularExpression::DotMatchesEverythingOption
-//! [6]
-}
-
-{
-//! [7]
-// match two digits followed by a space and a word
-QRegularExpression re("\\d\\d \\w+");
-QRegularExpressionMatch match = re.match("abc123 def");
-bool hasMatch = match.hasMatch(); // true
-//! [7]
-}
-
-{
-//! [8]
-QRegularExpression re("\\d\\d \\w+");
-QRegularExpressionMatch match = re.match("abc123 def");
-if (match.hasMatch()) {
- QString matched = match.captured(0); // matched == "23 def"
- // ...
-}
-//! [8]
-}
-
-{
-//! [9]
-QRegularExpression re("\\d\\d \\w+");
-QRegularExpressionMatch match = re.match("12 abc 45 def", 1);
-if (match.hasMatch()) {
- QString matched = match.captured(0); // matched == "45 def"
- // ...
-}
-//! [9]
-}
-
-{
-//! [10]
-QRegularExpression re("^(\\d\\d)/(\\d\\d)/(\\d\\d\\d\\d)$");
-QRegularExpressionMatch match = re.match("08/12/1985");
-if (match.hasMatch()) {
- QString day = match.captured(1); // day == "08"
- QString month = match.captured(2); // month == "12"
- QString year = match.captured(3); // year == "1985"
- // ...
-}
-//! [10]
-}
-
-{
-//! [11]
-QRegularExpression re("abc(\\d+)def");
-QRegularExpressionMatch match = re.match("XYZabc123defXYZ");
-if (match.hasMatch()) {
- int startOffset = match.capturedStart(1); // startOffset == 6
- int endOffset = match.capturedEnd(1); // endOffset == 9
- // ...
-}
-//! [11]
-}
-
-{
-//! [12]
-QRegularExpression re("^(?<date>\\d\\d)/(?<month>\\d\\d)/(?<year>\\d\\d\\d\\d)$");
-QRegularExpressionMatch match = re.match("08/12/1985");
-if (match.hasMatch()) {
- QString date = match.captured("date"); // date == "08"
- QString month = match.captured("month"); // month == "12"
- QString year = match.captured("year"); // year == 1985
-}
-//! [12]
-}
-
-{
-//! [13]
-QRegularExpression re("(\\w+)");
-QRegularExpressionMatchIterator i = re.globalMatch("the quick fox");
-//! [13]
-
-//! [14]
-QStringList words;
-while (i.hasNext()) {
- QRegularExpressionMatch match = i.next();
- QString word = match.captured(1);
- words << word;
-}
-// words contains "the", "quick", "fox"
-//! [14]
-}
-
-{
-//! [15]
-QString pattern("^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d\\d?, \\d\\d\\d\\d$");
-QRegularExpression re(pattern);
-
-QString input("Jan 21,");
-QRegularExpressionMatch match = re.match(input, 0, QRegularExpression::PartialPreferCompleteMatch);
-bool hasMatch = match.hasMatch(); // false
-bool hasPartialMatch = match.hasPartialMatch(); // true
-//! [15]
-}
-
-{
-QString pattern("^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d\\d?, \\d\\d\\d\\d$");
-QRegularExpression re(pattern);
-//! [16]
-QString input("Dec 8, 1985");
-QRegularExpressionMatch match = re.match(input, 0, QRegularExpression::PartialPreferCompleteMatch);
-bool hasMatch = match.hasMatch(); // true
-bool hasPartialMatch = match.hasPartialMatch(); // false
-//! [16]
-}
-
-{
-//! [17]
-QRegularExpression re("abc\\w+X|def");
-QRegularExpressionMatch match = re.match("abcdef", 0, QRegularExpression::PartialPreferCompleteMatch);
-bool hasMatch = match.hasMatch(); // true
-bool hasPartialMatch = match.hasPartialMatch(); // false
-QString captured = match.captured(0); // captured == "def"
-//! [17]
-}
-
-{
-//! [18]
-QRegularExpression re("abc\\w+X|defY");
-QRegularExpressionMatch match = re.match("abcdef", 0, QRegularExpression::PartialPreferCompleteMatch);
-bool hasMatch = match.hasMatch(); // false
-bool hasPartialMatch = match.hasPartialMatch(); // true
-QString captured = match.captured(0); // captured == "abcdef"
-//! [18]
-}
-
-{
-//! [19]
-QRegularExpression re("abc|ab");
-QRegularExpressionMatch match = re.match("ab", 0, QRegularExpression::PartialPreferFirstMatch);
-bool hasMatch = match.hasMatch(); // false
-bool hasPartialMatch = match.hasPartialMatch(); // true
-//! [19]
-}
-
-{
-//! [20]
-QRegularExpression re("abc(def)?");
-QRegularExpressionMatch match = re.match("abc", 0, QRegularExpression::PartialPreferFirstMatch);
-bool hasMatch = match.hasMatch(); // false
-bool hasPartialMatch = match.hasPartialMatch(); // true
-//! [20]
-}
-
-{
-//! [21]
-QRegularExpression re("(abc)*");
-QRegularExpressionMatch match = re.match("abc", 0, QRegularExpression::PartialPreferFirstMatch);
-bool hasMatch = match.hasMatch(); // false
-bool hasPartialMatch = match.hasPartialMatch(); // true
-//! [21]
-}
-
-{
-//! [22]
-QRegularExpression invalidRe("(unmatched|parenthesis");
-bool isValid = invalidRe.isValid(); // false
-//! [22]
-}
-
-{
-//! [23]
-QRegularExpression invalidRe("(unmatched|parenthesis");
-if (!invalidRe.isValid()) {
- QString errorString = invalidRe.errorString(); // errorString == "missing )"
- int errorOffset = invalidRe.patternErrorOffset(); // errorOffset == 22
- // ...
-}
-//! [23]
-}
-
-{
-//! [26]
-QString escaped = QRegularExpression::escape("a(x) = f(x) + g(x)");
-// escaped == "a\\(x\\)\\ \\=\\ f\\(x\\)\\ \\+\\ g\\(x\\)"
-//! [26]
-}
-
-{
-QString name;
-QString nickname;
-//! [27]
-QString pattern = "(" + QRegularExpression::escape(name) +
- "|" + QRegularExpression::escape(nickname) + ")";
-QRegularExpression re(pattern);
-//! [27]
-}
-
-{
-QString string;
-QRegularExpression re;
-//! [28]
-QRegularExpressionMatch match = re.match(string);
-for (int i = 0; i <= match.lastCapturedIndex(); ++i) {
- QString captured = match.captured(i);
- // ...
-}
-//! [28]
-}
-
-{
-//! [29]
-QRegularExpression re("(\\d\\d) (?<name>\\w+)");
-QRegularExpressionMatch match = re.match("23 Jordan");
-if (match.hasMatch()) {
- QString number = match.captured(1); // first == "23"
- QString name = match.captured("name"); // name == "Jordan"
-}
-//! [29]
-}
-
-{
-//! [30]
-// extracts the words
-QRegularExpression re("(\\w+)");
-QString subject("the quick fox");
-QRegularExpressionMatchIterator i = re.globalMatch(subject);
-while (i.hasNext()) {
- QRegularExpressionMatch match = i.next();
- // ...
-}
-//! [30]
-}
-
-{
-//! [31]
-QString wildcard = QRegularExpression::wildcardToRegularExpression("*.jpeg");
-// Will match files with names like:
-// foo.jpeg
-// f_o_o.jpeg
-// föö.jpeg
-//! [31]
-}
-
-//! [32]
- (?<day>\d\d)-(?<month>\d\d)-(?<year>\d\d\d\d) (\w+) (?<name>\w+)
-//! [32]
-
-//! [33]
- ("", "day", "month", "year", "", "name")
-//! [33]
-
-{
-//! [34]
-// using a raw string literal, R"(raw_characters)", to be able to use "\w"
-// without having to escape the backslash as "\\w"
-QRegularExpression re(R"(\w+)");
-QString subject("the quick fox");
-for (const QRegularExpressionMatch &match : re.globalMatch(subject)) {
- // ...
-}
-//! [34]
-}
-
-{
-//! [35]
-// matches two digits followed by a space and a word
-QRegularExpression re(R"(\d\d \w+)");
-//! [35]
-}
-
-{
-//! [36]
-QRegularExpression re("([a-z]+)|([A-Z]+)");
-QRegularExpressionMatch m = re.match("UPPERCASE");
-if (m.hasMatch()) {
- qDebug() << m.hasCaptured(0); // true
- qDebug() << m.hasCaptured(1); // false
- qDebug() << m.hasCaptured(2); // true
-}
-//! [36]
-}
+ {
+ //! [0]
+ QRegularExpression re("a pattern");
+ //! [0]
+ }
+
+ {
+ //! [1]
+ QRegularExpression re;
+ re.setPattern("another pattern");
+ //! [1]
+ }
+
+ {
+ //! [2]
+ // matches two digits followed by a space and a word
+ QRegularExpression re("\\d\\d \\w+");
+
+ // matches a backslash
+ QRegularExpression re2("\\\\");
+ //! [2]
+ }
+
+ {
+ //! [3]
+ QRegularExpression re("a third pattern");
+ QString pattern = re.pattern(); // pattern == "a third pattern"
+ //! [3]
+ }
+
+ {
+ //! [4]
+ // matches "Qt rocks", but also "QT rocks", "QT ROCKS", "qT rOcKs", etc.
+ QRegularExpression re("Qt rocks", QRegularExpression::CaseInsensitiveOption);
+ //! [4]
+ }
+
+ {
+ //! [5]
+ QRegularExpression re("^\\d+$");
+ re.setPatternOptions(QRegularExpression::MultilineOption);
+ // re matches any line in the subject string that contains only digits (but at least one)
+ //! [5]
+ }
+
+ {
+ //! [6]
+ QRegularExpression re = QRegularExpression("^two.*words$", QRegularExpression::MultilineOption
+ | QRegularExpression::DotMatchesEverythingOption);
+
+ QRegularExpression::PatternOptions options = re.patternOptions();
+ // options == QRegularExpression::MultilineOption | QRegularExpression::DotMatchesEverythingOption
+ //! [6]
+ }
+
+ {
+ //! [7]
+ // match two digits followed by a space and a word
+ QRegularExpression re("\\d\\d \\w+");
+ QRegularExpressionMatch match = re.match("abc123 def");
+ bool hasMatch = match.hasMatch(); // true
+ //! [7]
+ }
+
+ {
+ //! [8]
+ QRegularExpression re("\\d\\d \\w+");
+ QRegularExpressionMatch match = re.match("abc123 def");
+ if (match.hasMatch()) {
+ QString matched = match.captured(0); // matched == "23 def"
+ // ...
+ }
+ //! [8]
+ }
+
+ {
+ //! [9]
+ QRegularExpression re("\\d\\d \\w+");
+ QRegularExpressionMatch match = re.match("12 abc 45 def", 1);
+ if (match.hasMatch()) {
+ QString matched = match.captured(0); // matched == "45 def"
+ // ...
+ }
+ //! [9]
+ }
+
+ {
+ //! [10]
+ QRegularExpression re("^(\\d\\d)/(\\d\\d)/(\\d\\d\\d\\d)$");
+ QRegularExpressionMatch match = re.match("08/12/1985");
+ if (match.hasMatch()) {
+ QString day = match.captured(1); // day == "08"
+ QString month = match.captured(2); // month == "12"
+ QString year = match.captured(3); // year == "1985"
+ // ...
+ }
+ //! [10]
+ }
+
+ {
+ //! [11]
+ QRegularExpression re("abc(\\d+)def");
+ QRegularExpressionMatch match = re.match("XYZabc123defXYZ");
+ if (match.hasMatch()) {
+ int startOffset = match.capturedStart(1); // startOffset == 6
+ int endOffset = match.capturedEnd(1); // endOffset == 9
+ // ...
+ }
+ //! [11]
+ }
+
+ {
+ //! [12]
+ QRegularExpression re("^(?<date>\\d\\d)/(?<month>\\d\\d)/(?<year>\\d\\d\\d\\d)$");
+ QRegularExpressionMatch match = re.match("08/12/1985");
+ if (match.hasMatch()) {
+ QString date = match.captured("date"); // date == "08"
+ QString month = match.captured("month"); // month == "12"
+ QString year = match.captured("year"); // year == 1985
+ }
+ //! [12]
+ }
+
+ {
+ //! [13]
+ QRegularExpression re("(\\w+)");
+ QRegularExpressionMatchIterator i = re.globalMatch("the quick fox");
+ //! [13]
+
+ //! [14]
+ QStringList words;
+ while (i.hasNext()) {
+ QRegularExpressionMatch match = i.next();
+ QString word = match.captured(1);
+ words << word;
+ }
+ // words contains "the", "quick", "fox"
+ //! [14]
+ }
+
+ {
+ //! [15]
+ QString pattern("^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d\\d?, \\d\\d\\d\\d$");
+ QRegularExpression re(pattern);
+
+ QString input("Jan 21,");
+ QRegularExpressionMatch match = re.match(input, 0, QRegularExpression::PartialPreferCompleteMatch);
+ bool hasMatch = match.hasMatch(); // false
+ bool hasPartialMatch = match.hasPartialMatch(); // true
+ //! [15]
+ }
+
+ {
+ QString pattern("^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d\\d?, \\d\\d\\d\\d$");
+ QRegularExpression re(pattern);
+ //! [16]
+ QString input("Dec 8, 1985");
+ QRegularExpressionMatch match = re.match(input, 0, QRegularExpression::PartialPreferCompleteMatch);
+ bool hasMatch = match.hasMatch(); // true
+ bool hasPartialMatch = match.hasPartialMatch(); // false
+ //! [16]
+ }
+
+ {
+ //! [17]
+ QRegularExpression re("abc\\w+X|def");
+ QRegularExpressionMatch match = re.match("abcdef", 0, QRegularExpression::PartialPreferCompleteMatch);
+ bool hasMatch = match.hasMatch(); // true
+ bool hasPartialMatch = match.hasPartialMatch(); // false
+ QString captured = match.captured(0); // captured == "def"
+ //! [17]
+ }
+
+ {
+ //! [18]
+ QRegularExpression re("abc\\w+X|defY");
+ QRegularExpressionMatch match = re.match("abcdef", 0, QRegularExpression::PartialPreferCompleteMatch);
+ bool hasMatch = match.hasMatch(); // false
+ bool hasPartialMatch = match.hasPartialMatch(); // true
+ QString captured = match.captured(0); // captured == "abcdef"
+ //! [18]
+ }
+
+ {
+ //! [19]
+ QRegularExpression re("abc|ab");
+ QRegularExpressionMatch match = re.match("ab", 0, QRegularExpression::PartialPreferFirstMatch);
+ bool hasMatch = match.hasMatch(); // false
+ bool hasPartialMatch = match.hasPartialMatch(); // true
+ //! [19]
+ }
+
+ {
+ //! [20]
+ QRegularExpression re("abc(def)?");
+ QRegularExpressionMatch match = re.match("abc", 0, QRegularExpression::PartialPreferFirstMatch);
+ bool hasMatch = match.hasMatch(); // false
+ bool hasPartialMatch = match.hasPartialMatch(); // true
+ //! [20]
+ }
+
+ {
+ //! [21]
+ QRegularExpression re("(abc)*");
+ QRegularExpressionMatch match = re.match("abc", 0, QRegularExpression::PartialPreferFirstMatch);
+ bool hasMatch = match.hasMatch(); // false
+ bool hasPartialMatch = match.hasPartialMatch(); // true
+ //! [21]
+ }
+
+ {
+ //! [22]
+ QRegularExpression invalidRe("(unmatched|parenthesis");
+ bool isValid = invalidRe.isValid(); // false
+ //! [22]
+ }
+
+ {
+ //! [23]
+ QRegularExpression invalidRe("(unmatched|parenthesis");
+ if (!invalidRe.isValid()) {
+ QString errorString = invalidRe.errorString(); // errorString == "missing )"
+ int errorOffset = invalidRe.patternErrorOffset(); // errorOffset == 22
+ // ...
+ }
+ //! [23]
+ }
+
+ {
+ //! [26]
+ QString escaped = QRegularExpression::escape("a(x) = f(x) + g(x)");
+ // escaped == "a\\(x\\)\\ \\=\\ f\\(x\\)\\ \\+\\ g\\(x\\)"
+ //! [26]
+ }
+
+ {
+ QString name;
+ QString nickname;
+ //! [27]
+ QString pattern = "(" + QRegularExpression::escape(name) +
+ "|" + QRegularExpression::escape(nickname) + ")";
+ QRegularExpression re(pattern);
+ //! [27]
+ }
+
+ {
+ QString string;
+ QRegularExpression re;
+ //! [28]
+ QRegularExpressionMatch match = re.match(string);
+ for (int i = 0; i <= match.lastCapturedIndex(); ++i) {
+ QString captured = match.captured(i);
+ // ...
+ }
+ //! [28]
+ }
+
+ {
+ //! [29]
+ QRegularExpression re("(\\d\\d) (?<name>\\w+)");
+ QRegularExpressionMatch match = re.match("23 Jordan");
+ if (match.hasMatch()) {
+ QString number = match.captured(1); // first == "23"
+ QString name = match.captured("name"); // name == "Jordan"
+ }
+ //! [29]
+ }
+
+ {
+ //! [30]
+ // extracts the words
+ QRegularExpression re("(\\w+)");
+ QString subject("the quick fox");
+ QRegularExpressionMatchIterator i = re.globalMatch(subject);
+ while (i.hasNext()) {
+ QRegularExpressionMatch match = i.next();
+ // ...
+ }
+ //! [30]
+ }
+
+ {
+ //! [31]
+ QString wildcard = QRegularExpression::wildcardToRegularExpression("*.jpeg");
+ // Will match files with names like:
+ // foo.jpeg
+ // f_o_o.jpeg
+ // föö.jpeg
+ //! [31]
+ }
+
+#if 0
+ //! [32]
+ (?<day>\d\d)-(?<month>\d\d)-(?<year>\d\d\d\d) (\w+) (?<name>\w+)
+ //! [32]
+
+ //! [33]
+ ("", "day", "month", "year", "", "name")
+ //! [33]
+#endif
+
+ {
+ //! [34]
+ // using a raw string literal, R"(raw_characters)", to be able to use "\w"
+ // without having to escape the backslash as "\\w"
+ QRegularExpression re(R"(\w+)");
+ QString subject("the quick fox");
+ for (const QRegularExpressionMatch &match : re.globalMatch(subject)) {
+ // ...
+ }
+ //! [34]
+ }
+
+ {
+ //! [35]
+ // matches two digits followed by a space and a word
+ QRegularExpression re(R"(\d\d \w+)");
+ //! [35]
+ }
+
+ {
+ //! [36]
+ QRegularExpression re("([a-z]+)|([A-Z]+)");
+ QRegularExpressionMatch m = re.match("UPPERCASE");
+ if (m.hasMatch()) {
+ qDebug() << m.hasCaptured(0); // true
+ qDebug() << m.hasCaptured(1); // false
+ qDebug() << m.hasCaptured(2); // true
+ }
+ //! [36]
+ }
}
diff --git a/src/corelib/doc/snippets/code/src_corelib_text_qstaticlatin1stringmatcher.cpp b/src/corelib/doc/snippets/code/src_corelib_text_qstaticlatin1stringmatcher.cpp
index ae6e19e4715..9a56c22a62b 100644
--- a/src/corelib/doc/snippets/code/src_corelib_text_qstaticlatin1stringmatcher.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_text_qstaticlatin1stringmatcher.cpp
@@ -1,8 +1,15 @@
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+
+#include <QStaticLatin1StringMatcher>
+
//! [0]
-static constexpr auto matcher = qMakeStaticCaseSensitiveLatin1StringViewMatcher("needle");
+static constexpr auto matcher = qMakeStaticCaseSensitiveLatin1StringMatcher("needle");
//! [0]
-//! [1]
-static constexpr auto matcher = qMakeStaticCaseInsensitiveLatin1StringViewMatcher("needle");
-//! [1]
+
+void repetition()
+{
+ //! [1]
+ static constexpr auto matcher = qMakeStaticCaseInsensitiveLatin1StringMatcher("needle");
+ //! [1]
+}
diff --git a/src/corelib/doc/snippets/code/src_corelib_text_qstring.cpp b/src/corelib/doc/snippets/code/src_corelib_text_qstring.cpp
index 254985a1923..ebff9d96903 100644
--- a/src/corelib/doc/snippets/code/src_corelib_text_qstring.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_text_qstring.cpp
@@ -1,86 +1,142 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-//! [1]
-// Required for using the '_L1' string literal.
-using namespace Qt::StringLiterals;
-// ...
-QString url = "https://www.unicode.org/"_L1;
-//! [1]
-
-
-//! [2]
-double d = 12.34;
-QString str = QString("delta: %1").arg(d, 0, 'E', 3);
-// str == "delta: 1.234E+01"
-//! [2]
-
-
-//! [3]
-if (str == "auto" || str == "extern"
- || str == "static" || str == "register") {
- ...
-}
-//! [3]
-
-
-//! [4]
-if (str == QString("auto") || str == QString("extern")
- || str == QString("static") || str == QString("register")) {
- ...
-}
-//! [4]
-
-//! [4bis]
-str.append("Hello ").append("World");
-//! [4bis]
+#include <QString>
-//! [5]
-// Required for using the '_L1' string literal.
using namespace Qt::StringLiterals;
-// ...
-if (str == "auto"_L1
- || str == "extern"_L1
- || str == "static"_L1
- || str == "register"_L1 {
- ...
-}
-//! [5]
-
-
-//! [6]
-QLabel *label = new QLabel("MOD"_L1, this);
-//! [6]
-
-
-//! [7]
-QString plain = "#include <QtCore>"
-QString html = plain.toHtmlEscaped();
-// html == "#include &lt;QtCore&gt;"
-//! [7]
-//! [8]
-QString str("ab");
-str.repeated(4); // returns "abababab"
-//! [8]
-//! [9]
-// hasAttribute takes a QString argument
-if (node.hasAttribute("http-contents-length")) //...
-//! [9]
-
-//! [10]
-if (node.hasAttribute(QStringLiteral(u"http-contents-length"))) //...
-//! [10]
-
-//! [11]
-if (attribute.name() == "http-contents-length"_L1) //...
-//! [11]
-
-//! [qUtf8Printable]
-qWarning("%s: %s", qUtf8Printable(key), qUtf8Printable(value));
-//! [qUtf8Printable]
+struct Node
+{
+ QString name() const;
+ bool hasAttribute(const QString &name);
+};
+
+void example()
+{
+ {
+ //! [1]
+ // Required for using the '_L1' string literal.
+ using namespace Qt::StringLiterals;
+ // ...
+ QString url = "https://www.unicode.org/"_L1;
+ //! [1]
+ }
+
+ {
+ //! [2]
+ double d = 12.34;
+ QString str = QString("delta: %1").arg(d, 0, 'E', 3);
+ // str == "delta: 1.234E+01"
+ //! [2]
+ }
+
+ QString str;
+
+ {
+ //! [3]
+ if (str == "auto" || str == "extern"
+ || str == "static" || str == "register") {
+ //...
+ }
+ //! [3]
+ }
+
+ {
+ //! [4]
+ if (str == QString("auto") || str == QString("extern")
+ || str == QString("static") || str == QString("register")) {
+ //...
+ }
+ //! [4]
+ }
+
+ {
+ //! [4bis]
+ str.append("Hello ").append("World");
+ //! [4bis]
+ }
+
+ {
+ //! [5]
+ // Required for using the '_L1' string literal.
+ using namespace Qt::StringLiterals;
+ // ...
+ if (str == "auto"_L1
+ || str == "extern"_L1
+ || str == "static"_L1
+ || str == "register"_L1) {
+ //...
+ }
+ //! [5]
+ }
+
+ {
+ //! [7]
+ QString plain = "#include <QtCore>";
+ QString html = plain.toHtmlEscaped();
+ // html == "#include &lt;QtCore&gt;"
+ //! [7]
+ }
+
+ {
+ //! [8]
+ QString str("ab");
+ str.repeated(4); // returns "abababab"
+ //! [8]
+ }
+
+ {
+ Node node;
+ Node attribute;
+
+ //! [9]
+ // hasAttribute takes a QString argument
+ if (node.hasAttribute("http-contents-length")) {
+ //...
+ }
+ //! [9]
+
+ //! [10]
+ if (node.hasAttribute(QStringLiteral(u"http-contents-length"))){
+ //...
+ }
+ //! [10]
+
+ //! [11]
+ if (attribute.name() == "http-contents-length"_L1){
+ //...
+ }
+ //! [11]
+ }
+
+ {
+ QString key;
+ QString value;
+
+ //! [qUtf8Printable]
+ qWarning("%s: %s", qUtf8Printable(key), qUtf8Printable(value));
+ //! [qUtf8Printable]
+
+ //! [qUtf16Printable]
+ qWarning("%ls: %ls", qUtf16Printable(key), qUtf16Printable(value));
+ //! [qUtf16Printable]
+ }
+}
-//! [qUtf16Printable]
-qWarning("%ls: %ls", qUtf16Printable(key), qUtf16Printable(value));
-//! [qUtf16Printable]
+#if __has_include(<QWidget>)
+
+#include <QLabel>
+#include <QWidget>
+
+class LabelExample : public QWidget
+{
+ Q_OBJECT
+ void exampleWithLabel()
+ {
+ //! [6]
+ QLabel *label = new QLabel("MOD"_L1, this);
+ //! [6]
+ }
+};
+#endif
diff --git a/src/corelib/doc/snippets/code/src_corelib_text_qstringconverter.cpp b/src/corelib/doc/snippets/code/src_corelib_text_qstringconverter.cpp
index 4e44c364f37..f017ed01b13 100644
--- a/src/corelib/doc/snippets/code/src_corelib_text_qstringconverter.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_text_qstringconverter.cpp
@@ -1,68 +1,85 @@
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
-//! [0]
-QByteArray encodedString = "...";
-auto toUtf16 = QStringDecoder(QStringDecoder::Utf8);
-QString string = toUtf16(encodedString);
-//! [0]
+#include <QStringConverter>
+#include <QByteArray>
+bool new_data_available();
+QByteArray get_new_data();
-//! [1]
-QString string = "...";
-auto fromUtf16 = QStringEncoder(QStringEncoder::Utf8);
-QByteArray encodedString = fromUtf16(string);
-//! [1]
+void examples()
+{
+ {
+ //! [0]
+ QByteArray encodedString = "...";
+ auto toUtf16 = QStringDecoder(QStringDecoder::Utf8);
+ QString string = toUtf16(encodedString);
+ //! [0]
+ }
+ {
+ //! [1]
+ QString string = "...";
+ auto fromUtf16 = QStringEncoder(QStringEncoder::Utf8);
+ QByteArray encodedString = fromUtf16(string);
+ //! [1]
+ }
-//! [2]
-auto toUtf16 = QStringDecoder(QStringDecoder::Utf8);
+ {
+ //! [2]
+ auto toUtf16 = QStringDecoder(QStringDecoder::Utf8);
-QString string;
-while (new_data_available() && !toUtf16.hasError()) {
- QByteArray chunk = get_new_data();
- string += toUtf16(chunk);
-}
-auto result = toUtf16.finalize();
-if (result.error != QStringDecoder::FinalizeResult::NoError) {
- // Handle error
-}
-//! [2]
+ QString string;
+ while (new_data_available() && !toUtf16.hasError()) {
+ QByteArray chunk = get_new_data();
+ string += toUtf16(chunk);
+ }
+ auto result = toUtf16.finalize();
+ if (result.error != QStringDecoder::FinalizeResult::NoError) {
+ // Handle error
+ }
+ //! [2]
+ }
-//! [3]
-auto fromUtf16 = QStringEncoder(QStringEncoder::Utf8);
+ {
+ //! [3]
+ auto fromUtf16 = QStringEncoder(QStringEncoder::Utf8);
-QByteArray encoded;
-while (new_data_available() && !fromUtf16.hasError()) {
- QString chunk = get_new_data();
- encoded += fromUtf16(chunk);
-}
-auto result = fromUtf16.finalize();
-if (result.error != QStringEncoder::FinalizeResult::NoError) {
- // Handle error
-}
-//! [3]
+ QByteArray encoded;
+ while (new_data_available() && !fromUtf16.hasError()) {
+ QString chunk = get_new_data();
+ encoded += fromUtf16(chunk);
+ }
+ auto result = fromUtf16.finalize();
+ if (result.error != QStringEncoder::FinalizeResult::NoError) {
+ // Handle error
+ }
+ //! [3]
+ }
-{
-//! [4]
-QByteArray encodedString = "...";
-auto toUtf16 = QStringDecoder(QStringDecoder::Utf8);
-auto data = toUtf16(encodedString); // data's type is QStringDecoder::EncodedData<const QByteArray &>
-QString string = toUtf16(encodedString); // Implicit conversion to QString
+ using namespace Qt::StringLiterals;
-// Here you have to cast "data" to QString
-auto func = [&]() { return !toUtf16.hasError() ? QString(data) : u"foo"_s; }
-//! [4]
-}
+ {
+ //! [4]
+ QByteArray encodedString = "...";
+ auto toUtf16 = QStringDecoder(QStringDecoder::Utf8);
+ auto data = toUtf16(encodedString); // data's type is QStringDecoder::EncodedData<const QByteArray &>
+ QString string = toUtf16(encodedString); // Implicit conversion to QString
-{
-//! [5]
-QString string = "...";
-auto fromUtf16 = QStringEncoder(QStringEncoder::Utf8);
-auto data = fromUtf16(string); // data's type is QStringEncoder::DecodedData<const QString &>
-QByteArray encodedString = fromUtf16(string); // Implicit conversion to QByteArray
+ // Here you have to cast "data" to QString
+ auto func = [&]() { return !toUtf16.hasError() ? QString(data) : u"foo"_s; };
+ //! [4]
+ }
+
+ {
+ //! [5]
+ QString string = "...";
+ auto fromUtf16 = QStringEncoder(QStringEncoder::Utf8);
+ auto data = fromUtf16(string); // data's type is QStringEncoder::DecodedData<const QString &>
+ QByteArray encodedString = fromUtf16(string); // Implicit conversion to QByteArray
-// Here you have to cast "data" to QByteArray
-auto func = [&]() { return !fromUtf16.hasError() ? QByteArray(data) : "foo"_ba; }
-//! [5]
+ // Here you have to cast "data" to QByteArray
+ auto func = [&]() { return !fromUtf16.hasError() ? QByteArray(data) : "foo"_ba; };
+ //! [5]
+ }
}
diff --git a/src/corelib/doc/snippets/code/src_corelib_text_qstringview.cpp b/src/corelib/doc/snippets/code/src_corelib_text_qstringview.cpp
index 676baa5aee2..a314652d7e8 100644
--- a/src/corelib/doc/snippets/code/src_corelib_text_qstringview.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_text_qstringview.cpp
@@ -1,11 +1,13 @@
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+#include <QStringView>
+
//! [0]
void myfun1(QStringView sv); // preferred
void myfun2(const QStringView &sv); // compiles and works, but slower
//! [0]
//! [1]
- void fun(QChar ch) { fun(QStringView(&ch, 1)); }
+void fun(QChar ch) { myfun1(QStringView(&ch, 1)); }
//! [1]
diff --git a/src/corelib/doc/snippets/code/src_corelib_text_qutf8stringview.cpp b/src/corelib/doc/snippets/code/src_corelib_text_qutf8stringview.cpp
index 4b09796b362..e7f1aa91996 100644
--- a/src/corelib/doc/snippets/code/src_corelib_text_qutf8stringview.cpp
+++ b/src/corelib/doc/snippets/code/src_corelib_text_qutf8stringview.cpp
@@ -1,6 +1,8 @@
// Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
+#include <QUtf8StringView>
+
//! [0]
void myfun1(QUtf8StringView sv); // preferred
void myfun2(const QUtf8StringView &sv); // compiles and works, but slower