Files
OpenSTA/include/sta/PatternMatch.hh
T

102 lines
3.0 KiB
C++
Raw Normal View History

2018-09-28 08:54:21 -07:00
// OpenSTA, Static Timing Analyzer
2026-03-10 13:21:17 -07:00
// Copyright (c) 2026, 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/>.
2025-01-21 18:54:33 -07:00
//
// The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software.
//
// Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// This notice may not be removed or altered from any source distribution.
2018-09-28 08:54:21 -07:00
2020-02-15 17:13:16 -07:00
#pragma once
2018-09-28 08:54:21 -07:00
2020-04-06 22:19:50 -07:00
#include <string>
2026-03-28 19:13:35 -07:00
#include <string_view>
2023-06-15 08:59:56 -07:00
2018-09-28 08:54:21 -07:00
#include "Error.hh"
// Don't require all of tcl.h.
2026-01-03 16:59:35 -08:00
using Tcl_RegExp = struct Tcl_RegExp_ *;
using Tcl_Interp = struct Tcl_Interp;
2018-09-28 08:54:21 -07:00
namespace sta {
using ::Tcl_RegExp;
using ::Tcl_Interp;
class PatternMatch
{
public:
// If regexp is false, use unix glob style matching.
// If regexp is true, use TCL regular expression matching.
// Regular expressions are always anchored.
// If nocase is true, ignore case in the pattern.
// Tcl_Interp is optional for reporting regexp compile errors.
2026-03-28 19:13:35 -07:00
PatternMatch(std::string_view pattern,
2026-01-03 16:59:35 -08:00
bool is_regexp,
bool nocase,
Tcl_Interp *interp);
2018-09-28 08:54:21 -07:00
// Use unix glob style matching.
2026-03-28 19:13:35 -07:00
PatternMatch(std::string_view pattern);
PatternMatch(std::string_view pattern,
2026-01-03 16:59:35 -08:00
const PatternMatch *inherit_from);
2026-03-28 19:13:35 -07:00
bool match(std::string_view str) const;
bool matchNoCase(std::string_view str) const;
const std::string &pattern() const { return pattern_; }
2018-09-28 08:54:21 -07:00
bool isRegexp() const { return is_regexp_; }
bool nocase() const { return nocase_; }
Tcl_Interp *tclInterp() const { return interp_; }
bool hasWildcards() const;
private:
void compileRegexp();
2026-03-28 19:13:35 -07:00
std::string pattern_;
2018-09-28 08:54:21 -07:00
bool is_regexp_;
bool nocase_;
Tcl_Interp *interp_;
Tcl_RegExp regexp_;
};
// Error thrown by Pattern constructor.
2020-02-15 18:10:24 -07:00
class RegexpCompileError : public Exception
2018-09-28 08:54:21 -07:00
{
public:
2026-03-28 19:13:35 -07:00
RegexpCompileError(std::string_view pattern);
2026-04-13 14:58:16 -07:00
~RegexpCompileError() noexcept override = default;
const char *what() const noexcept override;
2018-09-28 08:54:21 -07:00
private:
2020-04-06 22:19:50 -07:00
std::string error_;
2018-09-28 08:54:21 -07:00
};
// Simple pattern match
// '*' matches zero or more characters
// '?' matches any character
bool
2026-03-28 19:13:35 -07:00
patternMatch(std::string_view pattern,
std::string_view str);
2018-09-28 08:54:21 -07:00
bool
2026-03-28 19:13:35 -07:00
patternMatchNoCase(std::string_view pattern,
std::string_view str,
2026-01-03 16:59:35 -08:00
bool nocase);
2018-09-28 08:54:21 -07:00
// Predicate to find out if there are wildcard characters in the pattern.
bool
2026-03-28 19:13:35 -07:00
patternWildcards(std::string_view pattern);
2018-09-28 08:54:21 -07:00
2026-04-13 14:58:16 -07:00
} // namespace sta