drop feature to set codecs for C strings and translations

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-06-17 23:40:40 +03:00
parent 4637cff951
commit 6265e347af
10 changed files with 57 additions and 214 deletions

View file

@ -60,7 +60,6 @@ static inline bool nameMatch(const QByteArray &name, const QByteArray &name2)
}
static QTextCodec *localeMapper = nullptr;
QTextCodec *QTextCodec::cftr = nullptr;
#ifndef QT_NO_THREAD
Q_GLOBAL_STATIC(QMutex, textCodecsMutex)
#endif
@ -878,69 +877,6 @@ QString QTextDecoder::toUnicode(const QByteArray &ba)
return c->toUnicode(ba.constData(), ba.length(), &state);
}
/*!
\fn QTextCodec* QTextCodec::codecForTr()
Returns the codec used by QObject::tr() on its argument. If this
function returns 0 (the default), tr() assumes Latin-1.
\sa setCodecForTr()
*/
/*!
\fn void QTextCodec::setCodecForTr(QTextCodec *c)
\nonreentrant
Sets the codec used by QObject::tr() on its argument to \a c. If
\a c is 0 (the default), tr() assumes Latin-1.
If the literal quoted text in the program is not in the Latin-1
encoding, this function can be used to set the appropriate
encoding. For example, software developed by Korean programmers
might use eucKR for all the text in the program, in which case the
main() function might look like this:
\snippet doc/src/snippets/code/src_corelib_codecs_qtextcodec.cpp 3
Note that this is not the way to select the encoding that the \e
user has chosen. For example, to convert an application containing
literal English strings to Korean, all that is needed is for the
English strings to be passed through tr() and for translation
files to be loaded. For details of internationalization, see
\l{Internationalization with Qt}.
\sa codecForTr(), setCodecForCStrings()
*/
/*!
\fn QTextCodec* QTextCodec::codecForCStrings()
Returns the codec used by QString to convert to and from \c{const
char *} and QByteArrays. If this function returns 0 (the default),
QString assumes Latin-1.
\sa setCodecForCStrings()
*/
/*!
\fn void QTextCodec::setCodecForCStrings(QTextCodec *codec)
\nonreentrant
Sets the codec used by QString to convert to and from \c{const
char *} and QByteArrays. If the \a codec is 0 (the default),
QString assumes Latin-1.
\warning Some codecs do not preserve the characters in the ASCII
range (0x00 to 0x7F). For example, the Japanese Shift-JIS
encoding maps the backslash character (0x5A) to the Yen
character. To avoid undesirable side-effects, we recommend
avoiding such codecs with setCodecsForCString().
\sa codecForCStrings(), setCodecForTr()
*/
/*!
\since 4.4

View file

@ -48,16 +48,6 @@ public:
static QTextCodec* codecForLocale();
static void setCodecForLocale(QTextCodec *c);
static inline QTextCodec* codecForTr()
{ return cftr; }
static inline void setCodecForTr(QTextCodec *c)
{ cftr = c; }
static inline QTextCodec* codecForCStrings()
{ return QString::codecForCStrings; }
static inline void setCodecForCStrings(QTextCodec *c)
{ QString::codecForCStrings = c; }
static QTextCodec *codecForHtml(const QByteArray &ba);
static QTextCodec *codecForHtml(const QByteArray &ba, QTextCodec *defaultCodec);
@ -121,7 +111,6 @@ protected:
private:
friend class QTextCodecCleanup;
static QTextCodec *cftr;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(QTextCodec::ConversionFlags)

View file

@ -1174,9 +1174,7 @@ bool QCoreApplication::event(QEvent *e)
This enum type defines the 8-bit encoding of character string
arguments to translate():
\value CodecForTr The encoding specified by
QTextCodec::codecForTr() (Latin-1 if none has
been set).
\value CodecForTr US-ASCII.
\value UnicodeUTF8 UTF-8.
\value DefaultCodec (Obsolete) Use CodecForTr instead.
@ -1314,7 +1312,7 @@ void QCoreApplication::removeTranslator(QTranslator *translationFile)
so will most likely result in crashes or other undesirable
behavior.
\sa QObject::tr() installTranslator() QTextCodec::codecForTr()
\sa QObject::tr() installTranslator()
*/
@ -1331,15 +1329,10 @@ QString QCoreApplication::translate(const char *context, const char *sourceText,
}
}
#ifdef QT_NO_TEXTCODEC
Q_UNUSED(encoding)
#else
if (encoding == UnicodeUTF8)
if (encoding == UnicodeUTF8) {
return QString::fromUtf8(sourceText);
else if (QTextCodec::codecForTr())
return QTextCodec::codecForTr()->toUnicode(sourceText);
#endif
return QString::fromLatin1(sourceText);
}
return QString::fromAscii(sourceText);
}
#endif // QT_NO_TRANSLATION

