mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
|
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. More... | |
template<typename typeT > | |
typeT | mx::ioutils::convertFromString (const std::string &str) |
Convert a string to a numerical value. More... | |
template<> | |
bool | mx::ioutils::convertFromString< bool > (const std::string &str) |
Template specialization of convertFromString for bool. More... | |
void | mx::ioutils::toLower (std::string &outstr, const std::string &instr) |
Convert a string to all lower case. More... | |
std::string | mx::ioutils::toLower (const std::string &instr) |
Convert a string to all lower case. More... | |
void | mx::ioutils::toUpper (std::string &outstr, const std::string &instr) |
Convert a string to all upper case. More... | |
std::string | mx::ioutils::toUpper (const std::string &instr) |
Convert a string to all upper case. More... | |
void | mx::ioutils::removeWhiteSpace (std::string &outstr, const std::string &instr) |
Remove all white space from a string. More... | |
std::string | mx::ioutils::removeWhiteSpace (const std::string &instr) |
Remove all white space from a string. More... | |
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. More... | |
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. More... | |
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. More... | |
typeT mx::ioutils::convertFromString | ( | const std::string & | str | ) |
Convert a string to a numerical value.
The default version attempts to do the conversion with a simple c style cast. Template specializations handle conversions to the basic types.
Example:
typeT | is the type of the numerical value desired |
[in] | str | is the string to convert |
Definition at line 148 of file stringUtils.hpp.
bool mx::ioutils::convertFromString< bool > | ( | const std::string & | str | ) |
Template specialization of convertFromString for bool.
First looks for 0/1, f/t, or F/T in the first non-space character of str. Otherwise, we use convertFromString<int>.
[in] | str | is the string to convert |
Definition at line 169 of file stringUtils.cpp.
References mx::astro::constants::c().
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:
To produce a fixed width 0 padded string from an integer:
typeT | is the type of value to convert |
width | specifies the maximum width, not including the '\0' |
pad | specifies the pad character |
[in] | value | the 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 82 of file stringUtils.hpp.
References mx::astro::constants::c().
Referenced by mx::AO::sim::simulatedAOSystem< _realT, _wfsT, _reconT, _filterT, _dmT, _turbSeqT, _coronT >::~simulatedAOSystem(), mx::fits::fitsErrText(), mx::app::appConfigurator::get(), mx::AO::analysis::fourierTemporalPSD< _realT, aosysT >::getGridPSD(), and mx::AO::analysis::fourierTemporalPSD< _realT, aosysT >::makePSDGrid().
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
is parsed to a vector as if it was initialized with
typeT | the type to convert the tokens too. |
[out] | v | the vector holding the parsed and converted tokens. Is cleared. |
[in] | s | the string to parse |
[in] | delim | [optional] the delimiter. Default is comma ' ,'. |
Definition at line 287 of file stringUtils.hpp.
Referenced by mx::improc::ADIobservation< _realT, _derotFunctObj >::readPSFSub().
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
is parsed to a vector as if it was initialized with
typeT | the type to convert the tokens too. |
[out] | v | the vector holding the parsed and converted tokens. Is cleared. |
[in] | s | the string to parse |
[in] | delims | the delimiters. |
Definition at line 325 of file stringUtils.hpp.
std::string mx::ioutils::removeWhiteSpace | ( | const std::string & | instr | ) |
Remove all white space from a string.
Uses std::remove_if.
[in] | instr | is the string to remove whitespace from |
Definition at line 234 of file stringUtils.cpp.
References mx::ioutils::removeWhiteSpace().
void mx::ioutils::removeWhiteSpace | ( | std::string & | outstr, |
const std::string & | instr | ||
) |
Remove all white space from a string.
Uses std::remove_if.
[out] | outstr | will contain the new string with no whitespace. |
[in] | instr | is the string to remove whitespace from |
Definition at line 224 of file stringUtils.cpp.
Referenced by mx::sys::gitRepo::getGitFileState(), mx::astro::numSpType(), and mx::ioutils::removeWhiteSpace().
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.
[out] | lines | each new entry contains a wrapped portion of the string. Not cleared, so can accumulate. |
[in] | str | the string to wrap |
[in] | width | the maximum width of the output strings |
Definition at line 244 of file stringUtils.cpp.
Referenced by mx::ioutils::textTable::addCell().
std::string mx::ioutils::toLower | ( | const std::string & | instr | ) |
Convert a string to all lower case.
Calls the c tolower function for each character in instr.
[in] | instr | is the string to convert |
Definition at line 193 of file stringUtils.cpp.
References mx::ioutils::toLower().
void 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.
[out] | outstr | will be resized and populated with the lower case characters |
[in] | instr | is the string to convert |
Definition at line 182 of file stringUtils.cpp.
Referenced by mx::astro::picklesSpectrum< _units >::fileName(), and mx::ioutils::toLower().
std::string mx::ioutils::toUpper | ( | const std::string & | instr | ) |
Convert a string to all upper case.
Calls the c toupper function for each character in instr.
[in] | instr | is the string to convert |
Definition at line 214 of file stringUtils.cpp.
References mx::ioutils::toUpper().
void 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.
[out] | outstr | will be resized and populated with the lower case characters |
[in] | instr | is the string to convert |
Definition at line 203 of file stringUtils.cpp.
Referenced by mx::astro::numSpType(), and mx::ioutils::toUpper().