2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2016-03-18 07:55:01 +01:00
|
|
|
#pragma once
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
#include "utils_global.h"
|
|
|
|
|
|
2018-03-28 16:03:11 +02:00
|
|
|
#include <QList>
|
|
|
|
|
#include <QString>
|
|
|
|
|
|
2020-12-07 13:36:56 +01:00
|
|
|
#include <functional>
|
|
|
|
|
|
2009-11-25 12:34:56 +01:00
|
|
|
QT_BEGIN_NAMESPACE
|
2017-07-27 10:27:20 +02:00
|
|
|
class QJsonValue;
|
2009-11-25 12:34:56 +01:00
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
namespace Utils {
|
|
|
|
|
|
2022-11-29 15:15:12 +01:00
|
|
|
class FilePath;
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
// Create a usable settings key from a category,
|
|
|
|
|
// for example Editor|C++ -> Editor_C__
|
2009-05-08 12:09:21 +02:00
|
|
|
QTCREATOR_UTILS_EXPORT QString settingsKey(const QString &category);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-11-25 12:34:56 +01:00
|
|
|
// Return the common prefix part of a string list:
|
|
|
|
|
// "C:\foo\bar1" "C:\foo\bar2" -> "C:\foo\bar"
|
|
|
|
|
QTCREATOR_UTILS_EXPORT QString commonPrefix(const QStringList &strings);
|
|
|
|
|
|
2017-07-07 14:05:06 +03:00
|
|
|
// Removes first unescaped ampersand in text
|
|
|
|
|
QTCREATOR_UTILS_EXPORT QString stripAccelerator(const QString &text);
|
2018-03-26 10:44:10 +02:00
|
|
|
// Quotes all ampersands
|
|
|
|
|
QTCREATOR_UTILS_EXPORT QString quoteAmpersands(const QString &text);
|
2017-07-07 14:05:06 +03:00
|
|
|
|
2017-07-27 10:27:20 +02:00
|
|
|
QTCREATOR_UTILS_EXPORT bool readMultiLineString(const QJsonValue &value, QString *out);
|
|
|
|
|
|
2018-03-13 15:54:27 +01:00
|
|
|
// Compare case insensitive and use case sensitive comparison in case of that being equal.
|
|
|
|
|
QTCREATOR_UTILS_EXPORT int caseFriendlyCompare(const QString &a, const QString &b);
|
|
|
|
|
|
2014-06-24 16:17:58 +02:00
|
|
|
class QTCREATOR_UTILS_EXPORT AbstractMacroExpander
|
|
|
|
|
{
|
2010-11-08 21:09:19 +01:00
|
|
|
public:
|
2010-12-06 14:00:37 +01:00
|
|
|
virtual ~AbstractMacroExpander() {}
|
2010-11-08 21:09:19 +01:00
|
|
|
// Not const, as it may change the state of the expander.
|
|
|
|
|
//! Find an expando to replace and provide a replacement string.
|
|
|
|
|
//! \param str The string to scan
|
|
|
|
|
//! \param pos Position to start scan on input, found position on output
|
|
|
|
|
//! \param ret Replacement string on output
|
|
|
|
|
//! \return Length of string part to replace, zero if no (further) matches found
|
|
|
|
|
virtual int findMacro(const QString &str, int *pos, QString *ret);
|
|
|
|
|
//! Provide a replacement string for an expando
|
|
|
|
|
//! \param name The name of the expando
|
|
|
|
|
//! \param ret Replacement string on output
|
|
|
|
|
//! \return True if the expando was found
|
2017-06-07 16:50:04 +02:00
|
|
|
virtual bool resolveMacro(const QString &name, QString *ret, QSet<AbstractMacroExpander *> &seen) = 0;
|
2014-09-12 19:14:54 +02:00
|
|
|
private:
|
|
|
|
|
bool expandNestedMacros(const QString &str, int *pos, QString *ret);
|
2010-11-08 21:09:19 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
QTCREATOR_UTILS_EXPORT void expandMacros(QString *str, AbstractMacroExpander *mx);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT QString expandMacros(const QString &str, AbstractMacroExpander *mx);
|
|
|
|
|
|
2017-08-23 09:36:47 +02:00
|
|
|
QTCREATOR_UTILS_EXPORT int parseUsedPortFromNetstatOutput(const QByteArray &line);
|
|
|
|
|
|
2022-11-29 15:15:12 +01:00
|
|
|
QTCREATOR_UTILS_EXPORT QString appendHelper(const QString &base, int n);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT FilePath appendHelper(const FilePath &base, int n);
|
|
|
|
|
|
2020-11-10 16:55:09 +01:00
|
|
|
template<typename T>
|
|
|
|
|
T makeUniquelyNumbered(const T &preferred, const std::function<bool(const T &)> &isOk)
|
2018-03-28 16:03:11 +02:00
|
|
|
{
|
2020-11-10 16:55:09 +01:00
|
|
|
if (isOk(preferred))
|
2018-03-28 16:03:11 +02:00
|
|
|
return preferred;
|
|
|
|
|
int i = 2;
|
2022-11-29 15:15:12 +01:00
|
|
|
T tryName = appendHelper(preferred, i);
|
2020-11-10 16:55:09 +01:00
|
|
|
while (!isOk(tryName))
|
2022-11-29 15:15:12 +01:00
|
|
|
tryName = appendHelper(preferred, ++i);
|
2018-03-28 16:03:11 +02:00
|
|
|
return tryName;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 16:55:09 +01:00
|
|
|
template<typename T, typename Container>
|
|
|
|
|
T makeUniquelyNumbered(const T &preferred, const Container &reserved)
|
|
|
|
|
{
|
|
|
|
|
const std::function<bool(const T &)> isOk
|
|
|
|
|
= [&reserved](const T &v) { return !reserved.contains(v); };
|
|
|
|
|
return makeUniquelyNumbered(preferred, isOk);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-28 15:09:35 +01:00
|
|
|
QTCREATOR_UTILS_EXPORT QString formatElapsedTime(qint64 elapsed);
|
2018-03-28 16:03:11 +02:00
|
|
|
|
2020-10-16 13:38:57 +02:00
|
|
|
/* This function is only necessary if you need to match the wildcard expression against a
|
|
|
|
|
* string that might contain path separators - otherwise
|
|
|
|
|
* QRegularExpression::wildcardToRegularExpression() can be used.
|
|
|
|
|
* Working around QRegularExpression::wildcardToRegularExpression() taking native separators
|
|
|
|
|
* into account and handling them to disallow matching a wildcard characters.
|
|
|
|
|
*/
|
|
|
|
|
QTCREATOR_UTILS_EXPORT QString wildcardToRegularExpression(const QString &original);
|
2022-01-10 18:06:48 +02:00
|
|
|
|
|
|
|
|
QTCREATOR_UTILS_EXPORT QString languageNameFromLanguageCode(const QString &languageCode);
|
|
|
|
|
|
2022-06-16 13:53:26 +02:00
|
|
|
|
|
|
|
|
#ifdef QT_WIDGETS_LIB
|
|
|
|
|
|
|
|
|
|
// Feeds the global clipboard and, when present, the primary selection
|
|
|
|
|
QTCREATOR_UTILS_EXPORT void setClipboardAndSelection(const QString &text);
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-05-31 11:16:44 +02:00
|
|
|
QTCREATOR_UTILS_EXPORT QString chopIfEndsWith(QString str, QChar c);
|
2022-08-11 10:50:06 +02:00
|
|
|
QTCREATOR_UTILS_EXPORT QStringView chopIfEndsWith(QStringView str, QChar c);
|
2022-05-31 11:16:44 +02:00
|
|
|
|
2023-01-27 18:37:27 +01:00
|
|
|
QTCREATOR_UTILS_EXPORT QString normalizeNewlines(const QString &text);
|
|
|
|
|
|
2023-02-03 04:01:00 +01:00
|
|
|
// Skips empty parts - see QTBUG-110900
|
|
|
|
|
QTCREATOR_UTILS_EXPORT QString joinStrings(const QStringList &strings, QChar separator);
|
2023-02-03 14:52:48 +01:00
|
|
|
QTCREATOR_UTILS_EXPORT QString trimFront(const QString &string, QChar ch);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT QString trimBack(const QString &string, QChar ch);
|
|
|
|
|
QTCREATOR_UTILS_EXPORT QString trim(const QString &string, QChar ch);
|
2023-01-27 18:37:27 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
} // namespace Utils
|