Core Library  1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
core_lib::string_utils Namespace Reference

The string_utils namespace. More...

Classes

struct  deletable_facet
 

Enumerations

enum  eSplitStringResult { eSplitStringResult::trimmed, eSplitStringResult::notTrimmed }
 Split string options enumeration. More...
 
enum  eFloatStringFormat { eFloatStringFormat::normal, eFloatStringFormat::fixed, eFloatStringFormat::scientific }
 Format float options enumeration. More...
 

Functions

void CORE_LIBRARY_DLL_SHARED_API PackStdString (std::string &line)
 Tidy a string obtained from getline function. More...
 
void CORE_LIBRARY_DLL_SHARED_API SplitString (std::string &subStr1, std::string &subStr2, const std::string &toSplit, const std::string &delim, eSplitStringResult option)
 Split a string into two parts given delimiters. More...
 
template<typename T >
std::string FormatFloatString (const T value, const int precision=15, eFloatStringFormat formatting=eFloatStringFormat::normal)
 Convert a floating point value to a formatted string representation. More...
 
template<typename T >
std::string AutoFormatFloatString (const T value, int decimalPlaces=1)
 Convert a floating point value to a string representation using the most suitable formatting. More...
 
std::wstring CORE_LIBRARY_DLL_SHARED_API RemoveIllegalChars (const std::wstring &text, const std::wstring &illegalChars=L"~#%&*{}<>?/+|\, wchar_t replacementChar=L '_')
 Return a string with any illegal chars replaced with replacement char. More...
 
std::string CORE_LIBRARY_DLL_SHARED_API RemoveIllegalChars (const std::string &text, const std::string &illegalChars="~#%&*{}<>?/+|\, char replacementChar='_')
 
std::wstring CORE_LIBRARY_DLL_SHARED_API StringToWString (const std::string &text)
 Convert a std::string to std::wstring. More...
 
std::string CORE_LIBRARY_DLL_SHARED_API WStringToString (const std::wstring &text)
 Convert a std::wstring to std::string. More...
 
bool CORE_LIBRARY_DLL_SHARED_API IsAlphaNumeric (const std::wstring &text)
 Return a flag to indicate whether string contains only alphanumeric chars. More...
 
bool CORE_LIBRARY_DLL_SHARED_API IsAlphaNumeric (const std::string &text)
 
template<typename Iter >
std::string MakeHexString (Iter first, Iter last, bool useUppercase, bool insertSpaces)
 Convert a range of data that is convertible to ints to a string of hex values. More...
 
std::vector< std::string > CORE_LIBRARY_DLL_SHARED_API TokeniseString (std::string const &text, std::string const &separator, bool keepEmptyTokens=false)
 Tokenise a string separated by a separator substring and split it into tokens. More...
 
std::string CORE_LIBRARY_DLL_SHARED_API ReplaceTokens (std::string const &text, std::map< std::string, std::string > const &tokenMap)
 Gieven a string containing tokens, replace tokens with specific string values. More...
 

Detailed Description

The string_utils namespace.

Enumeration Type Documentation

◆ eFloatStringFormat

Format float options enumeration.

This enum controls how the floatint point value is represented as a string.

Enumerator
normal 

Default formating.

fixed 

Fixed formatting.

scientific 

Scientific formatting.

◆ eSplitStringResult

Split string options enumeration.

This enum controls how the results are formatted ater splitting the input string.

Enumerator
trimmed 

Trim the results, removing pre/pro-ceeding spaces.

notTrimmed 

Do not trim the results.

Function Documentation

◆ AutoFormatFloatString()

template<typename T >
std::string core_lib::string_utils::AutoFormatFloatString ( const T  value,
int  decimalPlaces = 1 
)

Convert a floating point value to a string representation using the most suitable formatting.

Parameters
[in]value- Floating point value to convert to a string.
[in]decimalPlaces- Number of decimal places to present after the decimal point. return Resultant string.

Convert a single or double precision floating point number to a string representaion.

◆ FormatFloatString()

template<typename T >
std::string core_lib::string_utils::FormatFloatString ( const T  value,
const int  precision = 15,
eFloatStringFormat  formatting = eFloatStringFormat::normal 
)

Convert a floating point value to a formatted string representation.

Parameters
[in]value- Floating point value to convert to a string.
[in]precision- Precision to display the number to as a string.
[in]formatting- Formatting options. return Resultant string.

Convert a single or double precision floating point number to a string representaion.

◆ IsAlphaNumeric()

bool core_lib::string_utils::IsAlphaNumeric ( const std::wstring &  text)

Return a flag to indicate whether string contains only alphanumeric chars.

Parameters
[in]text- Source string potentially with illegal chars.
Returns
True if contains only alphanumeric chars, false otherwise.

◆ MakeHexString()

template<typename Iter >
std::string core_lib::string_utils::MakeHexString ( Iter  first,
Iter  last,
bool  useUppercase,
bool  insertSpaces 
)

Convert a range of data that is convertible to ints to a string of hex values.

Parameters
[in]first- Iterator to first item in range
[in]last- Iterator to end of range
[in]useUppercase- Use uppercase letters for hex values
[in]insertSpaces- Insert spaces between each byte value
Returns
Hex string representing input range.

◆ PackStdString()

void core_lib::string_utils::PackStdString ( std::string &  line)

Tidy a string obtained from getline function.

Parameters
[in,out]line- A string obtained using getline.

Safely convert string into sensible form due to bug in some implementations of std::getline() (looking at you Embarcadero C++ Builder) where size is greater than pos of null char.

◆ RemoveIllegalChars()

std::wstring core_lib::string_utils::RemoveIllegalChars ( const std::wstring &  text,
const std::wstring &  illegalChars = L"~#%&*{}\\:<>?/+|\"",
wchar_t  replacementChar = L'_' 
)

Return a string with any illegal chars replaced with replacement char.

Parameters
[in]text- Source string potentially with illegal chars.
[in]illegalChars- String containing illegal chars to look for in text.
[in]replacementChar- String containing replacement.
Returns
Copy of text with ilegal chars replaced.

◆ ReplaceTokens()

std::string core_lib::string_utils::ReplaceTokens ( std::string const &  text,
std::map< std::string, std::string > const &  tokenMap 
)

Gieven a string containing tokens, replace tokens with specific string values.

Parameters
[in]text- string to replace tokens in
[in]tokenMap- map of token strings and the string values to replace the tokens with in 'text' arg.
Returns
Vector of string tokens.
Copy of text with tokens replaced.

◆ SplitString()

void core_lib::string_utils::SplitString ( std::string &  subStr1,
std::string &  subStr2,
const std::string &  toSplit,
const std::string &  delim,
eSplitStringResult  option 
)

Split a string into two parts given delimiters.

Parameters
[out]subStr1- First output substring.
[out]subStr2- Second output substring.
[in]toSplit- Input string to be split into two.
[in]delim- Delimiters to look for to split around.
[in]option- Options to formt resultant substrings.

Given an input string and a string containing delimiters the input string is split into two parts either side of the deilimiter string. If none of the delimiters can be found then a std::invalid_argument exception is thrown. If the string will be split into more than two substrings then a std::runtime_error exception is thrown.

◆ StringToWString()

std::wstring core_lib::string_utils::StringToWString ( const std::string &  text)

Convert a std::string to std::wstring.

Parameters
[in]text- Source string.
Returns
Converted string.

◆ TokeniseString()

std::vector< std::string > core_lib::string_utils::TokeniseString ( std::string const &  text,
std::string const &  separator,
bool  keepEmptyTokens = false 
)

Tokenise a string separated by a separator substring and split it into tokens.

Parameters
[in]text- string to tokenise
[in]separator- separator string
[in]keepEmptyTokens- keep empty tokens in result vector
Returns
Vector of string tokens.

◆ WStringToString()

std::string core_lib::string_utils::WStringToString ( const std::wstring &  text)

Convert a std::wstring to std::string.

Parameters
[in]text- Source string
Returns
Converted string.