#include "common.h"
#include <dmlc/thread_local.h>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <string>
#include <utility>
#include "./random.h"
namespace xgboost::common {
struct RandomThreadLocalEntry {
GlobalRandomEngine engine;
};
using RandomThreadLocalStore = dmlc::ThreadLocalStore<RandomThreadLocalEntry>;
GlobalRandomEngine &GlobalRandom() { return RandomThreadLocalStore::Get()->engine; }
void EscapeU8(std::string const &string, std::string *p_buffer) {
auto &buffer = *p_buffer;
for (size_t i = 0; i < string.length(); i++) {
const auto ch = string[i];
if (ch == '\\') {
if (i < string.size() && string[i + 1] == 'u') {
buffer += "\\";
} else {
buffer += "\\\\";
}
} else if (ch == '"') {
buffer += "\\\"";
} else if (ch == '\b') {
buffer += "\\b";
} else if (ch == '\f') {
buffer += "\\f";
} else if (ch == '\n') {
buffer += "\\n";
} else if (ch == '\r') {
buffer += "\\r";
} else if (ch == '\t') {
buffer += "\\t";
} else if (static_cast<uint8_t>(ch) <= 0x1f) {
char buf[8];
snprintf(buf, sizeof buf, "\\u%04x", ch);
buffer += buf;
} else {
buffer += ch;
}
}
}
std::string HumanMemUnit(std::size_t n_bytes) {
auto n_bytes_f64 = static_cast<double>(n_bytes);
double constexpr k1024 = 1024.0;
using P = std::pair<std::int32_t, StringView>;
std::stringstream ss;
for (auto pu : {P{3, "GB"}, P{2, "MB"}, P{1, "KB"}}) {
auto const [power, unit] = pu; if (n_bytes_f64 >= (std::pow(k1024, power))) {
ss << (n_bytes_f64 / std::pow(k1024, power)) << unit;
return ss.str();
}
}
ss << n_bytes_f64 << "B";
return ss.str();
}
}