MCPcopy Create free account
hub / github.com/QNapi/qnapi / detectBufferEncoding

Method detectBufferEncoding

libqnapi/src/utils/encodingutils.cpp:134–166  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

132}
133
134QString EncodingUtils::detectBufferEncoding(const QByteArray &buffer) const {
135 QTextCodec::ConverterState state;
136 QTextCodec::codecForName("UTF-8")->toUnicode(buffer.constData(),
137 buffer.length(), &state);
138
139 if (state.invalidChars == 0) return "UTF-8";
140
141 int bestMatch = 0;
142 QString from;
143
144 foreach (QString codec, codecs) {
145 QTextCodec *tc = QTextCodec::codecForName(qPrintable(codec));
146
147 const QString text = tc->toUnicode(buffer.constData(), buffer.size());
148
149 QStringList chars = QString::fromUtf8("ą/ś/ż/ć/ń/ł/ó/ę").split("/");
150
151 int found = 0;
152
153 foreach (QString c, chars) {
154 if (text.contains(c, Qt::CaseInsensitive)) {
155 ++found;
156 }
157 }
158
159 if (found >= bestMatch) {
160 bestMatch = found;
161 from = codec;
162 }
163 }
164
165 return from;
166}
167
168QString EncodingUtils::detectFileEncoding(const QString &filename) const {
169 QFile f(filename);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected