|
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. | |
| 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. | |
| typeT mx::ioutils::convertFromString | ( | const std::string & | str, |
| error_t * | errc = nullptr |
||
| ) |
Convert a string to a numerical value.
see stoT
| [in] | str | the string to convert |
| [out] | errc | [optional] mxlib error code |
Definition at line 225 of file stringUtils.hpp.
| 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 85 of file stringUtils.hpp.
Referenced by mx::AO::sim::simulatedAOSystem< _realT, _wfsT, _reconT, _filterT, _dmT, _turbSeqT, _coronT >::~simulatedAOSystem().
| 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 329 of file stringUtils.hpp.
| 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 367 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 533 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 525 of file stringUtils.cpp.
Referenced by mx::sys::gitRepo::getGitFileState(), mx::astro::numSpType(), and mx::ioutils::removeWhiteSpace().
| 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.
| typeT | is the type of the numerical value desired |
Error Codes
| [in] | str | the string to convert |
| [out] | errc | [optional] mxlib error code set during the conversion |
Definition at line 211 of file stringUtils.hpp.
| 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:
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.
| typeT | is the type of the numerical value desired |
Error Codes
| [in] | str | the string to convert |
| [out] | errc | [optional] pointer to an mxlib error code set during the conversion |
Definition at line 189 of file stringUtils.hpp.
| 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 543 of file stringUtils.cpp.
Referenced by mx::ioutils::textTable::addCell().
| 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.
Errors
| [in] | instr | is 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().
| 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.
| [out] | outstr | will be resized and populated with the lower case characters |
| [in] | instr | is 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().
| 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.
Errors
| [in] | instr | is 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().
| 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.
| [out] | outstr | will be resized and populated with the lower case characters |
| [in] | instr | is 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().