7
By Roman Okolovich

Using QString effectively

Embed Size (px)

DESCRIPTION

Using QString effectively

Citation preview

Page 1: Using QString effectively

By Roman Okolovich

Page 2: Using QString effectively

General

The QString class provides a Unicode character string.

QString class is used for Initialize a Qstring from const char * Keep strings Compare strings Manipulate string data

(append, prepend, insert, replace, mid, left, right, etc)

Construct a complex string from multiple substrings

Etc

Page 3: Using QString effectively

Important

Many of QString's member functions are overloaded to accept const char * instead of QString.

QString converts the const char* data into Unicode using the fromAscii() function QString will allocate memory for the string (malloc

function is called) creates a deep copy of the C-style string

Applications that define QT_NO_CAST_FROM_ASCII don't have access to QString‘s const char* API.

Page 4: Using QString effectively

Exampleif(str1.contains("title")) // 1{ QString str2 = str1.mid(str1.indexOf("TX") + 2, 5); // 2*

if(str2 == "text1" || // 3 0 == str2.compare("text2")) // 4 { // todo }}

// hidden malloc is called 4 times// * mid() creates new instance of QString (+1 malloc)• bool contains(const QString & str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

• bool contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const• bool contains(const QRegExp & rx) const• bool contains(QRegExp & rx) const

• int compare(const QString & other) const• int compare(const QString & other, Qt::CaseSensitivity cs) const• int compare(const QLatin1String & other, Qt::CaseSensitivity cs=Qt::CaseSensitive) const

• int compare(const QStringRef & ref, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

• bool operator== (const char * other) const• The other const char pointer is converted to a QString using the fromAscii() function.

Page 5: Using QString effectively

Solutionif(str1.contains(QLatin1String("title"))){ QStringRef str2 = str1.midRef(str1.indexOf(QLatin1String("TX"))

+ 2, 4);

if(str2 == QLatin1String("text1") || // QString::operator==(const QLatin1String &)

0 == str2.compare(QLatin1String("text2"))) { // todo }}

• The QLatin1String class provides a thin wrapper around an ASCII/Latin-1 encoded string literal (It just holds a pointer to the C-string provided in it’s constructor)

• Thanks to the QString(const QLatin1String &) constructor, QLatin1String can be used everywhere a QString is expected. For example:

• QLabel *label = new QLabel(QLatin1String("MOD"), this);• QStringRef is designed to improve the performance of

substring handling when manipulating substrings obtained from existing QString instances. QStringRef avoids the memory allocation and reference counting overhead of a standard QString by simply referencing a part of the original string.

• Calling toString() returns a copy of the data as a real QString instance.

Page 6: Using QString effectively

More Efficient String Construction if(foo.startsWith( "(" + type + ") 0x“ ) )

This code requires at least 2 mallocs if(foo.startsWith(QLatin1String("(")

 + type + QLatin1String(") 0x")))

In 4.6, an internal template class QStringBuilder has been added. This class is marked internal and does not appear in the documentation.

QStringBuilder uses expression templates and reimplements the '%' operator so that when you use '%' for string concatenation instead of '+', multiple substring concatenations will be postponed until the final result is about to be assigned to a QString. At this point, the amount of memory required for the final result is known. The memory allocator is then called once to get the required space, and the substrings are copied into it one by one.

#include <QStringBuilder>

QString hello("hello");QStringRef el(&hello, 2, 3);QLatin1String world("world");QString message = hello % el % world % QChar('!');

Page 7: Using QString effectively

References

QString Class Reference QLatin1String Class Reference Qt wiki