Skip to content

Commit 5663210

Browse files
committed
use Boost.Endian for endianness
1 parent bacc644 commit 5663210

File tree

6 files changed

+8
-36
lines changed

6 files changed

+8
-36
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ function(boost_json_setup_properties target)
7878
Boost::container_hash
7979
Boost::core
8080
Boost::describe
81+
Boost::endian
8182
Boost::mp11
8283
Boost::system
8384
Boost::throw_exception

include/boost/json/detail/charconv/detail/fast_float/ascii_number.hpp

+3-8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#ifndef BOOST_JSON_DETAIL_CHARCONV_DETAIL_FASTFLOAT_ASCII_NUMBER_HPP
99
#define BOOST_JSON_DETAIL_CHARCONV_DETAIL_FASTFLOAT_ASCII_NUMBER_HPP
1010

11+
#include <boost/endian/conversion.hpp>
1112
#include <boost/json/detail/charconv/detail/fast_float/float_common.hpp>
1213
#include <cctype>
1314
#include <cstdint>
@@ -46,10 +47,7 @@ uint64_t read_u64(const char *chars) {
4647
}
4748
uint64_t val;
4849
::memcpy(&val, chars, sizeof(uint64_t));
49-
#ifdef BOOST_JSON_BIG_ENDIAN
50-
// Need to read as-if the number was in little-endian order.
51-
val = byteswap(val);
52-
#endif
50+
endian::little_to_native_inplace(val);
5351
return val;
5452
}
5553

@@ -63,10 +61,7 @@ void write_u64(uint8_t *chars, uint64_t val) {
6361
}
6462
return;
6563
}
66-
#ifdef BOOST_JSON_BIG_ENDIAN
67-
// Need to read as-if the number was in little-endian order.
68-
val = byteswap(val);
69-
#endif
64+
endian::native_to_little_inplace(val);
7065
::memcpy(chars, &val, sizeof(uint64_t));
7166
}
7267

include/boost/json/detail/config.hpp

-18
Original file line numberDiff line numberDiff line change
@@ -234,24 +234,6 @@
234234
# endif
235235
#endif
236236

237-
238-
#if ! defined(BOOST_JSON_BIG_ENDIAN) && ! defined(BOOST_JSON_LITTLE_ENDIAN)
239-
// Copied from Boost.Endian
240-
# if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
241-
# define BOOST_JSON_LITTLE_ENDIAN
242-
# elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
243-
# define BOOST_JSON_BIG_ENDIAN
244-
# elif defined(__LITTLE_ENDIAN__)
245-
# define BOOST_JSON_LITTLE_ENDIAN
246-
# elif defined(__BIG_ENDIAN__)
247-
# define BOOST_JSON_BIG_ENDIAN
248-
# elif defined(_MSC_VER) || defined(__i386__) || defined(__x86_64__)
249-
# define BOOST_JSON_LITTLE_ENDIAN
250-
# else
251-
# error The Boost.JSON library could not determine the endianness of this platform. Define either BOOST_JSON_BIG_ENDIAN or BOOST_JSON_LITTLE_ENDIAN.
252-
# endif
253-
#endif
254-
255237
#if defined(__cpp_constinit) && __cpp_constinit >= 201907L
256238
# define BOOST_JSON_CONSTINIT constinit
257239
#elif defined(__has_cpp_attribute) && defined(__clang__)

include/boost/json/detail/sse2.hpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ inline uint64_t parse_unsigned( uint64_t r, char const * p, std::size_t n ) noex
327327
#else
328328
uint32_t v;
329329
std::memcpy( &v, p, 4 );
330+
endian::native_to_little_inplace(v);
330331

331332
v -= 0x30303030;
332333

@@ -335,11 +336,7 @@ inline uint64_t parse_unsigned( uint64_t r, char const * p, std::size_t n ) noex
335336
unsigned w2 = (v >> 16) & 0xFF;
336337
unsigned w3 = (v >> 24);
337338

338-
#ifdef BOOST_JSON_BIG_ENDIAN
339-
r = (((r * 10 + w3) * 10 + w2) * 10 + w1) * 10 + w0;
340-
#else
341339
r = (((r * 10 + w0) * 10 + w1) * 10 + w2) * 10 + w3;
342-
#endif
343340
#endif
344341
p += 4;
345342
n -= 4;

include/boost/json/detail/utf8.hpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#ifndef BOOST_JSON_DETAIL_UTF8_HPP
1111
#define BOOST_JSON_DETAIL_UTF8_HPP
1212

13+
#include <boost/endian/conversion.hpp>
1314
#include <boost/json/detail/config.hpp>
1415

1516
#include <cstddef>
@@ -26,12 +27,7 @@ load_little_endian(void const* p)
2627
{
2728
std::uint32_t v = 0;
2829
std::memcpy(&v, p, N);
29-
#ifdef BOOST_JSON_BIG_ENDIAN
30-
v = ((v & 0xFF000000) >> 24) |
31-
((v & 0x00FF0000) >> 8) |
32-
((v & 0x0000FF00) << 8) |
33-
((v & 0x000000FF) << 24);
34-
#endif
30+
endian::little_to_native_inplace(v);
3531
return v;
3632
}
3733

test/cmake-subdir/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ add_subdirectory(../../../container boostorg/container)
1818
add_subdirectory(../../../container_hash boostorg/container_hash)
1919
add_subdirectory(../../../core boostorg/core)
2020
add_subdirectory(../../../describe boostorg/describe)
21+
add_subdirectory(../../../endian boostorg/endian)
2122
add_subdirectory(../../../intrusive boostorg/intrusive)
2223
add_subdirectory(../../../json boostorg/json)
2324
add_subdirectory(../../../move boostorg/move)

0 commit comments

Comments
 (0)