Browse Source

Format: Add support for unsigned char

master
Riyyi 2 years ago
parent
commit
8e2e1a13df
  1. 5
      src/ruc/format/formatter.cpp
  2. 13
      src/ruc/format/formatter.h
  3. 8
      test/unit/testutilformat.cpp

5
src/ruc/format/formatter.cpp

@ -81,6 +81,11 @@ void Formatter<const char*>::format(Builder& builder, const char* value) const
value != nullptr ? std::string_view { value, strlen(value) } : "nullptr"); value != nullptr ? std::string_view { value, strlen(value) } : "nullptr");
} }
void Formatter<const unsigned char*>::format(Builder& builder, const unsigned char* value) const
{
return Formatter<const char*>::format(builder, reinterpret_cast<const char*>(value));
}
// Pointer // Pointer
void Formatter<std::nullptr_t>::format(Builder& builder, std::nullptr_t) const void Formatter<std::nullptr_t>::format(Builder& builder, std::nullptr_t) const

13
src/ruc/format/formatter.h

@ -199,6 +199,19 @@ template<size_t N>
struct Formatter<char[N]> : Formatter<const char*> { struct Formatter<char[N]> : Formatter<const char*> {
}; };
template<>
struct Formatter<const unsigned char*> : Formatter<const char*> {
void format(Builder& builder, const unsigned char* value) const;
};
template<>
struct Formatter<unsigned char*> : Formatter<const unsigned char*> {
};
template<size_t N>
struct Formatter<unsigned char[N]> : Formatter<const unsigned char*> {
};
// Pointer // Pointer
template<typename T> template<typename T>

8
test/unit/testutilformat.cpp

@ -109,6 +109,14 @@ TEST_CASE(FormatString)
result = format("{}", cString); result = format("{}", cString);
EXPECT_EQ(result, "C string"); EXPECT_EQ(result, "C string");
const unsigned char unsignedCharArray[] = "µnsïgned çhãr";
result = format("{}", unsignedCharArray);
EXPECT_EQ(result, "µnsïgned çhãr");
const unsigned char* unsignedCharPointer = &unsignedCharArray[0];
result = format("{}", unsignedCharPointer);
EXPECT_EQ(result, "µnsïgned çhãr");
std::string string = "string"; std::string string = "string";
result = format("{}", string); result = format("{}", string);
EXPECT_EQ(result, "string"); EXPECT_EQ(result, "string");

Loading…
Cancel
Save