#include <grpc/support/port_platform.h>
#include "src/core/ext/transport/binder/client/connection_id_generator.h"
#ifndef GRPC_NO_BINDER
#include "absl/strings/str_cat.h"
namespace {
std::string Normalize(absl::string_view str_view) {
std::string s = std::string(str_view);
for (size_t i = 0; i < s.length(); i++) {
if (!isalnum(s[i]) && s[i] != '.') {
s[i] = '_';
}
}
return s;
}
std::string StripToLength(const std::string& s, size_t len) {
if (s.length() > len) {
return s.substr(s.length() - len, len);
}
return s;
}
}
namespace grpc_binder {
std::string ConnectionIdGenerator::Generate(absl::string_view uri) {
const size_t kReserveForNumbers = 15;
std::string s =
StripToLength(Normalize(uri), kPathLengthLimit - kReserveForNumbers);
std::string ret;
{
grpc_core::MutexLock l(&m_);
ret = absl::StrCat(s, "-", ++count_);
}
GPR_ASSERT(ret.length() < kPathLengthLimit);
return ret;
}
ConnectionIdGenerator* GetConnectionIdGenerator() {
static ConnectionIdGenerator* cig = new ConnectionIdGenerator();
return cig;
}
}
#endif