diff --git a/taglib/toolkit/tbyteswap.cpp b/taglib/toolkit/tbyteswap.cpp index 7aeea902..714033e8 100644 --- a/taglib/toolkit/tbyteswap.cpp +++ b/taglib/toolkit/tbyteswap.cpp @@ -70,8 +70,17 @@ || (defined(__clang__) && defined(__BIG_ENDIAN__)) # define TAGLIB_BIG_ENDIAN -#endif +#else +namespace { + bool isLittleEndian() + { + TagLib::ushort x = 1; + return (*reinterpret_cast(&x) == 1); + } +} + +#endif namespace TagLib { @@ -172,21 +181,17 @@ namespace TagLib #endif } - bool isLittleEndianSystem() - { #if defined(TAGLIB_LITTLE_ENDIAN) - return true; + const bool isLittleEndianSystem = true; #elif defined(TAGLIB_BIG_ENDIAN) - return false; + const bool isLittleEndianSystem = false; #else - ushort x = 1; - return (*reinterpret_cast(&x) == 1); + const bool isLittleEndianSystem = isLittleEndianSystem(); #endif - } } diff --git a/taglib/toolkit/tbyteswap.h b/taglib/toolkit/tbyteswap.h index 6121414c..012a3173 100644 --- a/taglib/toolkit/tbyteswap.h +++ b/taglib/toolkit/tbyteswap.h @@ -46,10 +46,10 @@ namespace TagLib ulonglong byteSwap64(ulonglong x); /*! - * Detects the system byte order. - * Returns \a true if little endian, \a false if big endian. + * Indicates the system byte order. + * \a true if little endian, \a false if big endian. */ - bool isLittleEndianSystem(); + extern const bool isLittleEndianSystem; } #endif diff --git a/taglib/toolkit/tbytevector.cpp b/taglib/toolkit/tbytevector.cpp index 510e6cf6..31e11d65 100644 --- a/taglib/toolkit/tbytevector.cpp +++ b/taglib/toolkit/tbytevector.cpp @@ -183,25 +183,25 @@ T byteSwap(T x) } template <> -unsigned short byteSwap(unsigned short x) +ushort byteSwap(ushort x) { return byteSwap16(x); } template <> -unsigned int byteSwap(unsigned int x) +uint byteSwap(uint x) { return byteSwap32(x); } template <> -unsigned long long byteSwap(unsigned long long x) +ulonglong byteSwap(ulonglong x) { return byteSwap64(x); } template -T toNumber(const ByteVector &v, bool swapBytes) +T toNumber(const ByteVector &v, bool mostSignificantByteFirst) { if(v.isEmpty()) { debug("toNumber() -- data is empty, returning 0"); @@ -212,26 +212,26 @@ T toNumber(const ByteVector &v, bool swapBytes) if(v.size() >= size) { - if(swapBytes) + if(isLittleEndianSystem == mostSignificantByteFirst) return byteSwap(*reinterpret_cast(v.data())); else return *reinterpret_cast(v.data()); } - const uint last = v.size() > size ? size - 1 : v.size() - 1; + const uint last = std::min(v.size() - 1, size); T sum = 0; for(uint i = 0; i <= last; i++) - sum |= (T) uchar(v[i]) << ((swapBytes ? last - i : i) * 8); + sum |= (T) uchar(v[i]) << ((mostSignificantByteFirst ? last - i : i) * 8); return sum; } template -ByteVector fromNumber(T value, bool swapBytes) +ByteVector fromNumber(T value, bool mostSignificantByteFirst) { const size_t size = sizeof(T); - if(swapBytes) + if(isLittleEndianSystem == mostSignificantByteFirst) value = byteSwap(value); return ByteVector(reinterpret_cast(&value), size); @@ -353,17 +353,17 @@ ByteVector ByteVector::fromCString(const char *s, uint length) ByteVector ByteVector::fromUInt(uint value, bool mostSignificantByteFirst) { - return fromNumber(value, (isLittleEndian == mostSignificantByteFirst)); + return fromNumber(value, mostSignificantByteFirst); } ByteVector ByteVector::fromShort(short value, bool mostSignificantByteFirst) { - return fromNumber(value, (isLittleEndian == mostSignificantByteFirst)); + return fromNumber(value, mostSignificantByteFirst); } ByteVector ByteVector::fromLongLong(long long value, bool mostSignificantByteFirst) { - return fromNumber(value, (isLittleEndian == mostSignificantByteFirst)); + return fromNumber(value, mostSignificantByteFirst); } //////////////////////////////////////////////////////////////////////////////// @@ -684,22 +684,22 @@ TagLib::uint ByteVector::checksum() const TagLib::uint ByteVector::toUInt(bool mostSignificantByteFirst) const { - return toNumber(*this, (isLittleEndian == mostSignificantByteFirst)); + return toNumber(*this, mostSignificantByteFirst); } short ByteVector::toShort(bool mostSignificantByteFirst) const { - return toNumber(*this, (isLittleEndian == mostSignificantByteFirst)); + return toNumber(*this, mostSignificantByteFirst); } unsigned short ByteVector::toUShort(bool mostSignificantByteFirst) const { - return toNumber(*this, (isLittleEndian == mostSignificantByteFirst)); + return toNumber(*this, mostSignificantByteFirst); } long long ByteVector::toLongLong(bool mostSignificantByteFirst) const { - return toNumber(*this, (isLittleEndian == mostSignificantByteFirst)); + return toNumber(*this, mostSignificantByteFirst); } const char &ByteVector::operator[](int index) const @@ -816,12 +816,6 @@ void ByteVector::detach() d = new ByteVectorPrivate(d->data->data, d->offset, d->length); } } - -//////////////////////////////////////////////////////////////////////////////// -// private members -//////////////////////////////////////////////////////////////////////////////// - -const bool ByteVector::isLittleEndian = isLittleEndianSystem(); } //////////////////////////////////////////////////////////////////////////////// diff --git a/taglib/toolkit/tbytevector.h b/taglib/toolkit/tbytevector.h index d43e3e78..ca810130 100644 --- a/taglib/toolkit/tbytevector.h +++ b/taglib/toolkit/tbytevector.h @@ -445,11 +445,6 @@ namespace TagLib { void detach(); private: - /*! - * Indicates the system endian. \a true if little endian, \a false if big endian. - */ - static const bool isLittleEndian; - class ByteVectorPrivate; ByteVectorPrivate *d; }; diff --git a/taglib/toolkit/tstring.cpp b/taglib/toolkit/tstring.cpp index d6e102c3..5fe6f45e 100644 --- a/taglib/toolkit/tstring.cpp +++ b/taglib/toolkit/tstring.cpp @@ -821,7 +821,7 @@ void String::copyFromUTF16(const char *s, size_t length, Type t) internalCopyFromUTF16(s, length, t); } -const String::Type String::WCharByteOrder = isLittleEndianSystem() ? String::UTF16LE : String::UTF16BE; +const String::Type String::WCharByteOrder = isLittleEndianSystem ? String::UTF16LE : String::UTF16BE; } ////////////////////////////////////////////////////////////////////////////////