Strings Class Reference

List of all members.

Static Public Member Functions

static QString colorizeString (const QString &str, int def=4)
static QString combinePaths (QString pathFront, QString pathEnd)
static QString createRandomAlphaNumericString (unsigned numChars)
 Creates a random string with specified length.
static QString createRandomAlphaNumericStringWithNewLines (unsigned numCharsPerLine, unsigned numLines)
static const QString & escape (QString &str)
 Adds escape characters to a string.
static QString formatDataAmount (qint64 bytes)
 Similar to formatDataSpeed().
static QString formatDataSpeed (float speedInBytesPerSecond)
 Formats a numerical speed value into a string.
static QString formatTime (float seconds)
 Formats a numerical time value into a string.
static bool isCharOnCharList (char c, const QString &charList)
static QString normalizePath (QString path)
 Creates a clean path.
static QByteArray readUntilByte (QDataStream &stream, unsigned char stopByte)
 Reads raw data from the current position of passed QDataStream until a specified byte is encountered.
static void translateServerAddress (const QString &addressString, QString &hostname, unsigned short &port, const QString &defaultAddress)
 Translates string in format "hostname:port" to atomic values.
static const QString & unescape (QString &str)
static bool isUrlSafe (const QString &url)
 Unsafe URLs begin with file:// and this functions returns false for such URLs.
static QString timestamp (const QString &format)
static QString & trim (QString &str, const QString &charList)
static QString & trimr (QString &str, const QString &charList)
static QString & triml (QString &str, const QString &charList)
static QString wrapUrlsWithHtmlATags (const QString &str)
 Detects all links within a given string and wraps them in <a href> tags.

Protected Types

enum  DataUnit { Byte = 0, Kilobyte = 1, Megabyte = 2, Gigabyte = 3 }

Static Protected Member Functions

static float scaleDataUnit (float bytes, DataUnit &outUnit)

Static Protected Attributes

static const unsigned RANDOM_CHAR_POOL_SIZE = 36
static const char RANDOM_CHAR_POOL [RANDOM_CHAR_POOL_SIZE]

Member Function Documentation

QString Strings::colorizeString ( const QString &  str,
int  def = 4 
) [static]

Colorizes the given string. Most useful for displaying colored names.

QString Strings::combinePaths ( QString  pathFront,
QString  pathEnd 
) [static]

Combines two strings into single path making sure there are no redundant slahes/backslashes present. Paths will be combined using backslash ('\') on Windows systems and slash ('/') on non-Windows systems. Example:

 QString pathBase = "C:\\games\\";
 QString pathSuffix = "\\doom\\doom.exe";
 QString fullPath = Strings::combinePaths(pathBase, pathSuffix);
 printf("%s\n", fullPath.toAscii().constData());
 // Output: "C:/games/doom/doom.exe"
Parameters:
pathFront - base path
pathEnd - part of the path that will be appended
Returns:
A string containing combined paths.
QString Strings::createRandomAlphaNumericString ( unsigned  numChars  )  [static]

Creates a random string with specified length.

The created string consists of lowercase letters and digits.

QString Strings::formatDataAmount ( qint64  bytes  )  [static]

Similar to formatDataSpeed().

The only difference is the unit type appended to the end of the returned string.

QString Strings::formatDataSpeed ( float  speedInBytesPerSecond  )  [static]

Formats a numerical speed value into a string.

This expects the parameter to represent speed in bytes per second. Data will be formatted into human readable format, ie. if speed is high enough it will be translated into kilobytes per second or megabytes per second. Also a string representing the type of value is appended: MB/s, kB/s or B/s.

QString Strings::formatTime ( float  seconds  )  [static]

Formats a numerical time value into a string.

Parameter is expected to be an amount of seconds. Output is transformed into a number of hours, minutes and seconds. The function will always try to generate the shortest possible string, ie. there will be no hour information if seconds parameter is smaller than 3600. The same goes for minutes. Factorial part in the parameter represents miliseconds and is always rounded up to a full second.

bool Strings::isUrlSafe ( const QString &  url  )  [static]

Unsafe URLs begin with file:// and this functions returns false for such URLs.

Someone might prepare such URL to make Doomseeker activate files on user's local drive and possibly cause damage to the system.

QString Strings::normalizePath ( QString  path  )  [static]

Creates a clean path.

Calls both QDir::fromNativeSeparators() and QDir::cleanPath(). The returned string will have only '/' separators, no '.' or '..' markers and no double separators.

QByteArray Strings::readUntilByte ( QDataStream &  stream,
unsigned char  stopByte 
) [static]

Reads raw data from the current position of passed QDataStream until a specified byte is encountered.

The primary use for this method is to read '' terminated strings.

The operator>>(char*&) in QDataStream expects the size of the string to be written first in a quint32 variable. Because this data not always exists, a custom method must be used. This is this method.

The read will occur from current position until the stopByte byte is encountered. Read may also stop when an end of stream is encountered in which case the data read up to this point will be returned.

Parameters:
stream Stream to read from.
stopByte When method encounters this byte, then the read will stop and the currently read data is returned.
Returns:
All read data plus the stopByte (if encountered).
void Strings::translateServerAddress ( const QString &  addressString,
QString &  hostname,
unsigned short &  port,
const QString &  defaultAddress 
) [static]

Translates string in format "hostname:port" to atomic values.

Parameters:
addressString - string to be translated.
[out] hostname - hostname output.
[out] port - port output.
defaultHostname - default hostname to use if translation fails.
defaultPort - default port to use if translation fails or no port inside addressString is specified.
QString Strings::wrapUrlsWithHtmlATags ( const QString &  str  )  [static]

Detects all links within a given string and wraps them in <a href> tags.

Valid URLs are found by seeking continuous no-whitespace substrings starting either with http://, ftp:// or REGEX: www\..*+

Furthermore, all URLs that begin with 'www.' will be forced to use the http:// scheme. Leaving them as they are causes problems with opening external URLs in QTextBrowser.


Member Data Documentation

const char Strings::RANDOM_CHAR_POOL [static, protected]
Initial value:
{
        'a', 'b', 'c', 'd', 'e', 'f', 'g',
        'h', 'i', 'j', 'k', 'l', 'm', 'n',
        'o', 'p', 'q', 'r', 's', 't', 'u',
        'v', 'w', 'x', 'y', 'z', '0', '1',
        '2', '3', '4', '5', '6', '7', '8',
        '9'
}

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