mirror of
https://bitbucket.org/smil3y/kdelibs.git
synced 2025-02-24 10:52:49 +00:00
kdecore: drop internal file encoding functions
these seems unnecessary because QString::fromLocal8Bit() is used if the function is not overriden, though some sorta UTF-16 support is suggested in the internal functions I have no trouble creating files with UTF-16 characters in theirs names and working with them. if there are any complaints on this it shall be reverted Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
parent
d957fc4852
commit
d4f3cb5ef4
3 changed files with 3 additions and 181 deletions
|
@ -23,7 +23,7 @@
|
|||
*/
|
||||
|
||||
#include "klocale.h"
|
||||
#include "klocale_unix_p.h"
|
||||
#include "klocale_unix_p.h"
|
||||
|
||||
#include <QtCore/QDateTime>
|
||||
#include <QtCore/QTextCodec>
|
||||
|
|
|
@ -46,7 +46,7 @@
|
|||
#include <QtCore/QRegExp>
|
||||
#include <QtCore/QLocale>
|
||||
#include <QtCore/QHash>
|
||||
#include <QtCore/qmutex.h>
|
||||
#include <QtCore/QMutexLocker>
|
||||
#include <QtCore/QStringList>
|
||||
|
||||
#include "kcatalog_p.h"
|
||||
|
@ -2670,16 +2670,11 @@ void KLocalePrivate::initFileNameEncoding()
|
|||
// If the following environment variable is set, assume all filenames
|
||||
// are in UTF-8 regardless of the current C locale.
|
||||
m_utf8FileEncoding = !qgetenv("KDE_UTF8_FILENAMES").isEmpty();
|
||||
if (m_utf8FileEncoding) {
|
||||
QFile::setEncodingFunction(KLocalePrivate::encodeFileNameUTF8);
|
||||
QFile::setDecodingFunction(KLocalePrivate::decodeFileNameUTF8);
|
||||
} else {
|
||||
if (!m_utf8FileEncoding) {
|
||||
const QByteArray ctype = setlocale(LC_CTYPE, 0);
|
||||
int indexOfDot = ctype.indexOf('.');
|
||||
if (indexOfDot != -1) {
|
||||
if (!qstrnicmp(ctype.data() + indexOfDot + 1, "UTF-8", 5)) {
|
||||
QFile::setEncodingFunction(KLocalePrivate::encodeFileNameUTF8);
|
||||
QFile::setDecodingFunction(KLocalePrivate::decodeFileNameUTF8);
|
||||
m_utf8FileEncoding = true;
|
||||
}
|
||||
return;
|
||||
|
@ -2694,8 +2689,6 @@ void KLocalePrivate::initFileNameEncoding()
|
|||
indexOfDot = lang.indexOf('.');
|
||||
if (indexOfDot != -1) {
|
||||
if (!qstrnicmp(lang.data() + indexOfDot + 1, "UTF-8", 5)) {
|
||||
QFile::setEncodingFunction(KLocalePrivate::encodeFileNameUTF8);
|
||||
QFile::setDecodingFunction(KLocalePrivate::decodeFileNameUTF8);
|
||||
m_utf8FileEncoding = true;
|
||||
}
|
||||
}
|
||||
|
@ -2704,167 +2697,6 @@ void KLocalePrivate::initFileNameEncoding()
|
|||
// which, on Unix platforms, use the locale's codec.
|
||||
}
|
||||
|
||||
static inline bool isUnicodeNonCharacter(uint ucs4)
|
||||
{
|
||||
return (ucs4 & 0xfffe) == 0xfffe || (ucs4 - 0xfdd0U) < 16;
|
||||
}
|
||||
|
||||
QByteArray KLocalePrivate::encodeFileNameUTF8(const QString & fileName)
|
||||
{
|
||||
if (fileName.isNull()) return QByteArray();
|
||||
int len = fileName.length();
|
||||
const QChar *uc = fileName.constData();
|
||||
|
||||
uchar replacement = '?';
|
||||
int rlen = 3*len;
|
||||
int surrogate_high = -1;
|
||||
|
||||
QByteArray rstr;
|
||||
rstr.resize(rlen);
|
||||
uchar* cursor = (uchar*)rstr.data();
|
||||
const QChar *ch = uc;
|
||||
int invalid = 0;
|
||||
|
||||
const QChar *end = ch + len;
|
||||
while (ch < end) {
|
||||
uint u = ch->unicode();
|
||||
if (surrogate_high >= 0) {
|
||||
if (ch->isLowSurrogate()) {
|
||||
u = QChar::surrogateToUcs4(surrogate_high, u);
|
||||
surrogate_high = -1;
|
||||
} else {
|
||||
// high surrogate without low
|
||||
*cursor = replacement;
|
||||
++ch;
|
||||
++invalid;
|
||||
surrogate_high = -1;
|
||||
continue;
|
||||
}
|
||||
} else if (ch->isLowSurrogate()) {
|
||||
// low surrogate without high
|
||||
*cursor = replacement;
|
||||
++ch;
|
||||
++invalid;
|
||||
continue;
|
||||
} else if (ch->isHighSurrogate()) {
|
||||
surrogate_high = u;
|
||||
++ch;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (u >= 0x10FE00 && u <= 0x10FE7F) {
|
||||
*cursor++ = (uchar)(u - 0x10FE00 + 128) ;
|
||||
}
|
||||
else if (u < 0x80) {
|
||||
*cursor++ = (uchar)u;
|
||||
} else {
|
||||
if (u < 0x0800) {
|
||||
*cursor++ = 0xc0 | ((uchar) (u >> 6));
|
||||
} else {
|
||||
// is it one of the Unicode non-characters?
|
||||
if (isUnicodeNonCharacter(u)) {
|
||||
*cursor++ = replacement;
|
||||
++ch;
|
||||
++invalid;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (u > 0xffff) {
|
||||
*cursor++ = 0xf0 | ((uchar) (u >> 18));
|
||||
*cursor++ = 0x80 | (((uchar) (u >> 12)) & 0x3f);
|
||||
} else {
|
||||
*cursor++ = 0xe0 | (((uchar) (u >> 12)) & 0x3f);
|
||||
}
|
||||
*cursor++ = 0x80 | (((uchar) (u >> 6)) & 0x3f);
|
||||
}
|
||||
*cursor++ = 0x80 | ((uchar) (u&0x3f));
|
||||
}
|
||||
++ch;
|
||||
}
|
||||
|
||||
rstr.resize(cursor - (const uchar*)rstr.constData());
|
||||
return rstr;
|
||||
}
|
||||
|
||||
QString KLocalePrivate::decodeFileNameUTF8(const QByteArray &localFileName)
|
||||
{
|
||||
const char *chars = localFileName;
|
||||
int len = qstrlen(chars);
|
||||
int need = 0;
|
||||
uint uc = 0;
|
||||
uint min_uc = 0;
|
||||
|
||||
QString result(2 * (len + 1), Qt::Uninitialized); // worst case
|
||||
ushort *qch = (ushort *)result.unicode();
|
||||
uchar ch;
|
||||
|
||||
for (int i = 0; i < len; ++i) {
|
||||
ch = chars[i];
|
||||
if (need) {
|
||||
if ((ch&0xc0) == 0x80) {
|
||||
uc = (uc << 6) | (ch & 0x3f);
|
||||
--need;
|
||||
if (!need) {
|
||||
bool nonCharacter;
|
||||
if (!(nonCharacter = isUnicodeNonCharacter(uc)) && uc > 0xffff && uc < 0x110000) {
|
||||
// surrogate pair
|
||||
Q_ASSERT((qch - (ushort*)result.unicode()) + 2 < result.length());
|
||||
*qch++ = QChar::highSurrogate(uc);
|
||||
*qch++ = QChar::lowSurrogate(uc);
|
||||
} else if ((uc < min_uc) || (uc >= 0xd800 && uc <= 0xdfff) || nonCharacter || uc >= 0x110000) {
|
||||
// error: overlong sequence, UTF16 surrogate or non-character
|
||||
goto error;
|
||||
} else {
|
||||
*qch++ = uc;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
goto error;
|
||||
}
|
||||
} else {
|
||||
if (ch < 128) {
|
||||
*qch++ = ushort(ch);
|
||||
} else if ((ch & 0xe0) == 0xc0) {
|
||||
uc = ch & 0x1f;
|
||||
need = 1;
|
||||
min_uc = 0x80;
|
||||
} else if ((ch & 0xf0) == 0xe0) {
|
||||
uc = ch & 0x0f;
|
||||
need = 2;
|
||||
min_uc = 0x800;
|
||||
} else if ((ch&0xf8) == 0xf0) {
|
||||
uc = ch & 0x07;
|
||||
need = 3;
|
||||
min_uc = 0x10000;
|
||||
} else {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (need > 0) {
|
||||
// unterminated UTF sequence
|
||||
goto error;
|
||||
}
|
||||
result.truncate(qch - (ushort *)result.unicode());
|
||||
return result;
|
||||
|
||||
error:
|
||||
|
||||
qch = (ushort *)result.unicode();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
ch = chars[i];
|
||||
if (ch < 128) {
|
||||
*qch++ = ushort(ch);
|
||||
} else {
|
||||
uint uc = ch - 128 + 0x10FE00; //U+10FE00-U+10FE7F
|
||||
*qch++ = QChar::highSurrogate(uc);
|
||||
*qch++ = QChar::lowSurrogate(uc);
|
||||
}
|
||||
}
|
||||
result.truncate(qch - (ushort *)result.unicode());
|
||||
return result;
|
||||
}
|
||||
|
||||
void KLocalePrivate::setDateFormat(const QString &format)
|
||||
{
|
||||
m_dateFormat = format.trimmed();
|
||||
|
|
|
@ -1061,16 +1061,6 @@ protected:
|
|||
*/
|
||||
virtual QByteArray systemCodeset() const;
|
||||
|
||||
/**
|
||||
* @internal A QFile filename encoding function (QFile::encodeFn).
|
||||
*/
|
||||
static QByteArray encodeFileNameUTF8(const QString &fileName);
|
||||
|
||||
/**
|
||||
* @internal QFile filename decoding function (QFile::decodeFn).
|
||||
*/
|
||||
static QString decodeFileNameUTF8(const QByteArray &localFileName);
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Reference in a new issue