View file

@ -1584,7 +1584,7 @@ void QObject::deleteLater()
translators while performing translations is not supported. Doing
so will probably result in crashes or other undesirable behavior.
\sa trUtf8(), QApplication::translate(), QTextCodec::setCodecForTr(), {Internationalization with Qt}
\sa trUtf8(), QApplication::translate(), {Internationalization with Katie}
*/
/*!
@ -1605,7 +1605,7 @@ void QObject::deleteLater()
\snippet doc/src/snippets/code/src_corelib_kernel_qobject.cpp 20
\sa tr(), QApplication::translate(), {Internationalization with Qt}
\sa tr(), QApplication::translate(), {Internationalization with Katie}
*/
#ifndef QT_NO_TRANSLATION
QString QObject::tr(const char *sourceText)

View file

@ -1221,8 +1221,7 @@ QVariant::QVariant(QDataStream &s)
\fn QVariant::QVariant(const char *val)
Constructs a new variant with a string value of \a val.
The variant creates a deep copy of \a val, using the encoding
set by QTextCodec::setCodecForCStrings().
The variant creates a deep copy of \a val, using US-ASCII encoding.
Note that \a val is converted to a QString for storing in the
variant and QVariant::type() will return QMetaType::QString for
@ -1231,7 +1230,7 @@ QVariant::QVariant(QDataStream &s)
You can disable this operator by defining \c
QT_NO_CAST_FROM_ASCII when you compile your applications.
\sa QTextCodec::setCodecForCStrings()
\sa QString::fromAscii()
*/
#ifndef QT_NO_CAST_FROM_ASCII

View file

