ug4
String Utilities

Functions

UG_API void ug::AppendCounterToString (std::string &str, std::string indicator, int counter, int maxCounter=-1)
 appends a counter number to a string More...
 
UG_API std::string ug::AppendSpacesToString (std::string &str, int totalLength)
 padding a string with spaces to predefined length More...
 
UG_API bool ug::Contains (const std::string &str, const std::string &search)
 Checks whether given string contains a specified substring. More...
 
UG_API std::string ug::FilenameAndPathWithoutExtension (std::string str)
 returns the best guess of the filename and path from given string More...
 
UG_API std::string ug::FilenameWithoutExtension (std::string str)
 returns the best guess of the filename from given string More...
 
UG_API std::string ug::FilenameWithoutPath (const std::string &str)
 returns best guess of a filename from a given string More...
 
UG_API std::vector< std::string > ug::FindDuplicates (const std::vector< std::string > &vec)
 Finds and returns all duplicate elements of given vector. More...
 
std::string::size_type ug::GetDirectorySeperatorPos (const std::string &str)
 determines last occurrence of '/' or '\' More...
 
string ug::GetFileLine (const char *filename, size_t line)
 get a specific line of a file More...
 
string ug::GetFileLines (const char *filename, size_t fromline, size_t toline, bool includeLineNumbers)
 get some specified lines of a file More...
 
UG_API std::string ug::GetFilenameExtension (const std::string &str)
 returns the best guess of a file extensions from given string More...
 
template<>
UG_API size_t ug::hash_key (const std::string &str)
 creates a hash key from a string value More...
 
UG_API bool ug::IsLonger (const std::string &a, const std::string &b)
 checks whether second string is longer than first string More...
 
UG_API size_t ug::LevenshteinDistance (const std::string &s1, const std::string &s2)
 Calculate Levenshtein Distance of to strings. More...
 
int ug::NumberOfDigits (int n)
 returns the number of digits of an integer (expressed with base 10) More...
 
UG_API std::string ug::PathFromFilename (const std::string &str)
 returns best guess of a path without a filename from a given string More...
 
void ug::RemoveWhitespaceFromString (std::string &string)
 removes all white space from a string, also within the string More...
 
string ug::repeat (char c, int nr)
 Builds a string with specified repetitions of given character. More...
 
UG_API std::string ug::ReplaceAll (std::string target, const std::string &oldstr, const std::string &newstr)
 Substitutes substrings of given string with other substrings. More...
 
UG_API std::string ug::SnipString (const std::string &str, size_t totalSize, size_t replaceLast=0, const char replace='.')
 creates a truncated string and may add truncation symbol at end More...
 
UG_API std::string ug::SnipStringFront (const std::string &str, size_t totalSize, size_t replaceFront=0, const char replace='.')
 creates a truncated string and may add truncation symbol at front More...
 
UG_API bool ug::StartsWith (const std::string &str, const std::string &search)
 checks whether a given string starts with a specified substring More...
 
vector< string > ug::TokenizeString (const char *str, const char delimiter=',')
 splits the string into parts based on a separating character More...
 
UG_API std::vector< std::string > ug::TokenizeString (const std::string &str, const char delimiter=',')
 splits the string into parts based on a separating character More...
 
UG_API void ug::TokenizeString (const std::string &str, std::vector< std::string > &vToken, const char delimiter=',')
 splits the string into parts based on a separating char More...
 
UG_API std::vector< std::string > ug::TokenizeTrimString (const std::string &str, const char delimiter=',')
 splits the string into trimmed parts based on a separating char More...
 
UG_API void ug::TokenizeTrimString (const std::string &str, std::vector< std::string > &vToken, const char delimiter=',')
 splits the string into trimmed parts based on a separating char More...
 
UG_API std::string ug::ToLower (std::string str)
 Returns a lower case version of the specified string. More...
 
template<typename T >
std::string ug::ToString (const T &t)
 Convert a object supporting 'std::cout << obj' to a string. More...
 
UG_API std::string ug::ToUpper (std::string str)
 Returns an upper case version of the specified string. More...
 
UG_API std::string ug::TrimString (const std::string &str)
 removes all white space from the front and end of a string More...
 
