From 7fb90066b3aa7e602466a61e0ffaadbf9002525c Mon Sep 17 00:00:00 2001 From: David Faure Date: Thu, 29 Sep 2011 13:31:39 +0200 Subject: [PATCH] Move Qt::escape to QtCore Merge-request: 56 Reviewed-by: Frederik Gladhorn Change-Id: I25c5f46cf53a653db26dbeb92865e61f69980bfd Reviewed-on: http://codereview.qt-project.org/5792 Reviewed-by: Frederik Gladhorn Reviewed-by: Qt Sanity Bot --- .../code/src_corelib_tools_qstring.cpp | 7 ++++ .../code/src_gui_text_qtextdocument.cpp | 9 +---- src/corelib/tools/qstring.cpp | 33 ++++++++++++++++++ src/corelib/tools/qstring.h | 3 ++ src/gui/text/qtextdocument.cpp | 34 +------------------ src/gui/text/qtextdocument.h | 1 - 6 files changed, 45 insertions(+), 42 deletions(-) diff --git a/doc/src/snippets/code/src_corelib_tools_qstring.cpp b/doc/src/snippets/code/src_corelib_tools_qstring.cpp index 0cd1801beb..740ec54af5 100644 --- a/doc/src/snippets/code/src_corelib_tools_qstring.cpp +++ b/doc/src/snippets/code/src_corelib_tools_qstring.cpp @@ -85,3 +85,10 @@ if (str == QLatin1String("auto") //! [6] QLabel *label = new QLabel(QLatin1String("MOD"), this); //! [6] + + +//! [7] +QString plain = "#include " +QString html = Qt::escape(plain); +// html == "#include <QtCore>" +//! [7] diff --git a/doc/src/snippets/code/src_gui_text_qtextdocument.cpp b/doc/src/snippets/code/src_gui_text_qtextdocument.cpp index 775cc0fcd1..8165086d09 100644 --- a/doc/src/snippets/code/src_gui_text_qtextdocument.cpp +++ b/doc/src/snippets/code/src_gui_text_qtextdocument.cpp @@ -39,12 +39,5 @@ ****************************************************************************/ //! [0] -QString plain = "#include " -QString html = Qt::escape(plain); -// html == "#include <QtCore>" -//! [0] - - -//! [1] ... -//! [1] +//! [0] diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index 3a2e35c322..37f8554381 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -8788,6 +8788,39 @@ QVector QStringRef::toUcs4() const return v; } +/*! + Converts the plain text string \a plain to a HTML string with + HTML metacharacters \c{<}, \c{>}, \c{&}, and \c{"} replaced by HTML + entities. + + Example: + + \snippet doc/src/snippets/code/src_gui_text_qtextdocument.cpp 0 + + This function is defined in the \c header file. + + \sa convertFromPlainText(), mightBeRichText() +*/ +QString Qt::escape(const QString &plain) +{ + QString rich; + rich.reserve(int(plain.length() * 1.1)); + for (int i = 0; i < plain.length(); ++i) { + if (plain.at(i) == QLatin1Char('<')) + rich += QLatin1String("<"); + else if (plain.at(i) == QLatin1Char('>')) + rich += QLatin1String(">"); + else if (plain.at(i) == QLatin1Char('&')) + rich += QLatin1String("&"); + else if (plain.at(i) == QLatin1Char('"')) + rich += QLatin1String("""); + else + rich += plain.at(i); + } + rich.squeeze(); + return rich; +} + /*! \macro QStringLiteral(str) \relates QString diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index 5009686d27..adb7e06ce1 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -1249,6 +1249,9 @@ inline QBool QStringRef::contains(QChar c, Qt::CaseSensitivity cs) const inline QBool QStringRef::contains(const QStringRef &s, Qt::CaseSensitivity cs) const { return QBool(indexOf(s, 0, cs) != -1); } +namespace Qt { + Q_CORE_EXPORT QString escape(const QString &plain); +} QT_END_NAMESPACE diff --git a/src/gui/text/qtextdocument.cpp b/src/gui/text/qtextdocument.cpp index c1714edb0c..c44e98b77e 100644 --- a/src/gui/text/qtextdocument.cpp +++ b/src/gui/text/qtextdocument.cpp @@ -141,38 +141,6 @@ bool Qt::mightBeRichText(const QString& text) return false; } -/*! - Converts the plain text string \a plain to a HTML string with - HTML metacharacters \c{<}, \c{>}, \c{&}, and \c{"} replaced by HTML - entities. - - Example: - - \snippet doc/src/snippets/code/src_gui_text_qtextdocument.cpp 0 - - This function is defined in the \c header file. - - \sa convertFromPlainText(), mightBeRichText() -*/ -QString Qt::escape(const QString& plain) -{ - QString rich; - rich.reserve(int(plain.length() * 1.1)); - for (int i = 0; i < plain.length(); ++i) { - if (plain.at(i) == QLatin1Char('<')) - rich += QLatin1String("<"); - else if (plain.at(i) == QLatin1Char('>')) - rich += QLatin1String(">"); - else if (plain.at(i) == QLatin1Char('&')) - rich += QLatin1String("&"); - else if (plain.at(i) == QLatin1Char('"')) - rich += QLatin1String("""); - else - rich += plain.at(i); - } - return rich; -} - /*! \fn QString Qt::convertFromPlainText(const QString &plain, WhiteSpaceMode mode) @@ -3002,7 +2970,7 @@ void QTextHtmlExporter::emitFrameStyle(const QTextFrameFormat &format, FrameType The \a encoding parameter specifies the value for the charset attribute in the html header. For example if 'utf-8' is specified then the beginning of the generated html will look like this: - \snippet doc/src/snippets/code/src_gui_text_qtextdocument.cpp 1 + \snippet doc/src/snippets/code/src_gui_text_qtextdocument.cpp 0 If no encoding is specified then no such meta information is generated. diff --git a/src/gui/text/qtextdocument.h b/src/gui/text/qtextdocument.h index 363d7abb21..9d9af2b5bb 100644 --- a/src/gui/text/qtextdocument.h +++ b/src/gui/text/qtextdocument.h @@ -84,7 +84,6 @@ namespace Qt }; Q_GUI_EXPORT bool mightBeRichText(const QString&); - Q_GUI_EXPORT QString escape(const QString& plain); Q_GUI_EXPORT QString convertFromPlainText(const QString &plain, WhiteSpaceMode mode = WhiteSpacePre); #ifndef QT_NO_TEXTCODEC