Files
OpenSTA/util/PatternMatch.cc
T

214 lines
4.4 KiB
C++
Raw Normal View History

2018-09-28 08:54:21 -07:00
// OpenSTA, Static Timing Analyzer
2023-02-18 17:55:40 -07:00
// Copyright (c) 2023, Parallax Software, Inc.
2018-09-28 08:54:21 -07:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
2022-01-04 10:17:08 -07:00
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2018-09-28 08:54:21 -07:00
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
2022-01-04 10:17:08 -07:00
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-09-28 08:54:21 -07:00
2020-04-05 14:53:44 -07:00
#include "PatternMatch.hh"
2023-06-15 08:59:56 -07:00
#include <cstring>
2018-09-28 08:54:21 -07:00
#include <tcl.h>
2020-04-05 11:35:51 -07:00
2018-09-28 08:54:21 -07:00
namespace sta {
using std::string;
PatternMatch::PatternMatch(const char *pattern,
bool is_regexp,
bool nocase,
Tcl_Interp *interp) :
pattern_(pattern),
is_regexp_(is_regexp),
nocase_(nocase),
interp_(interp),
2019-03-12 17:25:53 -07:00
regexp_(nullptr)
2018-09-28 08:54:21 -07:00
{
if (is_regexp_)
compileRegexp();
}
PatternMatch::PatternMatch(const char *pattern) :
pattern_(pattern),
is_regexp_(false),
nocase_(false),
2019-03-12 17:25:53 -07:00
interp_(nullptr),
regexp_(nullptr)
2018-09-28 08:54:21 -07:00
{
}
PatternMatch::PatternMatch(const char *pattern,
const PatternMatch *inherit_from) :
pattern_(pattern),
is_regexp_(inherit_from->is_regexp_),
nocase_(inherit_from->nocase_),
interp_(inherit_from->interp_),
2019-03-12 17:25:53 -07:00
regexp_(nullptr)
2018-09-28 08:54:21 -07:00
{
if (is_regexp_)
compileRegexp();
}
2023-03-27 12:44:28 -07:00
PatternMatch::PatternMatch(const string &pattern,
2023-03-26 06:34:36 -07:00
const PatternMatch *inherit_from) :
pattern_(pattern.c_str()),
is_regexp_(inherit_from->is_regexp_),
nocase_(inherit_from->nocase_),
interp_(inherit_from->interp_),
regexp_(nullptr)
{
if (is_regexp_)
compileRegexp();
}
2018-09-28 08:54:21 -07:00
void
PatternMatch::compileRegexp()
{
int flags = TCL_REG_ADVANCED;
if (nocase_)
flags |= TCL_REG_NOCASE;
string anchored_pattern;
anchored_pattern += '^';
anchored_pattern += pattern_;
anchored_pattern += '$';
Tcl_Obj *pattern_obj = Tcl_NewStringObj(anchored_pattern.c_str(),
anchored_pattern.size());
regexp_ = Tcl_GetRegExpFromObj(interp_, pattern_obj, flags);
2019-03-12 17:25:53 -07:00
if (regexp_ == nullptr && interp_)
2018-09-28 08:54:21 -07:00
throw RegexpCompileError(pattern_);
}
static bool
regexpWildcards(const char *pattern)
{
2019-03-12 17:25:53 -07:00
return strpbrk(pattern, ".+*?[]") != nullptr;
2018-09-28 08:54:21 -07:00
}
bool
PatternMatch::hasWildcards() const
{
if (is_regexp_)
return regexpWildcards(pattern_);
else
return patternWildcards(pattern_);
}
2023-03-26 06:34:36 -07:00
bool
PatternMatch::match(const string &str) const
{
return match(str.c_str());
}
2018-09-28 08:54:21 -07:00
bool
PatternMatch::match(const char *str) const
{
if (regexp_)
2019-03-12 17:25:53 -07:00
return Tcl_RegExpExec(nullptr, regexp_, str, str) == 1;
2018-09-28 08:54:21 -07:00
else
return patternMatch(pattern_, str);
}
bool
PatternMatch::matchNoCase(const char *str) const
{
if (regexp_)
return Tcl_RegExpExec(0, regexp_, str, str) == 1;
else
return patternMatchNoCase(pattern_, str, nocase_);
}
////////////////////////////////////////////////////////////////
RegexpCompileError::RegexpCompileError(const char *pattern) :
2020-02-15 18:10:24 -07:00
Exception()
2018-09-28 08:54:21 -07:00
{
2020-04-06 22:19:50 -07:00
error_ = "TCL failed to compile regular expression '";
error_ += pattern;
2020-04-06 22:33:06 -07:00
error_ += "'.";
2018-09-28 08:54:21 -07:00
}
const char *
2020-02-15 18:10:24 -07:00
RegexpCompileError::what() const noexcept
2018-09-28 08:54:21 -07:00
{
2020-04-06 22:19:50 -07:00
return error_.c_str();
2018-09-28 08:54:21 -07:00
}
////////////////////////////////////////////////////////////////
bool
patternMatch(const char *pattern,
const char *str)
{
const char *p = pattern;
const char *s = str;
while (*p && *s && (*s == *p || *p == '?')) {
p++;
s++;
}
if (*p == '\0' && *s == '\0')
return true;
else if (*p == '*') {
if (p[1] == '\0')
return true;
while (*s) {
if (patternMatch(p + 1, s))
return true;
s++;
}
}
return false;
}
inline
bool equalCase(char s,
char p,
bool nocase)
{
return nocase
? tolower(s) == tolower(p)
: s == p;
}
bool
patternMatchNoCase(const char *pattern,
const char *str,
bool nocase)
{
const char *p = pattern;
const char *s = str;
while (*p && *s && (equalCase(*s, *p, nocase) || *p == '?')) {
p++;
s++;
}
if (*p == '\0' && *s == '\0')
return true;
else if (*p == '*') {
if (p[1] == '\0')
return true;
while (*s) {
if (patternMatchNoCase(p + 1, s, nocase))
return true;
s++;
}
}
return false;
}
bool
patternWildcards(const char *pattern)
{
return strpbrk(pattern, "*?") != 0;
}
} // namespace