@ -37,12 +37,6 @@
QT_BEGIN_NAMESPACE
#ifndef QT_NO_CODEC_FOR_C_STRINGS
# ifdef QT_NO_TEXTCODEC
# define QT_NO_CODEC_FOR_C_STRINGS
# endif
#endif
static inline bool is_ascii_char(uint ucs4)
{
return ucs4 <= 127;
@ -402,29 +396,16 @@ static inline bool is_ascii_char(uint ucs4)
ch.
*/
QChar::QChar(const char ch)
: ucs(uchar(ch))
{
#ifndef QT_NO_CODEC_FOR_C_STRINGS
if (QTextCodec::codecForCStrings())
// #####
ucs = QTextCodec::codecForCStrings()->toUnicode(&ch, 1).at(0).unicode();
else
#endif
ucs = uchar(ch);
}
/*!
Constructs a QChar corresponding to ASCII/Latin-1 character \a ch.
*/
QChar::QChar(const uchar ch)
: ucs(ch)
{
#ifndef QT_NO_CODEC_FOR_C_STRINGS
if (QTextCodec::codecForCStrings()) {
// #####
const char c = char(ch);
ucs = QTextCodec::codecForCStrings()->toUnicode(&c, 1).at(0).unicode();
} else
#endif
ucs = ch;
}
/*!
@ -1400,7 +1381,7 @@ uint QChar::toCaseFolded(const uint ucs4)
Returns the Latin-1 character equivalent to the QChar, or 0. This
is mainly useful for non-internationalized software.
\sa toAscii(), unicode(), QTextCodec::codecForCStrings()
\sa toAscii(), unicode()
*/
/*!
@ -1415,15 +1396,10 @@ uint QChar::toCaseFolded(const uint ucs4)
in C strings. This is mainly useful for developers of non-internationalized
software.
\sa toLatin1(), unicode(), QTextCodec::codecForCStrings()
\sa toLatin1(), unicode()
*/
char QChar::toAscii() const
{
#ifndef QT_NO_CODEC_FOR_C_STRINGS
if (QTextCodec::codecForCStrings())
// #####
return QTextCodec::codecForCStrings()->fromUnicode(QString(*this)).at(0);
#endif
return ucs > 0xff ? 0 : char(ucs);
}
@ -1433,7 +1409,7 @@ char QChar::toAscii() const
Converts the Latin-1 character \a c to its equivalent QChar. This
is mainly useful for non-internationalized software.
\sa fromAscii(), unicode(), QTextCodec::codecForCStrings()
\sa fromAscii(), unicode()
*/
/*!
@ -1442,15 +1418,10 @@ char QChar::toAscii() const
An alternative is to use QLatin1Char.
\sa fromLatin1(), unicode(), QTextCodec::codecForCStrings()
\sa fromLatin1(), unicode()
*/
QChar QChar::fromAscii(const char c)
{
#ifndef QT_NO_CODEC_FOR_C_STRINGS
if (QTextCodec::codecForCStrings())
// #####
return QTextCodec::codecForCStrings()->toUnicode(&c, 1).at(0).unicode();
#endif
return QChar(ushort(uchar(c)));
}

View file

@ -50,10 +50,6 @@
QT_BEGIN_NAMESPACE
#ifndef QT_NO_TEXTCODEC
QTextCodec *QString::codecForCStrings = QTextCodec::codecForName("UTF-8");
#endif
// internal
int qFindString(const QChar *haystack, int haystackLen, int from,
const QChar *needle, int needleLen, Qt::CaseSensitivity cs);
@ -273,9 +269,7 @@ static int findChar(const QChar *str, int len, QChar ch, int from,
\snippet doc/src/snippets/qstring/main.cpp 0
QString converts the \c{const char *} data into Unicode using the
fromAscii() function. By default, fromAscii() treats character
characters as UTF-8, but this can be changed by calling
QTextCodec::setCodecForCStrings().
fromAscii() function.
In all of the QString functions that take \c{const char *}
parameters, the \c{const char *} is interpreted as a classic
@ -413,9 +407,7 @@ static int findChar(const QChar *str, int len, QChar ch, int from,
toLatin1(), toUtf8(), and toLocal8Bit().
\list
\o toAscii() returns an 8-bit string encoded using the codec
specified by QTextCodec::codecForCStrings (by default, that is
UTF-8).
\o toAscii() returns a US-ASCII encoded 8-bit string.
\o toLatin1() returns a Latin-1 (ISO 8859-1) encoded 8-bit string.
\o toUtf8() returns a UTF-8 encoded 8-bit string. UTF-8 is a
superset of US-ASCII (ANSI X3.4-1986) that supports the entire
@ -3177,10 +3169,6 @@ QByteArray QString::toLatin1() const
/*!
Returns an 8-bit representation of the string as a QByteArray.
If a codec has been set using QTextCodec::setCodecForCStrings(),
it is used to convert Unicode to 8-bit char; otherwise this
function does the same as toLatin1().
Note that, despite the name, this function does not necessarily return an US-ASCII
(ANSI X3.4-1986) string and its result may not be US-ASCII compatible.
@ -3188,11 +3176,7 @@ QByteArray QString::toLatin1() const
*/
QByteArray QString::toAscii() const
{
#ifndef QT_NO_TEXTCODEC
if (codecForCStrings)
return codecForCStrings->fromUnicode(unicode(), length());
#endif // QT_NO_TEXTCODEC
return toLatin1();
return QIcuCodec::convertFrom(constData(), length(), "US-ASCII");
}
static QByteArray toLocal8Bit_helper(const QChar *data, int length)
@ -3300,7 +3284,6 @@ QString::Data *QString::fromLatin1_helper(const char *str, int size)
QString::Data *QString::fromAscii_helper(const char *str, int size)
{
#ifndef QT_NO_TEXTCODEC
if (codecForCStrings) {
Data *d;
if (!str) {
d = &shared_null;
@ -3311,12 +3294,11 @@ QString::Data *QString::fromAscii_helper(const char *str, int size)
} else {
if (size < 0)
size = qstrlen(str);
QString s = codecForCStrings->toUnicode(str, size);
QString s = QTextCodec::codecForName("US-ASCII")->toUnicode(str, size);
d = s.d;
d->ref.ref();
}
return d;
}
#endif
return fromLatin1_helper(str, size);
}
@ -3335,8 +3317,6 @@ QString QString::fromLatin1(const char *str, int size)
return QString(fromLatin1_helper(str, size), 0);
}
/*!
Returns a QString initialized with the first \a size characters
of the 8-bit string \a str.
@ -3370,17 +3350,17 @@ QString QString::fromLocal8Bit(const char *str, int size)
If \a size is -1 (default), it is taken to be qstrlen(\a
str).
Note that, despite the name, this function actually uses the codec
defined by QTextCodec::setCodecForCStrings() to convert \a str to
Unicode. Depending on the codec, it may not accept valid US-ASCII (ANSI
X3.4-1986) input. If no codec has been set, this function does the same
as fromLatin1().
\sa toAscii(), fromLatin1(), fromUtf8(), fromLocal8Bit()
*/
QString QString::fromAscii(const char *str, int size)
{
return QString(fromAscii_helper(str, size), 0);
if (!str) {
return QString();
}
if (size < 0) {
size = qstrlen(str);
}
return QIcuCodec::convertTo(str, size, "US-ASCII");
}
/*!
@ -3406,11 +3386,12 @@ QString QString::fromAscii(const char *str, int size)
*/
QString QString::fromUtf8(const char *str, int size)
{
if (!str)
if (!str) {
return QString();
if (size < 0)
}
if (size < 0) {
size = qstrlen(str);
}
return QIcuCodec::convertTo(str, size, "UTF-8");
}
@ -3433,13 +3414,15 @@ QString QString::fromUtf8(const char *str, int size)
*/
QString QString::fromUtf16(const ushort *unicode, int size)
{
if (!unicode)
if (!unicode) {
return QString();
}
if (size < 0) {
size = 0;
while (unicode[size] != 0)
while (unicode[size] != 0) {
++size;
}
}
return QIcuCodec::convertTo((const char *)unicode, size, "UTF-16");
}
@ -3457,13 +3440,15 @@ QString QString::fromUtf16(const ushort *unicode, int size)
*/
QString QString::fromUcs4(const uint *unicode, int size)
{
if (!unicode)
if (!unicode) {
return QString();
}
if (size < 0) {
size = 0;
while (unicode[size] != 0)
while (unicode[size] != 0) {
++size;
}
}
return QIcuCodec::convertTo((const char *)unicode, size, "UTF-32");
}
@ -4442,10 +4427,7 @@ QString &QString::vsprintf(const char* cformat, va_list ap)
int i = 0;
while (*(c + i) != '\0' && *(c + i) != '%')
++i;
if (codecForCStrings)
result.append(codecForCStrings->toUnicode(c, i));
else
result.append(fromLatin1(c, i));
result.append(QString::fromAscii(c, i));
c += i;
#else
while (*c != '\0' && *c != '%')
@ -6313,9 +6295,7 @@ bool QString::isRightToLeft() const
This operator is mostly useful to pass a QString to a function
that accepts a std::string object.
If the QString contains Unicode characters that the
QTextCodec::codecForCStrings() codec cannot handle, using this operator
can lead to loss of information.
Using this operator can lead to loss of information.
This operator is only available if Qt is configured with STL
compatibility enabled.
@ -8148,10 +8128,6 @@ QByteArray QStringRef::toLatin1() const
Returns an 8-bit representation of the string as a QByteArray.
If a codec has been set using QTextCodec::setCodecForCStrings(),
it is used to convert Unicode to 8-bit char; otherwise this
function does the same as toLatin1().
Note that, despite the name, this function does not necessarily return an US-ASCII
(ANSI X3.4-1986) string and its result may not be US-ASCII compatible.
@ -8159,11 +8135,7 @@ QByteArray QStringRef::toLatin1() const
*/
QByteArray QStringRef::toAscii() const
{
#ifndef QT_NO_TEXTCODEC
if (QString::codecForCStrings)
return QString::codecForCStrings->fromUnicode(unicode(), length());
#endif // QT_NO_TEXTCODEC
return toLatin1();
return QIcuCodec::convertFrom(constData(), length(), "US-ASCII");
}
/*!

View file

@ -464,9 +464,6 @@ private:
static Data shared_empty;
Data *d;
QString(Data *dd, int /*dummy*/) : d(dd) {}
#ifndef QT_NO_TEXTCODEC
static QTextCodec *codecForCStrings;
#endif
static void freeData(Data *);
void reallocData(int alloc);
void expand(int i);
@ -719,10 +716,7 @@ inline QString::const_iterator QString::constEnd() const
#ifndef QT_NO_CAST_FROM_ASCII
inline bool qStringComparisonHelper(const QString &s1, const char *s2)
{
# ifndef QT_NO_TEXTCODEC
if (QString::codecForCStrings) return (s1 == QString::fromAscii(s2));
# endif
return (s1 == QLatin1String(s2));
return (s1 == QString::fromAscii(s2));
}
inline bool QString::operator==(const char *s) const
{ return qStringComparisonHelper(*this, s); }
@ -983,10 +977,7 @@ inline bool operator>=(const QStringRef &s1, const QStringRef &s2)
inline bool qStringComparisonHelper(const QStringRef &s1, const char *s2)
{
# ifndef QT_NO_TEXTCODEC
if (QString::codecForCStrings) return (s1 == QString::fromAscii(s2));
# endif
return (s1 == QLatin1String(s2));
return (s1 == QString::fromAscii(s2));
}
inline QT_ASCII_CAST_WARN bool operator==(const char *s1, const QStringRef &s2)

View file

@ -657,14 +657,6 @@ bool QApplicationPrivate::x11_apply_settings()
QApplication::setWheelScrollLines(num);
#endif
QString defaultcodec = settings.value(QLatin1String("Qt/defaultCodec"),
QVariant(QLatin1String("none"))).toString();
if (defaultcodec != QLatin1String("none")) {
QTextCodec *codec = QTextCodec::codecForName(defaultcodec.toLatin1());
if (codec)
QTextCodec::setCodecForTr(codec);
}
int w = settings.value(QLatin1String("Qt/globalStrut/width")).toInt();
int h = settings.value(QLatin1String("Qt/globalStrut/height")).toInt();
QSize strut(w, h);

View file

@ -34,7 +34,7 @@ extern bool qt_sendSpontaneousEvent(QObject*, QEvent*);
QKeyMapper::QKeyMapper()
: keyboardInputDirection(Qt::LeftToRight),
keyMapperCodec(QTextCodec::codecForCStrings())
keyMapperCodec(QTextCodec::codecForName("US-ASCII"))
{
clearMappings();
}