Commit d6cd4569 authored by Milian Wolff's avatar Milian Wolff
Browse files

Add some more assertions to document preconditions

Return early when findCommaOrEnd is called with the end pos
and add a unit test for the situation where this might occur.

This was suggested by Igor Kushnir during code review, thanks!
parent bb0b5a8c
......@@ -85,6 +85,8 @@ bool endsWithWordBoundary(QStringView str)
bool isOperator(const QString& str, int pos)
{
Q_ASSERT(pos >= 0 && pos < str.size());
const auto op = QLatin1String("operator");
if (pos < op.size()) {
return false;
......@@ -121,12 +123,16 @@ bool isOperator(const QString& str, int pos)
// check for operator-> but don't get confused by operator-->
bool isArrowOperator(const QString& str, int pos)
{
Q_ASSERT(pos >= 0 && pos < str.size());
Q_ASSERT(str[pos] == QLatin1Char('>'));
return pos > 0 && str[pos - 1] == QLatin1Char('-') && (pos == 1 || str[pos - 2] != QLatin1Char('-'));
}
int skipStringOrCharLiteral(const QString& str, int pos)
{
Q_ASSERT(pos >= 0 && pos < str.size());
const auto quote = str[pos];
Q_ASSERT(quote == QLatin1Char('\'') || quote == QLatin1Char('"'));
......@@ -172,6 +178,8 @@ bool parenFits(QChar c1, QChar c2)
int findClose(const QString& str, int pos)
{
Q_ASSERT(pos >= 0 && pos < str.size());
int depth = 1;
QVarLengthArray<QChar, 16> st;
st.append(str[pos]);
......@@ -217,6 +225,12 @@ int findClose(const QString& str, int pos)
int findCommaOrEnd(const QString& str, int pos, QChar validEnd)
{
if (pos == str.size()) {
return pos;
}
Q_ASSERT(pos >= 0 && pos < str.size());
for (int a = pos; a < str.length(); a++) {
switch (str[a].unicode()) {
case '"':
......
......@@ -139,6 +139,7 @@ void TestStringHelpers::testParamIterator_data()
addTest("Empty", {});
addTest("Foo<T1, T2>", {"T1", "T2"});
addTest("operator<", {});
addTest("__not_overloaded2<_Tp, foo<bar>, __void_t<decltype(std::declval<_Tp>().operator<(std::declval<_Up>()))>>",
{"_Tp", "foo<bar>", "__void_t<decltype(std::declval<_Tp>().operator<(std::declval<_Up>()))>"});
addTest("__not_overloaded2<_Tp, foo<bar>, __void_t<decltype(std::declval<_Tp>().operator>(std::declval<_Up>()))>>",
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment