Closed dapryor closed 3 years ago
Merging #31 into master will not change coverage. The diff coverage is
100.00%
.
@@ Coverage Diff @@
## master #31 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 4 4
Lines 121 84 -37
=========================================
- Hits 121 84 -37
Impacted Files | Coverage Δ | |
---|---|---|
matcher.go | 100.00% <100.00%> (ø) |
|
log.go | 100.00% <0.00%> (ø) |
|
marker.go | 100.00% <0.00%> (ø) |
|
builder.go | 100.00% <0.00%> (ø) |
Continue to review full report at Codecov.
Legend - Click here to learn more
Δ = absolute <relative> (impact)
,ø = not affected
,? = missing data
Powered by Codecov. Last update 78e46f9...1e542a7. Read the comment docs.
func findPatternMatchIndexes(str string, patternsToMatch []string) map[int]string {
patternMatchIndexes := make(map[int]string)
for _, patternToMatch := range patternsToMatch {
for strings.Contains(str, patternToMatch) {
matchIndex := strings.Index(str, patternToMatch)
str = strings.Replace(str, patternToMatch, "", 1)
patternMatchIndexes[matchIndex] = patternToMatch
}
}
return patternMatchIndexes
}
this was the original block of code. It incorrectly found indexes. i.e.
input: "I scream, you all scream, we all scream for ice cream."
patterns: "scream", "ice"
Output:
2 scream
12 scream
21 scream
26 ice
It also did not support patterns like the name suggest, only strings due to this line str = strings.Replace(str, patternToMatch, "", 1)
You would replace the match with an empty string so the first instance of scream would be correct, the second would be 6 spaces off, the third would be 12 spaces off
This is the new block of code:
func findPatternMatchIndexes(str string, patternsToMatch []string) map[int]string {
patternMatchIndexes := make(map[int]string)
pattern := strings.Join(patternsToMatch , "|")
patternRegex := regexp.MustCompile(pattern)
indices := patternRegex.FindAllStringIndex(str, -1)
for _, v := range(indices) {
start, end := v[0], v[1]
patternMatchIndexes[start] = str[start:end]
}
return patternMatchIndexes
}
It correctly finds the indexes: i.e.
input: "I scream, you all scream, we all scream for ice cream."
patterns: "scream", "ice"
Output:
2 scream
18 scream
33 scream
44 ice
and it also supports patterns since the new block uses regex
…iously removing chunks of the strings which was effect subsequent match index locations.
It also now supports regexp patterns to better match the name. Added a few test as well.
Please mark this as a hacktoberfest-accepted PR ;)