33 inline std::string
strip(
const std::string& str,
34 const std::string& whitespace =
" \t")
36 const auto strBegin = str.find_first_not_of(whitespace);
37 if (strBegin == std::string::npos)
39 const auto strEnd = str.find_last_not_of(whitespace);
40 const auto strRange = strEnd - strBegin + 1;
41 return str.substr(strBegin, strRange);
50 template<
typename ... Args>
53 size_t size = std::snprintf(
nullptr, 0, format.c_str(), args ... ) + 1;
54 std::unique_ptr<char[]> buf(
new char[ size ]);
55 std::snprintf(buf.get(), size, format.c_str(), args ...);
56 return std::string(buf.get(), buf.get() + size - 1);
65 inline std::pair<std::string, size_t>
search_replace(std::string in,
const std::string& from,
const std::string& to)
67 size_t replacements = 0;
70 std::string::size_type toLen = to.length();
71 std::string::size_type frLen = from.length();
72 std::string::size_type loc = 0;
74 while (std::string::npos != (loc = in.find(from, loc)))
76 in.replace(loc, frLen, to);
80 if (loc >= in.length())
85 return std::make_pair(in, replacements);
89 struct Latex_output_ID;
90 struct Debug_output_ID;
100 template <
typename ...Args>
109 template<
class Config = DefaultReprConfig,
typename T>
110 typename std::enable_if<std::is_integral<T>::value, std::string>::type
111 repr(T a) {
return std::to_string(a); }
113 template<
class Config,
class Float>
115 typename std::enable_if<std::is_floating_point<Float>::value, std::string>::type
117 std::string basic_output =
stator::string_format(
"%.*g", std::numeric_limits<Float>::max_digits10 - Config::Rounding_digits, a);
118 if (Config::Latex_output) {
124 if (fin.second)
return fin.first +
"}";
128 if (fin.second)
return fin.first +
"}";
std::pair< std::string, size_t > search_replace(std::string in, const std::string &from, const std::string &to)
Search and replace elements in a std::string.
std::enable_if< std::is_integral< T >::value, std::string >::type repr(T a)
std::string string_format(const std::string &format, Args ... args)
A C++ version of snprintf allowing simple formatting of output. format A printf-style formatting str...
std::string strip(const std::string &str, const std::string &whitespace=" \)
A C++ version of python's string trim function str The string to be trimmed. whitespace A string of...
The stator library namespace.