mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches

Various tools for working with strings

Files

file  stringUtils.hpp
 Utilities for working with strings.
 
file  stringUtils.cpp
 Implementation of utilities for working with strings.
 

Functions

template<typename typeT , unsigned width = 0, char pad = ' '>
std::string mx::ioutils::convertToString (const typeT &value, int precision=0)
 Convert a numerical value to a string.
 
template<typename typeT >
typeT mx::ioutils::stoT (const std::string &str, error_t *errc=nullptr)
 Convert a string to a numerical value.
 
template<typename typeT >
typeT mx::ioutils::stoT (const std::string &str, error_t &errc)
 Convert a string to a numerical value.
 
template<typename typeT >
typeT mx::ioutils::convertFromString (const std::string &str, error_t *errc=nullptr)
 Convert a string to a numerical value.
 
mx::error_t mx::ioutils::toLower (std::string &outstr, const std::string &instr)
 Convert a string to all lower case.
 
std::string mx::ioutils::toLower (const std::string &instr, mx::error_t *errc=nullptr)
 Convert a string to all lower case.
 
mx::error_t mx::ioutils::toUpper (std::string &outstr, const std::string &instr)
 Convert a string to all upper case.
 
std::string mx::ioutils::toUpper (const std::string &instr, mx::error_t *errc=nullptr)
 Convert a string to all upper case.
 
void mx::ioutils::removeWhiteSpace (std::string &outstr, const std::string &instr)
 Remove all white space from a string.
 
std::string mx::ioutils::removeWhiteSpace (const std::string &instr)
 Remove all white space from a string.
 
int mx::ioutils::stringWrap (std::vector< std::string > &lines, const std::string &str, int width)
 Wrap a string by breaking it into smaller sized portions of a desired width.
 
template<typename typeT >
void mx::ioutils::parseStringVector (std::vector< typeT > &v, const std::string &s, char delim=',')
 Parses a string into a vector of tokens delimited by a character.
 
template<typename typeT >
void mx::ioutils::parseStringVector (std::vector< typeT > &v, const std::string &s, const std::string &delims)
 Parses a string into a vector of tokens delimited by a set of characters.
 

Function Documentation

◆ convertFromString()

template<typename typeT >
typeT mx::ioutils::convertFromString ( const std::string &  str,
error_t errc = nullptr 
)

Convert a string to a numerical value.

see stoT

Deprecated:
Parameters
[in]strthe string to convert
[out]errc[optional] mxlib error code

Definition at line 225 of file stringUtils.hpp.

◆ convertToString()

template<typename typeT , unsigned width = 0, char pad = ' '>
std::string mx::ioutils::convertToString ( const typeT &  value,
int  precision = 0 
)

Convert a numerical value to a string.

The default version uses the stream library to convert. A specialization is provided to prevent conversion of std::string.

The precision parameter is only used for floating point types. If the default precision==0 is passed, then the maximum useful precision is used, from the value of std::numeric_limits<typeT>::max_digits10.

The width and pad template parameters can be used to set a fixed maximum width and a pad character.

Examples:

To convert a double precision number to string:

double d = 2.898434;
std::string str;
str = convertToString(d); //note that you will not normally need to specify <typeT>
std::string convertToString(const typeT &value, int precision=0)
Convert a numerical value to a string.

To produce a fixed width 0 padded string from an integer:

