Qt 4.8
Static Public Functions | List of all members
QUtf32 Struct Reference

#include <qutfcodec_p.h>

Static Public Functions

static QByteArray convertFromUnicode (const QChar *, int, QTextCodec::ConverterState *, DataEndianness=DetectEndianness)
 
static QString convertToUnicode (const char *, int, QTextCodec::ConverterState *, DataEndianness=DetectEndianness)
 

Detailed Description

Definition at line 80 of file qutfcodec_p.h.

Functions

◆ convertFromUnicode()

QByteArray QUtf32::convertFromUnicode ( const QChar uc,
int  len,
QTextCodec::ConverterState state,
DataEndianness  e = DetectEndianness 
)
static

Definition at line 388 of file qutfcodec.cpp.

Referenced by QUtf32Codec::convertFromUnicode().

389 {
390  DataEndianness endian = e;
391  int length = 4*len;
392  if (!state || (!(state->flags & QTextCodec::IgnoreHeader))) {
393  length += 4;
394  }
395  if (e == DetectEndianness) {
396  endian = (QSysInfo::ByteOrder == QSysInfo::BigEndian) ? BigEndianness : LittleEndianness;
397  }
398 
399  QByteArray d(length, Qt::Uninitialized);
400  char *data = d.data();
401  if (!state || !(state->flags & QTextCodec::IgnoreHeader)) {
402  if (endian == BigEndianness) {
403  data[0] = 0;
404  data[1] = 0;
405  data[2] = (char)0xfe;
406  data[3] = (char)0xff;
407  } else {
408  data[0] = (char)0xff;
409  data[1] = (char)0xfe;
410  data[2] = 0;
411  data[3] = 0;
412  }
413  data += 4;
414  }
415  if (endian == BigEndianness) {
416  for (int i = 0; i < len; ++i) {
417  uint cp = uc[i].unicode();
418  if (uc[i].isHighSurrogate() && i < len - 1)
419  cp = QChar::surrogateToUcs4(cp, uc[++i].unicode());
420  *(data++) = cp >> 24;
421  *(data++) = (cp >> 16) & 0xff;
422  *(data++) = (cp >> 8) & 0xff;
423  *(data++) = cp & 0xff;
424  }
425  } else {
426  for (int i = 0; i < len; ++i) {
427  uint cp = uc[i].unicode();
428  if (uc[i].isHighSurrogate() && i < len - 1)
429  cp = QChar::surrogateToUcs4(cp, uc[++i].unicode());
430  *(data++) = cp & 0xff;
431  *(data++) = (cp >> 8) & 0xff;
432  *(data++) = (cp >> 16) & 0xff;
433  *(data++) = cp >> 24;
434  }
435  }
436 
437  if (state) {
438  state->remainingChars = 0;
440  }
441  return d;
442 }
double d
Definition: qnumeric_p.h:62
ushort unicode() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qchar.h:251
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
DataEndianness
Definition: qutfcodec_p.h:61
static const char * data(const QByteArray &arr)
unsigned int uint
Definition: qglobal.h:996
ConversionFlags flags
Definition: qtextcodec.h:106
static uint surrogateToUcs4(ushort high, ushort low)
Converts a UTF16 surrogate pair with the given high and low values to its UCS-4 code point...
Definition: qchar.h:297

◆ convertToUnicode()

QString QUtf32::convertToUnicode ( const char *  chars,
int  len,
QTextCodec::ConverterState state,
DataEndianness  e = DetectEndianness 
)
static

Definition at line 444 of file qutfcodec.cpp.

Referenced by QUtf32Codec::convertToUnicode(), and QString::fromUcs4().

445 {
446  DataEndianness endian = e;
447  uchar tuple[4];
448  int num = 0;
449  bool headerdone = false;
450  if (state) {
451  headerdone = state->flags & QTextCodec::IgnoreHeader;
452  if (endian == DetectEndianness) {
453  endian = (DataEndianness)state->state_data[Endian];
454  }
455  num = state->remainingChars;
456  memcpy(tuple, &state->state_data[Data], 4);
457  }
458  if (headerdone && endian == DetectEndianness)
459  endian = (QSysInfo::ByteOrder == QSysInfo::BigEndian) ? BigEndianness : LittleEndianness;
460 
461  QString result;
462  result.resize((num + len) >> 2 << 1); // worst case
463  QChar *qch = (QChar *)result.unicode();
464 
465  const char *end = chars + len;
466  while (chars < end) {
467  tuple[num++] = *chars++;
468  if (num == 4) {
469  if (!headerdone) {
470  if (endian == DetectEndianness) {
471  if (endian == DetectEndianness) {
472  if (tuple[0] == 0xff && tuple[1] == 0xfe && tuple[2] == 0 && tuple[3] == 0 && endian != BigEndianness) {
473  endian = LittleEndianness;
474  num = 0;
475  continue;
476  } else if (tuple[0] == 0 && tuple[1] == 0 && tuple[2] == 0xfe && tuple[3] == 0xff && endian != LittleEndianness) {
477  endian = BigEndianness;
478  num = 0;
479  continue;
480  } else if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
481  endian = BigEndianness;
482  } else {
483  endian = LittleEndianness;
484  }
485  }
486  } else if (((endian == BigEndianness) ? qFromBigEndian<quint32>(tuple) : qFromLittleEndian<quint32>(tuple)) == QChar::ByteOrderMark) {
487  num = 0;
488  continue;
489  }
490  }
491  uint code = (endian == BigEndianness) ? qFromBigEndian<quint32>(tuple) : qFromLittleEndian<quint32>(tuple);
492  if (code >= 0x10000) {
493  *qch++ = QChar::highSurrogate(code);
494  *qch++ = QChar::lowSurrogate(code);
495  } else {
496  *qch++ = code;
497  }
498  num = 0;
499  }
500  }
501  result.truncate(qch - result.unicode());
502 
503  if (state) {
504  if (headerdone)
506  state->state_data[Endian] = endian;
507  state->remainingChars = num;
508  memcpy(&state->state_data[Data], tuple, 4);
509  }
510  return result;
511 }
quint32 qFromBigEndian< quint32 >(const uchar *src)
Definition: qendian.h:239
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
static ushort highSurrogate(uint ucs4)
Returns the high surrogate value of a ucs4 code point.
Definition: qchar.h:303
static ushort lowSurrogate(uint ucs4)
Returns the low surrogate value of a ucs4 code point.
Definition: qchar.h:306
unsigned char uchar
Definition: qglobal.h:994
void truncate(int pos)
Truncates the string at the given position index.
Definition: qstring.cpp:4603
const QChar * unicode() const
Returns a &#39;\0&#39;-terminated Unicode representation of the string.
Definition: qstring.h:706
DataEndianness
Definition: qutfcodec_p.h:61
quint32 qFromLittleEndian< quint32 >(const uchar *src)
Definition: qendian.h:148
unsigned int uint
Definition: qglobal.h:996
ConversionFlags flags
Definition: qtextcodec.h:106
void resize(int size)
Sets the size of the string to size characters.
Definition: qstring.cpp:1353
static const KeyPair *const end

The documentation for this struct was generated from the following files: