From d9bc5f1320332db9a4bf7e103b0813b94e369304 Mon Sep 17 00:00:00 2001 From: Alecto Irene Perez Date: Fri, 3 Mar 2023 12:53:25 -0500 Subject: [PATCH] Fix code causing spurious Wstringop-overflow warning See #2989, #3054, and others --- include/fmt/core.h | 14 +++----------- include/fmt/format.h | 3 ++- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 139a063a5e45..49c3db709b78 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -2206,20 +2206,12 @@ constexpr auto to_ascii(Char c) -> char { return c <= 0xff ? static_cast(c) : '\0'; } -FMT_CONSTEXPR inline auto code_point_length_impl(char c) -> int { - return "\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\0\0\0\0\0\0\0\2\2\2\2\3\3\4" - [static_cast(c) >> 3]; -} - +// Returns the number of code units in a code point or 1 on error. template FMT_CONSTEXPR auto code_point_length(const Char* begin) -> int { if (const_check(sizeof(Char) != 1)) return 1; - int len = code_point_length_impl(static_cast(*begin)); - - // Compute the pointer to the next character early so that the next - // iteration can start working on the next character. Neither Clang - // nor GCC figure out this reordering on their own. - return len + !len; + auto c = static_cast(*begin); + return static_cast((0x3a55000000000000ull >> (2 * (c >> 3))) & 0x3) + 1; } // Return the result via the out param to workaround gcc bug 77539. diff --git a/include/fmt/format.h b/include/fmt/format.h index 657142c4819f..9f53ecdb8e9e 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -651,7 +651,8 @@ FMT_CONSTEXPR inline auto utf8_decode(const char* s, uint32_t* c, int* e) constexpr const int shiftc[] = {0, 18, 12, 6, 0}; constexpr const int shifte[] = {0, 6, 4, 2, 0}; - int len = code_point_length_impl(*s); + int len = "\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\1\0\0\0\0\0\0\0\0\2\2\2\2\3\3\4" + [static_cast(*s) >> 3]; // Compute the pointer to the next character early so that the next // iteration can start working on the next character. Neither Clang // nor GCC figure out this reordering on their own.