54 inline std::string
indent(
int indentation)
56 return std::string(indentation,
'\t');
70 std::transform(s.begin(),
74 { return (char)std::tolower(c); });
97 inline std::string
escape_string(
const std::string& input,
const char escape_character,
const std::string& characters_to_escape)
99 std::ostringstream escaped;
100 for (
char ch : input)
102 if (characters_to_escape.find(ch) != std::string::npos)
104 escaped << escape_character;
108 return escaped.str();
132 inline std::string
unescape_string(
const std::string& input,
char escape_character,
const std::string& characters_to_unescape)
135 result.reserve(input.size());
137 auto it = input.begin();
138 while (it != input.end())
141 if (c == escape_character && it + 1 != input.end() && characters_to_unescape.find(*(it + 1)) != std::string::npos)
143 result.push_back(*(++it));
170 template <
typename T>
174 std::istringstream istr(str);
175 istr.imbue(std::locale(
"C"));
201 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
203 return converter.from_bytes(str);
205 catch (
const std::range_error&)
209 std::wstring converted;
212 converted.push_back(
static_cast<unsigned char>(c));
258 template <
typename T,
typename =
void>
263 template <
typename T>
264 struct has_insertion_operator<T, std::void_t<decltype(std::declval<std::ostream&>() << std::declval<T>())>> : std::true_type
275 template <typename T>
276 inline typename std::enable_if<has_insertion_operator<T>::value, std::string>::type to_string(const T& value)
278 std::stringstream stream;
279 stream.imbue(std::locale("C"));
291 template <typename T>
292 inline std::string to_string(T* const& val)
294 std::stringstream stream;
295 stream << std::hex << std::showbase << reinterpret_cast<std::uintptr_t>(val);
314 inline std::string to_string(const std::wstring& str)
318 CBEAM_SUPPRESS_WARNINGS_PUSH()
319 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
320 CBEAM_SUPPRESS_WARNINGS_POP()
321 return converter.to_bytes(str);
323 catch (const std::range_error&)
327 std::string converted;
328 for (wchar_t c : str)
332 converted.push_back(static_cast<char>(c >> 8));
336 converted.push_back(static_cast<char>(c & 0xff));
360 template <typename T>
361 inline std::string to_string(std::chrono::time_point<T> time)
365 time_t curr_time = T::to_time_t(time);
368#pragma warning(suppress : 4996)
369 struct tm* buffer = localtime(&curr_time);
370 strftime(sRep, sizeof(sRep), "%Y-%m-%d %H:%M:%S", buffer);
373 typename T::duration t = time.time_since_epoch();
374 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(t - std::chrono::duration_cast<std::chrono::seconds>(t));
376 std::stringstream result;
377 result << sRep << "." << std::setfill('0') << std::setw(3) << ms.count();
390 template <typename T>
391 std::wstring to_wstring(T value)
393 return from_string<std::wstring>(to_string(value));
Header file containing macros for compiler compatibility and warning suppression.
#define CBEAM_SUPPRESS_WARNINGS_PUSH()
Definition compiler_compatibility.hpp:64
#define CBEAM_SUPPRESS_WARNINGS_POP()
Definition compiler_compatibility.hpp:86
Contains conversion utilities to transform data between different formats and types....
Definition buffer.hpp:35
std::string unescape_string(const std::string &input, char escape_character, const std::string &characters_to_unescape)
Definition string.hpp:132
std::string to_lower(std::string s)
Converts characters A-Z in the given string to lower case and returns the modified string.
Definition string.hpp:68
std::string escape_string(const std::string &input, const char escape_character, const std::string &characters_to_escape)
Definition string.hpp:97
std::string indent(int indentation)
Returns a string consisting of indentation tab characters.
Definition string.hpp:54
T from_string(const std::string &str)
Converts a given std::string to a specified type.
Definition string.hpp:171
std::wstring from_string< std::wstring >(const std::string &str)
Converts the given std::string to std::wstring using UTF-8 to UTF-16 encoding.
Definition string.hpp:196
The has_insertion_operator trait provides static meta-information about whether a type T has overload...
Definition string.hpp:260