Package com.google.common.jimfs
Class GlobToRegex
- java.lang.Object
-
- com.google.common.jimfs.GlobToRegex
-
final class GlobToRegex extends java.lang.Object
Translates globs to regex patterns.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
GlobToRegex.State
Converter state.
-
Field Summary
Fields Modifier and Type Field Description private static GlobToRegex.State
BRACKET
State inside [brackets], but not at the first character inside the brackets.private static GlobToRegex.State
BRACKET_FIRST_CHAR
State immediately following the reading of a [.private java.lang.StringBuilder
builder
private static GlobToRegex.State
CURLY_BRACE
State inside {curly braces}.private static GlobToRegex.State
ESCAPE
State following the reading of a single \.private java.lang.String
glob
private int
index
private static GlobToRegex.State
NORMAL
Normal state.private static InternalCharMatcher
REGEX_RESERVED
private InternalCharMatcher
separatorMatcher
private java.lang.String
separators
private static GlobToRegex.State
STAR
State following the reading of a single *.private java.util.Deque<GlobToRegex.State>
states
-
Constructor Summary
Constructors Modifier Constructor Description private
GlobToRegex(java.lang.String glob, java.lang.String separators)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
append(char c)
Appends the regex form of the given normal character or separator from the glob.private void
appendBracketEnd()
Appends the regex form of the end of a glob [] section.private void
appendBracketStart()
Appends the regex form of the start of a glob [] section.private void
appendCurlyBraceEnd()
Appends the regex form of the end of a glob {} section.private void
appendCurlyBraceStart()
Appends the regex form of the start of a glob {} section.private void
appendExact(char c)
Appends the given character as-is to the regex.private void
appendInBracket(char c)
Appends the regex form of the given character within a glob [] section.private void
appendNonSeparator()
Appends the regex form that matches anything except the separators for the path type.private void
appendNormal(char c)
Appends the regex form of the given normal character from the glob.private void
appendQuestionMark()
Appends the regex form of the glob ? character.private void
appendSeparator()
Appends the regex form matching the separators for the path type.private void
appendStar()
Appends the regex form of the glob * character.private void
appendStarStar()
Appends the regex form of the glob ** pattern.private void
appendSubpatternSeparator()
Appends the regex form of the separator (,) within a glob {} section.private java.lang.String
convert()
Converts the glob to a regex one character at a time.private GlobToRegex.State
currentState()
Returns the current state.private void
popState()
Returns to the previous state.private void
pushState(GlobToRegex.State state)
Enters the given state.private java.util.regex.PatternSyntaxException
syntaxError(java.lang.String desc)
Throws aPatternSyntaxException
.static java.lang.String
toRegex(java.lang.String glob, java.lang.String separators)
Converts the given glob to a regular expression pattern.
-
-
-
Field Detail
-
REGEX_RESERVED
private static final InternalCharMatcher REGEX_RESERVED
-
glob
private final java.lang.String glob
-
separators
private final java.lang.String separators
-
separatorMatcher
private final InternalCharMatcher separatorMatcher
-
builder
private final java.lang.StringBuilder builder
-
states
private final java.util.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}.
-
-
Method Detail
-
toRegex
public static java.lang.String toRegex(java.lang.String glob, java.lang.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 java.lang.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 tobuilder
.
-
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 java.util.regex.PatternSyntaxException syntaxError(java.lang.String desc)
Throws aPatternSyntaxException
.
-
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.
-
-