Skip to main content
deleted 266 characters in body
Source Link
Deduplicator
  • 9.3k
  • 5
  • 33
  • 53

Well, you are missingJust use a crucial functionlambda for the predicate, namelyand then use the power of containsAny(). Using that, your filter-decision can be done with a simplestandard algorithms and selfshort-explanatory expressioncircuiting. Now, your code is far easier to understand and shorter, and needs neither continue nor breakNo need for any convoluted or exotic control-flow:

void ParsingTools::filterStrings (QStringList& list)
{
    for (int i = list.size(); i--;) {
        const auto& s = list[i];
        auto contains = [&](const QString& x) { return s.contains(x); };
        if (s.size() != m_Length 
 || !containsAny(s, m_Include) || containsAny(s, m_Exclude)
          || !std::all_of(m_Include.begin(), filterStringOutm_Include.end(i);, contains)
    }
}

And the missing helper, using standard algorithms:

bool containsAny(const QString& s, const QStringList& list)
{
    return  || std::any_of(listm_Exclude.begin(), listm_Exclude.end(), contains))
        [&](const QString& x){ return s.containsfilterStringOut(xi);
    });
}

Well, you are missing a crucial function, namely containsAny(). Using that, your filter-decision can be done with a simple and self-explanatory expression. Now, your code is far easier to understand and shorter, and needs neither continue nor break:

void ParsingTools::filterStrings (QStringList& list)
{
    for (int i = list.size(); i--;) {
        const auto& s = list[i];
        if (s.size() != m_Length || !containsAny(s, m_Include) || containsAny(s, m_Exclude)
            filterStringOut(i);
    }
}

And the missing helper, using standard algorithms:

bool containsAny(const QString& s, const QStringList& list)
{
    return std::any_of(list.begin(), list.end(),
        [&](const QString& x){ return s.contains(x); });
}

Just use a lambda for the predicate, and then use the power of standard algorithms and short-circuiting. No need for any convoluted or exotic control-flow:

void ParsingTools::filterStrings (QStringList& list)
{
    for (int i = list.size(); i--;) {
        const auto& s = list[i];
        auto contains = [&](const QString& x) { return s.contains(x); };
        if (s.size() != m_Length 
                || !std::all_of(m_Include.begin(), m_Include.end(), contains)
                || std::any_of(m_Exclude.begin(), m_Exclude.end(), contains))
            filterStringOut(i);
    }
}
Source Link
Deduplicator
  • 9.3k
  • 5
  • 33
  • 53

Well, you are missing a crucial function, namely containsAny(). Using that, your filter-decision can be done with a simple and self-explanatory expression. Now, your code is far easier to understand and shorter, and needs neither continue nor break:

void ParsingTools::filterStrings (QStringList& list)
{
    for (int i = list.size(); i--;) {
        const auto& s = list[i];
        if (s.size() != m_Length || !containsAny(s, m_Include) || containsAny(s, m_Exclude)
            filterStringOut(i);
    }
}

And the missing helper, using standard algorithms:

bool containsAny(const QString& s, const QStringList& list)
{
    return std::any_of(list.begin(), list.end(),
        [&](const QString& x){ return s.contains(x); });
}