Skip to content

Commit 90f2571

Browse files
deps: update nbytes to 0.1.1
PR-URL: #54277 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Moshe Atlow <[email protected]>
1 parent 9b3d22d commit 90f2571

File tree

3 files changed

+42
-23
lines changed

3 files changed

+42
-23
lines changed

deps/nbytes/include/nbytes.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ void ForceAscii(const char *src, char *dst, size_t len);
9393

9494
// Swaps bytes in place. nbytes is the number of bytes to swap and must be a
9595
// multiple of the word size (checked by function).
96-
bool SwapBytes16(void *data, size_t nbytes);
97-
bool SwapBytes32(void *data, size_t nbytes);
98-
bool SwapBytes64(void *data, size_t nbytes);
96+
bool SwapBytes16(char *data, size_t nbytes);
97+
bool SwapBytes32(char *data, size_t nbytes);
98+
bool SwapBytes64(char *data, size_t nbytes);
9999

100100
// ============================================================================
101101
// Base64 (legacy)
@@ -827,12 +827,12 @@ size_t SearchString(const char *haystack, size_t haystack_length,
827827

828828
// ============================================================================
829829
// Version metadata
830-
#define NBYTES_VERSION "0.1.0"
830+
#define NBYTES_VERSION "0.1.1"
831831

832832
enum {
833833
NBYTES_VERSION_MAJOR = 0,
834834
NBYTES_VERSION_MINOR = 1,
835-
NBYTES_VERSION_REVISION = 0,
835+
NBYTES_VERSION_REVISION = 1,
836836
};
837837

838838
} // namespace nbytes

deps/nbytes/src/nbytes.cpp

+14-17
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace {
4343
#endif
4444
} // namespace
4545

46-
bool SwapBytes16(void *data, size_t nbytes) {
46+
bool SwapBytes16(char *data, size_t nbytes) {
4747
if (nbytes % sizeof(uint16_t) != 0) return false;
4848

4949
#if defined(_MSC_VER)
@@ -59,17 +59,16 @@ bool SwapBytes16(void *data, size_t nbytes) {
5959
#endif
6060

6161
uint16_t temp;
62-
uint8_t *ptr = reinterpret_cast<uint8_t *>(data);
63-
for (size_t i = 0; i < nbytes; i += sizeof(uint16_t)) {
64-
memcpy(&temp, &ptr[i], sizeof(uint16_t));
62+
for (size_t i = 0; i < nbytes; i += sizeof(temp)) {
63+
memcpy(&temp, &data[i], sizeof(temp));
6564
temp = BSWAP_2(temp);
66-
memcpy(&ptr[i], &temp, sizeof(uint16_t));
65+
memcpy(&data[i], &temp, sizeof(temp));
6766
}
6867

6968
return true;
7069
}
7170

72-
bool SwapBytes32(void *data, size_t nbytes) {
71+
bool SwapBytes32(char *data, size_t nbytes) {
7372
if (nbytes % sizeof(uint32_t) != 0) return false;
7473

7574
#if defined(_MSC_VER)
@@ -84,18 +83,17 @@ bool SwapBytes32(void *data, size_t nbytes) {
8483
}
8584
#endif
8685

87-
uint32_t temp = 0;
88-
uint8_t *ptr = reinterpret_cast<uint8_t *>(data);
89-
for (size_t i = 0; i < nbytes; i += sizeof(uint32_t)) {
90-
memcpy(&temp, &ptr[i], sizeof(uint32_t));
86+
uint32_t temp;
87+
for (size_t i = 0; i < nbytes; i += sizeof(temp)) {
88+
memcpy(&temp, &data[i], sizeof(temp));
9189
temp = BSWAP_4(temp);
92-
memcpy(&ptr[i], &temp, sizeof(uint32_t));
90+
memcpy(&data[i], &temp, sizeof(temp));
9391
}
9492

9593
return true;
9694
}
9795

98-
bool SwapBytes64(void *data, size_t nbytes) {
96+
bool SwapBytes64(char *data, size_t nbytes) {
9997
if (nbytes % sizeof(uint64_t) != 0) return false;
10098

10199
#if defined(_MSC_VER)
@@ -110,12 +108,11 @@ bool SwapBytes64(void *data, size_t nbytes) {
110108
}
111109
#endif
112110

113-
uint64_t temp = 0;
114-
uint8_t *ptr = reinterpret_cast<uint8_t *>(data);
115-
for (size_t i = 0; i < nbytes; i += sizeof(uint64_t)) {
116-
memcpy(&temp, &ptr[i], sizeof(uint64_t));
111+
uint64_t temp;
112+
for (size_t i = 0; i < nbytes; i += sizeof(temp)) {
113+
memcpy(&temp, &data[i], sizeof(temp));
117114
temp = BSWAP_8(temp);
118-
memcpy(&ptr[i], &temp, sizeof(uint64_t));
115+
memcpy(&data[i], &temp, sizeof(temp));
119116
}
120117

121118
return true;

deps/nbytes/tests/basic.cpp

+23-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,26 @@
44

55
#include <gtest/gtest.h>
66

7-
TEST(basic, it_works) { SUCCEED(); }
7+
TEST(basic, swap_bytes16) {
8+
std::vector<char> input = {1, 2, 3, 4, 5, 6, 7, 8};
9+
nbytes::SwapBytes16(input.data(), input.size());
10+
std::vector<char> expected = {2, 1, 4, 3, 6, 5, 8, 7};
11+
EXPECT_EQ(input, expected);
12+
SUCCEED();
13+
}
14+
15+
TEST(basic, swap_bytes32) {
16+
std::vector<char> input = {1, 2, 3, 4, 5, 6, 7, 8};
17+
nbytes::SwapBytes32(input.data(), input.size());
18+
std::vector<char> expected = {4, 3, 2, 1, 8, 7, 6, 5};
19+
EXPECT_EQ(input, expected);
20+
SUCCEED();
21+
}
22+
23+
TEST(basic, swap_bytes64) {
24+
std::vector<char> input = {1, 2, 3, 4, 5, 6, 7, 8};
25+
nbytes::SwapBytes64(input.data(), input.size());
26+
std::vector<char> expected = {8, 7, 6, 5, 4, 3, 2, 1};
27+
EXPECT_EQ(input, expected);
28+
SUCCEED();
29+
}

0 commit comments

Comments
 (0)