KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
StringUtil Class Reference

StringUtil. More...

#include <stringutil.h>

Classes

class  Bitap
 Implements BITAP algorithm for fuzzy search. More...
 
class  Levenshtein
 Implements the Levenshtein algorithm to determine the distance between two strings. More...
 

Static Public Member Functions

static bool toBool (const QString &value)
 Convert the given string to a boolean.
 
static bool toBool (int value)
 Convert an integer to a boolean.
 
static QByteArray toByteArray (const QString &value)
 Convert a string to a byte array.
 
static QString toString (bool value)
 Convert a boolean to a string ("true" or "false").
 
static QString toString (double value, int precision=6)
 Convert a double to a string, trimming trailing zeros.
 
static QString toString (const QByteArray &value, const QString &delimiter=QString(" "))
 Convert a byte array to a hex string.
 
static QString toHexTable (const QByteArray &buffer, bool showOffset=true, bool showText=true, int tableWidth=16)
 Convert a byte array to a hexadecimal table.
 
static QString toString (const QList< QUuid > &value, const QString &delimiter=QString(" "))
 Convert a list of QUuids to a string.
 
static QList< QUuid > uuidsFromString (const QString &value, const QString &delimiter=QString(" "))
 Convert a delimited string to a list of UUIDs.
 
static QString toDelimitedString (const QStringList &list, char delimiter=' ')
 Convert a list of strings to a single delimited string.
 
static QString toDelimitedString (const QList< int > &list, char delimiter=' ')
 Convert a list of ints to a single delimited string.
 
static QString toDelimitedString (const QList< uint8_t > &list, char delimiter=' ')
 Convert a list of uint8_t values to a single delimited string.
 
static QString toKMG (qint64 byteCount)
 Format a byte count as an abbreviated string (K, M, G).
 
static QString trimFront (const QString &value, const QList< QChar > &chars)
 Trim the given characters from the front of the string.
 
static QString trimEnd (const QString &value)
 Trim trailing whitespace from the string.
 
static QString trimEnd (const QString &value, const QList< QChar > &chars)
 Trim the given characters from the end of the string.
 
static QString trimBothEnds (const QString &value, const QList< QChar > &chars)
 Trim the given characters from both ends of the string.
 
static QString unquoted (const QString &value)
 Return the region between the outermost quotes.
 
static QString quoted (const QString &value)
 Wrap a string in double quotes.
 
static QStringList trimmed (const QStringList &value, Qt::SplitBehavior behavior=Qt::KeepEmptyParts)
 Return a list of all the strings trimmed.
 
static QStringList splitWithQuotes (const QString &value, QChar separator, Qt::SplitBehavior behavior=Qt::KeepEmptyParts)
 Split the string respecting quoted substrings.
 
static QStringList splitWithQuotes (const QString &value, QList< QChar > separators, Qt::SplitBehavior behavior=Qt::KeepEmptyParts)
 Split the string respecting quoted substrings, using multiple separators.
 
static QString combineToEol (const QStringList &lines, int index, const QChar &eolCharacter=';', int *consumed=nullptr)
 Combine strings from the list until hitting one that ends with an EOL character.
 
static int indexOfWord (const QString &value, int wordNumber)
 Return the character index of a given word number (1-based) in the string.
 
static int fuzzyIndexOf (const QString &needle, const QString &haystack, int maxDistance=1)
 Calculate the index of a fuzzy match using the BITAP algorithm.
 
static int levenshteinDistance (const QString &s1, const QString &s2)
 Calculate the Levenshtein distance between two strings.
 

Detailed Description

StringUtil.

Some static helper methods for converting strings to and from other types.

Additionally, methods exist for fuzzy string comparisons using the BITAP and Levenshtein Distance algorithms.

Stephen Punak, February 12 2021

Static helper methods for string conversion, manipulation, and fuzzy matching.