bool ug::WildcardMatch (const char *str, const char *pattern)
 wildcard matches like bla.* or *.bla or t?st More...
 
UG_API std::string ug::XMLStringEscape (std::string s)
 returns a string suitable for XML files this functions escapes the characters <, >, ', " and & More...
 

Detailed Description

Function Documentation

◆ AppendCounterToString()

UG_API void ug::AppendCounterToString ( std::string &  str,
std::string  indicator,
int  counter,
int  maxCounter = -1 
)

appends a counter number to a string

This functions appends to a string a counter preceded by some indicator. If a maxCounter is passed, the field is adjusted to the maximum needed width and additional space is filled by zeros.

Parameters
[in,out]strstring to append the counter
[in]indicatorsome string preceding the counter
[in]countercounter added
[in]maxCountermaximum counter to be added

◆ AppendSpacesToString()

UG_API std::string ug::AppendSpacesToString ( std::string &  str,
int  totalLength 
)

padding a string with spaces to predefined length

Appends spaces to the given string so that the resulting string has a predefined length of totalLength

Parameters
[in]strstring to be padded
[in]totalLengthdesired total length of the string
Returns
padded string

◆ Contains()

UG_API bool ug::Contains ( const std::string &  str,
const std::string &  search 
)

Checks whether given string contains a specified substring.

Checks whether str contains search.

Parameters
[in]strstring
[in]searchstring to search for
Returns
true if str contains search; false otherwise

◆ FilenameAndPathWithoutExtension()

UG_API std::string ug::FilenameAndPathWithoutExtension ( std::string  str)

returns the best guess of the filename and path from given string

returns the part of the string without extension (e.g. /sw/bla.txt -> bla)

Parameters
[in]strto retrieve filename from
Returns
best guess of the filename without extension; if no guess can be made, the whole string is returned

◆ FilenameWithoutExtension()

UG_API std::string ug::FilenameWithoutExtension ( std::string  str)

returns the best guess of the filename from given string

returns the part of the string without path and extension (e.g. /sw/bla.txt -> bla)

Parameters
[in]strto retrieve filename from
Returns
best guess of the filename without path and extension; if no guess can be made, the whole string is returned

◆ FilenameWithoutPath()

UG_API std::string ug::FilenameWithoutPath ( const std::string &  str)

returns best guess of a filename from a given string

returns the part of the string after the last '/' or '\' character (e.g. /sw/bla.txt -> bla.txt)

Parameters
[in]strto retrieve the filename from
Returns
best guess of the file name from given path; if no guess can be made the complete string is returned

◆ FindDuplicates()

UG_API std::vector<std::string> ug::FindDuplicates ( const std::vector< std::string > &  vec)

Finds and returns all duplicate elements of given vector.

Searches for duplicates in the specified vector and returns a vector containing all elements that occur multiple times.

Parameters
[in]vecvector to analyze
Returns
a vector containing all elements that occur multiple times

◆ GetDirectorySeperatorPos()

std::string::size_type ug::GetDirectorySeperatorPos ( const std::string &  str)

determines last occurrence of '/' or '\'

Parameters
[in]strstring to lock in
Returns
position of the last occurrence of '/' or '\' in str; returns std::string::npos if none are found

◆ GetFileLine()

UG_API std::string ug::GetFileLine ( const char *  filename,
size_t  line 
)

get a specific line of a file

Parameters
filenamefile name
lineline number to extract
Returns
the line of the file

References ug::GetFileLines().

Referenced by ug::bridge::FunctionInfo(), ug::bridge::GetLuaFileAndLine(), ug::bridge::GetLuaLine(), ug::bridge::LuaCurrentLine(), ug::bridge::LuaGetScriptFunctionString(), ug::bridge::LuaStackTraceString(), and ug::script::UpdateDepth().

◆ GetFileLines()

UG_API std::string ug::GetFileLines ( const char *  filename,
size_t  fromline,
size_t  toline,
bool  includeLineNumbers 
)

get some specified lines of a file

Parameters
[in]filenamefile name
[in]fromlineline number to start from
[in]tolineline number to stop at
[in]includeLineNumbersif true, add the line number in front of each line and a tab.
Returns
lines fromline to toline of file filename.

References PROFILE_FUNC.

Referenced by ug::bridge::LUACompiler::createC(), ug::script::DebugList(), ug::GetFileLine(), ug::GetFileLinesParallel(), and ug::LUAParserClass::parse_luaFunction_StackTop().

◆ GetFilenameExtension()

UG_API std::string ug::GetFilenameExtension ( const std::string &  str)

returns the best guess of a file extensions from given string

returns the extension of the filename (e.g. /sw/bla.txt -> txt). Everything after the last dot ('.') of str is considered the file extension.

Parameters
[in]strto retrieve file extension from
Returns
best guess of the file extension; empty string if no guess can be made

◆ hash_key()

template<>
UG_API size_t ug::hash_key ( const std::string &  str)

creates a hash key from a string value

this template function creates a hash key for a string value

Parameters
[in]strstring to create hash for
Returns
hash key for given key
Note
Implementation is copied from some book or website. Can't remember... (sreiter)

◆ IsLonger()

UG_API bool ug::IsLonger ( const std::string &  a,
const std::string &  b 
)

checks whether second string is longer than first string

This can be used to get the longest string in a vector of strings:

int maxLength = (*max_element(vecStr.begin(), vecStr.end(), IsLonger)).size();
Parameters
[in]a
[in]b
Returns
true if b is longer then a; false otherwise

◆ LevenshteinDistance()

UG_API size_t ug::LevenshteinDistance ( const std::string &  s1,
const std::string &  s2 
)

Calculate Levenshtein Distance of to strings.

Levenshtein distance calculates the minimum number of edits to transform one string into the other with allowable edit operations insertion, deletion, or substitution of a single character.

Note
taken from http://en.wikipedia.org/wiki/Levenshtein_distance (check copyright or recreate!)
Parameters
[in]s1string 1
[in]s2string 2
Returns
minimum number of edits needed to transform one string into the other

◆ NumberOfDigits()

UG_API int ug::NumberOfDigits ( int  n)

returns the number of digits of an integer (expressed with base 10)

Determines the number of digits for the passed base-10 number. A minus sign is ignored.

Parameters
[in]nnumber to count the number of digits
Returns
number of digits

Referenced by ug::AppendCounterToString(), and ug::operator<<().

◆ PathFromFilename()

UG_API std::string ug::PathFromFilename ( const std::string &  str)

returns best guess of a path without a filename from a given string

returns the part of the string before the last '/' or '\' character (e.g. /sw/bla.txt -> /sw/)

Parameters
[in]strto retrieve the filename from
Returns
best guess of the file name from given path; if no guess can be made '.' is returned

◆ RemoveWhitespaceFromString()

◆ repeat()

◆ ReplaceAll()

UG_API std::string ug::ReplaceAll ( std::string  target,
const std::string &  oldstr,
const std::string &  newstr 
)

Substitutes substrings of given string with other substrings.

Replaces each substring of target string that is equal to oldstr with newstr

Parameters
[in]targetstring to modify
[in]oldstrstring to raplace
[in]newstrreplacement string
Returns
a copy of the specified target string where all occurences of oldstr are replaced with newstr.

◆ SnipString()

UG_API std::string ug::SnipString ( const std::string &  str,
size_t  totalSize,
size_t  replaceLast = 0,
const char  replace = '.' 
)

creates a truncated string and may add truncation symbol at end

Parameters
[in]stringthe string to modify
[in]totalSizethe total size of snippet
[in]replaceLastthe number of last chars to be replaced by symbol
[in]replacethe replace symbol
Returns
the modified string

◆ SnipStringFront()

UG_API std::string ug::SnipStringFront ( const std::string &  str,
size_t  totalSize,
size_t  replaceFront = 0,
const char  replace = '.' 
)

creates a truncated string and may add truncation symbol at front

Parameters
[in]stringthe string to modify
[in]totalSizethe total size of snippet
[in]replaceLastthe number of last chars to be replaced by symbol
[in]replacethe replace symbol
Returns
the modified string

◆ StartsWith()

UG_API bool ug::StartsWith ( const std::string &  str,
const std::string &  search 
)

checks whether a given string starts with a specified substring

Checks whether str starts with search.

Parameters
[in]strstring
[in]searchstring to search for
Returns
true if str starts with search; false otherwise