int i = 23;
std::string str;
str = convertToString<int, 5, '0'>(i); //result is "00023".
Template Parameters
typeTis the type of value to convert
widthspecifies the maximum width, not including the '\0'
padspecifies the pad character
Returns
a string representation of value
Parameters
[in]valuethe value of type typeT to be converted
[in]precision[optional] the precision (see http://www.cplusplus.com/reference/ios/ios_base/precision/) to use for floating point types.

Definition at line 85 of file stringUtils.hpp.

Referenced by mx::AO::sim::simulatedAOSystem< _realT, _wfsT, _reconT, _filterT, _dmT, _turbSeqT, _coronT >::~simulatedAOSystem().

◆ parseStringVector() [1/2]

template<typename typeT >
void mx::ioutils::parseStringVector ( std::vector< typeT > &  v,
const std::string &  s,
char  delim = ',' 
)

Parses a string into a vector of tokens delimited by a character.

E.g., the string

std::string s={"0,1,2,3,4"};
std::vector<int> v;
void parseStringVector(std::vector< typeT > &v, const std::string &s, char delim=',')
Parses a string into a vector of tokens delimited by a character.

is parsed to a vector as if it was initialized with

std::vector<int> v = {0,1,2,3,4};
Template Parameters
typeTthe type to convert the tokens too.
Parameters
[out]vthe vector holding the parsed and converted tokens. Is cleared.
[in]sthe string to parse
[in]delim[optional] the delimiter. Default is comma ','.

Definition at line 329 of file stringUtils.hpp.

◆ parseStringVector() [2/2]

template<typename typeT >
void mx::ioutils::parseStringVector ( std::vector< typeT > &  v,
const std::string &  s,
const std::string &  delims 
)

Parses a string into a vector of tokens delimited by a set of characters.

E.g., the string

std::string s={"0,1:2 3,4"};
std::vector<int> v;
parseStringVector(v, s, ",: ");

is parsed to a vector as if it was initialized with

std::vector<int> v = {0,1,2,3,4};
Template Parameters
typeTthe type to convert the tokens too.
Parameters
[out]vthe vector holding the parsed and converted tokens. Is cleared.
[in]sthe string to parse
[in]delimsthe delimiters.

Definition at line 367 of file stringUtils.hpp.

◆ removeWhiteSpace() [1/2]

std::string mx::ioutils::removeWhiteSpace ( const std::string &  instr)

Remove all white space from a string.

Uses std::remove_if.

Returns
the modified string.
Parameters
[in]instris the string to remove whitespace from

Definition at line 533 of file stringUtils.cpp.

References mx::ioutils::removeWhiteSpace().

◆ removeWhiteSpace() [2/2]

void mx::ioutils::removeWhiteSpace ( std::string &  outstr,
const std::string &  instr 
)

Remove all white space from a string.

Uses std::remove_if.

Parameters
[out]outstrwill contain the new string with no whitespace.
[in]instris the string to remove whitespace from

Definition at line 525 of file stringUtils.cpp.

Referenced by mx::sys::gitRepo::getGitFileState(), mx::astro::numSpType(), and mx::ioutils::removeWhiteSpace().

◆ stoT() [1/2]

template<typename typeT >
typeT mx::ioutils::stoT ( const std::string &  str,
error_t errc 
)

Convert a string to a numerical value.

Provides exception-less string conversion.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Template Parameters
typeTis the type of the numerical value desired
Returns
the converted numerical value.

Error Codes

Parameters
[in]strthe string to convert
[out]errc[optional] mxlib error code set during the conversion

Definition at line 211 of file stringUtils.hpp.

◆ stoT() [2/2]

template<typename typeT >
typeT mx::ioutils::stoT ( const std::string &  str,
error_t errc = nullptr 
)

Convert a string to a numerical value.

Provides exception-less string conversion.

Example:

std::string str = "2.34567";
double d;
d = stoT<double>(str, &errc);
{
//do something
}
error_t
The mxlib error codes.
Definition error_t.hpp:26
@ noerror
No error has occurred.

Values of typeT=bool are converted from strings that start with 0/f/F an 1/t/T as false and true respectively. If that fails the string is converted to long long and then to bool, so if it is a valid number that fits in long long, it will evaluate to true or false based on whether or not it is 0.

Complex types (std::complex<realT>) are not supported.

Template Parameters
typeTis the type of the numerical value desired
Returns
the converted numerical value.

Error Codes

Parameters
[in]strthe string to convert
[out]errc[optional] pointer to an mxlib error code set during the conversion

Definition at line 189 of file stringUtils.hpp.

◆ stringWrap()

int mx::ioutils::stringWrap ( std::vector< std::string > &  lines,
const std::string &  str,
int  width 
)

Wrap a string by breaking it into smaller sized portions of a desired width.

Whenever possible breaks at spaces. A single space is discarded at the break.

Parameters
[out]lineseach new entry contains a wrapped portion of the string. Not cleared, so can accumulate.
[in]strthe string to wrap
[in]widththe maximum width of the output strings

Definition at line 543 of file stringUtils.cpp.

Referenced by mx::ioutils::textTable::addCell().

◆ toLower() [1/2]

std::string mx::ioutils::toLower ( const std::string &  instr,
mx::error_t errc = nullptr 
)

Convert a string to all lower case.

Calls the c tolower function for each character in instr.

Returns
the all lower case string

Errors

Parameters
[in]instris the string to convert
[out]errc[optional] the error code indicating success or error

Definition at line 463 of file stringUtils.cpp.

References mx::ioutils::toLower().

◆ toLower() [2/2]

mx::error_t mx::ioutils::toLower ( std::string &  outstr,
const std::string &  instr 
)

Convert a string to all lower case.

Calls the c tolower function for each character in instr.

Returns
mx::error_t::noerror on success
mx::error_t::std_length_error if string::resize throws a length error
mx::error_t::std_bad_alloc if string::resize throws a bad alloc
mx::error_t::std_exception if string::resize throws a std::exception
mx::error_t::exception if string::resize throws any other exception
Parameters
[out]outstrwill be resized and populated with the lower case characters
[in]instris the string to convert

Definition at line 431 of file stringUtils.cpp.

References mx::exception, mx::noerror, mx::std_bad_alloc, mx::std_exception, and mx::std_length_error.

Referenced by mx::astro::picklesSpectrum< _units >::fileName(), and mx::ioutils::toLower().

◆ toUpper() [1/2]

std::string mx::ioutils::toUpper ( const std::string &  instr,
mx::error_t errc = nullptr 
)

Convert a string to all upper case.

Calls the c toupper function for each character in instr.

Returns
the all upper case string

Errors

Parameters
[in]instris the string to convert
[out]errc[optional] the error code indicating success or error

Definition at line 510 of file stringUtils.cpp.

References mx::ioutils::toUpper().

◆ toUpper() [2/2]

mx::error_t mx::ioutils::toUpper ( std::string &  outstr,
const std::string &  instr 
)

Convert a string to all upper case.

Calls the c toupper function for each character in instr.

Returns
mx::error_t::noerror on success
mx::error_t::std_length_error if string::resize throws a length error
mx::error_t::std_bad_alloc if string::resize throws a bad alloc
mx::error_t::std_exception if string::resize throws a std::exception
mx::error_t::exception if string::resize throws any other exception
Parameters
[out]outstrwill be resized and populated with the lower case characters
[in]instris the string to convert

Definition at line 478 of file stringUtils.cpp.

References mx::exception, mx::noerror, mx::std_bad_alloc, mx::std_exception, and mx::std_length_error.

Referenced by mx::astro::numSpType(), and mx::ioutils::toUpper().