mirror of
https://github.com/catchorg/Catch2.git
synced 2024-11-26 07:16:10 +01:00
Avoid technically UB type punning when determining endianness
This commit is contained in:
parent
2fbd66c51c
commit
7ada02e21e
@ -38,13 +38,11 @@ namespace Detail {
|
|||||||
enum Arch { Big, Little };
|
enum Arch { Big, Little };
|
||||||
|
|
||||||
static Arch which() {
|
static Arch which() {
|
||||||
union _{
|
int one = 1;
|
||||||
int asInt;
|
// If the lowest byte we read is non-zero, we can assume
|
||||||
char asChar[sizeof (int)];
|
// that little endian format is used.
|
||||||
} u;
|
auto value = *reinterpret_cast<char*>(&one);
|
||||||
|
return value ? Little : Big;
|
||||||
u.asInt = 1;
|
|
||||||
return ( u.asChar[sizeof(int)-1] == 1 ) ? Big : Little;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -241,13 +239,13 @@ std::string StringMaker<std::nullptr_t>::convert(std::nullptr_t) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int StringMaker<float>::precision = 5;
|
int StringMaker<float>::precision = 5;
|
||||||
|
|
||||||
std::string StringMaker<float>::convert(float value) {
|
std::string StringMaker<float>::convert(float value) {
|
||||||
return fpToString(value, precision) + 'f';
|
return fpToString(value, precision) + 'f';
|
||||||
}
|
}
|
||||||
|
|
||||||
int StringMaker<double>::precision = 10;
|
int StringMaker<double>::precision = 10;
|
||||||
|
|
||||||
std::string StringMaker<double>::convert(double value) {
|
std::string StringMaker<double>::convert(double value) {
|
||||||
return fpToString(value, precision);
|
return fpToString(value, precision);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user