Class GlobToRegex

java.lang.Object
com.google.common.jimfs.GlobToRegex

final class GlobToRegex extends Object
Translates globs to regex patterns.
  • Field Details

    • REGEX_RESERVED

      private static final InternalCharMatcher REGEX_RESERVED
    • glob

      private final String glob
    • separators

      private final String separators
    • separatorMatcher

      private final InternalCharMatcher separatorMatcher
    • builder

      private final StringBuilder builder
    • states

      private final Deque<GlobToRegex.State> states
    • index

      private int index
    • NORMAL

      private static final GlobToRegex.State NORMAL
      Normal state.
    • ESCAPE

      private static final GlobToRegex.State ESCAPE
      State following the reading of a single \.
    • STAR

      private static final GlobToRegex.State STAR
      State following the reading of a single *.
    • BRACKET_FIRST_CHAR

      private static final GlobToRegex.State BRACKET_FIRST_CHAR
      State immediately following the reading of a [.
    • BRACKET

      private static final GlobToRegex.State BRACKET
      State inside [brackets], but not at the first character inside the brackets.
    • CURLY_BRACE

      private static final GlobToRegex.State CURLY_BRACE
      State inside {curly braces}.
  • Constructor Details

    • GlobToRegex

      private GlobToRegex(String glob, String separators)
  • Method Details

    • toRegex

      public static String toRegex(String glob, String separators)
      Converts the given glob to a regular expression pattern. The given separators determine what characters the resulting expression breaks on for glob expressions such as * which should not cross directory boundaries.

      Basic conversions (assuming / as only separator):

      
       ?        = [^/]
       *        = [^/]*
       **       = .*
       [a-z]    = [[^/]&&[a-z]]
       [!a-z]   = [[^/]&&[^a-z]]
       {a,b,c}  = (a|b|c)
       
    • convert

      private String convert()
      Converts the glob to a regex one character at a time. A state stack (states) is maintained, with the state at the top of the stack being the current state at any given time. The current state is always used to process the next character. When a state processes a character, it may pop the current state or push a new state as the current state. The resulting regex is written to builder.
    • pushState

      private void pushState(GlobToRegex.State state)
      Enters the given state. The current state becomes the previous state.
    • popState

      private void popState()
      Returns to the previous state.
    • currentState

      private GlobToRegex.State currentState()
      Returns the current state.
    • syntaxError

      private PatternSyntaxException syntaxError(String desc)
    • appendExact

      private void appendExact(char c)
      Appends the given character as-is to the regex.
    • append

      private void append(char c)
      Appends the regex form of the given normal character or separator from the glob.
    • appendNormal

      private void appendNormal(char c)
      Appends the regex form of the given normal character from the glob.
    • appendSeparator

      private void appendSeparator()
      Appends the regex form matching the separators for the path type.
    • appendNonSeparator

      private void appendNonSeparator()
      Appends the regex form that matches anything except the separators for the path type.
    • appendQuestionMark

      private void appendQuestionMark()
      Appends the regex form of the glob ? character.
    • appendStar

      private void appendStar()
      Appends the regex form of the glob * character.
    • appendStarStar

      private void appendStarStar()
      Appends the regex form of the glob ** pattern.
    • appendBracketStart

      private void appendBracketStart()
      Appends the regex form of the start of a glob [] section.
    • appendBracketEnd

      private void appendBracketEnd()
      Appends the regex form of the end of a glob [] section.
    • appendInBracket

      private void appendInBracket(char c)
      Appends the regex form of the given character within a glob [] section.
    • appendCurlyBraceStart

      private void appendCurlyBraceStart()
      Appends the regex form of the start of a glob {} section.
    • appendSubpatternSeparator

      private void appendSubpatternSeparator()
      Appends the regex form of the separator (,) within a glob {} section.
    • appendCurlyBraceEnd

      private void appendCurlyBraceEnd()
      Appends the regex form of the end of a glob {} section.