Package com.google.common.jimfs
Class GlobToRegex
- java.lang.Object
-
- com.google.common.jimfs.GlobToRegex
-
final class GlobToRegex extends java.lang.ObjectTranslates globs to regex patterns.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classGlobToRegex.StateConverter state.
-
Field Summary
Fields Modifier and Type Field Description private static GlobToRegex.StateBRACKETState inside [brackets], but not at the first character inside the brackets.private static GlobToRegex.StateBRACKET_FIRST_CHARState immediately following the reading of a [.private java.lang.StringBuilderbuilderprivate static GlobToRegex.StateCURLY_BRACEState inside {curly braces}.private static GlobToRegex.StateESCAPEState following the reading of a single \.private java.lang.Stringglobprivate intindexprivate static GlobToRegex.StateNORMALNormal state.private static InternalCharMatcherREGEX_RESERVEDprivate InternalCharMatcherseparatorMatcherprivate java.lang.Stringseparatorsprivate static GlobToRegex.StateSTARState following the reading of a single *.private java.util.Deque<GlobToRegex.State>states
-
Constructor Summary
Constructors Modifier Constructor Description privateGlobToRegex(java.lang.String glob, java.lang.String separators)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidappend(char c)Appends the regex form of the given normal character or separator from the glob.private voidappendBracketEnd()Appends the regex form of the end of a glob [] section.private voidappendBracketStart()Appends the regex form of the start of a glob [] section.private voidappendCurlyBraceEnd()Appends the regex form of the end of a glob {} section.private voidappendCurlyBraceStart()Appends the regex form of the start of a glob {} section.private voidappendExact(char c)Appends the given character as-is to the regex.private voidappendInBracket(char c)Appends the regex form of the given character within a glob [] section.private voidappendNonSeparator()Appends the regex form that matches anything except the separators for the path type.private voidappendNormal(char c)Appends the regex form of the given normal character from the glob.private voidappendQuestionMark()Appends the regex form of the glob ? character.private voidappendSeparator()Appends the regex form matching the separators for the path type.private voidappendStar()Appends the regex form of the glob * character.private voidappendStarStar()Appends the regex form of the glob ** pattern.private voidappendSubpatternSeparator()Appends the regex form of the separator (,) within a glob {} section.private java.lang.Stringconvert()Converts the glob to a regex one character at a time.private GlobToRegex.StatecurrentState()Returns the current state.private voidpopState()Returns to the previous state.private voidpushState(GlobToRegex.State state)Enters the given state.private java.util.regex.PatternSyntaxExceptionsyntaxError(java.lang.String desc)Throws aPatternSyntaxException.static java.lang.StringtoRegex(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.
-
-