◆ TokenizeString() [1/3]

UG_API std::vector< std::string > ug::TokenizeString ( const char *  str,
const char  delimiter = ',' 
)

splits the string into parts based on a separating character

returns the string parts using a separator char in order to indicate parts

Parameters
[in]stroriginal string
[in]delimiterchar used as separator
Returns
tokenized parts

References ug::TokenizeString().

◆ TokenizeString() [2/3]

UG_API std::vector<std::string> ug::TokenizeString ( const std::string &  str,
const char  delimiter = ',' 
)

splits the string into parts based on a separating character

returns the string parts using a separator char in order to indicate parts

Parameters
[in]stroriginal string
[in]delimiterchar used as separator
Returns
tokenized parts

◆ TokenizeString() [3/3]

UG_API void ug::TokenizeString ( const std::string &  str,
std::vector< std::string > &  vToken,
const char  delimiter = ',' 
)

splits the string into parts based on a separating char

the string parts using a separator char in order to indicate parts

Note
any prior content of vToken will get deleted
Parameters
[in]stroriginal string
[in,out]vTokentokenized parts
[in]delimiterchar used as separator

◆ TokenizeTrimString() [1/2]

UG_API std::vector<std::string> ug::TokenizeTrimString ( const std::string &  str,
const char  delimiter = ',' 
)

splits the string into trimmed parts based on a separating char

returns the string parts separated by delimiter and trims all parts

Parameters
[in]stroriginal string
[in]delimiterchar used as separator
Returns
tokenized and trimmed parts

◆ TokenizeTrimString() [2/2]

UG_API void ug::TokenizeTrimString ( const std::string &  str,
std::vector< std::string > &  vToken,
const char  delimiter = ',' 
)

splits the string into trimmed parts based on a separating char

places the generated tokens in the given vector.

Note
any prior content of vToken will get deleted
Parameters
[in]stroriginal string
[in,out]vTokentokenized parts
[in]delimiterchar used as separator

◆ ToLower()

UG_API std::string ug::ToLower ( std::string  str)

Returns a lower case version of the specified string.

Note
this function does not support custom locales. Thus, only ascii strings shall be specified.
Parameters
[in]strstring to convert
Returns
a lower case version of the specified string

◆ ToString()

template<typename T >
std::string ug::ToString ( const T &  t)
inline

Convert a object supporting 'std::cout << obj' to a string.

Template Parameters
Ttype of the object; must support std::ostream operator<<()
Parameters
[in]tobject to convert to string
Returns
a string with the object as if you would use operator << (like std::cout)

Referenced by ug::AssembledMultiGridCycle< TDomain, TAlgebra >::log_debug_data(), ug::operator<<(), ug::StringTableStream::operator<<(), ug::StringTableStream::repeat_col(), ug::StringTableStream::set(), and ug::StdTransfer< TDomain, TAlgebra >::write_debug().

◆ ToUpper()

UG_API std::string ug::ToUpper ( std::string  str)

Returns an upper case version of the specified string.

Note
this function does not support custom locales. Thus, only ascii strings shall be specified.
Parameters
[in]strstring to convert
Returns
an upper case version of the specified string

◆ TrimString()

UG_API std::string ug::TrimString ( const std::string &  str)

removes all white space from the front and end of a string

Parameters
[in]stringthe string to modify
Returns
the modified string

◆ WildcardMatch()

UG_API bool ug::WildcardMatch ( const char *  str,
const char *  pattern 
)

wildcard matches like bla.* or *.bla or t?st

Parameters
[in]stra string
[in]patterna pattern with wildcards * or ?
Returns
true if match otherwise false

References UG_ASSERT.

Referenced by ug::bridge::RegisterBridge_Misc(), and ug::DebugIDManager::set_debug_level().

◆ XMLStringEscape()

UG_API std::string ug::XMLStringEscape ( std::string  s)

returns a string suitable for XML files this functions escapes the characters <, >, ', " and &

See also
http://www.hdfgroup.org/HDF5/XML/xml_escape_chars.htm
Parameters
[in]s
Returns
escaped string

this function replaces XML special characters with their escaped versions: & -> & " -> &quot; ' -> "' < -> <

-> >

Parameters
sa normal text
Returns
a text where special XML characters are escaped