MythTV  master
codecutil.cpp
Go to the documentation of this file.
1 
2 #include "codecutil.h"
3 
4 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
5 #include <QTextCodec>
6 #else
7 #include <QStringDecoder>
8 #endif
9 
10 bool CodecUtil::isValidUTF8(const QByteArray& data)
11 {
12  // NOTE: If you have a better way to determine this, then please update this
13  // Any chosen method MUST be able to identify UTF-8 encoded text without
14  // using the BOM Byte-Order Mark as that will probably not be present.
15 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
16  QTextCodec::ConverterState state;
17  QTextCodec *codec = QTextCodec::codecForName("UTF-8");
18  const QString text = codec->toUnicode(data.constData(), data.size(), &state);
19  if (state.invalidChars > 0)
20  return false;
21 #else
22  auto toUtf16 = QStringDecoder(QStringDecoder::Utf8);
23  QString text = toUtf16.decode(data);
24  if (toUtf16.hasError())
25  return false;
26 #endif
27 
28  Q_UNUSED(text);
29  return true;
30 }
CodecUtil::isValidUTF8
static bool isValidUTF8(const QByteArray &data)
Definition: codecutil.cpp:10
codecutil.h