From 8e2e1a13df67e4d274574b8962cff5b36dbc5cfa Mon Sep 17 00:00:00 2001 From: Riyyi Date: Thu, 22 Sep 2022 14:26:13 +0200 Subject: [PATCH] Format: Add support for unsigned char --- src/ruc/format/formatter.cpp | 5 +++++ src/ruc/format/formatter.h | 13 +++++++++++++ test/unit/testutilformat.cpp | 8 ++++++++ 3 files changed, 26 insertions(+) diff --git a/src/ruc/format/formatter.cpp b/src/ruc/format/formatter.cpp index 31b2778..bbf3a64 100644 --- a/src/ruc/format/formatter.cpp +++ b/src/ruc/format/formatter.cpp @@ -81,6 +81,11 @@ void Formatter::format(Builder& builder, const char* value) const value != nullptr ? std::string_view { value, strlen(value) } : "nullptr"); } +void Formatter::format(Builder& builder, const unsigned char* value) const +{ + return Formatter::format(builder, reinterpret_cast(value)); +} + // Pointer void Formatter::format(Builder& builder, std::nullptr_t) const diff --git a/src/ruc/format/formatter.h b/src/ruc/format/formatter.h index 0c09f10..89562e6 100644 --- a/src/ruc/format/formatter.h +++ b/src/ruc/format/formatter.h @@ -199,6 +199,19 @@ template struct Formatter : Formatter { }; +template<> +struct Formatter : Formatter { + void format(Builder& builder, const unsigned char* value) const; +}; + +template<> +struct Formatter : Formatter { +}; + +template +struct Formatter : Formatter { +}; + // Pointer template diff --git a/test/unit/testutilformat.cpp b/test/unit/testutilformat.cpp index 2df689a..eaf75c8 100644 --- a/test/unit/testutilformat.cpp +++ b/test/unit/testutilformat.cpp @@ -109,6 +109,14 @@ TEST_CASE(FormatString) result = format("{}", cString); 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"; result = format("{}", string); EXPECT_EQ(result, "string");