The SKIRT project
advanced radiative transfer for astrophysics
Static Public Member Functions | List of all members
StringUtils Class Referencefinal

#include <StringUtils.hpp>

Static Public Member Functions

static string addExtension (string filename, string extension)
 
static bool contains (const vector< string > &list, string find)
 
static bool contains (string text, string find)
 
static string dirPath (string filepath)
 
static bool endsWith (string text, string find)
 
static string filename (string filepath)
 
static string filenameBase (string filepath)
 
static int indexOf (const vector< string > &list, string find)
 
static bool isAbsolutePath (string filepath)
 
static bool isValidBool (string value)
 
static bool isValidDouble (string value)
 
static bool isValidInt (string value)
 
static string join (const vector< string > &list, string separator)
 
static string joinPaths (string segment1, string segment2)
 
static bool matches (string text, string pattern)
 
static string padLeft (string text, size_t length, char pad=' ')
 
static string padRight (string text, size_t length, char pad=' ')
 
static string replace (string text, string find, string replace)
 
static vector< string > split (string text, string separator)
 
static string squeeze (string text)
 
static bool startsWith (string text, string find)
 
static bool toBool (string value)
 
static double toDouble (string value, bool *ok=nullptr)
 
static int toInt (string value)
 
static string toLower (string text)
 
static string toMemSizeString (size_t value)
 
static string toString (bool value)
 
static string toString (double value)
 
static string toString (double value, char format, int precision=6, int width=1, char pad=' ')
 
static string toString (int value)
 
static string toUpper (string text)
 
static string toUpperFirst (string text)
 

Detailed Description

The StringUtils class offers various facilities for working with std::string objects.

Member Function Documentation

◆ addExtension()

static string StringUtils::addExtension ( string  filename,
string  extension 
)
static

Adds the specified filename extension to the specified filename (or file path), unless the filename already ends with the extension.

◆ contains() [1/2]

static bool StringUtils::contains ( const vector< string > &  list,
string  find 
)
static

Returns true if the string list contains the find string.

◆ contains() [2/2]

static bool StringUtils::contains ( string  text,
string  find 
)
static

Returns true if the text string contains the find string.

◆ dirPath()

static string StringUtils::dirPath ( string  filepath)
static

Removes the rightmost path segment from the specified path (leaving a trailing slash or backslash in place) and returns the result. If the input string does not contain at least two path segments (counting the empty root segment), the empty string is returned.

◆ endsWith()

static bool StringUtils::endsWith ( string  text,
string  find 
)
static

Returns true if the text string ends with the find string, or if the find string is empty. Otherwise returns false.

◆ filename()

static string StringUtils::filename ( string  filepath)
static

Returns the rightmost path segment in the specified path (after removing a trailing slash or backslash).

◆ filenameBase()

static string StringUtils::filenameBase ( string  filepath)
static

Returns the filename proper, without extension, in the specified path (after removing a trailing slash or backslash).

◆ indexOf()

static int StringUtils::indexOf ( const vector< string > &  list,
string  find 
)
static

Returns the zero-based index of the find string in the string list, or -1 if the string list does not contain the find string.

◆ isAbsolutePath()

static bool StringUtils::isAbsolutePath ( string  filepath)
static

Returns true if the specified path string represents an absolute path; false if it represents a relative path.

◆ isValidBool()

static bool StringUtils::isValidBool ( string  value)
static

Returns true if the specified string is non-empty and contains a valid representation of a boolean. Otherwise returns false. After converting the specified string to lowercase, the following contents are considered valid representations: "true", "t", "yes", "y", "1" (for boolean true) and "false", "f", "no", "n", "0" (for boolean false).

◆ isValidDouble()

static bool StringUtils::isValidDouble ( string  value)
static

Returns true if the specified string is non-empty and contains a valid string representation of a floating point number that can be represented as a double. Otherwise returns false.

◆ isValidInt()

static bool StringUtils::isValidInt ( string  value)
static

Returns true if the specified string is non-empty and contains a valid decimal string representation of an integer that fits in 32 bits (signed). Otherwise returns false.

◆ join()

static string StringUtils::join ( const vector< string > &  list,
string  separator 
)
static

Joins the list of strings by inserting the separator between consecutive non-empty strings, and returns the resulting single string.

◆ joinPaths()

static string StringUtils::joinPaths ( string  segment1,
string  segment2 
)
static

Joins the two specified path segments into a single path in a meaningful way, and returns the result. Specifically, if both segments are nonempty, they are joined such that there is exactly one (forward or backward) slash in between. If either (or both) segments are empty, no slash is added.

