HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
patternMatcher.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 #ifndef PXR_BASE_TF_PATTERN_MATCHER_H
8 #define PXR_BASE_TF_PATTERN_MATCHER_H
9 
10 /// \file tf/patternMatcher.h
11 /// \ingroup group_tf_String
12 /// A simple glob and regex matching utility.
13 
14 #include "pxr/pxr.h"
15 #include "pxr/base/tf/api.h"
16 #include "pxr/base/arch/regex.h"
17 
18 #include <string>
19 
21 
22 /// \class TfPatternMatcher
23 /// \ingroup group_tf_String
24 ///
25 /// Class for matching regular expressions.
26 ///
27 /// A matcher is good to use when you have many strings to match against one
28 /// pattern. This is because the matcher will only compile the regular
29 /// expression once.
30 ///
32 {
33 
34  public:
35 
36  /// Construct an empty (invalid) TfPatternMatcher.
38 
39  TF_API TfPatternMatcher(TfPatternMatcher &&) noexcept = default;
40  TF_API TfPatternMatcher& operator=(TfPatternMatcher &&) = default;
41 
42  /// Construct a TfPatternMatcher with a default configuration. Note that
43  /// pattern compilation will not occur until the first call to \a Match()
44  /// or \a IsValid().
45  TF_API
46  TfPatternMatcher( const std::string &pattern,
47  bool caseSensitive = false,
48  bool isGlob = false );
49 
50  /// Destructor.
52 
53  /// If \a IsValid() returns true, this will return the reason why (if any).
54  TF_API std::string GetInvalidReason() const;
55 
56  /// Returns true if the matcher has been set to be case sensitive, false
57  /// otherwise.
58  bool IsCaseSensitive() const {
59  return _caseSensitive;
60  }
61 
62  /// Returns true if the matcher has been set to treat patterns as glob
63  /// patterns, false otherwise.
64  bool IsGlobPattern() const {
65  return _isGlob;
66  }
67 
68  /// Returns the matcher's pattern string.
69  TF_API const std::string& GetPattern() const {
70  return _pattern;
71  }
72 
73  /// Returns true if the matcher has a valid pattern. Note that empty
74  /// patterns are considered invalid. This will cause a compile of
75  TF_API bool IsValid() const;
76 
77  /// Returns true if \a query matches the matcher's pattern.
78  ///
79  /// If there is an error in matching and errorMsg is not NULL, it will be
80  /// set with the error message. If the matcher is not valid, this will
81  /// return false. Note that this will cause a compile of the matcher's
82  /// pattern if it was not already compiled.
83  ///
84  /// \warning Unlike 'match' functions in other regular expression
85  /// libraries, this method does not implicitly anchor the pattern. If a
86  /// partial match is not acceptable, it is necessary to anchor the pattern
87  /// passed to the constructor, e.g. "^mypattern$".
88  ///
89  TF_API bool Match( const std::string &query,
90  std::string *errorMsg = NULL ) const;
91 
92  /// Set this matcher to match case-sensitively or not.
93  TF_API void SetIsCaseSensitive( bool sensitive );
94 
95  /// Set this matcher to treat its pattern as a glob pattern. Currently,
96  /// this means that the pattern will be transformed by replacing all
97  /// instances of '.' with '\.', '*' with '.*', and '?' with '.', in that
98  /// order before being compiled as a normal regular expression.
99  TF_API void SetIsGlobPattern( bool isGlob );
100 
101  /// Set the pattern that this matcher will use to match against.
102  TF_API void SetPattern( const std::string &pattern );
103 
104  private:
105  void _Compile() const;
106 
107  bool _caseSensitive;
108  bool _isGlob;
109  std::string _pattern;
110  mutable bool _recompile;
111  mutable ArchRegex _regex;
112 
113 };
114 
116 
117 #endif // PXR_BASE_TF_PATTERN_MATCHER_H
TF_API TfPatternMatcher & operator=(TfPatternMatcher &&)=default
TF_API TfPatternMatcher()
Construct an empty (invalid) TfPatternMatcher.
GLenum query
Definition: glad.h:2772
TF_API std::string GetInvalidReason() const
If IsValid() returns true, this will return the reason why (if any).
TF_API void SetPattern(const std::string &pattern)
Set the pattern that this matcher will use to match against.
#define TF_API
Definition: api.h:23
TF_API const std::string & GetPattern() const
Returns the matcher's pattern string.
bool IsCaseSensitive() const
TF_API void SetIsCaseSensitive(bool sensitive)
Set this matcher to match case-sensitively or not.
TF_API ~TfPatternMatcher()
Destructor.
bool IsGlobPattern() const
TF_API bool Match(const std::string &query, std::string *errorMsg=NULL) const
TF_API void SetIsGlobPattern(bool isGlob)
TF_API bool IsValid() const
GLushort pattern
Definition: glad.h:2583
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74