2018-02-03 17:49:39 +01:00
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
|
2018-02-11 16:58:18 +01:00
|
|
|
#include "td/utils/port/thread_local.h"
|
|
|
|
|
2018-02-03 17:49:39 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdlib>
|
2018-02-03 20:40:06 +01:00
|
|
|
#include <locale>
|
|
|
|
#include <sstream>
|
2018-02-03 17:49:39 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-02-03 21:01:33 +01:00
|
|
|
double to_double(Slice str) {
|
2018-02-03 20:40:06 +01:00
|
|
|
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;
|
2018-02-03 17:49:39 +01:00
|
|
|
}
|
|
|
|
|
2018-04-24 20:17:21 +02:00
|
|
|
Result<string> hex_decode(Slice hex) {
|
|
|
|
if (hex.size() % 2 != 0) {
|
|
|
|
return Status::Error("Wrong hex string length");
|
|
|
|
}
|
|
|
|
string result(hex.size() / 2, '\0');
|
|
|
|
for (size_t i = 0; i < result.size(); i++) {
|
|
|
|
int high = hex_to_int(hex[i + i]);
|
|
|
|
int low = hex_to_int(hex[i + i + 1]);
|
|
|
|
if (high == 16 || low == 16) {
|
|
|
|
return Status::Error("Wrong hex string");
|
|
|
|
}
|
|
|
|
result[i] = static_cast<char>(high * 16 + low); // TODO implementation-defined
|
|
|
|
}
|
|
|
|
return std::move(result);
|
|
|
|
}
|
|
|
|
|
2018-05-17 20:08:51 +02:00
|
|
|
static bool is_url_char(char c) {
|
|
|
|
return is_alnum(c) || c == '-' || c == '.' || c == '_' || c == '~';
|
|
|
|
}
|
|
|
|
|
|
|
|
string url_encode(Slice str) {
|
|
|
|
size_t length = 3 * str.size();
|
|
|
|
for (auto c : str) {
|
|
|
|
length -= 2 * is_url_char(c);
|
|
|
|
}
|
|
|
|
if (length == str.size()) {
|
|
|
|
return str.str();
|
|
|
|
}
|
|
|
|
string result;
|
|
|
|
result.reserve(length);
|
|
|
|
for (auto c : str) {
|
|
|
|
if (is_url_char(c)) {
|
|
|
|
result += c;
|
|
|
|
} else {
|
|
|
|
auto ch = static_cast<unsigned char>(c);
|
|
|
|
result += '%';
|
|
|
|
result += "0123456789ABCDEF"[ch / 16];
|
|
|
|
result += "0123456789ABCDEF"[ch % 16];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CHECK(result.size() == length);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-02-03 17:49:39 +01:00
|
|
|
} // namespace td
|