Class PathPattern

java.lang.Object
ghidra.trace.model.target.path.PathPattern
All Implemented Interfaces:
PathFilter

public class PathPattern extends Object implements PathFilter
  • Constructor Details

    • PathPattern

      public PathPattern(KeyPath pattern)
      TODO: This can get more sophisticated if needed, but for now, I don't think we even need regular expressions. Either we care about a path element, or we don't.

      This takes a keypath as a means of matching paths. The blank key serves as a wildcard accepting all keys in that position, e.g., the following matches all elements within Processes:

       PathFilter.parse("Processes[]");
       
      Parameters:
      pattern - a list of path elements
  • Method Details

    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toPatternString

      public String toPatternString()
      Convert this pattern to a string.,

      This is the inverse of PathFilter.parse(String).

      Returns:
      the string
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • or

      public PathFilter or(PathFilter that)
      Specified by:
      or in interface PathFilter
    • isWildcard

      public static boolean isWildcard(String pat)
    • matchesUpTo

      protected boolean matchesUpTo(KeyPath path, int length)
    • matchesBackTo

      protected boolean matchesBackTo(KeyPath path, int length)
    • matches

      public boolean matches(KeyPath path)
      Description copied from interface: PathFilter
      Check if the entire path passes
      Specified by:
      matches in interface PathFilter
      Parameters:
      path - the path to check
      Returns:
      true if it matches, false otherwise
    • successorCouldMatch

      public boolean successorCouldMatch(KeyPath path, boolean strict)
      Description copied from interface: PathFilter
      Check if the given path could have a matching successor

      This essentially checks if the given path is a viable prefix to the matcher.

      Specified by:
      successorCouldMatch in interface PathFilter
      Parameters:
      path - the path (prefix) to check
      strict - true to exclude the case where PathFilter.matches(KeyPath) would return true
      Returns:
      true if a successor could match, false otherwise
    • ancestorMatches

      public boolean ancestorMatches(KeyPath path, boolean strict)
      Description copied from interface: PathFilter
      Check if the given path has an ancestor that matches
      Specified by:
      ancestorMatches in interface PathFilter
      Parameters:
      path - the path to check
      strict - true to exclude the case where PathFilter.matches(KeyPath) would return true
      Returns:
      true if an ancestor matches, false otherwise
    • ancestorCouldMatchRight

      public boolean ancestorCouldMatchRight(KeyPath path, boolean strict)
      Description copied from interface: PathFilter
      Check if the given path could have a matching ancestor, right to left

      This essentially checks if the given path is a viable postfix to the matcher.

      Specified by:
      ancestorCouldMatchRight in interface PathFilter
      Parameters:
      path - the path (postfix) to check
      strict - true to exclude the case where PathFilter.matches(KeyPath) would return true
      Returns:
      true if an ancestor could match, false otherwise
    • getSingletonPath

      public KeyPath getSingletonPath()
      Description copied from interface: PathFilter
      If this predicate is known to match only one path, i.e., no wildcards, get that path
      Specified by:
      getSingletonPath in interface PathFilter
      Returns:
      the singleton path, or null
    • asPath

      public KeyPath asPath()
      Return the pattern as a key path of patterns
      Returns:
      the list of key patterns
    • countWildcards

      public int countWildcards()
      Count the number of wildcard keys in this pattern
      Returns:
      the count
    • getSingletonPattern

      public PathPattern getSingletonPattern()
      Description copied from interface: PathFilter
      If this predicate consists of a single pattern, get that pattern
      Specified by:
      getSingletonPattern in interface PathFilter
      Returns:
      the singleton pattern, or null
    • getPatterns

      public Set<PathPattern> getPatterns()
      Description copied from interface: PathFilter
      Get the patterns of this predicate
      Specified by:
      getPatterns in interface PathFilter
      Returns:
      the patterns
    • getNextKeys

      public Set<String> getNextKeys(KeyPath path)
      Description copied from interface: PathFilter
      Get the patterns for the next possible key

      If a successor of the given path cannot match this pattern, the empty set is returned.

      Specified by:
      getNextKeys in interface PathFilter
      Parameters:
      path - the ancestor path
      Returns:
      a set of patterns where indices are enclosed in brackets []
    • getNextNames

      public Set<String> getNextNames(KeyPath path)
      Description copied from interface: PathFilter
      Get the patterns for the next possible name

      If a successor of the given path cannot match this pattern, the empty set is returned. If the pattern could accept a name next, get all patterns describing those names

      Specified by:
      getNextNames in interface PathFilter
      Parameters:
      path - the ancestor path
      Returns:
      a set of patterns
    • getNextIndices

      public Set<String> getNextIndices(KeyPath path)
      Description copied from interface: PathFilter
      Assuming a successor of path could match, get the patterns for the next possible index

      If a successor of the given path cannot match this pattern, the empty set is returned. If the pattern could accept an index next, get all patterns describing those indices

      Specified by:
      getNextIndices in interface PathFilter
      Parameters:
      path - the ancestor path
      Returns:
      a set of patterns, without brackets []
    • getPrevKeys

      public Set<String> getPrevKeys(KeyPath path)
      Description copied from interface: PathFilter
      Get the patterns for the previous possible key (right-to-left matching)

      If an ancestor of the given path cannot match this pattern, the empty set is returned.

      Specified by:
      getPrevKeys in interface PathFilter
      Parameters:
      path - the successor path
      Returns:
      a set of patterns where indices are enclosed in brackets []
    • isNone

      public boolean isNone()
      Description copied from interface: PathFilter
      Test if any patterns are contained here

      Note that the presence of a pattern does not guarantee the presence of a matching object. However, the absence of any pattern does guarantee no object can match.

      Specified by:
      isNone in interface PathFilter
      Returns:
      true if equivalent to PathFilter.NONE
    • sanitizeKey

      public static String sanitizeKey(String key)
      Sanitize the given key.

      Because brackets [] indicate an index in a path, they cannot themselves be used in an index. The first closing bracket seen is taken to end the index. We could support escaping, e.g., by "\[," but that seems a bit onerous for what little it affords. We also should not endeavor to support things like "Memory[something[with][brackets]]," because we'd still have an issue if the user's brackets are not balanced. Instead, we'll just replace the square brackets with curley braces, unless/until that turns out to be a Bad Idea.

      Parameters:
      key - the key to sanitize
      Returns:
      the sanitized key
    • applyKeys

      public PathPattern applyKeys(PathFilter.Align align, List<String> keys)
      Description copied from interface: PathFilter
      Substitute wildcards from left to right for the given list of keys

      Takes each pattern and substitutes its wildcards for the given indices, according to the given alignment. This object is unmodified, and the result is returned.

      If there are fewer wildcards in a pattern than given, only the first keys are taken. If there are fewer keys than wildcards in a pattern, then the remaining wildcards are left in the resulting pattern. In this manner, the left-most wildcards are substituted for the left-most indices, or the right-most wildcards are substituted for the right-most indices, depending on the alignment.

      Specified by:
      applyKeys in interface PathFilter
      Parameters:
      align - the end to align
      keys - the keys to substitute
      Returns:
      the pattern or matcher with the applied substitutions
    • matchKeys

      public List<String> matchKeys(KeyPath path, boolean matchLength)
      If the given path matches, extract keys where matched by wildcards

      This is essentially the inverse of PathFilter.applyKeys(String...), but can only be asked of one pattern. The keys are returned from left to right, in the order matched by the pattern. Only those keys matched by a wildcard are included in the result. Indices are extracted with the brackets [] removed.

      Parameters:
      path - the path to match
      matchLength - true if the path must have the same number of keys as this pattern, or false if the path is allowed to have more keys than this pattern
      Returns:
      the list of matched keys or null if not matched
    • removeRight

      public PathMatcher removeRight(int count)
      Description copied from interface: PathFilter
      Remove count elements from the right
      Specified by:
      removeRight in interface PathFilter
      Parameters:
      count - the number of elements to remove
      Returns:
      the resulting filter