Merge pull request #107 from TsudaKageyu/endian-detect

Detect UTF-16 byte order at compile time
This commit is contained in:
Tsuda Kageyu 2013-03-20 18:28:27 -07:00
commit ca5ce53b1e
3 changed files with 41 additions and 7 deletions

View File

@ -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
/*!

View File

@ -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 <byteswap.h>
#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

View File

@ -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;