29#include "../../include/ioutils/stringUtils.hpp"
38std::string convertToString<std::string>(
const std::string &value,
int precision )
40 static_cast<void>( precision );
47template <
typename typeT>
48typeT stoInt32s(
const std::string &str,
error_t *errc )
52 long val = stoT( str, &_errc, meta::tagT<long>() );
56 if( val < std::numeric_limits<typeT>::lowest() )
59 val = std::numeric_limits<typeT>::lowest();
61 else if( val > std::numeric_limits<typeT>::max() )
64 val = std::numeric_limits<typeT>::max();
73 return static_cast<typeT
>( val );
76char stoT(
const std::string &str,
error_t *errc, meta::tagT<char> )
78 return stoInt32s<char>( str, errc );
81signed char stoT(
const std::string &str,
error_t *errc, meta::tagT<signed char> )
83 return stoInt32s<signed char>( str, errc );
86unsigned char stoT(
const std::string &str,
error_t *errc, meta::tagT<unsigned char> )
88 return stoInt32s<unsigned char>( str, errc );
91short stoT(
const std::string &str,
error_t *errc, meta::tagT<short> )
93 return stoInt32s<short>( str, errc );
96unsigned short stoT(
const std::string &str,
error_t *errc, meta::tagT<unsigned short> )
98 return stoInt32s<unsigned short>( str, errc );
101int stoT(
const std::string &str,
error_t *errc, meta::tagT<int> )
103 return stoInt32s<int>( str, errc );
106unsigned int stoT(
const std::string &str,
error_t *errc, meta::tagT<unsigned int> )
110 unsigned long val = stoT( str, &_errc, meta::tagT<unsigned long>() );
114 if( val > std::numeric_limits<unsigned int>::max() )
117 val = std::numeric_limits<unsigned int>::max();
126 return static_cast<unsigned int>( val );
129long stoT(
const std::string &str,
error_t *errc, meta::tagT<long> )
137 long val = std::strtol( str.c_str(), &end, 10 );
143 else if( end == str.c_str() )
160unsigned long stoT(
const std::string &str,
error_t *errc, meta::tagT<unsigned long> )
168 unsigned long val = std::strtoul( str.c_str(), &end, 10 );
174 else if( end == str.c_str() )
191long long stoT(
const std::string &str,
error_t *errc, meta::tagT<long long> )
199 long long val = std::strtoll( str.c_str(), &end, 10 );
205 else if( end == str.c_str() )
222unsigned long long stoT(
const std::string &str,
error_t *errc, meta::tagT<unsigned long long> )
230 unsigned long long val = std::strtoull( str.c_str(), &end, 10 );
236 else if( end == str.c_str() )
259bool stoT(
const std::string &str,
error_t *errc, meta::tagT<bool> )
263 while( i < str.length()-1 && isspace( c ) )
269 if( c ==
'0' || c ==
'f' || c ==
'F' )
278 else if( c ==
'1' || c ==
't' || c ==
'T' )
288 return static_cast<bool>( stoT( str, errc, meta::tagT<long long>() ) );
291float stoT(
const std::string &str,
error_t *errc, meta::tagT<float> )
299 float val = std::strtof( str.c_str(), &end );
305 else if( end == str.c_str() )
322double stoT(
const std::string &str,
error_t *errc, meta::tagT<double> )
330 double val = std::strtod( str.c_str(), &end );
336 else if( end == str.c_str() )
353long double stoT(
const std::string &str,
error_t *errc, meta::tagT<long double> )
361 long double val = std::strtold( str.c_str(), &end );
367 else if( end == str.c_str() )
385__float128 stoT(
const std::string &str,
error_t *errc, meta::tagT<__float128> )
393 __float128 val = ::strtof128( str.c_str(), &end );
399 else if( end == str.c_str() )
418std::string stoT(
const std::string &str,
error_t *errc, meta::tagT<std::string> )
435 outstr.resize( instr.size() );
437 catch(
const std::length_error &e )
441 catch(
const std::bad_alloc &e )
445 catch(
const std::exception &e )
454 for(
size_t i = 0; i < instr.size(); ++i )
456 outstr[i] = tolower( instr[i] );
482 outstr.resize( instr.size() );
484 catch(
const std::length_error &e )
488 catch(
const std::bad_alloc &e )
492 catch(
const std::exception &e )
501 for(
size_t i = 0; i < instr.size(); ++i )
503 outstr[i] = toupper( instr[i] );
529 outstr.erase( std::remove_if( outstr.begin(), outstr.end(), ::isspace ), outstr.end() );
543int stringWrap( std::vector<std::string> &lines,
const std::string &str,
int width )
545 int L = str.length();
549 int startPos, tmpPos, endPos;
560 endPos = startPos + width;
564 lines.push_back( str.substr( startPos, L - startPos ) );
571 while( !isspace( str[tmpPos] ) && tmpPos >= startPos )
575 if( tmpPos > startPos )
578 lines.push_back( str.substr( startPos, endPos - startPos ) );
583 if( str[startPos] ==
' ' )
constexpr units::realT c()
The speed of light.
error_t
The mxlib error codes.
static constexpr error_t errno2error_t(const int &err)
Convert an errno code to error_t.
@ noerror
No error has occurred.
@ std_exception
An exception was thrown.
@ exception
An exception was thrown.
@ std_bad_alloc
A bad allocation exception was thrown.
@ erange
Numerical result out of range (ERANGE)
@ invalidarg
An argument was invalid.
@ std_length_error
A length error exception was thrown.
int 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.
mx::error_t toLower(std::string &outstr, const std::string &instr)
Convert a string to all lower case.
mx::error_t toUpper(std::string &outstr, const std::string &instr)
Convert a string to all upper case.
void removeWhiteSpace(std::string &outstr, const std::string &instr)
Remove all white space from a string.