From c2fd7708adef3f47eca770c7d05195bb31325212 Mon Sep 17 00:00:00 2001 From: Matt Date: Thu, 31 Dec 2020 15:57:36 +0200 Subject: [PATCH] Use char_traits for length --- src/openrct2/localisation/Formatting.h | 31 ++++++++++++++++++-------- test/tests/FormattingTests.cpp | 2 +- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/openrct2/localisation/Formatting.h b/src/openrct2/localisation/Formatting.h index 8c93e1cf6b..cf056c8160 100644 --- a/src/openrct2/localisation/Formatting.h +++ b/src/openrct2/localisation/Formatting.h @@ -24,13 +24,14 @@ namespace OpenRCT2 { - template class FormatBufferBase + template> class FormatBufferBase { T _storage[StackSize]; T* _buffer; size_t _size; // NOTE: Capacity is on purpose uint32_t to have a fixed position for the flag on each architecture. uint32_t _capacity; + TTraits _traits; static constexpr uint32_t FlagLocalStorage = (1u << 31); @@ -39,7 +40,7 @@ namespace OpenRCT2 : _storage{} , _buffer(_storage) , _size{} - , _capacity(FlagLocalStorage | static_cast(std::size(_storage))) + , _capacity(FlagLocalStorage | static_cast(StackSize)) { } @@ -76,12 +77,9 @@ namespace OpenRCT2 return _buffer; } - auto& operator<<(const T* v) + template auto& operator<<(T const (&v)[N]) { - if (!v) - return *this; - - append(v, LenFn(v)); + append(v, N); return *this; } @@ -91,13 +89,27 @@ namespace OpenRCT2 return *this; } + auto& operator<<(const T* v) + { + if (!v) + return *this; + + append(v, _traits.length(v)); + return *this; + } + auto& operator<<(const std::basic_string_view v) { append(v.data(), v.size()); return *this; } - private: + auto& operator<<(const std::basic_string& v) + { + append(v.data(), v.size()); + return *this; + } + void append(const T* buf, size_t len) { ensure_capacity(len); @@ -108,6 +120,7 @@ namespace OpenRCT2 _buffer[_size] = T{}; } + private: void ensure_capacity(size_t additionalSize) { const size_t curSize = size(); @@ -130,7 +143,7 @@ namespace OpenRCT2 } }; - using FormatBuffer = FormatBufferBase; + using FormatBuffer = FormatBufferBase; using FormatArg_t = std::variant; diff --git a/test/tests/FormattingTests.cpp b/test/tests/FormattingTests.cpp index 373237df59..7f7790b0b5 100644 --- a/test/tests/FormattingTests.cpp +++ b/test/tests/FormattingTests.cpp @@ -607,7 +607,7 @@ TEST_F(FormattingTests, format_number_comma2dp32_large_value_negative) TEST_F(FormattingTests, buffer_storage_swap) { - FormatBufferBase ss; + FormatBufferBase ss; ss << "Hello World"; ASSERT_STREQ(ss.data(), "Hello World"); ss << ", Exceeding local storage";