Provides conversions between strings and common types (bool, double, QByteArray), trimming, splitting with quote awareness, and fuzzy matching via the BITAP and Levenshtein Distance algorithms.

Definition at line 28 of file stringutil.h.

Member Function Documentation

◆ combineToEol()

static QString StringUtil::combineToEol ( const QStringList &  lines,
int  index,
const QChar &  eolCharacter = ';',
int *  consumed = nullptr 
)
static

Combine strings from the list until hitting one that ends with an EOL character.

Parameters
linesSource string list
indexStarting index
eolCharacterEnd-of-line character to look for
consumedOutput set to the number of lines consumed (may be nullptr)
Returns
Combined string

◆ fuzzyIndexOf()

static int StringUtil::fuzzyIndexOf ( const QString &  needle,
const QString &  haystack,
int  maxDistance = 1 
)
static

Calculate the index of a fuzzy match using the BITAP algorithm.

Parameters
needlePattern to search for
haystackString to search in
maxDistanceMaximum Levenshtein distance for a match
Returns
Index of the match, or -1 if not found

◆ indexOfWord()

static int StringUtil::indexOfWord ( const QString &  value,
int  wordNumber 
)
static

Return the character index of a given word number (1-based) in the string.

Parameters
valueSource string
wordNumber1-based word ordinal
Returns
Character index of the word, or -1 if not found

◆ levenshteinDistance()

static int StringUtil::levenshteinDistance ( const QString &  s1,
const QString &  s2 
)
static

Calculate the Levenshtein distance between two strings.

Parameters
s1First string
s2Second string
Returns
Edit distance between the strings

◆ quoted()

static QString StringUtil::quoted ( const QString &  value)
inlinestatic

Wrap a string in double quotes.

Parameters
valueString to quote
Returns
Quoted string

Definition at line 161 of file stringutil.h.

◆ splitWithQuotes() [1/2]

static QStringList StringUtil::splitWithQuotes ( const QString &  value,
QChar  separator,
Qt::SplitBehavior  behavior = Qt::KeepEmptyParts 
)
static

Split the string respecting quoted substrings.

Parameters
valueString to split
separatorSeparator character
behaviorWhether to keep or skip empty parts
Returns
Split string list

◆ splitWithQuotes() [2/2]

static QStringList StringUtil::splitWithQuotes ( const QString &  value,
QList< QChar >  separators,
Qt::SplitBehavior  behavior = Qt::KeepEmptyParts 
)
static

Split the string respecting quoted substrings, using multiple separators.

Parameters
valueString to split
separatorsList of separator characters
behaviorWhether to keep or skip empty parts
Returns
Split string list

◆ toBool() [1/2]

static bool StringUtil::toBool ( const QString &  value)
inlinestatic

Convert the given string to a boolean.

"true" (case-insensitive) or "1" yields true; everything else yields false.

Parameters
valueString to convert
Returns
Resulting boolean

Definition at line 38 of file stringutil.h.

◆ toBool() [2/2]

static bool StringUtil::toBool ( int  value)
inlinestatic

Convert an integer to a boolean.

Parameters
valueInteger to convert (0 = false, non-zero = true)
Returns
Resulting boolean

Definition at line 44 of file stringutil.h.

◆ toByteArray()

static QByteArray StringUtil::toByteArray ( const QString &  value)
static

Convert a string to a byte array.

Parameters
valueString to convert
Returns
Resulting QByteArray

◆ toDelimitedString() [1/3]

static QString StringUtil::toDelimitedString ( const QList< int > &  list,
char  delimiter = ' ' 
)
static

Convert a list of ints to a single delimited string.

Parameters
listInteger list to join
delimiterSeparator character
Returns
Joined string

◆ toDelimitedString() [2/3]

static QString StringUtil::toDelimitedString ( const QList< uint8_t > &  list,
char  delimiter = ' ' 
)
static

Convert a list of uint8_t values to a single delimited string.

Parameters
listByte list to join
delimiterSeparator character
Returns
Joined string

