Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
TextUtils Namespace Reference

Pure string helpers with no dependencies on QDoc driver types. More...

Functions

QString separator (qsizetype wordPosition, qsizetype numberOfWords)
QString comma (qsizetype wordPosition, qsizetype numberOfWords)
QString asAsciiPrintable (const QString &str)
 Returns an ASCII-printable representation of str, preserving alphanumeric (alnum) characters ([a-zA-Z0-9]) and hyphens, but replacing all other characters with hyphens unless this would result in multiple adjacent hyphens.
QString protect (const QString &string)

Variables

static constexpr QLatin1StringView samp = "&"_L1
static constexpr QLatin1StringView slt = "<"_L1
static constexpr QLatin1StringView sgt = ">"_L1
static constexpr QLatin1StringView squot = """_L1

Detailed Description

Pure string helpers with no dependencies on QDoc driver types.

TextUtils groups text-manipulation helpers that the IR builders, the template generator, and the legacy generators all need, but that do not touch Node, Tree, Config, or Generator. Keeping them here lets QDocLib components call them without dragging in the rest of QDoc.

Function Documentation

◆ asAsciiPrintable()

QString TextUtils::asAsciiPrintable ( const QString & str)

Returns an ASCII-printable representation of str, preserving alphanumeric (alnum) characters ([a-zA-Z0-9]) and hyphens, but replacing all other characters with hyphens unless this would result in multiple adjacent hyphens.

Upper case characters are converted to lower case.

The string that is returned is normalized for use where ASCII-printable strings are required, such as in file names or fragment identifiers in URLs.

We distinguish between hyphens in str and hyphens used as placeholders for non-alphanumeric characters.

Placeholder hyphens are removed from the start and end of the output string, such that the resulting string does not start or end with a hyphen unless the original string included them. Placeholder hyphens are only generated to separate spans of alphanumeric characters.

If any character in str is non-latin, or latin and not found in the aforementioned subset (e.g. 'ß', 'å', or 'ö'), a hash of str is appended to the final string.

{Implementation note}

Previously, an optimized implementation was used that was equivalent to this code for the basic translation to an ASCII-printable representation:

name.replace(QRegularExpression("[^A-Za-z0-9]+"), " ");
name = name.simplified();
name.replace(QLatin1Char(' '), QLatin1Char('-'));
name = name.toLower();
\inmodule QtCore \reentrant
EGLImageKHR EGLint * name
\inmodule QtCore \reentrant
Definition qchar.h:18

Although it was measured to be approximately four times faster than this simple sequence of transformations, it could not distinguish between hyphens that were intentionally part of the input string and those that were generated by the implementation.

Definition at line 100 of file textutils.cpp.

◆ comma()

QString TextUtils::comma ( qsizetype wordPosition,
qsizetype numberOfWords )

Returns the inter-item punctuation for a list of numberOfWords words at wordPosition.

For a list of length one, returns an empty QString. For a list of length two, returns " and ". For longer lists, returns ", " for early items and ", and " for the item before the last. The last position returns an empty QString; its punctuation is the period returned by separator().

See also
separator()

Definition at line 51 of file textutils.cpp.

◆ protect()

QString TextUtils::protect ( const QString & str)

HTML-escapes the ampersand, less-than, greater-than, and double-quote characters in str, leaving other characters untouched.

Definition at line 163 of file textutils.cpp.

◆ separator()

QString TextUtils::separator ( qsizetype wordPosition,
qsizetype numberOfWords )

Returns the punctuation character for the word at wordPosition in a list of numberOfWords length. For the last position, returns "." (full stop). For any other word, delegates to comma().

See also
comma()

Definition at line 30 of file textutils.cpp.

Variable Documentation

◆ samp

QLatin1StringView TextUtils::samp = "&"_L1
staticconstexpr

Definition at line 37 of file textutils.h.

◆ sgt

QLatin1StringView TextUtils::sgt = ">"_L1
staticconstexpr

Definition at line 39 of file textutils.h.

◆ slt

QLatin1StringView TextUtils::slt = "<"_L1
staticconstexpr

Definition at line 38 of file textutils.h.

◆ squot

QLatin1StringView TextUtils::squot = """_L1
staticconstexpr

Definition at line 40 of file textutils.h.