diff options
Diffstat (limited to 'libs/tdlib/td/tdutils/td/utils/misc.cpp')
-rw-r--r-- | libs/tdlib/td/tdutils/td/utils/misc.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/libs/tdlib/td/tdutils/td/utils/misc.cpp b/libs/tdlib/td/tdutils/td/utils/misc.cpp new file mode 100644 index 0000000000..f3068ca6d3 --- /dev/null +++ b/libs/tdlib/td/tdutils/td/utils/misc.cpp @@ -0,0 +1,78 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#include "td/utils/misc.h" + +#include "td/utils/port/thread_local.h" + +#include <algorithm> +#include <cstdlib> +#include <locale> +#include <sstream> + +namespace td { + +char *str_dup(Slice str) { + char *res = static_cast<char *>(std::malloc(str.size() + 1)); + if (res == nullptr) { + return nullptr; + } + std::copy(str.begin(), str.end(), res); + res[str.size()] = '\0'; + return res; +} + +string implode(vector<string> v, char delimiter) { + string result; + for (auto &str : v) { + if (!result.empty()) { + result += delimiter; + } + result += str; + } + return result; +} + +string oneline(Slice str) { + string result; + result.reserve(str.size()); + bool after_new_line = true; + for (auto c : str) { + if (c != '\n') { + if (after_new_line) { + if (c == ' ') { + continue; + } + after_new_line = false; + } + result += c; + } else { + after_new_line = true; + result += ' '; + } + } + while (!result.empty() && result.back() == ' ') { + result.pop_back(); + } + return result; +} + +double to_double(Slice str) { + static TD_THREAD_LOCAL std::stringstream *ss; + if (init_thread_local<std::stringstream>(ss)) { + ss->imbue(std::locale::classic()); + } else { + ss->str(std::string()); + ss->clear(); + } + ss->write(str.begin(), narrow_cast<std::streamsize>(str.size())); + + double result = 0.0; + *ss >> result; + return result; +} + +} // namespace td |