forked from qt-creator/qt-creator
Utils: Add more contains functions
We have very often the following pattern. bool isKeyword(const QString &token) { static const QStringList keywords = {"if", "then", "else", ...}; return keywords.contains(token); } Instead we could remove the the allocation and give the compiler the opportunity to transform the code. constexpr makes that prossible. bool isKeyword(QStringView token) { static constexpr QStringView keywords[] = {u"if", u"then", u"else", ...}; return Utils::contains(keywords, token); } Change-Id: Ic25aa676085bf4bfc6e9ff3194813ff019bd40af Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -455,6 +455,30 @@ bool contains(const C &container, R S::*member)
|
||||
return anyOf(container, std::mem_fn(member));
|
||||
}
|
||||
|
||||
template<typename T, std::size_t Size, typename V>
|
||||
[[nodiscard]] bool contains(const T (&array)[Size], const V &value)
|
||||
{
|
||||
auto begin = std::begin(array);
|
||||
auto end = std::end(array);
|
||||
|
||||
auto found = std::find(begin, end, value);
|
||||
|
||||
return found != end;
|
||||
}
|
||||
|
||||
//////////////////
|
||||
// containsInSorted
|
||||
/////////////////
|
||||
|
||||
template<typename C, typename V>
|
||||
[[nodiscard]] bool containsInSorted(const C &container, const V &value)
|
||||
{
|
||||
auto begin = std::begin(container);
|
||||
auto end = std::end(container);
|
||||
|
||||
return std::binary_search(begin, end, value);
|
||||
}
|
||||
|
||||
//////////////////
|
||||
// findOr
|
||||
/////////////////
|
||||
|
@@ -551,17 +551,15 @@ static QString replaceIllegalPropertyNameChars(const QString &str)
|
||||
return ret;
|
||||
}
|
||||
|
||||
static bool idIsQmlKeyWord(const QString &id)
|
||||
static bool idIsQmlKeyWord(QStringView id)
|
||||
{
|
||||
static const QSet<QString> keywords = {"as", "break", "case", "catch",
|
||||
"continue", "debugger", "default", "delete",
|
||||
"do", "else", "finally", "for",
|
||||
"function", "if", "import", "in",
|
||||
"instanceof", "new", "return", "switch",
|
||||
"this", "throw", "try", "typeof",
|
||||
"var", "void", "while", "with"};
|
||||
static constexpr std::u16string_view keywords[] = {
|
||||
u"as", u"break", u"case", u"catch", u"continue", u"debugger", u"default",
|
||||
u"delete", u"do", u"else", u"finally", u"for", u"function", u"if",
|
||||
u"import", u"in", u"instanceof", u"new", u"return", u"switch", u"this",
|
||||
u"throw", u"try", u"typeof", u"var", u"void", u"while", u"with"};
|
||||
|
||||
return keywords.contains(id);
|
||||
return Utils::contains(keywords, std::u16string_view{id.utf16(), Utils::usize(id)});
|
||||
}
|
||||
|
||||
QString RewriterView::auxiliaryDataAsQML() const
|
||||
|
Reference in New Issue
Block a user