| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5fba67f commit 6b1731c
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -666,7 +666,7 @@ void Fill(const FunctionCallbackInfo<Value>& args) { | |||
| 666 | 666 | } else if (enc == UCS2) { | |
| 667 | 667 | str_length = str_obj->Length() * sizeof(uint16_t); | |
| 668 | 668 | node::TwoByteValue str(env->isolate(), args[1]); | |
| 669 | - if (IsBigEndian()) | ||
| 669 | + if constexpr (IsBigEndian()) | ||
| 670 | 670 | SwapBytes16(reinterpret_cast<char*>(&str[0]), str_length); | |
| 671 | 671 | ||
| 672 | 672 | memcpy(ts_obj_data + start, *str, std::min(str_length, fill_length)); | |
@@ -960,7 +960,7 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) { | |||
| 960 | 960 | return args.GetReturnValue().Set(-1); | |
| 961 | 961 | } | |
| 962 | 962 | ||
| 963 | - if (IsBigEndian()) { | ||
| 963 | + if constexpr (IsBigEndian()) { | ||
| 964 | 964 | StringBytes::InlineDecoder decoder; | |
| 965 | 965 | if (decoder.Decode(env, needle, enc).IsNothing()) return; | |
| 966 | 966 | const uint16_t* decoded_string = | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2262,10 +2262,12 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) { | |||
| 2262 | 2262 | auto ext = string->GetExternalOneByteStringResource(); | |
| 2263 | 2263 | buf = const_cast<char*>(ext->data()); | |
| 2264 | 2264 | len = ext->length(); | |
| 2265 | - } else if (enc == UCS2 && IsLittleEndian() && string->IsExternalTwoByte()) { | ||
| 2266 | - auto ext = string->GetExternalStringResource(); | ||
| 2267 | - buf = reinterpret_cast<char*>(const_cast<uint16_t*>(ext->data())); | ||
| 2268 | - len = ext->length() * sizeof(*ext->data()); | ||
| 2265 | + } else if (enc == UCS2 && string->IsExternalTwoByte()) { | ||
| 2266 | + if constexpr (IsLittleEndian()) { | ||
| 2267 | + auto ext = string->GetExternalStringResource(); | ||
| 2268 | + buf = reinterpret_cast<char*>(const_cast<uint16_t*>(ext->data())); | ||
| 2269 | + len = ext->length() * sizeof(*ext->data()); | ||
| 2270 | + } | ||
| 2269 | 2271 | } | |
| 2270 | 2272 | } | |
| 2271 | 2273 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -111,7 +111,7 @@ MaybeLocal<Object> ToBufferEndian(Environment* env, MaybeStackBuffer<T>* buf) { | |||
| 111 | 111 | ||
| 112 | 112 | static_assert(sizeof(T) == 1 || sizeof(T) == 2, | |
| 113 | 113 | "Currently only one- or two-byte buffers are supported"); | |
| 114 | - if (sizeof(T) > 1 && IsBigEndian()) { | ||
| 114 | + if constexpr (sizeof(T) > 1 && IsBigEndian()) { | ||
| 115 | 115 | SPREAD_BUFFER_ARG(ret.ToLocalChecked(), retbuf); | |
| 116 | 116 | SwapBytes16(retbuf_data, retbuf_length); | |
| 117 | 117 | } | |
@@ -128,7 +128,7 @@ void CopySourceBuffer(MaybeStackBuffer<UChar>* dest, | |||
| 128 | 128 | dest->AllocateSufficientStorage(length_in_chars); | |
| 129 | 129 | char* dst = reinterpret_cast<char*>(**dest); | |
| 130 | 130 | memcpy(dst, data, length); | |
| 131 | - if (IsBigEndian()) { | ||
| 131 | + if constexpr (IsBigEndian()) { | ||
| 132 | 132 | SwapBytes16(dst, length); | |
| 133 | 133 | } | |
| 134 | 134 | } | |
@@ -527,7 +527,7 @@ void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) { | |||
| 527 | 527 | ||
| 528 | 528 | char* value = reinterpret_cast<char*>(output) + beginning; | |
| 529 | 529 | ||
| 530 | - if (IsBigEndian()) { | ||
| 530 | + if constexpr (IsBigEndian()) { | ||
| 531 | 531 | SwapBytes16(value, length); | |
| 532 | 532 | } | |
| 533 | 533 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -339,8 +339,7 @@ size_t StringBytes::Write(Isolate* isolate, | |||
| 339 | 339 | // the Buffer, so we need to reorder on BE platforms. See | |
| 340 | 340 | // https://nodejs.org/api/buffer.html regarding Node's "ucs2" | |
| 341 | 341 | // encoding specification | |
| 342 | - if (IsBigEndian()) | ||
| 343 | - SwapBytes16(buf, nbytes); | ||
| 342 | + if constexpr (IsBigEndian()) SwapBytes16(buf, nbytes); | ||
| 344 | 343 | ||
| 345 | 344 | break; | |
| 346 | 345 | } | |
@@ -756,7 +755,7 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate, | |||
| 756 | 755 | ||
| 757 | 756 | case UCS2: { | |
| 758 | 757 | size_t str_len = buflen / 2; | |
| 759 | - if (IsBigEndian()) { | ||
| 758 | + if constexpr (IsBigEndian()) { | ||
| 760 | 759 | uint16_t* dst = node::UncheckedMalloc<uint16_t>(str_len); | |
| 761 | 760 | if (str_len != 0 && dst == nullptr) { | |
| 762 | 761 | *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate); | |
@@ -803,7 +802,7 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate, | |||
| 803 | 802 | // Buffer, so we need to reorder on BE platforms. See | |
| 804 | 803 | // https://nodejs.org/api/buffer.html regarding Node's "ucs2" | |
| 805 | 804 | // encoding specification | |
| 806 | - if (IsBigEndian()) { | ||
| 805 | + if constexpr (IsBigEndian()) { | ||
| 807 | 806 | uint16_t* dst = node::UncheckedMalloc<uint16_t>(buflen); | |
| 808 | 807 | if (dst == nullptr) { | |
| 809 | 808 | *error = node::ERR_MEMORY_ALLOCATION_FAILED(isolate); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -37,6 +37,7 @@ | |||
| 37 | 37 | #include <cstring> | |
| 38 | 38 | ||
| 39 | 39 | #include <array> | |
| 40 | + #include <bit> | ||
| 40 | 41 | #include <limits> | |
| 41 | 42 | #include <memory> | |
| 42 | 43 | #include <set> | |
@@ -778,24 +779,16 @@ inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context, | |||
| 778 | 779 | .Check(); \ | |
| 779 | 780 | } while (0) | |
| 780 | 781 | ||
| 781 | - enum class Endianness { LITTLE, BIG }; | ||
| 782 | - | ||
| 783 | - inline Endianness GetEndianness() { | ||
| 784 | - // Constant-folded by the compiler. | ||
| 785 | - const union { | ||
| 786 | - uint8_t u8[2]; | ||
| 787 | - uint16_t u16; | ||
| 788 | - } u = {{1, 0}}; | ||
| 789 | - return u.u16 == 1 ? Endianness::LITTLE : Endianness::BIG; | ||
| 782 | + constexpr inline bool IsLittleEndian() { | ||
| 783 | + return std::endian::native == std::endian::little; | ||
| 790 | 784 | } | |
| 791 | 785 | ||
| 792 | - inline bool IsLittleEndian() { | ||
| 793 | - return GetEndianness() == Endianness::LITTLE; | ||
| 786 | + constexpr inline bool IsBigEndian() { | ||
| 787 | + return std::endian::native == std::endian::big; | ||
| 794 | 788 | } | |
| 795 | 789 | ||
| 796 | - inline bool IsBigEndian() { | ||
| 797 | - return GetEndianness() == Endianness::BIG; | ||
| 798 | - } | ||
| 790 | + static_assert(IsLittleEndian() || IsBigEndian(), | ||
| 791 | + "Node.js does not support mixed-endian systems"); | ||
| 799 | 792 | ||
| 800 | 793 | // Round up a to the next highest multiple of b. | |
| 801 | 794 | template <typename T> | |
| Back | FazBrowse Home | New Git URL |
0 commit comments