diff --git a/taglib/toolkit/taglib.h b/taglib/toolkit/taglib.h index 9bc81655..0edd1f00 100755 --- a/taglib/toolkit/taglib.h +++ b/taglib/toolkit/taglib.h @@ -76,6 +76,18 @@ # endif #endif +// Detect CPU endian at compile time rather than run time if possible. +// This is a poor list. Hope someone enrich it. +#if (defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))) \ + || (defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))) \ + || (defined(__clang__) && (defined(__i386__) || defined(__x86_64__))) +# define TAGLIB_LITTLE_ENDIAN +/* +#elif .... +# define TAGLIB_BIG_ENDIAN +*/ +#endif + //! A namespace for all TagLib related classes and functions /*! diff --git a/taglib/toolkit/tstring.cpp b/taglib/toolkit/tstring.cpp index 2eae6b04..ec3dd828 100644 --- a/taglib/toolkit/tstring.cpp +++ b/taglib/toolkit/tstring.cpp @@ -31,7 +31,7 @@ // Determine if the compiler supports codecvt. -#if (defined(_MSC_VER) && _MSC_VER >= 1600) // VC++2010 or later +#if (defined(_MSC_VER) && _MSC_VER >= 1600) # define TAGLIB_USE_CODECVT #endif @@ -42,16 +42,24 @@ # include "unicode.h" #endif +#if defined(__GNUC__) +# include +#endif + using namespace TagLib; namespace { inline unsigned short byteSwap(unsigned short x) { -#if defined(_MSC_VER) && (_MSC_VER >= 1400) // VC++2005 or later +#if defined(_MSC_VER) && (_MSC_VER >= 1400) return _byteswap_ushort(x); +#elif defined(__GNUC__) + + return __bswap_16(x); + #else return (((x) >> 8) & 0xff) | (((x) & 0xff) << 8); @@ -64,9 +72,11 @@ namespace { return (c1 << 8) | c2; } +#if !defined(TAGLIB_LITTLE_ENDIAN) && !defined(TAGLIB_BIG_ENDIAN) + String::Type wcharByteOrder() { - // Detect CPU endian. + // Detect CPU endian at run time. union { TagLib::ushort w; char c; @@ -77,8 +87,9 @@ namespace { else return String::UTF16BE; } -} +#endif +} class String::StringPrivate : public RefCounter { @@ -1000,8 +1011,19 @@ void String::copyFromUTF16(const char *s, size_t length, Type t) } } -String::Type String::WCharByteOrder = wcharByteOrder(); +#if defined(TAGLIB_LITTLE_ENDIAN) +const String::Type String::WCharByteOrder = String::UTF16LE; + +#elif defined(TAGLIB_BIG_ENDIAN) + +const String::Type String::WCharByteOrder = String::UTF16BE; + +#else + +const String::Type String::WCharByteOrder = wcharByteOrder(); + +#endif //////////////////////////////////////////////////////////////////////////////// // related functions diff --git a/taglib/toolkit/tstring.h b/taglib/toolkit/tstring.h index 2ebec75f..32ce6e49 100644 --- a/taglib/toolkit/tstring.h +++ b/taglib/toolkit/tstring.h @@ -512,9 +512,9 @@ namespace TagLib { /*! * Indicates which byte order of UTF-16 is used to store strings internally. * - * \note Set to \e UTF16BE or \e UTF16LE at run time. + * \note \e String::UTF16BE or \e String::UTF16LE */ - static Type WCharByteOrder; + static const Type WCharByteOrder; class StringPrivate;