#include "util.h"
#include <atomic>
#include <iostream>
namespace sentencepiece {
namespace {
constexpr unsigned int kDefaultSeed = …;
static std::atomic<unsigned int> g_seed = …;
static std::atomic<int> g_minloglevel = …;
}
void SetRandomGeneratorSeed(unsigned int seed) { … }
uint32 GetRandomGeneratorSeed() { … }
namespace logging {
int GetMinLogLevel() { … }
void SetMinLogLevel(int v) { … }
}
namespace string_util {
char32 DecodeUTF8(const char *begin, const char *end, size_t *mblen) { … }
bool IsStructurallyValid(absl::string_view str) { … }
size_t EncodeUTF8(char32 c, char *output) { … }
std::string UnicodeCharToUTF8(const char32 c) { … }
UnicodeText UTF8ToUnicodeText(absl::string_view utf8) { … }
std::string UnicodeTextToUTF8(const UnicodeText &utext) { … }
}
namespace random {
#ifdef SPM_NO_THREADLOCAL
namespace {
class RandomGeneratorStorage {
public:
RandomGeneratorStorage() {
pthread_key_create(&key_, &RandomGeneratorStorage::Delete);
}
virtual ~RandomGeneratorStorage() { pthread_key_delete(key_); }
std::mt19937 *Get() {
auto *result = static_cast<std::mt19937 *>(pthread_getspecific(key_));
if (result == nullptr) {
result = new std::mt19937(GetRandomGeneratorSeed());
pthread_setspecific(key_, result);
}
return result;
}
private:
static void Delete(void *value) { delete static_cast<std::mt19937 *>(value); }
pthread_key_t key_;
};
}
std::mt19937 *GetRandomGenerator() {
static RandomGeneratorStorage *storage = new RandomGeneratorStorage;
return storage->Get();
}
#else
std::mt19937 *GetRandomGenerator() { … }
#endif
}
namespace util {
std::string StrError(int errnum) { … }
std::vector<std::string> StrSplitAsCSV(absl::string_view text) { … }
#ifdef OS_WIN
std::wstring Utf8ToWide(absl::string_view input) {
int output_length = ::MultiByteToWideChar(
CP_UTF8, 0, input.data(), static_cast<int>(input.size()), nullptr, 0);
if (output_length == 0) {
return L"";
}
std::wstring output(output_length, 0);
const int result = ::MultiByteToWideChar(CP_UTF8, 0, input.data(),
static_cast<int>(input.size()),
output.data(), output.size());
return result == output_length ? output : L"";
}
#endif
}
}