File indexing completed on 2024-05-05 04:38:46

0001 /*
0002     SPDX-FileCopyrightText: 2009 Andreas Pakulat <apaku@gmx.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KDEVPLATFORM_KDEVSTRINGHANDLER_H
0008 #define KDEVPLATFORM_KDEVSTRINGHANDLER_H
0009 
0010 #include "utilexport.h"
0011 
0012 #include <QString>
0013 
0014 class QStringRef;
0015 class QByteArray;
0016 class QChar;
0017 class QStringList;
0018 class QVariant;
0019 
0020 namespace KDevelop {
0021 KDEVPLATFORMUTIL_EXPORT QStringList splitWithEscaping(const QString& input, QChar splitChar,
0022                                                       QChar escapeChar);
0023 KDEVPLATFORMUTIL_EXPORT QString joinWithEscaping(const QStringList& input, QChar joinChar,
0024                                                  QChar escapeChar);
0025 
0026 /**
0027 * convert the @p variant into a string which can then be stored
0028 * easily in a KConfig entry. This supports any QVariant type (including custom types)
0029 * for which there is a QDataStream operator defined
0030 * @returns a QString containing the data from the QVariant.
0031 */
0032 KDEVPLATFORMUTIL_EXPORT QString qvariantToString(const QVariant& variant);
0033 
0034 /**
0035 * convert the @p s into a QVariant, usually the string is read from KConfig.
0036 * This supports any QVariant type (including custom types)
0037 * for which there is a QDataStream operator defined
0038 * @returns a QVariant created from the bytearray
0039 */
0040 KDEVPLATFORMUTIL_EXPORT QVariant stringToQVariant(const QString& s);
0041 
0042 enum HtmlToPlainTextMode {
0043     FastMode,         /**< Fast (conversion via regular expression) */
0044     CompleteMode,     /**< Slower, but with expected behavior (conversion via QTextDocument::toPlainText).
0045             This also replaces <br/> with newline chars, for example. */
0046 };
0047 
0048 /**
0049  * Strip HTML tags from string @p s
0050  *
0051  * @return String no longer containing any HTML tags
0052  */
0053 KDEVPLATFORMUTIL_EXPORT QString htmlToPlainText(const QString& s, HtmlToPlainTextMode mode = FastMode);
0054 
0055 /**
0056  * Replace special JavaScript characters with escape sequences
0057  *
0058  * @return a string ready to be enclosed in single or double quotes and used in JavaScript
0059  */
0060 KDEVPLATFORMUTIL_EXPORT QByteArray escapeJavaScriptString(const QByteArray& str);
0061 
0062 /**
0063  * Match a prefix of @p str to an ASCII-only identifier, i.e. [a-zA-Z_][a-zA-Z0-9_]*
0064  *
0065  * @return The length of the matched prefix or 0 if there is no match
0066  */
0067 KDEVPLATFORMUTIL_EXPORT int findAsciiIdentifierLength(const QStringRef& str);
0068 
0069 struct KDEVPLATFORMUTIL_EXPORT VariableMatch {
0070     int length;     ///< The length of the matched substring in the source string
0071     QString name;   ///< The name of the matched variable
0072 };
0073 
0074 /**
0075  * Match a prefix of @p str to an ASCII-only identifier or {identifier}
0076  *
0077  * @return The matching result or {} if there is no match
0078  */
0079 KDEVPLATFORMUTIL_EXPORT VariableMatch matchPossiblyBracedAsciiVariable(const QStringRef& str);
0080 
0081 /**
0082  * Strip ANSI sequences from string @p str
0083  */
0084 KDEVPLATFORMUTIL_EXPORT QString stripAnsiSequences(const QString& str);
0085 
0086 /**
0087  * Replace all occurrences of "\r" or "\r\n" in @p text with "\n".
0088  */
0089 KDEVPLATFORMUTIL_EXPORT void normalizeLineEndings(QByteArray& text);
0090 }
0091 
0092 #endif // KDEVPLATFORM_KDEVSTRINGHANDLER_H