mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-31 19:24:48 +02:00
Fix endianness bug in write_digit2_separated (#2699)
* Fix endianness bug in write_digit2_separated * Move endianness check to compile time if it possible * Turn 8 into a constant
This commit is contained in:
committed by
GitHub
parent
17a5c808da
commit
214cf13f17
@@ -558,7 +558,15 @@ inline void write_digit2_separated(char* buf, unsigned a, unsigned b,
|
|||||||
auto usep = static_cast<unsigned long long>(sep);
|
auto usep = static_cast<unsigned long long>(sep);
|
||||||
// Add ASCII '0' to each digit byte and insert separators.
|
// Add ASCII '0' to each digit byte and insert separators.
|
||||||
digits |= 0x3030003030003030 | (usep << 16) | (usep << 40);
|
digits |= 0x3030003030003030 | (usep << 16) | (usep << 40);
|
||||||
memcpy(buf, &digits, 8);
|
|
||||||
|
constexpr const size_t len = 8;
|
||||||
|
if (const_check(is_big_endian())) {
|
||||||
|
char tmp[len];
|
||||||
|
memcpy(tmp, &digits, len);
|
||||||
|
std::reverse_copy(tmp, tmp + len, buf);
|
||||||
|
} else {
|
||||||
|
memcpy(buf, &digits, len);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Period> FMT_CONSTEXPR inline const char* get_units() {
|
template <typename Period> FMT_CONSTEXPR inline const char* get_units() {
|
||||||
|
@@ -296,10 +296,18 @@ FMT_CONSTEXPR20 auto bit_cast(const From& from) -> To {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline auto is_big_endian() -> bool {
|
inline auto is_big_endian() -> bool {
|
||||||
|
#ifdef _WIN32
|
||||||
|
return false;
|
||||||
|
#elif defined(__BIG_ENDIAN__)
|
||||||
|
return true;
|
||||||
|
#elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__)
|
||||||
|
return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__;
|
||||||
|
#else
|
||||||
struct bytes {
|
struct bytes {
|
||||||
char data[sizeof(int)];
|
char data[sizeof(int)];
|
||||||
};
|
};
|
||||||
return bit_cast<bytes>(1).data[0] == 0;
|
return bit_cast<bytes>(1).data[0] == 0;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// A fallback implementation of uintptr_t for systems that lack it.
|
// A fallback implementation of uintptr_t for systems that lack it.
|
||||||
@@ -309,7 +317,7 @@ struct fallback_uintptr {
|
|||||||
fallback_uintptr() = default;
|
fallback_uintptr() = default;
|
||||||
explicit fallback_uintptr(const void* p) {
|
explicit fallback_uintptr(const void* p) {
|
||||||
*this = bit_cast<fallback_uintptr>(p);
|
*this = bit_cast<fallback_uintptr>(p);
|
||||||
if (is_big_endian()) {
|
if (const_check(is_big_endian())) {
|
||||||
for (size_t i = 0, j = sizeof(void*) - 1; i < j; ++i, --j)
|
for (size_t i = 0, j = sizeof(void*) - 1; i < j; ++i, --j)
|
||||||
std::swap(value[i], value[j]);
|
std::swap(value[i], value[j]);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user