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

0001 /*
0002     SPDX-FileCopyrightText: 2007 David Nolden <david.nolden.kdevelop@art-master.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #ifndef KDEVPLATFORM_STRINGHELPERS_H
0008 #define KDEVPLATFORM_STRINGHELPERS_H
0009 
0010 #include <language/languageexport.h>
0011 
0012 #include <QChar>
0013 #include <QScopedPointer>
0014 
0015 class QByteArray;
0016 class QString;
0017 class QStringView;
0018 
0019 namespace KDevelop {
0020 class ParamIteratorPrivate;
0021 
0022 /**
0023  * @return true if QChar::isSpace() returns true for all elements of @p str.
0024  */
0025 bool KDEVPLATFORMLANGUAGE_EXPORT consistsOfWhitespace(QStringView str);
0026 
0027 /**
0028  * Searches in the given string for a ',' or a closing bracket equal to @p validEnd,
0029  * while skipping everything between opened brackets, string and character literals, comments.
0030  * @param str string to search
0031  * @param pos position where to start searching
0032  * @param validEnd a closing bracket type that stops the search
0033  * @return  On fail, str.length() is returned, else the position of the closing character.
0034  */
0035 int KDEVPLATFORMLANGUAGE_EXPORT findCommaOrEnd(QStringView str, int pos, QChar validEnd);
0036 
0037 /**
0038  * Extracts the interesting information out of a comment.
0039  * For example it removes all the stars at the beginning, and re-indents the text.
0040  */
0041 QString KDEVPLATFORMLANGUAGE_EXPORT formatComment(const QString& comment);
0042 
0043 /**
0044  * Extracts the interesting information out of a comment.
0045  * For example it removes all the stars at the beginning, and re-indents the text.
0046  */
0047 QByteArray KDEVPLATFORMLANGUAGE_EXPORT formatComment(const QByteArray& comment);
0048 
0049 /**
0050  * Removes all whitespace from the string
0051  */
0052 QString KDEVPLATFORMLANGUAGE_EXPORT removeWhitespace(const QString& str);
0053 
0054 /**
0055  * Can be used to iterate through different kinds of parameters, for example template-parameters
0056  */
0057 class KDEVPLATFORMLANGUAGE_EXPORT ParamIterator
0058 {
0059 public:
0060     /**
0061      * @param parens Should be a string containing the two parens between which the parameters are searched.
0062      * Example: "<>" or "()" Optionally it can also contain one third end-character.
0063      * If that end-character is encountered in the prefix, the iteration will be stopped.
0064      *
0065      * Example: When "<>:" is given, ParamIterator will only parse the first identifier of a C++ scope
0066      *
0067      * @warning The QStringView arguments must remain valid and unchanged during ParamIterator's lifetime.
0068      */
0069     explicit ParamIterator(QStringView parens, QStringView source, int start = 0);
0070     ~ParamIterator();
0071 
0072     ParamIterator& operator ++();
0073 
0074     /**
0075      * Returns current found parameter
0076      */
0077     QStringView operator*() const;
0078 
0079     /**
0080      * Returns whether there is a current found parameter
0081      */
0082     operator bool() const;
0083 
0084     /**
0085      * Returns the text in front of the first opening-paren(if none found then the whole text)
0086      */
0087     QStringView prefix() const;
0088 
0089     uint position() const;
0090 
0091 private:
0092     const QScopedPointer<class ParamIteratorPrivate> d_ptr;
0093     Q_DECLARE_PRIVATE(ParamIterator)
0094 };
0095 }
0096 
0097 #endif