flatten 20260225
This commit is contained in:
43
tjp/core/string/to_cstring.hpp
Normal file
43
tjp/core/string/to_cstring.hpp
Normal file
@@ -0,0 +1,43 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
namespace tjp::core {
|
||||
|
||||
template<typename T>
|
||||
String to_cstring(const T &v)
|
||||
{
|
||||
std::ostringstream out_;
|
||||
for (auto &c_ : v)
|
||||
{
|
||||
auto c = static_cast<unsigned char>(c_);
|
||||
|
||||
out_ << "\\x" << std::hex << std::setw(2) << std::setfill('0') << (static_cast<unsigned int>(c) & 0xff);
|
||||
}
|
||||
|
||||
return out_.str();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
String to_printable_with_dots(const T &v)
|
||||
{
|
||||
std::ostringstream out_;
|
||||
for (auto &c_ : v)
|
||||
{
|
||||
if (isprint(static_cast<unsigned char>(c_) && c_ != '\n'))
|
||||
{
|
||||
out_ << c_;
|
||||
}
|
||||
else
|
||||
{
|
||||
out_ << ".";
|
||||
}
|
||||
}
|
||||
|
||||
return out_.str();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
Reference in New Issue
Block a user