From 9fa60bafe314a74488b7d3bad9c61fff810c612d Mon Sep 17 00:00:00 2001 From: Martin Ridgers Date: Tue, 16 Nov 2021 13:48:28 +0100 Subject: Renamed operator<<(bool) to AppendBool() to avoid subtle errors Pointers will get implicitly cast to a boolean type which is unlikely to be the intention when building strings. In particular, left-shifting in a wchar_t* into a char-bacled string builder will end up writing 'true' into the string instead of the intended wchar_t string's value. --- zencore/include/zencore/string.h | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/zencore/include/zencore/string.h b/zencore/include/zencore/string.h index 7af27ca20..a43d57327 100644 --- a/zencore/include/zencore/string.h +++ b/zencore/include/zencore/string.h @@ -219,6 +219,19 @@ public: return AppendRange(String.data(), String.data() + String.size()); } + inline StringBuilderImpl& AppendBool(bool v) + { + // This is a method instead of a << operator overload as the latter can + // easily get called with non-bool types like pointers. It is a very + // subtle behaviour that can cause bugs. + using namespace std::literals; + if (v) + { + return AppendAscii("true"sv); + } + return AppendAscii("false"sv); + } + inline void RemoveSuffix(uint32_t Count) { ZEN_ASSERT(Count <= Size()); @@ -291,15 +304,6 @@ public: inline StringBuilderImpl& operator<<(const char* str) { return AppendAscii(str); } inline StringBuilderImpl& operator<<(const std::string_view str) { return AppendAscii(str); } inline StringBuilderImpl& operator<<(const std::u8string_view str) { return AppendAscii(str); } - inline StringBuilderImpl& operator<<(bool v) - { - using namespace std::literals; - if (v) - { - return AppendAscii("true"sv); - } - return AppendAscii("false"sv); - } protected: inline void Init(C* Base, size_t Capacity) -- cgit v1.2.3