◆ matches()

static bool StringUtils::matches ( string  text,
string  pattern 
)
static

Returns true if the text string matches the pattern string, which may include * and ? characters.

◆ padLeft()

static string StringUtils::padLeft ( string  text,
size_t  length,
char  pad = ' ' 
)
static

Pads the text string with the specified pad character at the left until it has the specified length (so that the string is right-aligned), and returns the result. The default pad character is a space. If the length of the input string is equal to or larger than the specified length, the input string is returned unchanged.

◆ padRight()

static string StringUtils::padRight ( string  text,
size_t  length,
char  pad = ' ' 
)
static

Pads the text string with the specified pad character at the right until it has the specified length (so that the string is left-aligned), and returns the result. The default pad character is a space. If the length of the input string is equal to or larger than the specified length, the input string is returned unchanged.

◆ replace()

static string StringUtils::replace ( string  text,
string  find,
string  replace 
)
static

Replaces all occurences in the text string of 'find' by 'replace', and returns the result.

◆ split()

static vector< string > StringUtils::split ( string  text,
string  separator 
)
static

Splits the text string into substrings wherever the separator string occurs, and returns the list of those strings. If the separator does not match anywhere, the function returns a single-element list containing the complete text string.

◆ squeeze()

static string StringUtils::squeeze ( string  text)
static

Compresses consecutive white space (space, tab and newline characters) in the text string to a single space, removes any white space at the start and at the end, and returns the result.

◆ startsWith()

static bool StringUtils::startsWith ( string  text,
string  find 
)
static

Returns true if the text string starts with the find string, or if the find string is empty. Otherwise returns false.

◆ toBool()

static bool StringUtils::toBool ( string  value)
static

Returns the boolean value represented by the specified string, or false if the string is empty or contains an invalid representation. See isValidBool() for the valid representations.

◆ toDouble()

static double StringUtils::toDouble ( string  value,
bool *  ok = nullptr 
)
static

Returns the floating point value represented by the specified string, or zero if the string is empty or contains an invalid representation. If the optional argument ok is provided and not null, the flag is set to true if the conversion succeeded, and to false if not.

◆ toInt()

static int StringUtils::toInt ( string  value)
static

Returns the integer value represented by the specified string, or zero if the string is empty or contains an invalid representation. See isValidInt() for the valid representations.

◆ toLower()

static string StringUtils::toLower ( string  text)
static

Converts upper-case characters in the text string to lower case, and returns the result.

◆ toMemSizeString()

static string StringUtils::toMemSizeString ( size_t  value)
static

Returns a user-friendly string representation of the specified memory size value with 3 significant digits and the appropriate units (KB, MB, GB or TB).

◆ toString() [1/4]

static string StringUtils::toString ( bool  value)
static

Returns the string "true" or "false" depending on the specified boolean value.

◆ toString() [2/4]

static string StringUtils::toString ( double  value)
static

Returns a smart string representation of the specified floating point value with up to 10 significant digits. The function automatically selects fixed point or scientific notation, and removes leading and trailing zeroes where applicable, even in the exponent.

◆ toString() [3/4]

static string StringUtils::toString ( double  value,
char  format,
int  precision = 6,
int  width = 1,
char  pad = ' ' 
)
static

Returns a string representation of the specified floating point value with the given format, precision, and minimum field width.

The format is 'd' for integer notation (although the value is passed as a double), 'f' for fixed point notation, 'e' for scientific notation, and 'g' for the most concise 'f' or 'e'. For the 'd' format, the precision is not used and can be omitted (unless the minimum field width is specified as well). For the 'f' and 'e' formats, the precision represents the number of digits after the decimal point. For the 'g' format, the precision represents the maximum number of significant digits (trailing zeroes are omitted). If the format specifier is not one of the above, the function silently uses 'd' as a fallback. The precision has a default value of 6 and is limited to a maximum of 18 and a minimum of zero.

If the formatted string is shorter than the specified minimum field width, it is padded with the specified pad character so that it becomes right-aligned in the field. The default minimum field width is one, which means that no padding occurs. The default pad character is a space. The formatted string is never truncated, regardless of the specified width.

◆ toString() [4/4]

static string StringUtils::toString ( int  value)
static

Returns a string representation of the specified integer value.

◆ toUpper()

static string StringUtils::toUpper ( string  text)
static

Converts lower-case characters in the text string to upper case, and returns the result.

◆ toUpperFirst()

static string StringUtils::toUpperFirst ( string  text)
static

Converts the first character in the string to upper-case, if applicable, and returns the result.


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