diff options
| author | Martin Ridgers <[email protected]> | 2021-11-16 13:48:28 +0100 |
|---|---|---|
| committer | Martin Ridgers <[email protected]> | 2021-11-16 14:32:21 +0100 |
| commit | 9fa60bafe314a74488b7d3bad9c61fff810c612d (patch) | |
| tree | 5ca73b78384bbbc0bb7fca99f30c6a17da7a7706 | |
| parent | libcrypto links again libdl so the latter also needs careful handling in xmake (diff) | |
| download | zen-9fa60bafe314a74488b7d3bad9c61fff810c612d.tar.xz zen-9fa60bafe314a74488b7d3bad9c61fff810c612d.zip | |
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.
| -rw-r--r-- | zencore/include/zencore/string.h | 22 |
1 files 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) |