2023-11-03 02:48:21 -07:00
|
|
|
#include "glacier/string/str_format.h"
|
|
|
|
|
|
|
|
namespace glcr {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
void StrFormatNumber(StringBuilder& builder, uint64_t value, uint64_t base) {
|
|
|
|
const char* kHexCharacters = "0123456789ABCDEF";
|
|
|
|
|
|
|
|
if (value < base) {
|
|
|
|
builder.PushBack(kHexCharacters[value]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
StrFormatNumber(builder, value / base, base);
|
|
|
|
|
|
|
|
builder.PushBack(kHexCharacters[value % base]);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
template <>
|
2023-11-19 20:33:15 -08:00
|
|
|
void StrFormatValue(StringBuilder& builder, const uint8_t& value,
|
|
|
|
StringView opts) {
|
2023-11-03 02:48:21 -07:00
|
|
|
StrFormatValue(builder, static_cast<uint64_t>(value), opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2023-11-19 20:33:15 -08:00
|
|
|
void StrFormatValue(StringBuilder& builder, const uint16_t& value,
|
|
|
|
StringView opts) {
|
2023-11-03 02:48:21 -07:00
|
|
|
StrFormatValue(builder, static_cast<uint64_t>(value), opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2023-11-19 20:33:15 -08:00
|
|
|
void StrFormatValue(StringBuilder& builder, const int32_t& value,
|
|
|
|
StringView opts) {
|
2023-11-03 02:48:21 -07:00
|
|
|
StrFormatValue(builder, static_cast<uint64_t>(value), opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2023-11-19 20:33:15 -08:00
|
|
|
void StrFormatValue(StringBuilder& builder, const uint32_t& value,
|
|
|
|
StringView opts) {
|
2023-11-03 02:48:21 -07:00
|
|
|
StrFormatValue(builder, static_cast<uint64_t>(value), opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2023-11-19 20:33:15 -08:00
|
|
|
void StrFormatValue(StringBuilder& builder, const uint64_t& value,
|
|
|
|
StringView opts) {
|
2023-11-03 02:48:21 -07:00
|
|
|
if (opts.find('x') != opts.npos) {
|
|
|
|
builder.PushBack("0x");
|
|
|
|
StrFormatNumber(builder, value, 16);
|
|
|
|
} else {
|
|
|
|
StrFormatNumber(builder, value, 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2023-11-19 20:33:15 -08:00
|
|
|
void StrFormatValue(StringBuilder& builder, const ErrorCode& value,
|
|
|
|
StringView opts) {
|
2023-11-22 12:17:10 -08:00
|
|
|
StrFormatValue(builder, ErrorCodeToStr(value), opts);
|
2023-11-03 02:48:21 -07:00
|
|
|
}
|
|
|
|
|
2023-11-05 09:24:09 -08:00
|
|
|
template <>
|
2023-11-19 20:33:15 -08:00
|
|
|
void StrFormatValue(StringBuilder& builder, const char& value,
|
|
|
|
StringView opts) {
|
2023-11-05 09:24:09 -08:00
|
|
|
builder.PushBack(value);
|
|
|
|
}
|
|
|
|
|
2023-11-03 02:48:21 -07:00
|
|
|
template <>
|
2023-11-19 20:33:15 -08:00
|
|
|
void StrFormatValue(StringBuilder& builder, char const* const& value,
|
2023-11-03 02:48:21 -07:00
|
|
|
StringView opts) {
|
2023-11-19 20:33:15 -08:00
|
|
|
StrFormatInternal(builder, StringView(value));
|
2023-11-03 02:48:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2023-11-19 20:33:15 -08:00
|
|
|
void StrFormatValue(StringBuilder& builder, const StringView& value,
|
|
|
|
StringView opts) {
|
2023-11-03 02:48:21 -07:00
|
|
|
StrFormatInternal(builder, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StrFormatInternal(StringBuilder& builder, StringView format) {
|
|
|
|
// TODO: Consider throwing an error if there are unhandled format
|
|
|
|
builder.PushBack(format);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace glcr
|