Loading [MathJax]/extensions/tex2jax.js
mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages

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::convertFromString (const std::string &str)
 Convert a string to a numerical value.
 
template<>
bool mx::ioutils::convertFromString< bool > (const std::string &str)
 Template specialization of convertFromString for bool.
 
void 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)
 Convert a string to all lower case.
 
void 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)
 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)

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:

std::string str = "2.34567";
double d;
d = convertFromString<double>(str);
Template Parameters
typeTis the type of the numerical value desired
Returns
the converted numerical value.
Parameters
[in]stris the string to convert

Definition at line 144 of file stringUtils.hpp.

◆ convertFromString< bool >()

template<>
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>.

Returns
the converted numerical value.
Parameters
[in]stris the string to convert

Definition at line 165 of file stringUtils.cpp.

◆ 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 83 of file stringUtils.hpp.

Referenced by mx::AO::sim::simulatedAOSystem< _realT, _wfsT, _reconT, _filterT, _dmT, _turbSeqT, _coronT >::~simulatedAOSystem(), mx::AO::analysis::fourierTemporalPSD< _realT, aosysT >::analyzePSDGrid(), mx::fits::fitsErrText(), mx::app::appConfigurator::get(), mx::app::appConfigurator::get(), mx::AO::analysis::fourierTemporalPSD< _realT, aosysT >::getGridPSD(), mx::AO::analysis::fourierTemporalPSD< _realT, aosysT >::intensityPSD(), and mx::AO::analysis::fourierTemporalPSD< _realT, aosysT >::makePSDGrid().

◆ 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 282 of file stringUtils.hpp.

Referenced by mx::improc::ADIobservation< _realT, _derotFunctObj >::readPSFSub().

◆ 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 320 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 227 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 219 of file stringUtils.cpp.

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

◆ 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 237 of file stringUtils.cpp.

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

◆ toLower() [1/2]

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.

Returns
the all lower case string
Parameters
[in]instris the string to convert

Definition at line 190 of file stringUtils.cpp.

References mx::ioutils::toLower().

◆ toLower() [2/2]

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.

Parameters
[out]outstrwill be resized and populated with the lower case characters
[in]instris the string to convert

Definition at line 181 of file stringUtils.cpp.

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

◆ toUpper() [1/2]

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.

Returns
the all lower case string
Parameters
[in]instris the string to convert

Definition at line 209 of file stringUtils.cpp.

References mx::ioutils::toUpper().

◆ toUpper() [2/2]

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.

Parameters
[out]outstrwill be resized and populated with the lower case characters
[in]instris the string to convert

Definition at line 200 of file stringUtils.cpp.

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