Skip to content

Commit

Permalink
Optimised writeNumeric (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlegiantJGC authored Sep 5, 2024
1 parent dd04147 commit c580f57
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/amulet_nbt/include/amulet_nbt/io/binary_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ namespace AmuletNBT {
/**
* Fix the endianness of the numeric value and write it to the buffer.
*/
template <typename T> inline void writeNumeric(const T& value) {
// Create
char* src = (char*)&value;

// Copy
if (endianness == std::endian::native){
data.append(src, sizeof(T));
} else {
for (size_t i = 0; i < sizeof(T); i++){
data.push_back(src[sizeof(T) - i - 1]);
template <typename T> void writeNumeric(const T& value) {
if (endianness == std::endian::native) {
data.append((char*)&value, sizeof(T));
}
else {
T value_reverse;
char* src = (char*)&value;
char* dst = (char*)&value_reverse;
for (size_t i = 0; i < sizeof(T); i++) {
dst[i] = src[sizeof(T) - i - 1];
}
data.append(dst, sizeof(T));
}
}

Expand Down

0 comments on commit c580f57

Please sign in to comment.