◆ toDelimitedString() [3/3]

static QString StringUtil::toDelimitedString ( const QStringList &  list,
char  delimiter = ' ' 
)
static

Convert a list of strings to a single delimited string.

Parameters
listString list to join
delimiterSeparator character
Returns
Joined string

◆ toHexTable()

static QString StringUtil::toHexTable ( const QByteArray &  buffer,
bool  showOffset = true,
bool  showText = true,
int  tableWidth = 16 
)
static

Convert a byte array to a hexadecimal table.

Parameters
bufferByte array to display
showOffsetWhether to show byte offsets
showTextWhether to show ASCII text column
tableWidthNumber of bytes per row
Returns
Formatted hex table string

◆ toKMG()

static QString StringUtil::toKMG ( qint64  byteCount)
static

Format a byte count as an abbreviated string (K, M, G).

Parameters
byteCountNumber of bytes
Returns
Abbreviated string (e.g. "1.5 M")

◆ toString() [1/4]

static QString StringUtil::toString ( bool  value)
inlinestatic

Convert a boolean to a string ("true" or "false").

Parameters
valueBoolean to convert
Returns
"true" or "false"

Definition at line 56 of file stringutil.h.

◆ toString() [2/4]

static QString StringUtil::toString ( const QByteArray &  value,
const QString &  delimiter = QString(" ") 
)
static

Convert a byte array to a hex string.

Parameters
valueByte array to convert
delimiterSeparator between hex bytes
Returns
Hex string representation

◆ toString() [3/4]

static QString StringUtil::toString ( const QList< QUuid > &  value,
const QString &  delimiter = QString(" ") 
)
static

Convert a list of QUuids to a string.

Parameters
valueList of UUIDs
delimiterSeparator between UUIDs
Returns
Delimited string of UUIDs

◆ toString() [4/4]

static QString StringUtil::toString ( double  value,
int  precision = 6 
)
static

Convert a double to a string, trimming trailing zeros.

Parameters
valueDouble to convert
precisionMaximum number of decimal places
Returns
Formatted string

◆ trimBothEnds()

static QString StringUtil::trimBothEnds ( const QString &  value,
const QList< QChar > &  chars 
)
static

Trim the given characters from both ends of the string.

Parameters
valueSource string
charsCharacters to trim
Returns
Trimmed string

◆ trimEnd() [1/2]

static QString StringUtil::trimEnd ( const QString &  value)
static

Trim trailing whitespace from the string.

Parameters
valueSource string
Returns
String with trailing whitespace removed

◆ trimEnd() [2/2]

static QString StringUtil::trimEnd ( const QString &  value,
const QList< QChar > &  chars 
)
static

Trim the given characters from the end of the string.

Parameters
valueSource string
charsCharacters to trim
Returns
Trimmed string

◆ trimFront()

static QString StringUtil::trimFront ( const QString &  value,
const QList< QChar > &  chars 
)
static

Trim the given characters from the front of the string.

Parameters
valueSource string
charsCharacters to trim
Returns
Trimmed string

◆ trimmed()

static QStringList StringUtil::trimmed ( const QStringList &  value,
Qt::SplitBehavior  behavior = Qt::KeepEmptyParts 
)
static

Return a list of all the strings trimmed.

Parameters
valueString list to trim
behaviorWhether to keep or skip empty parts
Returns
List of trimmed strings

◆ unquoted()

static QString StringUtil::unquoted ( const QString &  value)
static

Return the region between the outermost quotes.

Parameters
valueQuoted string
Returns
Unquoted content

◆ uuidsFromString()

static QList< QUuid > StringUtil::uuidsFromString ( const QString &  value,
const QString &  delimiter = QString(" ") 
)
static

Convert a delimited string to a list of UUIDs.

Parameters
valueDelimited string containing UUIDs
delimiterSeparator between UUIDs
Returns
List of parsed QUuids

The documentation for this class was generated from the following file: