Skip to content

Commit

Permalink
astutils.cpp: fixed suboptimal string comparisons / small cleanup (#6799
Browse files Browse the repository at this point in the history
)
  • Loading branch information
firewave committed Sep 26, 2024
1 parent bb4d0a0 commit b14f100
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
18 changes: 17 additions & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

#include <algorithm>
#include <cassert>
#include <cstring>
#include <functional>
#include <initializer_list>
#include <iterator>
Expand Down Expand Up @@ -107,6 +108,21 @@ static int getArgumentPos(const Token* ftok, const Token* tokToFind){
return findArgumentPos(startTok, tokToFind);
}

template<class T, class OuputIterator, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
static void astFlattenCopy(T* tok, const char* op, OuputIterator out, nonneg int depth = 100)
{
--depth;
if (!tok || depth < 0)
return;
if (strcmp(tok->str().c_str(), op) == 0) {
astFlattenCopy(tok->astOperand1(), op, out, depth);
astFlattenCopy(tok->astOperand2(), op, out, depth);
} else {
*out = tok;
++out;
}
}

std::vector<const Token*> astFlatten(const Token* tok, const char* op)
{
std::vector<const Token*> result;
Expand All @@ -126,7 +142,7 @@ nonneg int astCount(const Token* tok, const char* op, int depth)
--depth;
if (!tok || depth < 0)
return 0;
if (tok->str() == op)
if (strcmp(tok->str().c_str(), op) == 0)
return astCount(tok->astOperand1(), op, depth) + astCount(tok->astOperand2(), op, depth);
return 1;
}
Expand Down
15 changes: 0 additions & 15 deletions lib/astutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,6 @@ const Token* findExpression(nonneg int exprid,
const std::function<bool(const Token*)>& pred);
const Token* findExpression(const Token* start, nonneg int exprid);

template<class T, class OuputIterator, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
void astFlattenCopy(T* tok, const char* op, OuputIterator out, nonneg int depth = 100)
{
--depth;
if (!tok || depth < 0)
return;
if (tok->str() == op) {
astFlattenCopy(tok->astOperand1(), op, out, depth);
astFlattenCopy(tok->astOperand2(), op, out, depth);
} else {
*out = tok;
++out;
}
}

std::vector<const Token*> astFlatten(const Token* tok, const char* op);
std::vector<Token*> astFlatten(Token* tok, const char* op);

Expand Down

0 comments on commit b14f100

Please sign in to comment.