forked from konveyor/analyzer-lsp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
testing a change that would reduce the use of the regexp package to o…
…nly when needed Signed-off-by: Shawn Hurley <[email protected]>
- Loading branch information
1 parent
2dbd0f1
commit 342ea9a
Showing
2 changed files
with
108 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package builtin | ||
|
||
import ( | ||
"regexp" | ||
"slices" | ||
|
||
"github.com/dlclark/regexp2" | ||
) | ||
|
||
var LOOK_SYNTAX = []string{"(?=", "(?!", "(?<=", "(?<!"} | ||
|
||
type regexSelector struct { | ||
goRegex *regexp.Regexp | ||
lookRegex *regexp2.Regexp | ||
} | ||
|
||
func compileRegex(pattern string) (*regexSelector, error) { | ||
if slices.Contains(LOOK_SYNTAX, pattern) { | ||
regex, err := regexp2.Compile(pattern, regexp2.None) | ||
return ®exSelector{ | ||
lookRegex: regex, | ||
}, err | ||
} | ||
regex, err := regexp.Compile(pattern) | ||
return ®exSelector{ | ||
goRegex: regex, | ||
}, err | ||
} | ||
|
||
func (r *regexSelector) MatchString(s string) (bool, error) { | ||
if r.goRegex != nil { | ||
return r.goRegex.MatchString(s), nil | ||
} | ||
return r.lookRegex.MatchString(s) | ||
} | ||
|
||
func (r *regexSelector) FindStringSubmatch(s string) []string { | ||
if r.goRegex != nil { | ||
return r.goRegex.FindStringSubmatch(s) | ||
} | ||
// regexp2 does not have this ability, until we need to handle it with the matches ignore. | ||
return []string{} | ||
} | ||
|
||
func (r *regexSelector) FindStringMatch(s string) (string, int, error) { | ||
if r.goRegex != nil { | ||
matchString := r.goRegex.FindString(s) | ||
matchIndex := r.goRegex.FindStringIndex(s) | ||
if matchIndex == nil || len(matchIndex) < 1 { | ||
return "", 0, nil | ||
} | ||
return matchString, matchIndex[0], nil | ||
} | ||
match, err := r.lookRegex.FindStringMatch(s) | ||
if err != nil || match == nil { | ||
return "", 0, err | ||
} | ||
return match.String(), match.Index, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters