diff --git a/src/Expr.cc b/src/Expr.cc index 18deec804c..9f333c641e 100644 --- a/src/Expr.cc +++ b/src/Expr.cc @@ -2383,11 +2383,10 @@ IndexExpr::IndexExpr(ExprPtr arg_op1, ListExprPtr arg_op2, bool arg_is_slice, bo return; if ( op1->GetType()->Tag() == TYPE_TABLE ) { // Check for a table[pattern] being indexed by a string - auto table_type = op1->GetType()->AsTableType(); - auto& it = table_type->GetIndexTypes(); - auto& rhs_type = op2->GetType()->AsTypeList()->GetTypes(); - if ( it.size() == 1 && it[0]->Tag() == TYPE_PATTERN && table_type->Yield() && rhs_type.size() == 1 && - rhs_type[0]->Tag() == TYPE_STRING ) { + const auto& table_type = op1->GetType()->AsTableType(); + const auto& rhs_type = op2->GetType()->AsTypeList()->GetTypes(); + if ( table_type->IsPatternIndex() && table_type->Yield() && rhs_type.size() == 1 && + IsString(rhs_type[0]->Tag()) ) { is_pattern_table = true; SetType(make_intrusive(op1->GetType()->Yield())); return; diff --git a/src/Type.cc b/src/Type.cc index 4fd9fa9f52..a9da69e760 100644 --- a/src/Type.cc +++ b/src/Type.cc @@ -389,6 +389,13 @@ bool IndexType::IsSubNetIndex() const { return false; } +bool IndexType::IsPatternIndex() const { + const auto& types = indices->GetTypes(); + if ( types.size() == 1 && types[0]->Tag() == TYPE_PATTERN ) + return true; + return false; +} + detail::TraversalCode IndexType::Traverse(detail::TraversalCallback* cb) const { auto tc = cb->PreType(this); HANDLE_TC_TYPE_PRE(tc); diff --git a/src/Type.h b/src/Type.h index 6b69eb8e37..142fe2d331 100644 --- a/src/Type.h +++ b/src/Type.h @@ -356,6 +356,9 @@ public: // Returns true if this table is solely indexed by subnet. bool IsSubNetIndex() const; + // Returns true if this table has a single index of type pattern. + bool IsPatternIndex() const; + detail::TraversalCode Traverse(detail::TraversalCallback* cb) const override; protected: diff --git a/src/Val.cc b/src/Val.cc index d861d69386..a8f6f0d2e2 100644 --- a/src/Val.cc +++ b/src/Val.cc @@ -1547,8 +1547,7 @@ void TableVal::Init(TableTypePtr t, bool ordered) { else subnets = nullptr; - auto& it = table_type->GetIndexTypes(); - if ( it.size() == 1 && it[0]->Tag() == TYPE_PATTERN && table_type->Yield() ) + if ( table_type->IsPatternIndex() && table_type->Yield() ) pattern_matcher = new TablePatternMatcher(this, table_type->Yield()); table_hash = new detail::CompositeHash(table_type->GetIndices()); diff --git a/src/script_opt/CPP/Exprs.cc b/src/script_opt/CPP/Exprs.cc index afef3ce3cd..f798b4f91c 100644 --- a/src/script_opt/CPP/Exprs.cc +++ b/src/script_opt/CPP/Exprs.cc @@ -391,8 +391,7 @@ string CPPCompile::GenIndexExpr(const Expr* e, GenType gt) { auto ind_expr = e->GetOp2()->AsListExpr()->Exprs()[0]; auto is_pat_str_ind = false; - auto& indices = aggr_t->AsTableType()->GetIndices()->GetTypes(); - if ( indices.size() == 1 && indices[0]->Tag() == TYPE_PATTERN && ind_expr->GetType()->Tag() == TYPE_STRING ) + if ( aggr_t->AsTableType()->IsPatternIndex() && ind_expr->GetType()->Tag() == TYPE_STRING ) is_pat_str_ind = true; if ( inside_when ) {